-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.ts
233 lines (202 loc) · 6.53 KB
/
main.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as yaml from 'js-yaml';
async function run() {
try {
// Configuration parameters
var configPath = core.getInput('configuration-path', { required: true });
const token = core.getInput('repo-token', { required: true });
const enableVersionedRegex = parseInt(core.getInput('enable-versioned-regex', { required: true }));
const versionedRegex = new RegExp(core.getInput('versioned-regex', { required: false }));
const notBefore = Date.parse(core.getInput('not-before', { required: false }));
const bodyMissingRexexLabel = core.getInput('body-missing-regex-label', { required: false });
const issue_number = getIssueNumber();
const issue_body = getIssueBody();
if (!issue_number || !issue_body) {
console.log('Could not get issue number or issue body from context, exiting');
return;
}
// A client to load data from GitHub
const client = new github.GitHub(token);
const addLabel: string[] = []
const removeLabelItems: string[] = []
if (enableVersionedRegex == 1) {
const regexVersion = versionedRegex.exec(issue_body)
if (!regexVersion || !regexVersion[1]) {
if (bodyMissingRexexLabel) {
addLabels(client, issue_number, [bodyMissingRexexLabel]);
}
console.log(`Issue #${issue_number} does not contain regex version in the body of the issue, exiting.`)
return 0;
}
else {
if (bodyMissingRexexLabel) {
removeLabelItems.push(bodyMissingRexexLabel);
}
}
configPath = regexifyConfigPath(configPath, regexVersion[1])
}
// If the notBefore parameter has been set to a valid timestamp, exit if the current issue was created before notBefore
if (notBefore) {
const issue = client.issues.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issue_number,
});
const issueCreatedAt = Date.parse((await issue).data.created_at)
if (issueCreatedAt < notBefore) {
console.log("Issue is before `notBefore` configuration parameter. Exiting...")
process.exit(0);
}
}
// Load our regex rules from the configuration path
const labelRegexes: Map<string, string[]> = await getLabelRegexes(
client,
configPath
);
for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issue_body, globs)) {
addLabel.push(label)
}
else {
removeLabelItems.push(label)
}
}
if (addLabel.length > 0) {
console.log(`Adding labels ${addLabel.toString()} to issue #${issue_number}`)
addLabels(client, issue_number, addLabel)
}
removeLabelItems.forEach(function (label, index) {
console.log(`Removing label ${label} from issue #${issue_number}`)
removeLabel(client, issue_number, label)
});
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
function getIssueNumber(): number | undefined {
const issue = github.context.payload.issue;
if (!issue) {
return;
}
return issue.number;
}
function getIssueBody(): string | undefined {
const issue = github.context.payload.issue;
if (!issue) {
return;
}
return issue.body;
}
function regexifyConfigPath(configPath: string, version: string) {
var lastIndex = configPath.lastIndexOf('.')
return `${configPath.substr(0, lastIndex)}-v${version}.yml`
}
async function getLabelRegexes(
client: github.GitHub,
configurationPath: string
): Promise<Map<string, string[]>> {
const configurationContent: string = await fetchContent(
client,
configurationPath
);
// loads (hopefully) a `{[label:string]: string | string[]}`, but is `any`:
const configObject: any = yaml.safeLoad(configurationContent);
// transform `any` => `Map<string,string[]>` or throw if yaml is malformed:
return getLabelRegexesMapFromObject(configObject);
}
// Load the configuration file
async function fetchContent(
client: github.GitHub,
repoPath: string
): Promise<string> {
const response = await client.repos.getContents({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha
});
const data: any = response.data
if (!data.content) {
console.log('The configuration path provided is not a valid file. Exiting')
process.exit(1);
}
return Buffer.from(data.content, 'base64').toString('utf8');
}
function getLabelRegexesMapFromObject(configObject: any): Map<string, string[]> {
const labelRegexes: Map<string, string[]> = new Map();
for (const label in configObject) {
if (typeof configObject[label] === 'string') {
labelRegexes.set(label, [configObject[label]]);
} else if (Array.isArray(configObject[label])) {
labelRegexes.set(label, configObject[label]);
} else {
throw Error(
`found unexpected type for label ${label} (should be string or array of regex)`
);
}
}
return labelRegexes;
}
function checkRegexes(issue_body: string, regexes: string[]): boolean {
var found;
// If several regex entries are provided we require all of them to match for the label to be applied.
for (const regEx of regexes) {
const isRegEx = regEx.match(/^\/(.+)\/(.*)$/)
if (isRegEx) {
found = issue_body.match(new RegExp(isRegEx[1], isRegEx[2]))
} else {
found = issue_body.match(regEx)
}
if (!found) {
return false;
}
}
return true;
}
async function getLabels(
client: github.GitHub,
issue_number: number,
): Promise<string[]> {
const response = await client.issues.listLabelsOnIssue({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issue_number,
});
const data = response.data
if (response.status != 200) {
console.log('Unable to load labels. Exiting...')
process.exit(1);
}
const labels: string[] = [];
for (let i = 0; i < Object.keys(data).length; i++) {
labels.push(data[i].name)
}
return labels;
}
async function addLabels(
client: github.GitHub,
issue_number: number,
labels: string[]
) {
await client.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issue_number,
labels: labels
});
}
async function removeLabel(
client: github.GitHub,
issue_number: number,
name: string
) {
await client.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issue_number,
name: name
});
}
run();