-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·67 lines (55 loc) · 1.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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const minimatch = require('minimatch')
const core = require('@actions/core')
const github = require('@actions/github')
const applyPackageLabels = async (client, args) => {
const { rules_json_path, owner, pull_number, repo } = args
const rules = JSON.parse(fs.readFileSync(rules_json_path, 'utf8'))
const res = await client.request(
`GET /repos/${owner}/${repo}/pulls/${pull_number}/files`,
)
if (!res || !res.status || res.status !== 200) {
core.setFailed('error in getting changed files.')
return
}
const target_labels = new Set()
for (const file of res.data) {
for (const rule of rules) {
if (minimatch(file.filename, rule.path)) {
target_labels.add(rule.label_name)
}
}
}
if (target_labels.size > 0) {
client.issues.addLabels({
issue_number: pull_number,
labels: Array.from(target_labels),
owner,
repo,
})
}
console.log({
labels: Array.from(target_labels),
owner,
pull_number,
repo,
})
}
try {
const token = core.getInput('token')
const pull_number = core.getInput('pull_number')
const rules_json_path = core.getInput('rules')
const {
context: {
repo: { owner, repo },
},
} = github
const client = new github.GitHub(token)
const args = { rules_json_path, owner, pull_number, repo }
applyPackageLabels(client, args)
.catch(error => core.setFailed(error.message))
} catch (error) {
core.setFailed(error.message);
}