Skip to content

Commit c7d65b3

Browse files
author
Ben Wilhelm
committed
Add option to return long tag format
1 parent 28ed6df commit c7d65b3

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

action.yml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
name: 'Query Tag'
2-
description: 'Queries for a git tag using git describe'
3-
author: 'Jim Schubert'
1+
name: "Query Tag"
2+
description: "Queries for a git tag using git describe"
3+
author: "Jim Schubert"
44
inputs:
55
include:
6-
description: 'Glob pattern of tags to include'
6+
description: "Glob pattern of tags to include"
77
required: false
8-
default: '*'
8+
default: "*"
99
exclude:
10-
description: 'Glob pattern of tags to exclude'
10+
description: "Glob pattern of tags to exclude"
1111
required: false
1212
commit-ish:
13-
description: 'Commit-ish object names to describe'
13+
description: "Commit-ish object names to describe"
1414
required: false
15-
default: 'HEAD~'
15+
default: "HEAD~"
1616
skip-unshallow:
1717
description: 'Skip the unshallow operation: "true" or "false"'
1818
required: false
19-
default: 'false'
19+
default: "false"
20+
long:
21+
description: "Return long format tag: <tagName>-<revisionsSinceTag>-<currentCommit>"
22+
required: false
23+
default: "false"
24+
2025
outputs:
2126
tag:
22-
description: 'The found tag'
27+
description: "The found tag"
2328
runs:
24-
using: 'node12'
25-
main: 'main.js'
29+
using: "node12"
30+
main: "main.js"
2631
branding:
27-
icon: 'hash'
28-
color: 'green'
32+
icon: "hash"
33+
color: "green"

main.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,64 @@
1-
const { exec } = require('child_process');
1+
const { exec } = require("child_process");
22

33
// github actions pass inputs as environment variables prefixed with INPUT_ and uppercased
44
function getInput(key) {
5-
var variable = 'INPUT_'+key;
6-
var result = process.env[variable.toUpperCase()];
7-
console.log(`Using input for ${key}: ${result}`);
8-
return result;
5+
var variable = "INPUT_" + key;
6+
var result = process.env[variable.toUpperCase()];
7+
console.log(`Using input for ${key}: ${result}`);
8+
return result;
99
}
1010

1111
// rather than npm install @actions/core, output using the console logging syntax
1212
// see https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter
1313
function setOutput(key, value) {
14-
console.log(`::set-output name=${key}::${value}`)
14+
console.log(`::set-output name=${key}::${value}`);
1515
}
1616

1717
try {
18-
const include = getInput('include');
19-
const exclude = getInput('exclude');
20-
const commitIsh = getInput('commit-ish');
21-
const skipUnshallow = getInput('skip-unshallow') === 'true';
18+
const include = getInput("include");
19+
const exclude = getInput("exclude");
20+
const commitIsh = getInput("commit-ish");
21+
const long = getInput("long") === "true";
22+
const skipUnshallow = getInput("skip-unshallow") === "true";
2223

23-
var includeOption = '';
24-
var excludeOption = '';
25-
var commitIshOption = '';
24+
var includeOption = "";
25+
var excludeOption = "";
26+
var commitIshOption = "";
27+
var abbrevOption = long ? "" : "--abbrev=0";
2628

27-
if (typeof include === 'string' && include.length > 0) {
28-
includeOption = `--match '${include}'`;
29-
}
29+
if (typeof include === "string" && include.length > 0) {
30+
includeOption = `--match '${include}'`;
31+
}
3032

31-
if (typeof exclude === 'string' && exclude.length > 0) {
32-
excludeOption = `--exclude '${exclude}'`;
33-
}
33+
if (typeof exclude === "string" && exclude.length > 0) {
34+
excludeOption = `--exclude '${exclude}'`;
35+
}
3436

35-
if (typeof commitIsh === 'string') {
36-
if (commitIsh === '' || commitIsh === 'HEAD') {
37-
console.warn('Passing empty string or HEAD to commit-ish will get the "current" tag rather than "previous". For previous tag, try "HEAD~".');
38-
}
39-
commitIshOption = `'${commitIsh}'`;
37+
if (typeof commitIsh === "string") {
38+
if (commitIsh === "" || commitIsh === "HEAD") {
39+
console.warn(
40+
'Passing empty string or HEAD to commit-ish will get the "current" tag rather than "previous". For previous tag, try "HEAD~".'
41+
);
4042
}
43+
commitIshOption = `'${commitIsh}'`;
44+
}
4145

42-
var unshallowCmd = skipUnshallow ? '' : 'git fetch --prune --unshallow &&'
46+
var unshallowCmd = skipUnshallow ? "" : "git fetch --prune --unshallow &&";
4347

44-
// actions@checkout performs a shallow checkout. Need to unshallow for full tags access.
45-
var cmd = `${unshallowCmd} git describe --tags --abbrev=0 ${includeOption} ${excludeOption} ${commitIshOption}`.replace(/[ ]+/, ' ').trim();
46-
console.log(`Executing: ${cmd}`);
48+
// actions@checkout performs a shallow checkout. Need to unshallow for full tags access.
49+
var cmd = `${unshallowCmd} git describe --tags ${abbrevOption} ${includeOption} ${excludeOption} ${commitIshOption}`
50+
.replace(/[ ]+/, " ")
51+
.trim();
52+
console.log(`Executing: ${cmd}`);
4753

48-
exec(cmd, (err, tag, stderr) => {
49-
if (err) {
50-
console.error(`Unable to find an earlier tag.\n${stderr}`);
51-
return process.exit(1);
52-
}
53-
console.log(`Outputting tag: ${tag.trim()}`)
54-
return setOutput('tag', tag.trim());
55-
});
54+
exec(cmd, (err, tag, stderr) => {
55+
if (err) {
56+
console.error(`Unable to find an earlier tag.\n${stderr}`);
57+
return process.exit(1);
58+
}
59+
console.log(`Outputting tag: ${tag.trim()}`);
60+
return setOutput("tag", tag.trim());
61+
});
5662
} catch (error) {
57-
core.setFailed(error.message);
63+
core.setFailed(error.message);
5864
}

0 commit comments

Comments
 (0)