-
Notifications
You must be signed in to change notification settings - Fork 44
147 lines (123 loc) · 4.84 KB
/
Copy pathpr-assigner.yaml
File metadata and controls
147 lines (123 loc) · 4.84 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
name: Assign PR Assignees
on:
pull_request_target:
types: [opened, ready_for_review, synchronize, reopened]
permissions:
contents: read
pull-requests: write # required to list PR files and to assign on PRs via the Issues API
issues: write
jobs:
assign-assignees:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Assign internal-services assignees from CODEOWNERS
uses: actions/github-script@v8
with:
script: |
const fs = require("fs");
const RELEASE_SERVICE_BOT = "release-service-bot";
const pr = context.payload.pull_request;
if (!pr) {
core.info("No pull request in event payload. Skipping.");
return;
}
if (context.payload.action === "opened" && pr.draft) {
core.info("Draft PR opened. Skipping assignment until ready_for_review.");
return;
}
const changedFiles = await github.paginate(
github.rest.pulls.listFiles,
{
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
per_page: 100,
}
);
const isInternalServicesPR = changedFiles.some((file) =>
file.filename.startsWith("components/internal-services/")
);
if (!isInternalServicesPR) {
core.info("PR does not touch internal-services component. Skipping assignment.");
return;
}
const codeownersPath = ".github/CODEOWNERS";
const codeowners = fs.readFileSync(codeownersPath, "utf8");
const internalServicesLine = codeowners
.split("\n")
.map((line) => line.trim())
.find((line) => line.startsWith("/components/internal-services/ "));
if (!internalServicesLine) {
core.setFailed("Could not find /components/internal-services/ entry in .github/CODEOWNERS.");
return;
}
const owners = internalServicesLine
.split(/\s+/)
.slice(1)
.map((owner) => owner.replace(/^@/, "").trim())
.filter(Boolean);
const excluded = new Set([
RELEASE_SERVICE_BOT,
pr.user.login,
]);
const existingAssignees = (pr.assignees || []).map((assignee) => assignee.login);
for (const assignee of existingAssignees) {
excluded.add(assignee);
}
const candidates = owners.filter((owner) => !excluded.has(owner));
const neededAssignees = Math.max(0, 2 - existingAssignees.length);
if (neededAssignees === 0) {
core.info("PR already has 2 or more assignees.");
return;
}
if (candidates.length === 0) {
core.info("No eligible internal-services CODEOWNERS assignees available.");
return;
}
const collaboratorChecks = await Promise.all(
candidates.map(async (candidate) => {
try {
await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: candidate,
});
return candidate;
} catch (error) {
if (error.status === 404) {
core.info(`Skipping non-collaborator assignee candidate: ${candidate}`);
return null;
}
throw error;
}
})
);
const collaboratorCandidates = collaboratorChecks.filter(Boolean);
if (collaboratorCandidates.length === 0) {
core.info("No collaborator candidates available for assignment.");
return;
}
const shuffled = [...collaboratorCandidates].sort(() => Math.random() - 0.5);
const selected = shuffled.slice(0, neededAssignees);
if (selected.length === 0) {
core.info("No assignees selected.");
return;
}
try {
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
assignees: selected,
});
} catch (error) {
if (error.status === 422) {
core.warning(`Unable to assign one or more assignees: ${error.message}`);
return;
}
throw error;
}
core.info(`Assigned assignees: ${selected.join(", ")}`);