Skip to content

Commit

Permalink
Fix/rollback tag (#1059)
Browse files Browse the repository at this point in the history
* fix: delete remote tag in case push of commit has failed

* docs: add .nvmrc file
  • Loading branch information
cakeinpanic committed Nov 11, 2023
1 parent e9fa310 commit da0e93a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
16
23 changes: 20 additions & 3 deletions lib/plugin/git/Git.js
Expand Up @@ -56,8 +56,11 @@ class Git extends GitBase {
const { tagName } = this.config.getContext();
const { isCommitted, isTagged } = this.getContext();
if (isTagged) {
this.log.info(`Deleting local tag ${tagName}`);
this.exec(`git tag --delete ${tagName}`);
}

this.log.info(`Resetting local changes made`);
this.exec(`git reset --hard HEAD${isCommitted ? '~1' : ''}`);
}

Expand Down Expand Up @@ -207,9 +210,23 @@ class Git extends GitBase {
async push({ args = this.options.pushArgs } = {}) {
const { pushRepo } = this.options;
const upstreamArgs = await this.getUpstreamArgs(pushRepo);
const push = await this.exec(['git', 'push', ...fixArgs(args), ...upstreamArgs]);
this.disableRollback();
return push;
try {
const push = await this.exec(['git', 'push', ...fixArgs(args), ...upstreamArgs]);
this.disableRollback();
return push;
} catch (error) {
await this.rollbackTagPush();
throw error;
}
}

async rollbackTagPush() {
const { isTagged } = this.getContext();
if (isTagged) {
const { tagName } = this.config.getContext();
this.log.info(`Rolling back remote tag push ${tagName}`);
await this.exec(`git push origin --delete ${tagName}`);
}
}

afterRelease() {
Expand Down
29 changes: 29 additions & 0 deletions test/git.js
Expand Up @@ -298,6 +298,35 @@ test.serial('should roll back when cancelled', async t => {
t.is(exec.args[12][0], 'git reset --hard HEAD~1');
});

test.serial('should remove remote tag when push to branch failed', async t => {
sh.exec('git init');
sh.exec(`git remote add origin file://foo`);
const version = '1.2.3';
gitAdd(`{"version":"${version}"}`, 'package.json', 'Add package.json');
const options = { git: { requireCleanWorkingDir: true, commit: true, tag: true, tagName: 'v${version}' } };
const gitClient = factory(Git, { options });
const exec = sinon.spy(gitClient.shell, 'execFormattedCommand');
sh.exec(`git push`);
sh.exec(`git checkout HEAD~1`);
gitAdd('line', 'file', 'Add file');

await gitClient.init();

sh.exec('npm --no-git-tag-version version patch');

gitClient.bump('1.2.4');
await gitClient.beforeRelease();
await gitClient.stage('package.json');
await gitClient.commit({ message: 'Add this' });
await gitClient.tag();
try {
await gitClient.push();
} catch (e) {
// push would fail with an error since HEAD is behind origin
}
t.is(exec.args[15][0], 'git push origin --delete v1.2.4');
});

test.serial('should not touch existing history when rolling back', async t => {
sh.exec('git init');
const version = '1.2.3';
Expand Down

0 comments on commit da0e93a

Please sign in to comment.