-
Notifications
You must be signed in to change notification settings - Fork 324
198 lines (168 loc) · 6.86 KB
/
auto-label.yml
File metadata and controls
198 lines (168 loc) · 6.86 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
name: Auto-label PRs
on:
pull_request:
types: [opened, synchronize]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to label'
required: true
type: number
permissions: {}
concurrency:
group: auto-label-${{ github.event.number || inputs.pr_number }}
cancel-in-progress: true
jobs:
# -----------------------------------------------------------------
# Check if this re-run would create skipped check runs that
# overwrite existing successful status.
# -----------------------------------------------------------------
check-existing-success:
runs-on: ubuntu-latest
permissions:
checks: read
pull-requests: read
outputs:
should-run: ${{ steps.check.outputs.should-run }}
steps:
- name: Check if re-run would overwrite successful status
id: check
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const headSha = context.sha;
const runId = context.runId;
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT) || 1;
core.info(`Run info: SHA=${headSha}, runId=${runId}, attempt=${runAttempt}`);
// First attempts always proceed
if (runAttempt === 1) {
core.info('First attempt - proceeding');
core.setOutput('should-run', 'true');
return;
}
// Check if jobs would actually run or would skip
const pr = context.payload.pull_request;
const isDraft = pr?.draft === true;
const isFork = pr?.head?.repo?.full_name !== pr?.base?.repo?.full_name;
const isWorkflowDispatch = context.eventName === 'workflow_dispatch';
const jobsWouldRun = isWorkflowDispatch || (!isDraft && !isFork);
if (jobsWouldRun) {
core.info('Re-run would execute jobs - proceeding');
core.setOutput('should-run', 'true');
return;
}
// Jobs would be skipped. Check if successful runs already exist.
core.info('Re-run would skip jobs - checking for existing successful runs');
try {
const { data: checkRuns } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: headSha,
per_page: 100,
});
const successfulOtherRuns = checkRuns.check_runs.filter(cr => {
const isOurJob = cr.name === 'auto-label' || cr.name === 'check-existing-success';
const isSuccessful = cr.conclusion === 'success';
const fromDifferentRun = cr.html_url && !cr.html_url.includes(`/runs/${runId}/`);
return isOurJob && isSuccessful && fromDifferentRun;
});
if (successfulOtherRuns.length > 0) {
core.info(`Found ${successfulOtherRuns.length} successful runs - aborting to preserve status`);
core.setOutput('should-run', 'false');
return;
}
} catch (error) {
core.warning(`Could not check existing runs: ${error.message}`);
}
core.info('No existing successful runs - proceeding');
core.setOutput('should-run', 'true');
auto-label:
needs: check-existing-success
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
# Skip draft PRs and fork PRs (workflow_dispatch always runs)
if: |
needs.check-existing-success.outputs.should-run == 'true' &&
(github.event_name == 'workflow_dispatch' ||
(!github.event.pull_request.draft &&
github.event.pull_request.head.repo.full_name == github.repository))
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
sparse-checkout: |
content
data/products.yml
scripts/lib/content-utils.js
.github/scripts/workflow-utils.js
package.json
sparse-checkout-cone-mode: false
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: 22
- name: Install js-yaml
run: npm install --no-save --ignore-scripts --no-package-lock --legacy-peer-deps js-yaml
- name: Apply product labels
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const {
getProductLabelMap,
matchFilesToLabels,
} = await import(
`${process.cwd()}/.github/scripts/workflow-utils.js`
);
const prNumber =
context.issue.number ||
Number('${{ inputs.pr_number }}');
if (!prNumber) {
core.setFailed('No PR number available');
return;
}
// --- Build path-to-label mapping from products.yml ---
const pathToLabel = await getProductLabelMap();
core.info(
`Loaded ${pathToLabel.size} path-to-label mappings from products.yml`
);
// --- Get changed files from the PR (paginated) ---
const files = await github.paginate(
github.rest.pulls.listFiles,
{
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100,
}
);
const changedFiles = files.map(f => f.filename);
core.info(`PR has ${changedFiles.length} changed files`);
// --- Match files to product labels ---
const labelsToAdd = matchFilesToLabels(changedFiles, pathToLabel);
if (labelsToAdd.size === 0) {
core.info('No product labels to add');
return;
}
// --- Get existing PR labels to avoid duplicates ---
const { data: prData } = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingLabels = new Set(prData.labels.map(l => l.name));
const newLabels = [...labelsToAdd].filter(
l => !existingLabels.has(l)
);
if (newLabels.length === 0) {
core.info('All matching labels already present');
return;
}
// --- Apply labels ---
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: newLabels,
});
core.info(`Added labels: ${newLabels.join(', ')}`);