-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
134 lines (116 loc) · 3.54 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const core = require("@actions/core");
const github = require("@actions/github");
const { parse } = require("csv-parse/sync");
async function run() {
try {
const tagsInput = core.getInput("tags", { required: true });
const defaultRef = core.getInput("ref");
const token = core.getInput("github_token", { required: true });
const whenExists = core.getInput("when_exists") || "update";
const octokit = github.getOctokit(token);
const parsedTags = parse(tagsInput, {
delimiter: ",",
trim: true,
relax_column_count: true,
}).flat();
const { owner, repo } = github.context.repo;
const uniqueRefs = new Set();
const refToSha = {};
const tags = {};
for (const tag of parsedTags) {
const [t, tagRef] = tag.split(":").map((s) => s.trim());
const ref = tagRef || defaultRef;
tags[t] = ref;
uniqueRefs.add(ref);
}
// Pre-resolve all unique refs
for (const ref of uniqueRefs) {
refToSha[ref] = await resolveRefToSha(octokit, owner, repo, ref);
}
const created = [];
const updated = [];
// Create or update all tags by looping through tags
for (const tagName in tags) {
if (!tagName) {
core.setFailed(`Invalid tag: '${tagName}'`);
return;
}
const tagRef = tags[tagName];
const sha = refToSha[tagRef];
try {
// Check if the ref exists
const existing = await octokit.rest.git.getRef({
owner,
repo,
ref: `tags/${tagName}`,
});
// If the ref exists, decide action based on 'when_exists'
if (whenExists === "update") {
const existingSHA = existing.data.object.sha;
if (existingSHA === sha) {
core.info(
`Tag '${tagName}' already exists with desired SHA ${sha}.`
);
continue;
}
core.info(
`Tag '${tagName}' exists, updating to SHA ${sha} ` +
`(was ${existingSHA}).`
);
await octokit.rest.git.updateRef({
owner,
repo,
ref: `tags/${tagName}`,
sha,
force: true,
});
updated.push(tagName);
} else if (whenExists === "skip") {
core.info(`Tag '${tagName}' exists, skipping.`);
} else if (whenExists === "fail") {
core.setFailed(`Tag '${tagName}' already exists.`);
return;
} else {
core.setFailed(
`Invalid value for 'when_exists': '${whenExists}'. ` +
`Valid values are 'update', 'skip', and 'fail'.`
);
return;
}
} catch (error) {
if (error.status !== 404) {
throw error;
}
// If the ref doesn't exist, create it
core.info(`Tag '${tagName}' does not exist, creating with SHA ${sha}.`);
await octokit.rest.git.createRef({
owner,
repo,
ref: `refs/tags/${tagName}`,
sha,
});
created.push(tagName);
}
}
core.setOutput("created", created);
core.setOutput("updated", updated);
core.setOutput("tags", created.concat(updated));
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);
}
}
async function resolveRefToSha(octokit, owner, repo, ref) {
try {
const {
data: { sha },
} = await octokit.rest.repos.getCommit({
owner,
repo,
ref,
});
return sha;
} catch (error) {
core.setFailed(`Failed to resolve ref '${ref}' to a SHA: ${error}`);
}
}
run();