Debian Codegraph @

.github/workflows/auto-label.yml

raw
1 name: Auto Label PRs
2
3 on:
4 pull_request:
5 types: [opened, synchronize, reopened, closed]
6 workflow_run:
7 workflows: ["ABI Compliance Check"]
8 types: [completed]
9
10 jobs:
11 apply-labels:
12 runs-on: AMD-ROCm-Internal-dev1
13 permissions:
14 pull-requests: write
15 actions: read
16 contents: read
17 steps:
18 - name: Add/Remove labels based on branch names and ABI results
19 uses: actions/github-script@v6
20 with:
21 script: |
22 const pr = context.payload.pull_request;
23 let prNumber, headSha, baseBranch, headBranch;
24
25 // Handle different event types
26 if (context.eventName === 'pull_request') {
27 prNumber = pr.number;
28 headSha = pr.head.sha;
29 baseBranch = pr.base.ref;
30 headBranch = pr.head.ref;
31 } else if (context.eventName === 'workflow_run') {
32 // Find the associated PR for workflow_run events
33 const workflowRun = context.payload.workflow_run;
34 console.log(`Workflow run completed: ${workflowRun.name} with conclusion: ${workflowRun.conclusion}`);
35
36 if (workflowRun.event !== 'pull_request') {
37 console.log('Workflow run was not triggered by a pull request, skipping');
38 return;
39 }
40
41 const prs = await github.rest.pulls.list({
42 owner: context.repo.owner,
43 repo: context.repo.repo,
44 state: 'open',
45 head: `${context.repo.owner}:${workflowRun.head_branch}`
46 });
47
48 const associatedPr = prs.data.find(p => p.head.sha === workflowRun.head_sha);
49
50 if (!associatedPr) {
51 console.log('No associated PR found for this workflow run');
52 return;
53 }
54
55 prNumber = associatedPr.number;
56 headSha = associatedPr.head.sha;
57 baseBranch = associatedPr.base.ref;
58 headBranch = associatedPr.head.ref;
59 } else {
60 console.log('Unsupported event type');
61 return;
62 }
63
64 let labelsApplied = false;
65
66 // Debug information
67 console.log(`Processing PR #${prNumber}: Head: ${headBranch}, Base: ${baseBranch}`);
68
69 // Get current PR data to check existing labels
70 const { data: currentPr } = await github.rest.pulls.get({
71 owner: context.repo.owner,
72 repo: context.repo.repo,
73 pull_number: prNumber
74 });
75 const existingLabels = currentPr.labels.map(label => label.name);
76
77 // Condition 1: PR targeting amd-mainline
78 if (baseBranch === 'amd-mainline' && context.eventName === 'pull_request') {
79 const labelToAdd = 'Merge amd-mainline';
80 try {
81 if (!existingLabels.includes(labelToAdd)) {
82 await github.rest.issues.addLabels({
83 owner: context.repo.owner,
84 repo: context.repo.repo,
85 issue_number: prNumber,
86 labels: [labelToAdd]
87 });
88 console.log(`Added label "${labelToAdd}" to PR #${prNumber}`);
89 labelsApplied = true;
90 }
91 } catch (error) {
92 console.error(`Error adding label "${labelToAdd}": ${error.message}`);
93 }
94 }
95
96 // Condition 2: Cherry-pick based on head branch name or release target
97 if (context.eventName === 'pull_request') {
98 const isCherryPickHead = /cherry.*pick/i.test(headBranch);
99 const isReleaseTargetBase = baseBranch.startsWith('release/');
100
101 if (isCherryPickHead || isReleaseTargetBase) {
102 const labelToAdd = 'cherry-pick';
103 try {
104 if (!existingLabels.includes(labelToAdd)) {
105 await github.rest.issues.addLabels({
106 owner: context.repo.owner,
107 repo: context.repo.repo,
108 issue_number: prNumber,
109 labels: [labelToAdd]
110 });
111 console.log(`Added label "${labelToAdd}" to PR #${prNumber}`);
112 labelsApplied = true;
113 } else {
114 console.log(`Label "${labelToAdd}" already exists on PR #${prNumber}`);
115 }
116 } catch (error) {
117 console.error(`Error adding label "${labelToAdd}": ${error.message}`);
118 }
119 }
120 }
121
122 // ABI BREAKAGE LOGIC: Check on both workflow_run AND pull_request events
123 let shouldCheckABI = false;
124 let hasMajorAbiBreakage = false;
125 let hasMinorAbiBreakage = false;
126
127 if (context.eventName === 'workflow_run') {
128 // Handle workflow_run events (existing logic)
129 const workflowRun = context.payload.workflow_run;
130
131 if (workflowRun.name === 'ABI Compliance Check') {
132 shouldCheckABI = true;
133 console.log(`ABI Compliance Check completed with conclusion: ${workflowRun.conclusion}`);
134
135 try {
136 const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({
137 owner: context.repo.owner,
138 repo: context.repo.repo,
139 run_id: workflowRun.id
140 });
141
142 // Check job conclusions for ABI breakage
143 for (const job of jobs.jobs) {
144 console.log(`Job: ${job.name}, Conclusion: ${job.conclusion}`);
145
146 if (job.name.includes('Major ABI') && job.conclusion === 'failure') {
147 hasMajorAbiBreakage = true;
148 console.log('Major ABI breakage detected from job failure');
149 }
150
151 if (job.name.includes('Minor ABI') && job.conclusion === 'failure') {
152 hasMinorAbiBreakage = true;
153 console.log('Minor ABI breakage detected from job failure');
154 }
155 }
156
157 // If workflow succeeded, no ABI breakage
158 if (workflowRun.conclusion === 'success') {
159 console.log('ABI Compliance Check succeeded - no ABI breakage');
160 hasMajorAbiBreakage = false;
161 hasMinorAbiBreakage = false;
162 }
163
164 } catch (error) {
165 console.log(`Could not fetch job details: ${error.message}`);
166 return;
167 }
168 }
169 } else if (context.eventName === 'pull_request') {
170 // NEW: Check if amdsmi.h has been reverted on PR events
171 const hasAbiLabels = existingLabels.includes('MAJOR ABI BREAKAGE') || existingLabels.includes('MINOR ABI BREAKAGE');
172
173 if (hasAbiLabels) {
174 console.log('PR has ABI labels, checking if amdsmi.h changes were reverted...');
175 shouldCheckABI = true;
176
177 try {
178 // Get the diff for amdsmi.h between base and head
179 const { data: comparison } = await github.rest.repos.compareCommits({
180 owner: context.repo.owner,
181 repo: context.repo.repo,
182 base: currentPr.base.sha,
183 head: currentPr.head.sha
184 });
185
186 // Check if amdsmi.h has any changes
187 const amdsmiFile = comparison.files?.find(file => file.filename === 'include/amd_smi/amdsmi.h');
188
189 if (!amdsmiFile) {
190 console.log('No changes to amdsmi.h found in this PR - removing ABI labels');
191 hasMajorAbiBreakage = false;
192 hasMinorAbiBreakage = false;
193 } else if (amdsmiFile.changes === 0) {
194 console.log('amdsmi.h file exists but has no changes - removing ABI labels');
195 hasMajorAbiBreakage = false;
196 hasMinorAbiBreakage = false;
197 } else {
198 console.log(`amdsmi.h has ${amdsmiFile.changes} changes - keeping existing ABI labels`);
199 // Keep existing labels since we can't determine ABI status without running the check
200 hasMajorAbiBreakage = existingLabels.includes('MAJOR ABI BREAKAGE');
201 hasMinorAbiBreakage = existingLabels.includes('MINOR ABI BREAKAGE');
202 }
203
204 } catch (error) {
205 console.log(`Error checking file changes: ${error.message}`);
206 // If we can't check, preserve existing labels
207 hasMajorAbiBreakage = existingLabels.includes('MAJOR ABI BREAKAGE');
208 hasMinorAbiBreakage = existingLabels.includes('MINOR ABI BREAKAGE');
209 }
210 }
211 }
212
213 // Manage ABI breakage labels (only if we determined ABI status)
214 if (shouldCheckABI) {
215 const abiLabels = {
216 'MAJOR ABI BREAKAGE': hasMajorAbiBreakage,
217 'MINOR ABI BREAKAGE': hasMinorAbiBreakage
218 };
219
220 const wasMajorAbiBreakage = existingLabels.includes('MAJOR ABI BREAKAGE');
221 const wasMinorAbiBreakage = existingLabels.includes('MINOR ABI BREAKAGE');
222
223 for (const [labelName, shouldHaveLabel] of Object.entries(abiLabels)) {
224 const hasLabel = existingLabels.includes(labelName);
225
226 if (shouldHaveLabel && !hasLabel) {
227 // Add label
228 try {
229 await github.rest.issues.addLabels({
230 owner: context.repo.owner,
231 repo: context.repo.repo,
232 issue_number: prNumber,
233 labels: [labelName]
234 });
235 console.log(`✅ Added label "${labelName}" to PR #${prNumber}`);
236 labelsApplied = true;
237 } catch (error) {
238 console.error(`❌ Error adding label "${labelName}": ${error.message}`);
239 }
240 } else if (!shouldHaveLabel && hasLabel) {
241 // Remove label
242 try {
243 await github.rest.issues.removeLabel({
244 owner: context.repo.owner,
245 repo: context.repo.repo,
246 issue_number: prNumber,
247 name: labelName
248 });
249 console.log(`🗑️ Removed label "${labelName}" from PR #${prNumber}`);
250 labelsApplied = true;
251 } catch (error) {
252 console.error(`❌ Error removing label "${labelName}": ${error.message}`);
253 }
254 }
255 }
256
257 // Add comments when ABI issues are detected or resolved
258 if (context.eventName === 'workflow_run') {
259 // Only add comments for workflow_run events (actual ABI check results)
260 if (hasMajorAbiBreakage && !wasMajorAbiBreakage) {
261 await github.rest.issues.createComment({
262 owner: context.repo.owner,
263 repo: context.repo.repo,
264 issue_number: prNumber,
265 body: '⚠️ **MAJOR ABI BREAKAGE detected** in the latest ABI compliance check. Please review the ABI compliance report and fix any breaking changes.'
266 });
267 }
268
269 if (hasMinorAbiBreakage && !wasMinorAbiBreakage) {
270 await github.rest.issues.createComment({
271 owner: context.repo.owner,
272 repo: context.repo.repo,
273 issue_number: prNumber,
274 body: '⚠️ **MINOR ABI BREAKAGE detected** in the latest ABI compliance check. Please review the ABI compliance report for details.'
275 });
276 }
277
278 if (!hasMajorAbiBreakage && wasMajorAbiBreakage) {
279 await github.rest.issues.createComment({
280 owner: context.repo.owner,
281 repo: context.repo.repo,
282 issue_number: prNumber,
283 body: '✅ **MAJOR ABI BREAKAGE resolved** - ABI compliance check is now passing!'
284 });
285 }
286
287 if (!hasMinorAbiBreakage && wasMinorAbiBreakage) {
288 await github.rest.issues.createComment({
289 owner: context.repo.owner,
290 repo: context.repo.repo,
291 issue_number: prNumber,
292 body: '✅ **MINOR ABI BREAKAGE resolved** - ABI compliance check is now passing!'
293 });
294 }
295 } else if (context.eventName === 'pull_request') {
296 // Add comment when labels are removed due to file reversion
297 if (!hasMajorAbiBreakage && wasMajorAbiBreakage) {
298 await github.rest.issues.createComment({
299 owner: context.repo.owner,
300 repo: context.repo.repo,
301 issue_number: prNumber,
302 body: '✅ **MAJOR ABI BREAKAGE resolved** - `amdsmi.h` changes have been reverted.'
303 });
304 }
305
306 if (!hasMinorAbiBreakage && wasMinorAbiBreakage) {
307 await github.rest.issues.createComment({
308 owner: context.repo.owner,
309 repo: context.repo.repo,
310 issue_number: prNumber,
311 body: '✅ **MINOR ABI BREAKAGE resolved** - `amdsmi.h` changes have been reverted.'
312 });
313 }
314 }
315 }
316
317 if (!labelsApplied && context.eventName === 'pull_request') {
318 console.log(`PR #${prNumber} did not match criteria for automatic labeling by this workflow.`);
319 }