Skip to content

Commit 0bfeda6

Browse files
author
Eunjae Lee
authored
fix: create github release with rest api instead of hub (#458)
* fix: create github release with rest api instead of hub * fix: wrong argument * fix: add missing import * fix: lint error
1 parent 1badafd commit 0bfeda6

File tree

7 files changed

+228
-80
lines changed

7 files changed

+228
-80
lines changed

packages/shipjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"esm": "3.2.25",
4646
"globby": "^10.0.1",
4747
"inquirer": "7.0.0",
48+
"mime-types": "^2.1.25",
4849
"mkdirp": "^0.5.1",
4950
"open": "^7.0.0",
5051
"prettier": "^1.18.2",

packages/shipjs/src/helper/getChangelog.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { extractSpecificChangelog } from './';
55
export default function getChangelog({ version, dir }) {
66
const changelogPath = path.resolve(dir, 'CHANGELOG.md');
77
try {
8-
const changelogFile = fs.readFileSync(changelogPath, 'utf-8').toString();
9-
return extractSpecificChangelog({ changelogFile, version });
8+
const changelog = fs.readFileSync(changelogPath, 'utf-8').toString();
9+
return extractSpecificChangelog({ changelog, version });
1010
} catch (err) {
1111
if (err.code === 'ENOENT') {
1212
return null;

packages/shipjs/src/step/prepare/__tests__/createPullRequest.spec.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { getRepoInfo } from 'shipjs-lib';
2-
import tempWrite from 'temp-write';
32
import Octokit from '@octokit/rest';
43
import createPullRequest from '../createPullRequest';
54
import { run } from '../../../util';
65
import { getDestinationBranchName } from '../../../helper';
7-
jest.mock('temp-write');
86
jest.mock('@octokit/rest');
97

108
const getDefaultParams = ({
@@ -43,7 +41,6 @@ describe('createPullRequest', () => {
4341
branch: 'master',
4442
url: 'https://github.com/my/repo',
4543
}));
46-
tempWrite.sync.mockImplementationOnce(() => '/temp/path');
4744
getDestinationBranchName.mockImplementation(() => 'master');
4845
});
4946

packages/shipjs/src/step/prepare/createPullRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default async ({
6363
run({ command: `git remote prune ${remote}`, dir, dryRun });
6464

6565
if (dryRun) {
66-
print('Creates a pull request with the following:');
66+
print('Creating a pull request with the following:');
6767
print(` - Title: ${title}`);
6868
print(` - Message: ${message}`);
6969
return {};

packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js

Lines changed: 132 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import tempWrite from 'temp-write';
21
import globby from 'globby';
2+
import fs from 'fs';
3+
import mime from 'mime-types';
4+
import { getRepoInfo } from 'shipjs-lib';
5+
import Octokit from '@octokit/rest';
36
import createGitHubRelease from '../createGitHubRelease';
4-
import { run } from '../../../util';
57
import { hubInstalled, hubConfigured } from '../../../helper';
68
jest.mock('temp-write');
9+
jest.mock('@octokit/rest');
710
jest.mock('globby');
11+
jest.mock('shipjs-lib');
12+
jest.mock('fs');
13+
jest.mock('mime-types');
814

915
const getDefaultParams = ({
1016
assetsToUpload,
@@ -20,91 +26,180 @@ const getDefaultParams = ({
2026
dryRun: false,
2127
});
2228

29+
const createRelease = jest.fn().mockImplementation(() => ({
30+
data: {
31+
upload_url: 'https://dummy/upload/url', // eslint-disable-line camelcase
32+
},
33+
}));
34+
const uploadReleaseAsset = jest.fn();
35+
Octokit.mockImplementation(function() {
36+
this.repos = { createRelease, uploadReleaseAsset };
37+
});
38+
2339
describe('createGitHubRelease', () => {
2440
beforeEach(() => {
2541
hubInstalled.mockImplementation(() => true);
2642
hubConfigured.mockImplementation(() => true);
43+
getRepoInfo.mockImplementation(() => ({
44+
owner: 'my',
45+
name: 'repo',
46+
}));
47+
fs.readFileSync = jest.fn();
48+
fs.statSync = jest.fn().mockImplementation(() => ({ size: 1024 }));
49+
mime.lookup.mockImplementation(() => 'application/zip');
50+
globby.mockImplementation(path => Promise.resolve([path]));
2751
});
2852

2953
it('works without assets', async () => {
30-
tempWrite.sync.mockImplementation(() => `/my chan"ge"log/temp/path`);
3154
await createGitHubRelease(getDefaultParams());
32-
expect(run).toHaveBeenCalledTimes(1);
33-
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
55+
expect(createRelease).toHaveBeenCalledTimes(1);
56+
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
3457
Array [
3558
Object {
36-
"command": "hub release create -F '/my chan\\"ge\\"log/temp/path' v1.2.3",
37-
"dir": ".",
38-
"dryRun": false,
59+
"body": "",
60+
"name": "v1.2.3",
61+
"owner": "my",
62+
"repo": "repo",
63+
"tag_name": "v1.2.3",
3964
},
4065
]
4166
`);
67+
expect(uploadReleaseAsset).toHaveBeenCalledTimes(0);
4268
});
4369

4470
it('works with assets (fn)', async () => {
45-
tempWrite.sync.mockImplementation(() => `/temp/path`);
4671
await createGitHubRelease(
4772
getDefaultParams({
4873
assetsToUpload: () => {
4974
return Promise.resolve(['/path1', '/path2']);
5075
},
5176
})
5277
);
53-
expect(run).toHaveBeenCalledTimes(1);
54-
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
78+
expect(createRelease).toHaveBeenCalledTimes(1);
79+
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
5580
Array [
5681
Object {
57-
"command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
58-
"dir": ".",
59-
"dryRun": false,
82+
"body": "",
83+
"name": "v1.2.3",
84+
"owner": "my",
85+
"repo": "repo",
86+
"tag_name": "v1.2.3",
6087
},
6188
]
6289
`);
90+
expect(uploadReleaseAsset).toHaveBeenCalledTimes(2);
91+
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
92+
Array [
93+
Array [
94+
Object {
95+
"file": undefined,
96+
"headers": Object {
97+
"content-length": 1024,
98+
"content-type": "application/zip",
99+
},
100+
"name": "path1",
101+
"url": "https://dummy/upload/url",
102+
},
103+
],
104+
Array [
105+
Object {
106+
"file": undefined,
107+
"headers": Object {
108+
"content-length": 1024,
109+
"content-type": "application/zip",
110+
},
111+
"name": "path2",
112+
"url": "https://dummy/upload/url",
113+
},
114+
],
115+
]
116+
`);
63117
});
64118

65119
it('works with assets (list)', async () => {
66-
tempWrite.sync.mockImplementation(() => `/temp/path`);
67-
globby.mockImplementation(path => Promise.resolve([path]));
68120
await createGitHubRelease(
69121
getDefaultParams({
70122
assetsToUpload: ['/path1', '/path2'],
71123
})
72124
);
73-
expect(run).toHaveBeenCalledTimes(1);
74-
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
125+
expect(createRelease).toHaveBeenCalledTimes(1);
126+
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
75127
Array [
76128
Object {
77-
"command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
78-
"dir": ".",
79-
"dryRun": false,
129+
"body": "",
130+
"name": "v1.2.3",
131+
"owner": "my",
132+
"repo": "repo",
133+
"tag_name": "v1.2.3",
80134
},
81135
]
82136
`);
137+
expect(uploadReleaseAsset).toHaveBeenCalledTimes(2);
138+
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
139+
Array [
140+
Array [
141+
Object {
142+
"file": undefined,
143+
"headers": Object {
144+
"content-length": 1024,
145+
"content-type": "application/zip",
146+
},
147+
"name": "path1",
148+
"url": "https://dummy/upload/url",
149+
},
150+
],
151+
Array [
152+
Object {
153+
"file": undefined,
154+
"headers": Object {
155+
"content-length": 1024,
156+
"content-type": "application/zip",
157+
},
158+
"name": "path2",
159+
"url": "https://dummy/upload/url",
160+
},
161+
],
162+
]
163+
`);
83164
});
84165

85166
it('works with assets (string)', async () => {
86-
tempWrite.sync.mockImplementation(() => `/temp/path`);
87-
globby.mockImplementation(path => Promise.resolve([path]));
88167
await createGitHubRelease(
89168
getDefaultParams({
90169
assetsToUpload: '/path1',
91170
})
92171
);
93-
expect(run).toHaveBeenCalledTimes(1);
94-
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
172+
expect(createRelease).toHaveBeenCalledTimes(1);
173+
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
95174
Array [
96175
Object {
97-
"command": "hub release create -F /temp/path -a /path1 v1.2.3",
98-
"dir": ".",
99-
"dryRun": false,
176+
"body": "",
177+
"name": "v1.2.3",
178+
"owner": "my",
179+
"repo": "repo",
180+
"tag_name": "v1.2.3",
100181
},
101182
]
102183
`);
184+
expect(uploadReleaseAsset).toHaveBeenCalledTimes(1);
185+
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
186+
Array [
187+
Array [
188+
Object {
189+
"file": undefined,
190+
"headers": Object {
191+
"content-length": 1024,
192+
"content-type": "application/zip",
193+
},
194+
"name": "path1",
195+
"url": "https://dummy/upload/url",
196+
},
197+
],
198+
]
199+
`);
103200
});
104201

105202
it('works with extractChangelog', async () => {
106-
tempWrite.sync.mockImplementation(() => `/temp/path`);
107-
globby.mockImplementation(path => Promise.resolve([path]));
108203
const mockExtractChangelog = jest
109204
.fn()
110205
.mockImplementation(({ version, dir }) => `# v${version} (${dir})`);
@@ -117,13 +212,15 @@ describe('createGitHubRelease', () => {
117212
version: '1.2.3',
118213
dir: '.',
119214
});
120-
expect(run).toHaveBeenCalledTimes(1);
121-
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
215+
expect(createRelease).toHaveBeenCalledTimes(1);
216+
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
122217
Array [
123218
Object {
124-
"command": "hub release create -F /temp/path v1.2.3",
125-
"dir": ".",
126-
"dryRun": false,
219+
"body": "# v1.2.3 (.)",
220+
"name": "v1.2.3",
221+
"owner": "my",
222+
"repo": "repo",
223+
"tag_name": "v1.2.3",
127224
},
128225
]
129226
`);

0 commit comments

Comments
 (0)