-
Notifications
You must be signed in to change notification settings - Fork 3.4k
255 lines (229 loc) · 10.4 KB
/
label-and-milestone-issues.yml
File metadata and controls
255 lines (229 loc) · 10.4 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
# This workflow automatically labels issues with the preview/RC version when their fixing PR
# is merged into main or a release branch, and sets their milestone to the current version.
# The version is inferred from tags and branches in the repo, with the major/minor on main taken from eng/Versions.props.
name: Label and milestone closed issues
on:
pull_request_target:
types: [closed]
branches:
- main
- release/**
permissions:
issues: write
contents: read
pull-requests: read
jobs:
label:
if: github.event.pull_request.merged == true
runs-on: ubuntu-slim
steps:
- name: Label issues and update milestones
uses: actions/github-script@v9
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.payload.pull_request.number;
// Find issues closed by this PR using GraphQL (include current milestone)
const query = `
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 50) {
nodes {
number
milestone {
title
}
}
}
}
}
}
`;
const result = await github.graphql(query, { owner, repo, prNumber });
const closingIssues = result.repository.pullRequest.closingIssuesReferences.nodes;
if (closingIssues.length === 0) {
console.log('No closing issues linked to this PR, skipping');
return;
}
// Detect version and label from tags/branches based on the target branch
const targetBranch = context.payload.pull_request.base.ref;
let targetMilestoneName;
let label;
const releaseBranchMatch = targetBranch.match(/^release\/(\d+)\.(\d+)$/);
if (releaseBranchMatch) {
// Servicing branch (e.g. release/10.0): find the next patch version from tags
const major = releaseBranchMatch[1];
const minor = releaseBranchMatch[2];
const tagRefs = await github.paginate(
github.rest.git.listMatchingRefs,
{
owner,
repo,
ref: `tags/v${major}.${minor}.`,
per_page: 100
}
);
let highestPatch = -1;
for (const ref of tagRefs) {
const m = ref.ref.match(/^refs\/tags\/v\d+\.\d+\.(\d+)$/);
if (m) {
const patch = parseInt(m[1]);
if (patch < 100 && patch > highestPatch) {
highestPatch = patch;
}
}
}
targetMilestoneName = `${major}.${minor}.${highestPatch + 1}`;
// No preview/rc label for servicing branches
} else if (targetBranch === 'main') {
// Main branch: read major.minor from Versions.props, then infer
// the next preview/rc from existing release branches
const { data: versionFileData } = await github.rest.repos.getContent({
owner,
repo,
path: 'eng/Versions.props',
ref: context.payload.pull_request.merge_commit_sha
});
const versionFileContent = Buffer.from(versionFileData.content, 'base64').toString('utf-8');
const versionPrefixMatch = versionFileContent.match(/<VersionPrefix>(\d+)\.(\d+)\.\d+<\/VersionPrefix>/);
if (!versionPrefixMatch) {
throw new Error('Could not parse VersionPrefix from eng/Versions.props');
}
const major = versionPrefixMatch[1];
const minor = versionPrefixMatch[2];
targetMilestoneName = `${major}.${minor}.0`;
// List release branches for this major.minor to find what's already been branched
const branchRefs = await github.paginate(
github.rest.git.listMatchingRefs,
{
owner,
repo,
ref: `heads/release/${major}.${minor}-`,
per_page: 100
}
);
let highestPreview = 0;
let highestRc = 0;
for (const ref of branchRefs) {
const previewMatch = ref.ref.match(/^refs\/heads\/release\/\d+\.\d+-preview(\d+)$/);
if (previewMatch) {
highestPreview = Math.max(highestPreview, parseInt(previewMatch[1]));
continue;
}
const rcMatch = ref.ref.match(/^refs\/heads\/release\/\d+\.\d+-rc(\d+)$/);
if (rcMatch) {
highestRc = Math.max(highestRc, parseInt(rcMatch[1]));
}
}
if (highestRc >= 2) {
// After rc2, we're heading to GA — no label
} else if (highestRc === 1) {
label = 'rc-2';
} else if (highestPreview >= 7) {
label = 'rc-1';
} else {
label = `preview-${highestPreview + 1}`;
}
} else {
throw new Error(`Unexpected target branch: ${targetBranch}`);
}
console.log(`Target branch: ${targetBranch}, milestone: ${targetMilestoneName}, label: ${label ?? 'none'}`);
// Label all closing issues
// (don't filter by state to avoid race conditions where GitHub
// hasn't closed the issue yet when this workflow runs)
const errors = [];
const labelsToApply = [];
if (label) {
labelsToApply.push(label);
}
// If the PR has the community-contribution label, propagate it to closing issues
if (context.payload.pull_request.labels.some(l => l.name === 'community-contribution')) {
labelsToApply.push('community-contribution');
}
if (labelsToApply.length > 0) {
for (const issue of closingIssues) {
console.log(`Adding labels [${labelsToApply.join(', ')}] to issue #${issue.number}`);
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: issue.number,
labels: labelsToApply
});
} catch (error) {
errors.push(`Failed to add labels to issue #${issue.number}: ${error.message}`);
}
}
}
// Look up the target milestone via GraphQL, including closed milestones
// to avoid recreating one that already exists. The GraphQL query parameter
// does fuzzy/substring matching (no exact match option), so we fetch
// multiple results and filter client-side.
const milestoneResult = await github.graphql(`
query($owner: String!, $repo: String!, $title: String!) {
repository(owner: $owner, name: $repo) {
milestones(query: $title, states: [OPEN, CLOSED], first: 10) {
nodes {
number
title
}
}
}
}
`, { owner, repo, title: targetMilestoneName });
let milestoneNode = milestoneResult.repository.milestones.nodes
.find(m => m.title === targetMilestoneName);
if (!milestoneNode) {
console.log(`Milestone '${targetMilestoneName}' not found, creating it`);
try {
const { data: created } = await github.rest.issues.createMilestone({
owner,
repo,
title: targetMilestoneName
});
milestoneNode = { number: created.number, title: created.title };
} catch (error) {
throw new Error(`Failed to create milestone '${targetMilestoneName}': ${error.message}`);
}
}
// Set the milestone on closing issues, applying a "min" strategy:
// only update if the issue has no version milestone or the target is earlier
const targetVersion = parseVersion(targetMilestoneName);
for (const issue of closingIssues) {
const currentTitle = issue.milestone?.title;
const currentVersion = currentTitle ? parseVersion(currentTitle) : null;
if (currentVersion && compareVersions(currentVersion, targetVersion) <= 0) {
console.log(`Issue #${issue.number} already has milestone '${currentTitle}' <= '${targetMilestoneName}', skipping`);
continue;
}
const from = currentTitle ? `'${currentTitle}'` : 'none';
console.log(`Setting milestone on issue #${issue.number} from ${from} to '${targetMilestoneName}'`);
try {
await github.rest.issues.update({
owner,
repo,
issue_number: issue.number,
milestone: milestoneNode.number
});
} catch (error) {
errors.push(`Failed to set milestone on issue #${issue.number}: ${error.message}`);
}
}
if (errors.length > 0) {
throw new Error(`Errors processing issues:\n${errors.join('\n')}`);
}
console.log(`Done. Processed ${closingIssues.length} issue(s) with labels [${labelsToApply.join(', ') || 'none'}] and milestone '${targetMilestoneName}'`);
// Parses a milestone title as a semver version, or returns null for
// non-version milestones (e.g. "Backlog", "MQ", "Discussions")
function parseVersion(title) {
const m = title.match(/^(\d+)\.(\d+)\.(\d+)$/);
return m ? [parseInt(m[1]), parseInt(m[2]), parseInt(m[3])] : null;
}
function compareVersions(a, b) {
for (let i = 0; i < 3; i++) {
if (a[i] !== b[i]) return a[i] - b[i];
}
return 0;
}