forked from IvorySQL/IvorySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
271 lines (234 loc) · 9.94 KB
/
Copy pathpr_governance.yml
File metadata and controls
271 lines (234 loc) · 9.94 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
name: pr-governance
on:
pull_request_target:
types:
- opened
- edited
- synchronize
- reopened
- ready_for_review
- labeled
- unlabeled
permissions:
contents: read
pull-requests: read
issues: read
jobs:
policy:
name: policy
runs-on: ubuntu-latest
steps:
- name: Validate PR governance policy
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
const owner = context.repo.owner;
const repo = context.repo.repo;
const body = pr.body || "";
const title = (pr.title || "").trim();
const errors = [];
const warnings = [];
const issueRefRegex =
/(?:^|[\s(])(?:(?<repo>[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+))?#(?<number>\d+)\b/g;
function uniqueById(items) {
const map = new Map();
for (const item of items) {
map.set(item.id ?? `${item.repository?.nameWithOwner || ""}#${item.number}`, item);
}
return [...map.values()];
}
function extractIssueReferencesFromBody(text) {
const refs = [];
for (const match of text.matchAll(issueRefRegex)) {
const repoName = match.groups?.repo || `${owner}/${repo}`;
const number = Number(match.groups?.number);
refs.push({
repository: { nameWithOwner: repoName },
number,
});
}
return refs;
}
async function filterOutPullRequestsFromBodyReferences(refs) {
const validated = [];
for (const ref of refs) {
try {
const [refOwner, refRepo] = ref.repository.nameWithOwner.split("/");
const { data } = await github.rest.issues.get({
owner: refOwner,
repo: refRepo,
issue_number: ref.number,
});
// GitHub returns a pull_request field when the number belongs to a PR.
if (!data.pull_request) {
validated.push({
id: data.node_id,
number: data.number,
repository: { nameWithOwner: ref.repository.nameWithOwner },
});
}
} catch (e) {
warnings.push(
`Could not validate whether ${ref.repository.nameWithOwner}#${ref.number} is an issue or a pull request.`
);
}
}
return validated;
}
async function getSameRepoManuallyLinkedIssues(prNodeId) {
// Best-effort same-repository manual-link detection.
// Scans issue timeline ConnectedEvent entries and looks for this PR as the source.
const found = new Map();
let issuesCursor = null;
let issuesHasNextPage = true;
let scannedPages = 0;
const maxIssuePages = 10; // safety cap
while (issuesHasNextPage && scannedPages < maxIssuePages) {
const query = `
query($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
issues(
first: 100,
after: $cursor,
states: [OPEN, CLOSED],
orderBy: {field: UPDATED_AT, direction: DESC}
) {
nodes {
number
timelineItems(first: 20, itemTypes: [CONNECTED_EVENT]) {
nodes {
__typename
... on ConnectedEvent {
source {
__typename
... on PullRequest {
id
number
}
}
subject {
__typename
... on Issue {
id
number
}
}
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
const res = await github.graphql(query, {
owner,
repo,
cursor: issuesCursor,
});
const issuesConn = res.repository?.issues;
if (!issuesConn) break;
for (const issue of issuesConn.nodes || []) {
for (const item of issue.timelineItems?.nodes || []) {
if (
item?.__typename === "ConnectedEvent" &&
item?.source?.__typename === "PullRequest" &&
item?.source?.id === prNodeId &&
item?.subject?.__typename === "Issue"
) {
found.set(item.subject.id, {
id: item.subject.id,
number: item.subject.number,
repository: { nameWithOwner: `${owner}/${repo}` },
});
}
}
}
issuesHasNextPage = issuesConn.pageInfo?.hasNextPage || false;
issuesCursor = issuesConn.pageInfo?.endCursor || null;
scannedPages += 1;
}
if (issuesHasNextPage) {
warnings.push(
"Manual-link scan hit the page cap before exhausting all issues. Increase maxIssuePages if needed."
);
}
return [...found.values()];
}
// 1) Detect linked issues
// Policy passes if either:
// - the PR body references at least one issue
// - a same-repository manual sidebar link is detected
const rawBodyReferences = extractIssueReferencesFromBody(body);
const issuesMentionedInBody = await filterOutPullRequestsFromBodyReferences(rawBodyReferences);
const manualLinkedIssues = await getSameRepoManuallyLinkedIssues(pr.node_id);
const linkedIssues = uniqueById([
...issuesMentionedInBody,
...manualLinkedIssues,
]);
if (linkedIssues.length === 0) {
errors.push(
"PR must reference at least one issue in the description or be linked to one issue through the sidebar."
);
}
// 2) Review status
// Keep this as a warning. Use branch protection / rulesets for hard enforcement.
const reviews = await github.paginate(github.rest.pulls.listReviews, {
owner,
repo,
pull_number: pr.number,
per_page: 100,
});
const approvedReviews = reviews.filter(r => r.state === "APPROVED");
if (approvedReviews.length < 1) {
warnings.push(
"No APPROVED review was detected. For hard enforcement, enable required reviews in branch protection or rulesets."
);
}
// 3) Helpful diagnostics
if (issuesMentionedInBody.length > 0) {
const crossRepoBodyRefs = issuesMentionedInBody.filter(
issue => issue.repository?.nameWithOwner !== `${owner}/${repo}`
);
if (crossRepoBodyRefs.length > 0) {
warnings.push(
"Cross-repository issue references were found in the PR description. Manual sidebar-link validation only covers the current repository."
);
}
}
if (manualLinkedIssues.length > 0) {
warnings.push(
"Manual sidebar links were detected only within the current repository. Cross-repository manual links are not fully validated by this workflow."
);
}
const summary = [];
summary.push("## PR Governance Report");
summary.push(`- PR #${pr.number}`);
summary.push(`- Title: ${title}`);
summary.push(`- Issues referenced in PR body: ${issuesMentionedInBody.length}`);
summary.push(`- Same-repository manual-linked issues detected: ${manualLinkedIssues.length}`);
summary.push(`- Total linked issues accepted by policy: ${linkedIssues.length}`);
summary.push(`- Files changed: ${pr.changed_files}`);
summary.push(`- Additions: ${pr.additions}`);
summary.push(`- Deletions: ${pr.deletions}`);
summary.push("");
if (warnings.length) {
summary.push("### Warnings");
for (const w of warnings) summary.push(`- ${w}`);
summary.push("");
}
if (errors.length) {
summary.push("### Errors");
for (const e of errors) summary.push(`- ${e}`);
await core.summary.addRaw(summary.join("\n")).write();
core.setFailed(errors.join(" | "));
return;
}
summary.push("### Result");
summary.push("- All required governance checks passed.");
await core.summary.addRaw(summary.join("\n")).write();