-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (140 loc) · 4.41 KB
/
Copy pathindex.js
File metadata and controls
190 lines (140 loc) · 4.41 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const core = require("@actions/core");
const github = require("@actions/github");
const token = core.getInput("token");
const octokit = github.getOctokit(token);
const name = input("name", "1");
const amount = input("amount", "1");
const visibility = input("visibility", "all");
const push_to_org = (input("org", "") !== "");
const owner = input("owner", github.context.payload.repository.owner.login);
const repository = input("repository", github.context.payload.repository.name);
function path_() {
if (push_to_org) return "/orgs/" + owner;
if (repository.includes("/")) return "/repos/" + repository;
return "/repos/" + owner + "/" + repository;
}
function input(name, def) {
let inp = core.getInput(name).trim();
if (inp === "" || inp.toLowerCase() === "false") return def;
return inp;
}
function parseAmount(amount) {
const step = Number.parseInt(amount, 10);
if (!Number.isInteger(step) || step.toString() !== amount.trim()) {
throw new Error(`Invalid amount '${amount}', expected an integer.`);
}
return step;
}
function increment(string, step) {
const value = String(string || "");
// Extract string's numbers
const matches = [...value.matchAll(/\d+/g)];
if (matches.length === 0) {
throw new Error(`Value '${value}' does not contain a number to increment.`);
}
// Increment the last number by the amount
const lastMatch = matches[matches.length - 1];
const oldNumber = lastMatch[0];
const oldIndex = lastMatch.index;
let newNumber = (Number.parseInt(oldNumber, 10) + step).toString();
// Reconstruct the string with incremented number and leading zeroes
if (oldNumber.startsWith("0") && !newNumber.startsWith("-")) {
newNumber = newNumber.padStart(oldNumber.length, "0");
}
return value.slice(0, oldIndex) + newNumber + value.slice(oldIndex + oldNumber.length);
}
const createVariable = (data) => {
let url = "POST " + path_();
url += "/actions/variables";
if (push_to_org) {
return octokit.request(url, {
name: name,
visibility: visibility,
value: data
});
}
return octokit.request(url, {
name: name,
value: data
});
};
const setVariable = (data) => {
let url = "PATCH " + path_();
url += "/actions/variables/" + encodeURIComponent(name);
return octokit.request(url, {
name: name,
value: data
});
};
const getVariable = (varname) => {
let url = "GET " + path_();
url += "/actions/variables/" + encodeURIComponent(varname);
return octokit.request(url);
};
const bootstrap = async () => {
let exists = false;
let old_value = "";
const step = parseAmount(amount);
try {
if (name === "") {
throw new Error("No name was specified!");
}
const response = await getVariable(name);
exists = response.status === 200;
if (exists) old_value = response.data.value;
} catch (e) {
if (e.status !== 404) {
core.setFailed(path_() + ": " + e.message);
console.error(e);
return;
}
// Variable does not exist
}
try {
if (exists) {
let new_value = increment(old_value, step);
const response = await setVariable(new_value);
if (response.status === 204) {
core.setOutput("value", new_value);
if (step === 0) {
return ("Amount was set to zero, value stays at " + old_value + ".");
}
if (step < 0) {
return ("Successfully decremented " + name + " from " + old_value + " to " + new_value + ".");
}
if (step > 0) {
return ("Successfully incremented " + name + " from " + old_value + " to " + new_value + ".");
}
}
throw new Error("ERROR: Wrong status was returned: " + response.status);
} else {
const value = step.toString();
const response = await createVariable(value);
if (response.status === 201) {
core.setOutput("value", value);
return "Successfully created variable " + name + " with value " + value + ".";
}
throw new Error("ERROR: Wrong status was returned: " + response.status);
}
} catch (e) {
core.setFailed(path_() + ": " + e.message);
console.error(e);
}
};
bootstrap()
.then(
(result) => {
// eslint-disable-next-line no-console
if (result != null) {
console.log(result);
}
},
(err) => {
// eslint-disable-next-line no-console
core.setFailed(err.message);
console.error(err);
}
)
.then(() => {
process.exit();
});