-
Notifications
You must be signed in to change notification settings - Fork 11
112 lines (97 loc) · 3.29 KB
/
pr-workflow.yml
File metadata and controls
112 lines (97 loc) · 3.29 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: PR workflow
on:
pull_request:
types: [opened, synchronize]
jobs:
pr-automation:
runs-on: ubuntu-latest
steps:
- name: Get changed files
id: files
uses: tj-actions/changed-files@v44
- name: Assign, label, and request reviewers
uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.payload.pull_request.number;
const author = context.payload.pull_request.user.login;
const changedFiles = "${{ steps.files.outputs.all_changed_files }}".split(" ");
// CONFIGURATION
const rules = {
frontend: {
path: "frontend/",
label: "frontend",
reviewers: ["MaddieWright"]
},
backend: {
path: "backend/",
label: "backend",
reviewers: ["HeisSteve"]
},
infra: {
path: ".github/",
label: "infra",
reviewers: ["HeisSteve", "MaddieWright"]
}
};
const labelsToAdd = new Set();
const reviewersToAdd = new Set();
// Determine applicable rules
for (const file of changedFiles) {
for (const rule of Object.values(rules)) {
if (file.startsWith(rule.path)) {
labelsToAdd.add(rule.label);
rule.reviewers.forEach(r => reviewersToAdd.add(r));
}
}
}
// Assign PR author
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: prNumber,
assignees: [author]
});
// Get existing labels
const existingLabels = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: prNumber
});
const managedLabels = Object.values(rules).map(r => r.label);
const existingManagedLabels = existingLabels.data
.map(l => l.name)
.filter(l => managedLabels.includes(l));
// Remove labels that no longer apply
for (const label of existingManagedLabels) {
if (!labelsToAdd.has(label)) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: label
});
}
}
// Add labels
if (labelsToAdd.size > 0) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: [...labelsToAdd]
});
}
// Remove PR author from reviewers if present
reviewersToAdd.delete(author);
// Request reviewers
if (reviewersToAdd.size > 0) {
await github.rest.pulls.requestReviewers({
owner,
repo,
pull_number: prNumber,
reviewers: [...reviewersToAdd]
});
}