-
Notifications
You must be signed in to change notification settings - Fork 3.4k
181 lines (160 loc) · 7.5 KB
/
label-and-milestone-issues.yml
File metadata and controls
181 lines (160 loc) · 7.5 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
# 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.
# All version information is read from eng/Versions.props at the merge commit.
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-latest
steps:
- name: Label issues and update milestones
uses: actions/github-script@v8
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.payload.pull_request.number;
const mergeCommitSha = context.payload.pull_request.merge_commit_sha;
// 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;
}
// Read version info from eng/Versions.props at the actual merge commit
const { data: versionFileData } = await github.rest.repos.getContent({
owner,
repo,
path: 'eng/Versions.props',
ref: mergeCommitSha
});
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 versionPrefix = versionPrefixMatch[1];
const preReleaseLabelMatch = versionFileContent.match(/<PreReleaseVersionLabel>(preview|rc)<\/PreReleaseVersionLabel>/);
const preReleaseIterationMatch = versionFileContent.match(/<PreReleaseVersionIteration>(\d+)<\/PreReleaseVersionIteration>/);
let label;
if (preReleaseLabelMatch && preReleaseIterationMatch) {
label = `${preReleaseLabelMatch[1]}-${preReleaseIterationMatch[1]}`;
}
console.log(`Version: ${versionPrefix}, 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 = [];
if (label) {
for (const issue of closingIssues) {
console.log(`Adding label '${label}' to issue #${issue.number}`);
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: issue.number,
labels: [label]
});
} catch (error) {
errors.push(`Failed to add label to issue #${issue.number}: ${error.message}`);
}
}
}
// Look up the target milestone (e.g. "11.0.0", "10.0.5") 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 targetMilestoneName = versionPrefix;
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(versionPrefix);
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 label '${label ?? '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;
}