-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (65 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const core = require('@actions/core');
const github = require('@actions/github');
const semver = require('semver');
async function createTag(major_tag) {
const token = core.getInput('github_token', { required: false }) || github.context.token;
const octokit = github.getOctokit(token);
const sha = core.getInput('sha') || github.context.sha;
const ref = `refs/tags/${major_tag}`;
await octokit.rest.git.createRef({
...github.context.repo,
ref,
sha
});
console.log(`Tag: "${major_tag}" with sha: "${sha}" successfully written.`);
}
async function checkIfTagExists(major_tag) {
const token = core.getInput('github_token', { required: false }) || github.context.token;
const octokit = github.getOctokit(token);
const owner = github.context.payload.repository.organization || github.context.payload.organization.login;
const repo = github.context.payload.repository.name;
const ref = `tags/${major_tag}`;
core.debug(`Owner: ${owner} - Repo: ${repo} - REF: ${ref}`);
try {
await octokit.rest.git.getRef({
owner,
repo,
ref,
});
try {
console.log(`Ref "${ref}" already exists. Removing to replace.`);
await octokit.rest.git.deleteRef({
owner,
repo,
ref,
});
console.log(`Successfully deleted ref. (ref: "${ref}")`);
}
catch (error) {
console.log(`Failed to delete ref (ref: ${ref}) - Error: "${error.message}"`);
}
}
catch (error) {
console.log(`Ref does not exist. (ref: ${ref}) - Error: "${error.message}"`);
}
}
async function run() {
try {
const tag = core.getInput('tag') || process.env.VERSION || (github.context.ref).replace('refs/tags/', '');
console.log(`Using tag: "${tag}"`);
const prefix = core.getInput('prefix') || "";
let major_tag = prefix + semver.major(tag);
core.setOutput('major_tag', major_tag);
core.setOutput('origin_tag', tag);
console.log(`Using tag prefix "${prefix}"`);
console.log(`Major Release Tag: "${major_tag}" (origin tag: "${tag}")`);
if (core.getInput('dry_run') !== 'true') {
await checkIfTagExists(major_tag);
await createTag(major_tag);
}
}
catch (error) {
core.setFailed(error.message);
}
}
run();