From 627763f33962f148389fdaaa7489eea12ee05f09 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Wed, 24 Aug 2022 09:25:28 +0200 Subject: [PATCH] Add `npm.name` to config.context and extend context for `tagName` (closes #933) --- docs/git.md | 6 +++++- lib/plugin/GitBase.js | 24 +++++++++++++----------- lib/plugin/git/Git.js | 4 ++-- lib/plugin/npm/npm.js | 1 + test/tasks.js | 17 ++++++++++++----- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/docs/git.md b/docs/git.md index 5d7bfb1e..090f9ea8 100644 --- a/docs/git.md +++ b/docs/git.md @@ -41,7 +41,11 @@ different git url. Use `git.tagName` to set a custom tag, not strictly equal to the (prefixed) version. When the latest tag has the `v` prefix, it will be used again. No need to configure `git.tagName: "v${version}"` in this case. -Example: `--git.tagName=${name}@${version}` +Examples: + +- `--git.tagName=${branchName}-${version}` +- `--git.tagName=${repo.project}-${version}` +- `--git.tagName=${npm.name}@${version}` ## Tag Match diff --git a/lib/plugin/GitBase.js b/lib/plugin/GitBase.js index 072772e1..ee8bd053 100644 --- a/lib/plugin/GitBase.js +++ b/lib/plugin/GitBase.js @@ -7,16 +7,18 @@ const changelogFallback = 'git log --pretty=format:"* %s (%h)"'; class GitBase extends Plugin { async init() { - this.remoteUrl = await this.getRemoteUrl(); - await this.fetch(); + const remoteUrl = await this.getRemoteUrl(); + await this.fetch(remoteUrl); + const branchName = await this.getBranchName(); - this.setContext({ branchName }); - const repo = parseGitUrl(this.remoteUrl); - const latestTag = await this.getLatestTagName(repo); + const repo = parseGitUrl(remoteUrl); + this.setContext({ remoteUrl, branchName, repo }); + this.config.setContext({ remoteUrl, branchName, repo }); + + const latestTag = await this.getLatestTagName(); 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, branchName }); + this.config.setContext({ latestTag, secondLatestTag, tagTemplate }); } getName() { @@ -81,15 +83,15 @@ class GitBase extends Plugin { return this.exec(`git config --get branch.${branch}.remote`, { options }).catch(() => null); } - fetch() { + fetch(remoteUrl) { return this.exec('git fetch').catch(err => { this.debug(err); - throw new Error(`Unable to fetch from ${this.remoteUrl}${EOL}${err.message}`); + throw new Error(`Unable to fetch from ${remoteUrl}${EOL}${err.message}`); }); } - getLatestTagName(repo) { - const context = Object.assign({ repo }, this.getContext(), { version: '*' }); + getLatestTagName() { + const context = Object.assign({}, this.config.getContext(), { version: '*' }); const match = format(this.options.tagMatch || this.options.tagName || '${version}', context); return this.exec(`git describe --tags --match=${match} --abbrev=0`, { options }).then( stdout => stdout || null, diff --git a/lib/plugin/git/Git.js b/lib/plugin/git/Git.js index 4482711d..bf894451 100644 --- a/lib/plugin/git/Git.js +++ b/lib/plugin/git/Git.js @@ -39,7 +39,8 @@ class Git extends GitBase { await super.init(); - if (this.options.push && !this.remoteUrl) { + const remoteUrl = this.getContext('remoteUrl'); + if (this.options.push && !remoteUrl) { throw e(`Could not get remote Git url.${EOL}Please add a remote repository.`, docs); } if (this.options.requireUpstream && !(await this.hasUpstreamBranch())) { @@ -48,7 +49,6 @@ class Git extends GitBase { if (this.options.requireCommits && (await this.getCommitsSinceLatestTag()) === 0) { throw e(`There are no commits since the latest tag.`, docs); } - this.config.setContext({ repo: this.getContext('repo') }); } rollback() { diff --git a/lib/plugin/npm/npm.js b/lib/plugin/npm/npm.js index 8d4fce33..3524d477 100644 --- a/lib/plugin/npm/npm.js +++ b/lib/plugin/npm/npm.js @@ -29,6 +29,7 @@ class npm extends Plugin { async init() { const { name, version: latestVersion, private: isPrivate, publishConfig } = readJSON(path.resolve(MANIFEST_PATH)); this.setContext({ name, latestVersion, private: isPrivate, publishConfig }); + this.config.setContext({ npm: { name } }); const { publish, skipChecks } = this.options; diff --git a/test/tasks.js b/test/tasks.js index 671c63e6..51701efd 100644 --- a/test/tasks.js +++ b/test/tasks.js @@ -194,17 +194,22 @@ test.serial('should release with correct tag name', async t => { const { stdout } = sh.exec('git rev-parse --abbrev-ref HEAD'); const branchName = stdout.trim(); gitAdd(`{"name":"${pkgName}","version":"1.0.0"}`, 'package.json', 'Add package.json'); - sh.exec(`git tag ${branchName}-1.0.0`); + sh.exec(`git tag ${pkgName}-${branchName}-1.0.0`); const sha = gitAdd('line', 'file', 'More file'); interceptGitHubCreate({ owner, project, - body: { tag_name: `${branchName}-1.0.1`, name: 'Release 1.0.1', body: `* More file (${sha})`, prerelease: false } + body: { + tag_name: `${pkgName}-${branchName}-1.0.1`, + name: 'Release 1.0.1', + body: `* More file (${sha})`, + prerelease: false + } }); const container = getContainer({ - git: { tagName: '${branchName}-${version}' }, + git: { tagName: '${npm.name}-${branchName}-${version}' }, github: { release: true, skipChecks: true, pushRepo: `https://github.com/${owner}/${project}` } }); @@ -214,9 +219,11 @@ test.serial('should release with correct tag name', async t => { const gitArgs = getArgs(container.shell.exec.args, 'git'); - t.true(gitArgs.includes(`git tag --annotate --message Release 1.0.1 ${branchName}-1.0.1`)); + t.true(gitArgs.includes(`git tag --annotate --message Release 1.0.1 ${pkgName}-${branchName}-1.0.1`)); t.true( - log.log.secondCall.args[0].endsWith(`https://github.com/${owner}/${project}/releases/tag/${branchName}-1.0.1`) + log.log.secondCall.args[0].endsWith( + `https://github.com/${owner}/${project}/releases/tag/${pkgName}-${branchName}-1.0.1` + ) ); exec.restore();