-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (71 loc) · 2.41 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
const core = require('@actions/core');
const github = require('@actions/github');
const yaml = require('js-yaml');
async function run() {
try {
const gh_token = process.env.GITHUB_TOKEN
const octokit = github.getOctokit(token=gh_token)
if (!github.context.payload.pull_request) {
throw new Error(
"Payload doesn't contain `pull_request`. Make sure this Action is being triggered by a pull_request event (https://help.github.com/en/articles/events-that-trigger-workflows#pull-request-event-pull_request)."
)
}
const title = github.context.payload.pull_request.title
core.info(title)
let prefix_map = {
"API": "api",
"BENCH": "bench",
"BLD": "build",
"BUG": "bug",
"DEP": "deprecation",
"DEV": "development",
"DOC": "documentation",
"ENH": "enhancement",
"MAINT": "maintenance",
"REV": "revert",
"STY": "style",
"TST": "test",
"REL": "release",
}
const configPath = core.getInput('configuration-path', {required: false});
if (configPath !== '') {
try {
const config = await fetchConent(octokit, configPath)
prefix_map = yaml.safeLoad(config)
} catch (error) {
if (error.status !== 404) {
throw error
}
}
}
core.info(prefix_map)
let label = [];
for (const [key, value ] of Object.entries(prefix_map)) {
if (title.startsWith(key)) {
label.push(value)
break
}
}
if (label.length > 0) {
await octokit.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request.number,
labels: label
})
}
}
catch (error) {
core.setFailed(error.message)
}
}
async function fetchConent(client, repoPath) {
const response = await client.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha
});
return Buffer.from(response.data.content, response.data.encoding).toString();
}
run()