Skip to content

Commit

Permalink
fix: create github release with rest api instead of hub (#458)
Browse files Browse the repository at this point in the history
* fix: create github release with rest api instead of hub

* fix: wrong argument

* fix: add missing import

* fix: lint error
  • Loading branch information
Eunjae Lee authored Nov 27, 2019
1 parent 1badafd commit 0bfeda6
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 80 deletions.
1 change: 1 addition & 0 deletions packages/shipjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"esm": "3.2.25",
"globby": "^10.0.1",
"inquirer": "7.0.0",
"mime-types": "^2.1.25",
"mkdirp": "^0.5.1",
"open": "^7.0.0",
"prettier": "^1.18.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/helper/getChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { extractSpecificChangelog } from './';
export default function getChangelog({ version, dir }) {
const changelogPath = path.resolve(dir, 'CHANGELOG.md');
try {
const changelogFile = fs.readFileSync(changelogPath, 'utf-8').toString();
return extractSpecificChangelog({ changelogFile, version });
const changelog = fs.readFileSync(changelogPath, 'utf-8').toString();
return extractSpecificChangelog({ changelog, version });
} catch (err) {
if (err.code === 'ENOENT') {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { getRepoInfo } from 'shipjs-lib';
import tempWrite from 'temp-write';
import Octokit from '@octokit/rest';
import createPullRequest from '../createPullRequest';
import { run } from '../../../util';
import { getDestinationBranchName } from '../../../helper';
jest.mock('temp-write');
jest.mock('@octokit/rest');

const getDefaultParams = ({
Expand Down Expand Up @@ -43,7 +41,6 @@ describe('createPullRequest', () => {
branch: 'master',
url: 'https://github.com/my/repo',
}));
tempWrite.sync.mockImplementationOnce(() => '/temp/path');
getDestinationBranchName.mockImplementation(() => 'master');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/step/prepare/createPullRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async ({
run({ command: `git remote prune ${remote}`, dir, dryRun });

if (dryRun) {
print('Creates a pull request with the following:');
print('Creating a pull request with the following:');
print(` - Title: ${title}`);
print(` - Message: ${message}`);
return {};
Expand Down
167 changes: 132 additions & 35 deletions packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import tempWrite from 'temp-write';
import globby from 'globby';
import fs from 'fs';
import mime from 'mime-types';
import { getRepoInfo } from 'shipjs-lib';
import Octokit from '@octokit/rest';
import createGitHubRelease from '../createGitHubRelease';
import { run } from '../../../util';
import { hubInstalled, hubConfigured } from '../../../helper';
jest.mock('temp-write');
jest.mock('@octokit/rest');
jest.mock('globby');
jest.mock('shipjs-lib');
jest.mock('fs');
jest.mock('mime-types');

const getDefaultParams = ({
assetsToUpload,
Expand All @@ -20,91 +26,180 @@ const getDefaultParams = ({
dryRun: false,
});

const createRelease = jest.fn().mockImplementation(() => ({
data: {
upload_url: 'https://dummy/upload/url', // eslint-disable-line camelcase
},
}));
const uploadReleaseAsset = jest.fn();
Octokit.mockImplementation(function() {
this.repos = { createRelease, uploadReleaseAsset };
});

describe('createGitHubRelease', () => {
beforeEach(() => {
hubInstalled.mockImplementation(() => true);
hubConfigured.mockImplementation(() => true);
getRepoInfo.mockImplementation(() => ({
owner: 'my',
name: 'repo',
}));
fs.readFileSync = jest.fn();
fs.statSync = jest.fn().mockImplementation(() => ({ size: 1024 }));
mime.lookup.mockImplementation(() => 'application/zip');
globby.mockImplementation(path => Promise.resolve([path]));
});

it('works without assets', async () => {
tempWrite.sync.mockImplementation(() => `/my chan"ge"log/temp/path`);
await createGitHubRelease(getDefaultParams());
expect(run).toHaveBeenCalledTimes(1);
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
expect(createRelease).toHaveBeenCalledTimes(1);
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"command": "hub release create -F '/my chan\\"ge\\"log/temp/path' v1.2.3",
"dir": ".",
"dryRun": false,
"body": "",
"name": "v1.2.3",
"owner": "my",
"repo": "repo",
"tag_name": "v1.2.3",
},
]
`);
expect(uploadReleaseAsset).toHaveBeenCalledTimes(0);
});

it('works with assets (fn)', async () => {
tempWrite.sync.mockImplementation(() => `/temp/path`);
await createGitHubRelease(
getDefaultParams({
assetsToUpload: () => {
return Promise.resolve(['/path1', '/path2']);
},
})
);
expect(run).toHaveBeenCalledTimes(1);
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
expect(createRelease).toHaveBeenCalledTimes(1);
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
"dir": ".",
"dryRun": false,
"body": "",
"name": "v1.2.3",
"owner": "my",
"repo": "repo",
"tag_name": "v1.2.3",
},
]
`);
expect(uploadReleaseAsset).toHaveBeenCalledTimes(2);
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"file": undefined,
"headers": Object {
"content-length": 1024,
"content-type": "application/zip",
},
"name": "path1",
"url": "https://dummy/upload/url",
},
],
Array [
Object {
"file": undefined,
"headers": Object {
"content-length": 1024,
"content-type": "application/zip",
},
"name": "path2",
"url": "https://dummy/upload/url",
},
],
]
`);
});

it('works with assets (list)', async () => {
tempWrite.sync.mockImplementation(() => `/temp/path`);
globby.mockImplementation(path => Promise.resolve([path]));
await createGitHubRelease(
getDefaultParams({
assetsToUpload: ['/path1', '/path2'],
})
);
expect(run).toHaveBeenCalledTimes(1);
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
expect(createRelease).toHaveBeenCalledTimes(1);
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
"dir": ".",
"dryRun": false,
"body": "",
"name": "v1.2.3",
"owner": "my",
"repo": "repo",
"tag_name": "v1.2.3",
},
]
`);
expect(uploadReleaseAsset).toHaveBeenCalledTimes(2);
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"file": undefined,
"headers": Object {
"content-length": 1024,
"content-type": "application/zip",
},
"name": "path1",
"url": "https://dummy/upload/url",
},
],
Array [
Object {
"file": undefined,
"headers": Object {
"content-length": 1024,
"content-type": "application/zip",
},
"name": "path2",
"url": "https://dummy/upload/url",
},
],
]
`);
});

it('works with assets (string)', async () => {
tempWrite.sync.mockImplementation(() => `/temp/path`);
globby.mockImplementation(path => Promise.resolve([path]));
await createGitHubRelease(
getDefaultParams({
assetsToUpload: '/path1',
})
);
expect(run).toHaveBeenCalledTimes(1);
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
expect(createRelease).toHaveBeenCalledTimes(1);
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"command": "hub release create -F /temp/path -a /path1 v1.2.3",
"dir": ".",
"dryRun": false,
"body": "",
"name": "v1.2.3",
"owner": "my",
"repo": "repo",
"tag_name": "v1.2.3",
},
]
`);
expect(uploadReleaseAsset).toHaveBeenCalledTimes(1);
expect(uploadReleaseAsset.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"file": undefined,
"headers": Object {
"content-length": 1024,
"content-type": "application/zip",
},
"name": "path1",
"url": "https://dummy/upload/url",
},
],
]
`);
});

it('works with extractChangelog', async () => {
tempWrite.sync.mockImplementation(() => `/temp/path`);
globby.mockImplementation(path => Promise.resolve([path]));
const mockExtractChangelog = jest
.fn()
.mockImplementation(({ version, dir }) => `# v${version} (${dir})`);
Expand All @@ -117,13 +212,15 @@ describe('createGitHubRelease', () => {
version: '1.2.3',
dir: '.',
});
expect(run).toHaveBeenCalledTimes(1);
expect(run.mock.calls[0]).toMatchInlineSnapshot(`
expect(createRelease).toHaveBeenCalledTimes(1);
expect(createRelease.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"command": "hub release create -F /temp/path v1.2.3",
"dir": ".",
"dryRun": false,
"body": "# v1.2.3 (.)",
"name": "v1.2.3",
"owner": "my",
"repo": "repo",
"tag_name": "v1.2.3",
},
]
`);
Expand Down
Loading

0 comments on commit 0bfeda6

Please sign in to comment.