Skip to content

Commit ce2b477

Browse files
committed
Generate compare URL and add to release
1 parent 44537e9 commit ce2b477

2 files changed

Lines changed: 67 additions & 14 deletions

File tree

src/__tests__/index.test.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ jest.mock('fs')
88
jest.mock('gh-got')
99
jest.mock('read-pkg')
1010

11-
it('creates github release', async () => {
12-
execa.mockImplementation()
11+
afterEach(jest.resetAllMocks)
12+
13+
it('creates initial github release', async () => {
14+
execa.mockImplementation((command: 'git', [subCommand]: string[]) =>
15+
subCommand === 'rev-list'
16+
? Promise.resolve({ stdout: '0a1b2c3d4e5f' })
17+
: undefined
18+
)
1319

1420
readPkg.mockResolvedValue({
1521
name: 'a-fixture',
@@ -34,12 +40,53 @@ it('creates github release', async () => {
3440

3541
await require('..').default()
3642

37-
expect(execa).toHaveBeenCalledWith('git', ['push', '--follow-tags'])
43+
expect(execa).toHaveBeenNthCalledWith(1, 'git', ['push', '--follow-tags'])
44+
expect(execa).toHaveBeenNthCalledWith(2, 'git', [
45+
'rev-list',
46+
'--max-parents=0',
47+
'HEAD',
48+
])
3849
expect(ghGot).toHaveBeenCalledWith('repos/wyze/a-fixture/releases', {
3950
body: {
4051
body:
41-
'* A bug fix PR ([@wyze](https://github.com/wyze) in [#1](https://github.com/wyze/a-fixture/pull/1))\n* Initial Commit ([@wyze](https://github.com/wyze) in [65578dd](https://github.com/wyze/a-fixture/commit/65578dd))',
52+
'* A bug fix PR ([@wyze](https://github.com/wyze) in [#1](https://github.com/wyze/a-fixture/pull/1))\n* Initial Commit ([@wyze](https://github.com/wyze) in [65578dd](https://github.com/wyze/a-fixture/commit/65578dd))\n\nhttps://github.com/wyze/a-fixture/compare/0a1b2c3...v1.0.0',
4253
tag_name: 'v1.0.0',
4354
},
4455
})
4556
})
57+
58+
it('creates second github release', async () => {
59+
execa.mockImplementation()
60+
61+
readPkg.mockResolvedValue({
62+
name: 'a-fixture',
63+
version: '2.0.0',
64+
license: 'MIT',
65+
repository: {
66+
type: 'git',
67+
url: 'git+https://github.com/wyze/a-fixture.git',
68+
},
69+
})
70+
71+
ghGot.mockImplementation()
72+
73+
fs.readFile
74+
// Read changelog
75+
.mockImplementationOnce((_, cb) =>
76+
cb(
77+
null,
78+
'## Change Log\n\n### [v2.0.0](https://github.com/wyze/a-fixture/releases/tag/v2.0.0) (2019-05-05)\n\n* A breaking change ([@wyze](https://github.com/wyze) in [#2](https://github.com/wyze/a-fixture/pull/2))\n\n### [v1.0.0](https://github.com/wyze/a-fixture/releases/tag/v1.0.0) (2017-05-05)\n\n* A bug fix PR ([@wyze](https://github.com/wyze) in [#1](https://github.com/wyze/a-fixture/pull/1))\n* Initial Commit ([@wyze](https://github.com/wyze) in [65578dd](https://github.com/wyze/a-fixture/commit/65578dd))\n\n'
79+
)
80+
)
81+
82+
await require('..').default()
83+
84+
expect(execa).toHaveBeenCalledWith('git', ['push', '--follow-tags'])
85+
expect(ghGot).toHaveBeenCalledWith('repos/wyze/a-fixture/releases', {
86+
body: {
87+
body:
88+
'* A breaking change ([@wyze](https://github.com/wyze) in [#2](https://github.com/wyze/a-fixture/pull/2))\n\nhttps://github.com/wyze/a-fixture/compare/v1.0.0...v2.0.0',
89+
tag_name: 'v2.0.0',
90+
},
91+
})
92+
})

src/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@ import ghGot from 'gh-got'
55
import readPkg from 'read-pkg'
66

77
type UrlVersion = {
8+
slug: string
89
url: string
910
version: string
1011
}
1112

1213
type ChangesUrlVersion = UrlVersion & {
1314
changes: string[]
15+
latest: string
1416
}
1517

1618
const readFile = promisify(fs.readFile)
1719

1820
const getUrlAndVersion = async (): Promise<UrlVersion> => {
1921
const { repository, version } = await readPkg()
20-
const url = repository.url
21-
.replace(/(^git\+|\.git$)/g, '')
22-
.split('/')
23-
.slice(-2)
24-
.join('/')
22+
const url = repository.url.replace(/(^git\+|\.git$)/g, '')
23+
const slug = url.split('/').slice(-2).join('/')
2524

26-
return { url, version }
25+
return { slug, url, version }
2726
}
2827

2928
const getChanges = async () =>
@@ -36,12 +35,14 @@ const getIndex = (changes: string[], startIndex: number) =>
3635

3736
const createGithubRelease = async ({
3837
changes,
38+
latest,
39+
slug,
3940
url,
4041
version,
4142
}: ChangesUrlVersion) =>
42-
await ghGot(`repos/${url}/releases`, {
43+
await ghGot(`repos/${slug}/releases`, {
4344
body: {
44-
body: changes.join('\n'),
45+
body: changes.join('\n') + `\n\n${url}/compare/${latest}...v${version}`,
4546
tag_name: `v${version}`,
4647
},
4748
})
@@ -51,13 +52,18 @@ export default async () => {
5152
// Push existing tags so the tag is there to create the release on
5253
await execa('git', ['push', '--follow-tags'])
5354

54-
const { url, version } = await getUrlAndVersion()
5555
const rawChanges = await getChanges()
5656
const startIndex = getIndex(rawChanges, 0) + 1
5757
const endIndex = getIndex(rawChanges, startIndex)
5858
const changes = rawChanges
5959
.slice(startIndex, endIndex)
6060
.filter((value) => value !== '')
61+
const latest =
62+
endIndex > 0
63+
? rawChanges[endIndex].match(/\[([^\]]+)\]/)[1]
64+
: (
65+
await execa('git', ['rev-list', '--max-parents=0', 'HEAD'])
66+
).stdout.slice(0, 7)
6167

62-
await createGithubRelease({ changes, url, version })
68+
await createGithubRelease({ changes, latest, ...(await getUrlAndVersion()) })
6369
}

0 commit comments

Comments
 (0)