Skip to content

Commit

Permalink
Add npm.name to config.context and extend context for tagName (cl…
Browse files Browse the repository at this point in the history
…oses #933)
  • Loading branch information
webpro committed Aug 24, 2022
1 parent 898202c commit 627763f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
6 changes: 5 additions & 1 deletion docs/git.md
Expand Up @@ -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

Expand Down
24 changes: 13 additions & 11 deletions lib/plugin/GitBase.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin/git/Git.js
Expand Up @@ -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())) {
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions lib/plugin/npm/npm.js
Expand Up @@ -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;

Expand Down
17 changes: 12 additions & 5 deletions test/tasks.js
Expand Up @@ -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}` }
});

Expand All @@ -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();
Expand Down

0 comments on commit 627763f

Please sign in to comment.