Skip to content

Commit

Permalink
fix: skip creating a release at github when hub cli is not found (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee committed Oct 21, 2019
1 parent c540ae5 commit c408a00
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
12 changes: 12 additions & 0 deletions packages/shipjs/src/helper/hubConfigured.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { silentExec } from 'shipjs-lib';

export default function hubConfigured() {
return (
silentExec(
`yes "" | GITHUB_TOKEN=${process.env.GITHUB_TOKEN || ''} hub api user`,
{
ignoreError: true,
}
).code === 0
);
}
5 changes: 5 additions & 0 deletions packages/shipjs/src/helper/hubInstalled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { silentExec } from 'shipjs-lib';

export default function hubInstalled() {
return silentExec('hub --version', { ignoreError: true }).code === 0;
}
2 changes: 2 additions & 0 deletions packages/shipjs/src/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export {
export { default as validateBeforePrepare } from './validateBeforePrepare';
export { default as getChangelog } from './getChangelog';
export { default as gitPush } from './gitPush';
export { default as hubInstalled } from './hubInstalled';
export { default as hubConfigured } from './hubConfigured';
17 changes: 4 additions & 13 deletions packages/shipjs/src/step/__tests__/checkHub.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { silentExec } from 'shipjs-lib';
import { print, exitProcess } from '../../util';
import { hubInstalled, hubConfigured } from '../../helper';
import { mockPrint } from '../../../tests/util';
import checkHub from '../checkHub';

describe('checkHub', () => {
it('prints error if it is not installed', () => {
const output = [];
mockPrint(print, output);
const nonZeroValue = 1;
silentExec.mockImplementationOnce(() => ({
code: nonZeroValue,
}));
hubInstalled.mockImplementationOnce(() => false);
checkHub();
expect(output).toMatchInlineSnapshot(`
Array [
Expand All @@ -26,14 +23,8 @@ describe('checkHub', () => {
it('prints error if it is not configured', () => {
const output = [];
mockPrint(print, output);
const nonZeroValue = 1;
silentExec
.mockImplementationOnce(() => ({
code: 0,
}))
.mockImplementationOnce(() => ({
code: nonZeroValue,
}));
hubInstalled.mockImplementationOnce(() => true);
hubConfigured.mockImplementationOnce(() => false);
checkHub();
expect(output).toMatchInlineSnapshot(`
Array [
Expand Down
25 changes: 8 additions & 17 deletions packages/shipjs/src/step/checkHub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { silentExec } from 'shipjs-lib';
import runStep from './runStep';
import { print, exitProcess } from '../util';
import { hubInstalled, hubConfigured } from '../helper';
import { error } from '../color';

export default () =>
Expand All @@ -9,27 +9,18 @@ export default () =>
title: 'Checking if `hub` exists.',
},
() => {
const exists =
silentExec('hub --version', { ignoreError: true }).code === 0;
if (!exists) {
if (!hubInstalled()) {
print(error('You need to install `hub` first.'));
print(
' > https://github.com/algolia/shipjs/blob/master/GUIDE.md#install-hub'
);
exitProcess(1);
} else {
const token = process.env.GITHUB_TOKEN || '';
const configured =
silentExec(`yes "" | GITHUB_TOKEN=${token} hub api user`, {
ignoreError: true,
}).code === 0;
if (!configured) {
print(error('You need to configure `hub`.'));
print(
' > https://github.com/algolia/shipjs/blob/master/GUIDE.md#install-hub'
);
exitProcess(1);
}
} else if (!hubConfigured()) {
print(error('You need to configure `hub`.'));
print(
' > https://github.com/algolia/shipjs/blob/master/GUIDE.md#install-hub'
);
exitProcess(1);
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import tempWrite from 'temp-write';
import globby from 'globby';
import createGitHubRelease from '../createGitHubRelease';
import { run } from '../../../util';
import { hubInstalled, hubConfigured } from '../../../helper';
jest.mock('temp-write');
jest.mock('globby');

Expand All @@ -17,6 +18,11 @@ const getDefaultParams = ({ assetsToUpload } = {}) => ({
});

describe('createGitHubRelease', () => {
beforeEach(() => {
hubInstalled.mockImplementation(() => true);
hubConfigured.mockImplementation(() => true);
});

it('works without assets', async () => {
tempWrite.sync.mockImplementation(() => `/my chan"ge"log/temp/path`);
await createGitHubRelease(getDefaultParams());
Expand Down
9 changes: 7 additions & 2 deletions packages/shipjs/src/step/release/createGitHubRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import tempWrite from 'temp-write';
import { quote } from 'shell-quote';
import runStep from '../runStep';
import { run } from '../../util';
import { getChangelog } from '../../helper';
import { getChangelog, hubInstalled, hubConfigured } from '../../helper';

const cannotUseHub = () => !hubInstalled() || !hubConfigured();

export default async ({ version, config, dir, dryRun }) =>
await runStep(
{ title: 'Creating a release on GitHub repository' },
{
title: 'Creating a release on GitHub repository',
skipIf: cannotUseHub,
},
async () => {
const { getTagName, releases, updateChangelog } = config;
const tagName = getTagName({ version });
Expand Down

0 comments on commit c408a00

Please sign in to comment.