Skip to content

Commit

Permalink
Create release draft even when pushing to protected branches failed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bunysae committed Aug 29, 2020
1 parent 7e64cc6 commit 3fc1e77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 14 additions & 0 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ exports.commitLogFromRevision = async revision => {
return stdout;
};

exports.pushGraceful = async remoteIsOnGitHub => {
try {
await exports.push();
} catch (error) {
if (remoteIsOnGitHub && error.stderr && error.stderr.includes('GH006')) {
// Try to push tags only, when commits can't be pushed due to branch protection
await execa('git', ['push', '--tags']);
return {pushed: 'tags', reason: 'Branch protection: np can`t push the commits. Push them manually.'};
}

throw error;
}
};

exports.push = async () => {
await execa('git', ['push', '--follow-tags']);
};
Expand Down
10 changes: 9 additions & 1 deletion source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const pkgDir = require('pkg-dir');
const hostedGitInfo = require('hosted-git-info');
const onetime = require('onetime');
const exitHook = require('async-exit-hook');
const logSymbols = require('log-symbols');
const prerequisiteTasks = require('./prerequisite-tasks');
const gitTasks = require('./git-tasks');
const publish = require('./npm/publish');
Expand Down Expand Up @@ -58,6 +59,7 @@ module.exports = async (input = 'patch', options) => {
const testCommand = options.testScript ? ['run', testScript] : [testScript];

let publishStatus = 'UNKNOWN';
let pushedObjects;

const rollback = onetime(async () => {
console.log('\nPublish failed. Rolling back to the previous state…');
Expand Down Expand Up @@ -250,7 +252,9 @@ module.exports = async (input = 'patch', options) => {
return 'Couldn\'t publish package to npm; not pushing.';
}
},
task: () => git.push()
task: async () => {
pushedObjects = await git.pushGraceful(isOnGitHub);
}
});

tasks.add({
Expand All @@ -268,6 +272,10 @@ module.exports = async (input = 'patch', options) => {

await tasks.run();

if (pushedObjects) {
console.error(`\n${logSymbols.error} ${pushedObjects.reason}`);
}

const {packageJson: newPkg} = await readPkgUp();
return newPkg;
};
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('skip enabling 2FA if the package exists', async t => {
'./git-tasks': sinon.stub(),
'./git-util': {
hasUpstream: sinon.stub().returns(true),
push: sinon.stub()
pushGraceful: sinon.stub()
},
'./npm/enable-2fa': enable2faStub,
'./npm/publish': sinon.stub().returns({pipe: sinon.stub()})
Expand All @@ -72,7 +72,7 @@ test('skip enabling 2FA if the `2fa` option is false', async t => {
'./git-tasks': sinon.stub(),
'./git-util': {
hasUpstream: sinon.stub().returns(true),
push: sinon.stub()
pushGraceful: sinon.stub()
},
'./npm/enable-2fa': enable2faStub,
'./npm/publish': sinon.stub().returns({pipe: sinon.stub()})
Expand Down

0 comments on commit 3fc1e77

Please sign in to comment.