Repository navigation
66 lines (57 loc) · 2.51 KB
/
Copy pathlabel-prs.yml
File metadata and controls
66 lines (57 loc) · 2.51 KB
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
name: Label PRs
on:
pull_request_target:
types: [opened, edited, reopened, synchronize]
permissions:
contents: read
pull-requests: write
concurrency:
group: label-prs-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
label:
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.user.type != 'Bot' }}
steps:
- name: Apply category label from conventional-commit title
uses: actions/github-script@v9
with:
script: |
const TYPE_TO_LABEL = {
feat: "type: feature",
fix: "type: fix",
perf: "type: performance",
refactor: "type: refactor",
docs: "type: docs",
build: "type: build",
ci: "type: build",
chore: "type: chore",
};
function labelFor(title) {
const m = (title || "").match(/^([a-zA-Z]+)(?:\([^)]*\))?!?:/);
return m ? (TYPE_TO_LABEL[m[1].toLowerCase()] || null) : null;
}
const pr = context.payload.pull_request;
const label = labelFor(pr.title);
if (!label) {
core.info(`No category mapping for title "${pr.title}" — leaving labels unchanged.`);
return;
}
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = pr.number;
const managed = new Set(Object.values(TYPE_TO_LABEL));
const current = (pr.labels || []).map((l) => l.name);
for (const name of current) {
if (managed.has(name) && name !== label) {
await github.rest.issues
.removeLabel({ owner, repo, issue_number, name })
.catch(() => {});
}
}
if (current.includes(label)) {
core.info(`"${label}" already present.`);
} else {
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [label] });
core.info(`Applied "${label}".`);
}