-
Notifications
You must be signed in to change notification settings - Fork 421
87 lines (75 loc) · 3.22 KB
/
label_pr_on_title.yml
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
82
83
84
85
86
87
name: Label PR based on title
on:
workflow_run:
workflows: ["Record PR number"]
types:
- completed
jobs:
upload:
runs-on: ubuntu-latest
# Guardrails to only ever run if PR recording workflow was indeed
# run in a PR event and ran successfully
if: >
${{ github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' }}
steps:
- name: 'Download artifact'
uses: actions/github-script@v6
# For security, we only download artifacts tied to the successful PR recording workflow
with:
script: |
const fs = require('fs');
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
const matchArtifact = artifacts.data.artifacts.filter(artifact => artifact.name == "pr")[0];
const artifact = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(artifact.data));
# NodeJS standard library doesn't provide ZIP capabilities; use system `unzip` command instead
- run: unzip pr.zip
- name: 'Label PR based on title'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# This safely runs in our base repo, not on fork
# thus allowing us to provide a write access token to label based on PR title
# and label PR based on semantic title accordingly
script: |
const fs = require('fs');
const pr_number = Number(fs.readFileSync('./number'));
const pr_title = fs.readFileSync('./title', 'utf-8').trim();
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,
}
for (const label in labels) {
const matcher = new RegExp(labels[label])
const isMatch = matcher.exec(pr_title)
if (isMatch != null) {
console.info(`Auto-labeling PR ${pr_number} with ${label}`)
await github.rest.issues.addLabels({
issue_number: pr_number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [label]
})
break
}
}