Skip to content

Commit

Permalink
fix(release): more reliable branch detection
Browse files Browse the repository at this point in the history
Fixes dry-run issue on detached head checkouts.
  • Loading branch information
Jacob Wejendorp committed Jun 30, 2018
1 parent 565a897 commit bbba857
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"cross-env": "^5.0.5",
"cross-spawn": "^6.0.4",
"doctoc": "^1.3.0",
"env-ci": "^2.1.1",
"eslint": "^4.7.2",
"eslint-config-tradeshift": "^5.0.0",
"eslint-plugin-jest": "^21.17.0",
Expand Down
1 change: 1 addition & 0 deletions src/__mocks__/env-ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = jest.fn();
16 changes: 6 additions & 10 deletions src/scripts/__tests__/travis-after-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,24 @@ cases(
({ version = '0.0.0-semantically-released', hasCoverageDir = true, branch = 'master' }) => {
// beforeEach
const { sync: crossSpawnSyncMock } = require('cross-spawn');
const envCiMock = require('env-ci');

const utils = require('../../utils');
utils.resolveBin = (modName, { executable = modName } = {}) => executable;
const originalExit = process.exit;
process.exit = jest.fn();

// tests
crossSpawnSyncMock.mockClear();
crossSpawnSyncMock.mockReturnValue({
stdout: {
toString() {
return branch;
}
}
});
envCiMock.mockReturnValue({ branch });
if (version) {
utils.pkg.version = version;
}
utils.hasFile = filename => filename === 'coverage' && hasCoverageDir;
require('../travis-after-success');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2);
const [, secondCall] = crossSpawnSyncMock.mock.calls;
const [script, calledArgs] = secondCall;
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, calledArgs] = firstCall;
expect([script, ...calledArgs].join(' ')).toMatchSnapshot();

// afterEach
Expand Down
17 changes: 7 additions & 10 deletions src/scripts/travis-after-success.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const envCi = require('env-ci');
const spawn = require('cross-spawn');
const { resolveBin, getConcurrentlyArgs, hasFile, fromRoot, pkg } = require('../utils');

Expand All @@ -7,15 +7,10 @@ const autorelease = pkg.version === '0.0.0-semantically-released';
// We don't care about the TravisCI validation
const releaseConfig = hasFile('release.config.js') ? require(fromRoot('release.config.js')) : {};
const releaseBranch = releaseConfig.branch || 'master';
const currentBranch = spawn
.sync('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
.stdout.toString()
.trim();
const { branch } = envCi();
// Run a dry-run release when not on master
const dryRun = currentBranch !== releaseBranch;
const releaseFlags = ['--no-ci', dryRun ? '--dry-run' : '']
.filter(Boolean)
.join(' ');
const dryRun = branch !== releaseBranch;
const releaseFlags = ['--no-ci', dryRun ? '--dry-run' : ''].filter(Boolean).join(' ');

const result = spawn.sync(
resolveBin('concurrently'),
Expand All @@ -25,7 +20,9 @@ const result = spawn.sync(
? "echo installing codecov && npx -p codecov -c 'echo running codecov && codecov'"
: null,
release: autorelease
? `echo installing semantic-release && npx -p semantic-release@15 -c 'echo running semantic-release ${dryRun ? 'dry run' : ''} && semantic-release ${releaseFlags}'`
? `echo installing semantic-release && npx -p semantic-release@15 -c 'echo running semantic-release ${
dryRun ? 'dry run' : ''
} && semantic-release ${releaseFlags}'`
: null
},
{ killOthers: false }
Expand Down

0 comments on commit bbba857

Please sign in to comment.