generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
70 lines (61 loc) 路 2.63 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
import * as core from "@actions/core";
import * as github from "@actions/github";
import matcher from "matcher";
import { getConfig } from "./config";
import { ConfigEntry } from "./ConfigEntry";
const CONFIG_FILENAME = "pr-branch-labeler.yml";
const defaults: ConfigEntry[] = [
{ label: "feature", head: "feature/*", base: undefined },
{ label: "bugfix", head: ["bugfix/*", "hotfix/*"], base: undefined },
{ label: "chore", head: "chore/*", base: undefined }
];
// Export the context to be able to mock the payload during tests.
export const context = github.context;
export async function run() {
const repoToken: string = core.getInput("repo-token", { required: true });
core.debug(`context: ${context ? JSON.stringify(context) : ''}`);
if (context && context.payload && context.payload.repository && context.payload.pull_request) {
const octokit = new github.GitHub(repoToken);
const repoConfig: ConfigEntry[] = await getConfig(octokit, CONFIG_FILENAME, context);
core.debug(`repoConfig: ${JSON.stringify(repoConfig)}`);
const config: ConfigEntry[] = repoConfig.length > 0 ? repoConfig : defaults;
core.debug(`config: ${JSON.stringify(config)}`);
const headRef = context.payload.pull_request.head.ref;
const baseRef = context.payload.pull_request.base.ref;
const labelsToAdd = config.reduce((labels: string[], entry) => {
if (entry.head && entry.base) {
if (isMatch(headRef, entry.head) && isMatch(baseRef, entry.base)) {
core.info(`Matched "${headRef}" to "${entry.head}" and "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
} else if (entry.head && isMatch(headRef, entry.head)) {
core.info(`Matched "${headRef}" to "${entry.head}". Setting label to "${entry.label}"`);
labels.push(entry.label);
} else if (entry.base && isMatch(baseRef, entry.base)) {
core.info(`Matched "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
return labels;
}, []);
if (labelsToAdd.length > 0) {
core.debug(`Adding labels: ${labelsToAdd}`);
await octokit.issues.addLabels({
issue_number: context.payload.pull_request.number,
labels: labelsToAdd,
...context.repo
});
}
}
}
function isMatch(ref: string, patterns: string | string[]): boolean {
return Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(ref, pattern))
: matcher.isMatch(ref, patterns);
}
try {
run();
} catch (error) {
core.error(`ERROR! ${JSON.stringify(error)}`);
core.setFailed(error.message);
throw error;
}