Skip to content

Commit

Permalink
Lift tagName to global/config context
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 11, 2021
1 parent 676eced commit 628edc6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 31 deletions.
24 changes: 12 additions & 12 deletions lib/plugin/GitBase.js
Expand Up @@ -10,11 +10,11 @@ class GitBase extends Plugin {
this.remoteUrl = await this.getRemoteUrl();
await this.fetch();
const repo = parseGitUrl(this.remoteUrl);
const latestTagName = await this.getLatestTagName(repo);
const secondLatestTagName = !this.config.isIncrement ? await this.getSecondLatestTagName(latestTagName) : null;
const tagTemplate = this.options.tagName || ((latestTagName || '').match(/^v/) ? 'v${version}' : '${version}');
this.setContext({ repo, tagTemplate, latestTagName, secondLatestTagName });
this.config.setContext({ latestTag: latestTagName });
const latestTag = await this.getLatestTagName(repo);
const secondLatestTag = !this.config.isIncrement ? await this.getSecondLatestTagName(latestTag) : null;
const tagTemplate = this.options.tagName || ((latestTag || '').match(/^v/) ? 'v${version}' : '${version}');
this.setContext({ repo });
this.config.setContext({ tagTemplate, latestTag, secondLatestTag });
}

getName() {
Expand All @@ -27,14 +27,14 @@ class GitBase extends Plugin {
}

async getChangelog() {
const { latestTagName, secondLatestTagName } = this.getContext();
const context = { latestTag: latestTagName, from: latestTagName, to: 'HEAD' };
const { latestTag, secondLatestTag } = this.config.getContext();
const context = { latestTag, from: latestTag, to: 'HEAD' };
const { changelog } = this.options;
if (!changelog) return null;

if (latestTagName && !this.config.isIncrement) {
context.from = secondLatestTagName;
context.to = `${latestTagName}^1`;
if (latestTag && !this.config.isIncrement) {
context.from = secondLatestTag;
context.to = `${latestTag}^1`;
}

if (!context.from && changelog.includes('${from}')) {
Expand All @@ -45,10 +45,10 @@ class GitBase extends Plugin {
}

bump(version) {
const { tagTemplate } = this.getContext();
const { tagTemplate } = this.config.getContext();
const context = Object.assign(this.config.getContext(), { version });
const tagName = format(tagTemplate, context) || version;
this.setContext({ version, tagName });
this.setContext({ version });
this.config.setContext({ tagName });
}

Expand Down
9 changes: 5 additions & 4 deletions lib/plugin/git/Git.js
Expand Up @@ -46,7 +46,8 @@ class Git extends GitBase {

rollback() {
this.log.info('Rolling back changes...');
const { isCommitted, isTagged, tagName } = this.getContext();
const { tagName } = this.config.getContext();
const { isCommitted, isTagged } = this.getContext();
if (isTagged) {
this.exec(`git tag --delete ${tagName}`);
}
Expand Down Expand Up @@ -175,12 +176,12 @@ class Git extends GitBase {

tag({ name, annotation = this.options.tagAnnotation, args = this.options.tagArgs } = {}) {
const message = format(annotation, this.config.getContext());
const tagName = name || this.getContext('tagName');
const tagName = name || this.config.getContext('tagName');
return this.exec(['git', 'tag', '--annotate', '--message', message, ...fixArgs(args), tagName])
.then(() => this.setContext({ isTagged: true }))
.catch(err => {
const { latestTagName, tagName } = this.getContext();
if (/tag '.+' already exists/.test(err) && latestTagName === tagName) {
const { latestTag, tagName } = this.config.getContext();
if (/tag '.+' already exists/.test(err) && latestTag === tagName) {
this.log.warn(`Tag "${tagName}" already exists`);
} else {
throw err;
Expand Down
9 changes: 5 additions & 4 deletions lib/plugin/github/GitHub.js
Expand Up @@ -61,10 +61,10 @@ class GitHub extends Release {
}

if (update) {
const { latestTagName } = this.getContext();
const { latestTag } = this.config.getContext();
try {
const { id, upload_url, tag_name } = await this.getLatestRelease();
if (latestTagName === tag_name) {
if (latestTag === tag_name) {
this.setContext({ isUpdate: true, isReleased: true, releaseId: id, upload_url });
} else {
this.setContext({ isUpdate: false });
Expand All @@ -73,7 +73,7 @@ class GitHub extends Release {
this.setContext({ isUpdate: false });
}
if (!this.getContext('isUpdate')) {
this.log.warn(`GitHub release for tag ${latestTagName} was not found. Creating new release.`);
this.log.warn(`GitHub release for tag ${latestTag} was not found. Creating new release.`);
}
}
}
Expand Down Expand Up @@ -179,7 +179,8 @@ class GitHub extends Release {
getOctokitReleaseOptions(options = {}) {
const { owner, project: repo } = this.getContext('repo');
const { releaseName, draft = false, preRelease = false } = this.options;
const { version, tagName, releaseNotes } = this.getContext();
const { tagName } = this.config.getContext();
const { version, releaseNotes } = this.getContext();
const { isPreRelease } = parseVersion(version);
const name = format(releaseName, this.config.getContext());
const body = releaseNotes;
Expand Down
3 changes: 2 additions & 1 deletion lib/plugin/gitlab/GitLab.js
Expand Up @@ -132,7 +132,8 @@ class GitLab extends Release {

async createRelease() {
const { releaseName } = this.options;
const { id, tagName, releaseNotes, repo, origin } = this.getContext();
const { tagName } = this.config.getContext();
const { id, releaseNotes, repo, origin } = this.getContext();
const { isDryRun } = this.config;
const name = format(releaseName, this.config.getContext());
const description = releaseNotes || '-';
Expand Down
14 changes: 7 additions & 7 deletions test/git.init.js
Expand Up @@ -77,23 +77,23 @@ test.serial('should detect and include version prefix ("v")', async t => {
sh.exec('git tag v1.0.0');
await gitClient.init();
await gitClient.bump('1.0.1');
t.is(gitClient.getContext('tagName'), 'v1.0.1');
t.is(gitClient.config.getContext('tagName'), 'v1.0.1');
});

test.serial('should detect and exclude version prefix', async t => {
const gitClient = factory(Git, { options: { git } });
sh.exec('git tag 1.0.0');
await gitClient.init();
await gitClient.bump('1.0.1');
t.is(gitClient.getContext('tagName'), '1.0.1');
t.is(gitClient.config.getContext('tagName'), '1.0.1');
});

test.serial('should detect and exclude version prefix (configured)', async t => {
const gitClient = factory(Git, { options: { git: { tagName: 'v${version}' } } });
sh.exec('git tag 1.0.0');
await gitClient.init();
await gitClient.bump('1.0.1');
t.is(gitClient.getContext('tagName'), 'v1.0.1');
t.is(gitClient.config.getContext('tagName'), 'v1.0.1');
});

test.serial('should honor custom tagName configuration', async t => {
Expand All @@ -102,7 +102,7 @@ test.serial('should honor custom tagName configuration', async t => {
await gitClient.init();
await gitClient.bump('1.0.1');
const { project } = gitClient.getContext('repo');
t.is(gitClient.getContext('tagName'), `TAGNAME-${project}-v1.0.1`);
t.is(gitClient.config.getContext('tagName'), `TAGNAME-${project}-v1.0.1`);
});

test.serial('should get the latest tag after fetch', async t => {
Expand All @@ -117,7 +117,7 @@ test.serial('should get the latest tag after fetch', async t => {
sh.exec('git push --tags');
sh.pushd('-q', target);
await gitClient.init();
t.is(gitClient.getContext('latestTagName'), '1.0.0');
t.is(gitClient.config.getContext('latestTag'), '1.0.0');
});

test.serial('should get the latest custom tag after fetch when tagName is configured', async t => {
Expand All @@ -137,7 +137,7 @@ test.serial('should get the latest custom tag after fetch when tagName is config
sh.exec('git push --tags');
sh.pushd('-q', target);
await gitClient.init();
t.is(gitClient.getContext('latestTagName'), 'TAGNAME-v1.0.0');
t.is(gitClient.config.getContext('latestTag'), 'TAGNAME-v1.0.0');
});

test.serial('should get the latest tag based on tagMatch', async t => {
Expand All @@ -157,7 +157,7 @@ test.serial('should get the latest tag based on tagMatch', async t => {
sh.exec('git push --tags');
sh.pushd('-q', target);
await gitClient.init();
t.is(gitClient.getContext('latestTagName'), '21.04.3');
t.is(gitClient.config.getContext('latestTag'), '21.04.3');
});

test.serial('should generate correct changelog', async t => {
Expand Down
4 changes: 2 additions & 2 deletions test/git.js
Expand Up @@ -45,7 +45,7 @@ test.serial('should throw if tag exists', async t => {
sh.touch('file');
gitAdd('line', 'file', 'Add file');
sh.exec('git tag 0.0.2');
gitClient.setContext({ latestTagName: '0.0.1', tagName: '0.0.2' });
gitClient.config.setContext({ latestTag: '0.0.1', tagName: '0.0.2' });
const expected = { instanceOf: Error, message: /fatal: tag '0\.0\.2' already exists/ };
await t.throwsAsync(gitClient.tag({ name: '0.0.2' }), expected);
});
Expand All @@ -57,7 +57,7 @@ test.serial('should only warn if tag exists intentionally', async t => {
sh.touch('file');
gitAdd('line', 'file', 'Add file');
sh.exec('git tag 1.0.0');
gitClient.setContext({ latestTagName: '1.0.0', tagName: '1.0.0' });
gitClient.config.setContext({ latestTag: '1.0.0', tagName: '1.0.0' });
await t.notThrowsAsync(gitClient.tag());
t.is(warn.callCount, 1);
t.is(warn.firstCall.args[0], 'Tag "1.0.0" already exists');
Expand Down
3 changes: 2 additions & 1 deletion test/util/index.js
Expand Up @@ -68,7 +68,8 @@ export let runTasks = async plugin => {
await plugin.beforeBump();
await plugin.bump(version);

plugin.config.setContext({ tagName: version });
const tagName = plugin.config.getContext('tagName') || version;
plugin.config.setContext({ tagName });

await plugin.beforeRelease();
await plugin.release();
Expand Down

0 comments on commit 628edc6

Please sign in to comment.