-
Notifications
You must be signed in to change notification settings - Fork 14.5k
211 lines (188 loc) · 7.37 KB
/
Copy pathexplore-triage-commenter.yml
File metadata and controls
211 lines (188 loc) · 7.37 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
name: Explore PR Triage Commenter
# Computes maintainer triage data for Explore PRs in an unprivileged
# pull_request workflow. A separate workflow_run workflow writes the sticky
# comment after re-fetching PR state; no privileged job checks out PR code.
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'topics/**'
- 'collections/**'
concurrency:
group: explore-triage-commenter-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: read
jobs:
build-comment-data:
runs-on: ubuntu-latest
steps:
- name: Build triage comment data
uses: actions/github-script@v9
env:
OUTPUT_PATH: ${{ runner.temp }}/explore-triage-comment.json
with:
script: |
const fs = require('fs');
const pr = context.payload.pull_request;
const baseOwner = context.repo.owner;
const baseRepo = context.repo.repo;
const prNumber = pr.number;
const prAuthor = pr.user.login.toLowerCase();
const headSha = pr.head.sha;
const payload = {
schema: 'explore-triage-comment/v1',
owner: baseOwner,
repo: baseRepo,
prNumber,
headSha,
baseRepoFullName: pr.base.repo.full_name,
headRepoFullName: pr.head.repo && pr.head.repo.full_name,
hasChanges: false,
topics: [],
collections: [],
};
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: baseOwner,
repo: baseRepo,
pull_number: prNumber,
per_page: 100,
});
const SLUG = /^[a-z0-9](?:[a-z0-9-]{0,80}[a-z0-9])?$/i;
const topics = new Set();
const collections = new Set();
for (const f of files) {
if (f.status === 'removed') continue;
const m = f.filename.match(/^(topics|collections)\/([^/]+)\//);
if (!m) continue;
const slug = m[2];
if (!SLUG.test(slug)) continue;
if (m[1] === 'topics') topics.add(slug);
else collections.add(slug);
}
if (topics.size === 0 && collections.size === 0) {
core.info('No topic or collection changes detected; nothing to do.');
writePayload(payload);
return;
}
payload.hasChanges = true;
for (const slug of [...topics].sort()) {
const topic = { slug, count: null };
try {
const res = await github.rest.search.repos({
q: `topic:${slug}`,
per_page: 1,
});
topic.count = res.data.total_count;
} catch (err) {
core.warning(`Search failed for topic '${slug}': ${err.message}`);
}
payload.topics.push(topic);
}
for (const slug of [...collections].sort()) {
const collection = {
slug,
readStatus: 'ok',
errorStatus: null,
items: [],
};
let content;
try {
content = await readCollectionIndex(slug);
} catch (err) {
collection.readStatus = err.status === 404 ? 'not-found' : 'error';
collection.errorStatus = String(err.status || 'error');
payload.collections.push(collection);
continue;
}
const items = parseCollectionItems(content);
for (const item of items) {
if (!/^[\w.-]+\/[\w.-]+$/.test(item)) {
collection.items.push({ name: item, valid: false });
continue;
}
const [owner, repo] = item.split('/');
try {
const r = await github.rest.repos.get({ owner, repo });
const notes = [];
if (owner.toLowerCase() === prAuthor) notes.push('possible-self-submission');
if (r.data.archived) notes.push('archived');
if (r.data.disabled) notes.push('disabled');
collection.items.push({
name: item,
valid: true,
lookupStatus: 'ok',
stars: r.data.stargazers_count,
pushed: r.data.pushed_at ? r.data.pushed_at.slice(0, 10) : null,
ownerType: r.data.owner.type,
notes,
});
} catch (err) {
collection.items.push({
name: item,
valid: true,
lookupStatus: err.status === 404 ? 'not-found' : 'error',
errorStatus: String(err.status || 'error'),
});
}
}
payload.collections.push(collection);
}
writePayload(payload);
async function readCollectionIndex(slug) {
const attempts = [];
if (pr.head.repo) {
attempts.push({
owner: pr.head.repo.owner.login,
repo: pr.head.repo.name,
ref: headSha,
});
}
attempts.push({ owner: baseOwner, repo: baseRepo, ref: headSha });
let lastError;
for (const attempt of attempts) {
try {
const res = await github.rest.repos.getContent({
...attempt,
path: `collections/${slug}/index.md`,
});
if (Array.isArray(res.data) || res.data.type !== 'file' || !res.data.content) {
const err = new Error('Collection index is not a file');
err.status = 'invalid';
throw err;
}
return Buffer.from(res.data.content, 'base64').toString('utf8');
} catch (err) {
lastError = err;
}
}
throw lastError;
}
function parseCollectionItems(text) {
const fmMatch = text.match(/^---\n([\s\S]*?)\n---/);
if (!fmMatch) return [];
const lines = fmMatch[1].split('\n');
const items = [];
let inItems = false;
for (const line of lines) {
if (/^items:\s*$/.test(line)) { inItems = true; continue; }
if (inItems && /^[a-zA-Z_]\w*\s*:/.test(line)) break;
if (inItems) {
const m = line.match(/^\s*-\s*([^\s#]+)/);
if (m) items.push(m[1]);
}
}
return items;
}
function writePayload(data) {
fs.writeFileSync(process.env.OUTPUT_PATH, JSON.stringify(data, null, 2));
core.info(`Wrote ${process.env.OUTPUT_PATH}`);
}
- name: Upload triage comment data
uses: actions/upload-artifact@v7
with:
name: explore-triage-comment
path: ${{ runner.temp }}/explore-triage-comment.json
if-no-files-found: error
retention-days: 1