-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathlabel_pr_based_on_title.js
63 lines (56 loc) · 2.28 KB
/
label_pr_based_on_title.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
const { PR_NUMBER, PR_TITLE } = require("./constants")
module.exports = async ({github, context, core}) => {
const FEAT_REGEX = /feat(\((.+)\))?(:.+)/
const BUG_REGEX = /(fix|bug)(\((.+)\))?(:.+)/
const DOCS_REGEX = /(docs|doc)(\((.+)\))?(:.+)/
const CHORE_REGEX = /(chore)(\((.+)\))?(:.+)/
const DEPRECATED_REGEX = /(deprecated)(\((.+)\))?(:.+)/
const REFACTOR_REGEX = /(refactor)(\((.+)\))?(:.+)/
const labels = {
"feature": FEAT_REGEX,
"bug": BUG_REGEX,
"documentation": DOCS_REGEX,
"internal": CHORE_REGEX,
"enhancement": REFACTOR_REGEX,
"deprecated": DEPRECATED_REGEX,
}
// get PR labels from env
const prLabels = process.env.PR_LABELS.replaceAll("\"", "").split(",");
const labelKeys = Object.keys(labels);
// Maintenance: We should keep track of modified PRs in case their titles change
let miss = 0;
try {
for (const label in labels) {
const matcher = new RegExp(labels[label])
const matches = matcher.exec(PR_TITLE)
if (matches != null) {
core.info(`Auto-labeling PR ${PR_NUMBER} with ${label}`)
for (const prLabel of prLabels) {
if (labelKeys.includes(prLabel) && prLabel !== label) {
core.info(`PR previously tagged with: ${prLabel}, removing.`);
await github.rest.issues.removeLabel({
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
name: prLabel
})
}
}
await github.rest.issues.addLabels({
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [label]
})
return;
} else {
core.debug(`'${PR_TITLE}' didn't match '${label}' semantic.`)
miss += 1
}
}
} finally {
if (miss == Object.keys(labels).length) {
core.notice(`PR ${PR_NUMBER} title '${PR_TITLE}' doesn't follow semantic titles; skipping...`)
}
}
}