Skip to content

Commit da0e93a

Browse files
authoredNov 11, 2023
Fix/rollback tag (#1059)
* fix: delete remote tag in case push of commit has failed * docs: add .nvmrc file
1 parent e9fa310 commit da0e93a

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed
 

Diff for: ‎.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

Diff for: ‎lib/plugin/git/Git.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ class Git extends GitBase {
5656
const { tagName } = this.config.getContext();
5757
const { isCommitted, isTagged } = this.getContext();
5858
if (isTagged) {
59+
this.log.info(`Deleting local tag ${tagName}`);
5960
this.exec(`git tag --delete ${tagName}`);
6061
}
62+
63+
this.log.info(`Resetting local changes made`);
6164
this.exec(`git reset --hard HEAD${isCommitted ? '~1' : ''}`);
6265
}
6366

@@ -207,9 +210,23 @@ class Git extends GitBase {
207210
async push({ args = this.options.pushArgs } = {}) {
208211
const { pushRepo } = this.options;
209212
const upstreamArgs = await this.getUpstreamArgs(pushRepo);
210-
const push = await this.exec(['git', 'push', ...fixArgs(args), ...upstreamArgs]);
211-
this.disableRollback();
212-
return push;
213+
try {
214+
const push = await this.exec(['git', 'push', ...fixArgs(args), ...upstreamArgs]);
215+
this.disableRollback();
216+
return push;
217+
} catch (error) {
218+
await this.rollbackTagPush();
219+
throw error;
220+
}
221+
}
222+
223+
async rollbackTagPush() {
224+
const { isTagged } = this.getContext();
225+
if (isTagged) {
226+
const { tagName } = this.config.getContext();
227+
this.log.info(`Rolling back remote tag push ${tagName}`);
228+
await this.exec(`git push origin --delete ${tagName}`);
229+
}
213230
}
214231

215232
afterRelease() {

Diff for: ‎test/git.js

+29
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,35 @@ test.serial('should roll back when cancelled', async t => {
298298
t.is(exec.args[12][0], 'git reset --hard HEAD~1');
299299
});
300300

301+
test.serial('should remove remote tag when push to branch failed', async t => {
302+
sh.exec('git init');
303+
sh.exec(`git remote add origin file://foo`);
304+
const version = '1.2.3';
305+
gitAdd(`{"version":"${version}"}`, 'package.json', 'Add package.json');
306+
const options = { git: { requireCleanWorkingDir: true, commit: true, tag: true, tagName: 'v${version}' } };
307+
const gitClient = factory(Git, { options });
308+
const exec = sinon.spy(gitClient.shell, 'execFormattedCommand');
309+
sh.exec(`git push`);
310+
sh.exec(`git checkout HEAD~1`);
311+
gitAdd('line', 'file', 'Add file');
312+
313+
await gitClient.init();
314+
315+
sh.exec('npm --no-git-tag-version version patch');
316+
317+
gitClient.bump('1.2.4');
318+
await gitClient.beforeRelease();
319+
await gitClient.stage('package.json');
320+
await gitClient.commit({ message: 'Add this' });
321+
await gitClient.tag();
322+
try {
323+
await gitClient.push();
324+
} catch (e) {
325+
// push would fail with an error since HEAD is behind origin
326+
}
327+
t.is(exec.args[15][0], 'git push origin --delete v1.2.4');
328+
});
329+
301330
test.serial('should not touch existing history when rolling back', async t => {
302331
sh.exec('git init');
303332
const version = '1.2.3';

0 commit comments

Comments
 (0)
Please sign in to comment.