-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
201 lines (158 loc) · 4.5 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
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
191
192
193
194
195
196
197
198
199
200
201
const core = require("@actions/core");
const github = require("@actions/github");
const axios = require("axios");
const token = core.getInput("token");
const octokit = github.getOctokit(token);
const name = input("name", "1");
const amount = input("amount", "1");
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;
}
/**
*
* @param name
* @param def
*/
function input(name, def) {
let inp = core.getInput(name).trim();
if (inp === "" || inp.toLowerCase() === "false") return def;
return inp;
}
/**
*
* @param string
* @param amount
*/
function increment(string, amount) {
// Extract string's numbers
var numbers = string.match(/\d+/g) || [];
// Increment the last number by the amount
var lastNumberIndex = numbers.length - 1;
var lastNumber = parseInt(numbers[lastNumberIndex], 10) || 0;
numbers[lastNumberIndex] = (lastNumber + parseInt(amount, 10)).toString();
// Reconstruct the string with incremented numbers and leading zeroes
var result = string.replace(/\d+/g, function(match) {
var currentNumber = numbers.shift();
if (match.startsWith("0")) {
while (currentNumber.length < match.length) {
currentNumber = "0" + currentNumber;
}
}
return currentNumber;
});
return result;
}
const createVariable = (data) => {
let url = "POST " + path_();
url += "/actions/variables";
return octokit.request(url, {
owner: owner,
repo: repository,
name: name,
value: data
});
};
const setVariable = (data) => {
let url = "PATCH " + path_();
url += "/actions/variables/" + name;
return octokit.request(url, {
owner: owner,
repo: repository,
name: name,
value: data
});
};
const getVariable = (varname) => {
let url = "GET " + path_();
url += "/actions/variables/" + varname;
return octokit.request(url, {
owner: owner,
repo: repository,
name: varname
});
};
const bootstrap = async () => {
await validateSubscription();
let exists = false;
let old_value = "";
try {
const response = await getVariable(name);
exists = response.status === 200;
if (exists) old_value = response.data.value;
} catch (e) {
// Variable does not exist
}
try {
if (name === "") {
throw new Error("No name was specified!");
}
if (exists) {
let new_value = increment(old_value, amount);
const response = await setVariable(new_value);
if (response.status === 204) {
core.setOutput("value", new_value);
if (parseInt(amount, 10) === 0) {
return ("Amount was set to zero, value stays at " + old_value + ".");
}
if (parseInt(amount, 10) < 0) {
return ("Succesfully decremented " + name + " from " + old_value + " to " + new_value + ".");
}
if (parseInt(amount, 10) > 0) {
return ("Succesfully incremented " + name + " from " + old_value + " to " + new_value + ".");
}
}
throw new Error("ERROR: Wrong status was returned: " + response.status);
} else {
const response = await createVariable(amount);
if (response.status === 201) {
core.setOutput("value", amount);
return "Succesfully created variable " + name + " with value " + amount + ".";
}
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();
});
/**
*
*/
async function validateSubscription() {
const API_URL = `https://agent.api.stepsecurity.io/v1/github/${process.env.GITHUB_REPOSITORY}/actions/subscription`;
try {
await axios.get(API_URL, { timeout: 3000 });
} catch (error) {
if (error.response) {
console.error("Subscription is not valid. Reach out to support@stepsecurity.io");
process.exit(1);
} else {
core.info("Timeout or API not reachable. Continuing to next step.");
}
}
}