Skip to content

Commit

Permalink
Merge pull request #2 from salsify/annotate-tags
Browse files Browse the repository at this point in the history
Annotate tags by default
  • Loading branch information
dfreeman committed Jul 13, 2020
2 parents 785a5aa + f4d3114 commit c4476f4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: salsify/action-detect-and-tag-new-version@v1
- uses: salsify/action-detect-and-tag-new-version@v2
with:
version-command: |
cat current-version.txt
Expand All @@ -35,6 +35,10 @@ All inputs are optional.
- `create-tag`: may be set to `false` to only detect version changes and not create a new tag when the version changes.
- `tag-template`: a template for producing a tag name from the current version of your repository. Any instance of
`{VERSION}` in the string will be replaced with the actual detected version. Defaults to `v{VERSION}`.
- `tag-annotation-template`: a template for producing a tag annotation from the current version of your repository. As
with `tag-template`, any instance of `{VERSION}` in the string will be replaced with the actual detected version.
May be set to an empty string to produce a lightweight (i.e. annotation-less) tag. Defaults to
`Released version {VERSION}`.

### Outputs

Expand Down
10 changes: 5 additions & 5 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ describe('createTag', () => {
await initRepository('upstream');
await addAndTrackRemote('foo', 'upstream/.git');

await git.createTag('foo-bar');
await git.createTag('foo-bar', 'Here is my commit annotation.');

let localTag = await execa('git', ['tag']);
expect(localTag.stdout.trim()).toBe('foo-bar');
let localTag = await execa('git', ['tag', '-n1']);
expect(localTag.stdout.trim()).toMatch(/^foo-bar\s+Here is my commit annotation.$/);

let remoteTag = await execa('git', ['tag'], { cwd: 'upstream' });
expect(remoteTag.stdout.trim()).toBe('foo-bar');
let remoteTag = await execa('git', ['tag', '-n1'], { cwd: 'upstream' });
expect(remoteTag.stdout.trim()).toMatch(/^foo-bar\s+Here is my commit annotation.$/);
});
});
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ inputs:
A template for producing a tag name from the current version of your
repository, replacing '{VERSION}' with the deteced version.
default: 'v{VERSION}'
tag-annotation-template:
description: >
A template for producing a tag annotation from the current version of your
repository, replacing '{VERSION}' with the deteced version. May be set to
an empty string to produce a lightweight (i.e. annotation-less) tag.
default: 'Released version {VERSION}'
outputs:
previous-version:
description: the detected previous version of this repository
Expand Down
9 changes: 7 additions & 2 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export async function checkout(ref: string): Promise<void> {
await execa('git', ['checkout', ref]);
}

export async function createTag(name: string): Promise<void> {
await execa('git', ['tag', name]);
export async function createTag(name: string, annotation: string): Promise<void> {
let tagArgs = ['tag', name];
if (annotation.length) {
tagArgs.push('-m', annotation);
}

await execa('git', tagArgs);
await execa('git', ['push', '--tags']);
}
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { determineVersion } from './determine-version';
import { validateHistoryDepth, checkout, createTag, refExists } from './git';
import { getEnv } from './utils';

const VERSION_PLACEHOLDER = /{VERSION}/g;

async function run(): Promise<void> {
await validateHistoryDepth();
await checkout('HEAD~1');
Expand All @@ -21,15 +23,18 @@ async function run(): Promise<void> {

if (currentVersion !== previousVersion && getInput('create-tag') !== 'false') {
let tagTemplate = getInput('tag-template') || 'v{VERSION}';
let tag = tagTemplate.replace(/{VERSION}/g, currentVersion);
let tag = tagTemplate.replace(VERSION_PLACEHOLDER, currentVersion);

let annotationTemplate = getInput('tag-annotation-template') || 'Released version {VERSION}';
let annotation = annotationTemplate.replace(VERSION_PLACEHOLDER, currentVersion);

if (await refExists(tag)) {
info(`Tag ${tag} already exists`);
} else {
info(`Creating tag ${tag}`);
setOutput('tag', tag);

await createTag(tag);
await createTag(tag, annotation);
}
}
}
Expand Down

0 comments on commit c4476f4

Please sign in to comment.