forked from redhat-developer/rhdh-plugin-export-overlays
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel-mandatory-workspace-prs.yaml
More file actions
278 lines (240 loc) · 11.5 KB
/
Copy pathlabel-mandatory-workspace-prs.yaml
File metadata and controls
278 lines (240 loc) · 11.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: Label Workspace PRs
on:
schedule:
- cron: '0 6 * * *' # Daily at 6:00 AM UTC
workflow_dispatch: # Allow manual triggering
workflow_call: # Allow calling from other workflows
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true
jobs:
label-workspace-prs:
runs-on: ubuntu-latest
name: Label PRs based on Workspace Changes
permissions:
contents: read
pull-requests: write
issues: read
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Label PRs based on workspace changes
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs');
// read the rhdh-supported-packages.txt and rhdh-community-packages.txt
const downstreamPluginsContent = fs.readFileSync('rhdh-supported-packages.txt', 'utf8') +
fs.readFileSync('rhdh-community-packages.txt', 'utf8');
const requiredPlugins = [];
const lines = downstreamPluginsContent.split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines and comments
if (trimmedLine === '' || trimmedLine.startsWith('#')) {
continue;
}
requiredPlugins.push(trimmedLine);
}
console.log(`Found ${requiredPlugins.length} required plugins in rhdh-supported-packages.txt and rhdh-community-packages.txt`);
// function to check if a workspace contains required plugins
function workspaceHasRequiredPlugins(workspace) {
// Check if any required plugin line starts with the workspace name
return requiredPlugins.some(pluginLine => pluginLine.startsWith(`${workspace}/`));
}
// function to check if a workspace directory exists on the target branch
async function workspaceExistsOnTargetBranch(workspace, targetBranch) {
try {
await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: `workspaces/${workspace}`,
ref: targetBranch
});
return true;
} catch (error) {
if (error.status === 404) {
return false;
}
throw error;
}
}
// Define the labels we'll apply
const LABELS = {
UPDATE: 'workspace-update',
ADDITION: 'workspace-addition',
OUTSIDE: 'non-workspace-changes',
MANDATORY: 'mandatory-workspace',
RELEASE_PATCH: 'release-branch-patch'
};
// Ensure all labels exist
for (const [key, labelName] of Object.entries(LABELS)) {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName
});
} catch (error) {
if (error.status === 404) {
let description, color;
switch (key) {
case 'UPDATE':
description = 'PR modifies files in an existing workspace';
color = '0075ca'; // Blue
break;
case 'ADDITION':
description = 'PR adds a new workspace';
color = '0e8a16'; // Green
break;
case 'OUTSIDE':
description = 'PR changes files outside workspace directories';
color = '6f42c1'; // Purple
break;
case 'MANDATORY':
description = 'PR affects a workspace with required plugins for releases';
color = 'd73a4a'; // Red
break;
case 'RELEASE_PATCH':
description = 'PR modifies workspace on a release branch';
color = 'fbca04'; // Yellow
break;
}
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName,
description: description,
color: color
});
console.log(`Created label: ${labelName}`);
} else {
throw error;
}
}
}
// Get all open PRs
const prs = await github.paginate(github.rest.pulls.list, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100
});
console.log(`Found ${prs.length} open PRs`);
for (const pr of prs) {
try {
console.log(`\n--- Processing PR #${pr.number}: ${pr.title} ---`);
// Get current labels on the PR
const currentLabels = pr.labels.map(label => label.name);
const currentWorkspaceLabels = currentLabels.filter(label =>
Object.values(LABELS).includes(label)
);
// Analyze PR files to know what changes this PR contains
const prFiles = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number
});
// Categorize files
const workspaceFiles = [];
const nonWorkspaceFiles = [];
const allAffectedWorkspaces = new Set();
for (const file of prFiles.data) {
const workspaceMatch = file.filename.match(/^workspaces\/([^\/]+)\/.*/);
if (workspaceMatch) {
const workspace = workspaceMatch[1];
workspaceFiles.push({ file, workspace });
allAffectedWorkspaces.add(workspace);
} else {
nonWorkspaceFiles.push(file);
}
}
const newWorkspaces = new Set();
const existingWorkspaces = new Set();
for (const workspace of allAffectedWorkspaces) {
const exists = await workspaceExistsOnTargetBranch(workspace, pr.base.ref);
if (exists) {
existingWorkspaces.add(workspace);
console.log(`Workspace ${workspace} exists on ${pr.base.ref} - treating as update`);
} else {
newWorkspaces.add(workspace);
console.log(`Workspace ${workspace} doesn't exist on ${pr.base.ref} - treating as addition`);
}
}
// Determine label(s)
let targetLabels = [];
let logMessage = `PR #${pr.number}`;
const isMainBranch = pr.base.ref === 'main';
const isReleaseBranch = pr.base.ref.startsWith('release-');
if (workspaceFiles.length === 0) {
// No workspace files changed - outside workspaces
targetLabels = [LABELS.OUTSIDE];
logMessage += ` affects only non-workspace files`;
} else {
const totalAffectedWorkspaces = newWorkspaces.size + existingWorkspaces.size;
if (totalAffectedWorkspaces === 1) {
const workspace = newWorkspaces.size === 1
? Array.from(newWorkspaces)[0]
: Array.from(existingWorkspaces)[0];
if (newWorkspaces.has(workspace)) {
targetLabels = [LABELS.ADDITION];
logMessage += ` adds new workspace: ${workspace}`;
} else {
targetLabels = [LABELS.UPDATE];
logMessage += ` updates workspace: ${workspace}`;
}
// Add branch-specific labels
if (isMainBranch && workspaceHasRequiredPlugins(workspace)) {
targetLabels.push(LABELS.MANDATORY);
logMessage += ` (contains required plugins, main branch)`;
} else if (isReleaseBranch) {
targetLabels.push(LABELS.RELEASE_PATCH);
logMessage += ` (release branch patch)`;
}
} else {
// Multiple workspaces affected - this should not be labeled for publishing at least from what i understand
targetLabels = []; // No specific labels
const allWorkspaceNames = [...newWorkspaces, ...existingWorkspaces];
logMessage += ` affects multiple workspaces: ${allWorkspaceNames.join(', ')}`;
// Note: we intentionally don't label multi-workspace PRs as they can't be published or they are hard to publish after talk with david
}
}
console.log(logMessage);
// Apply label changes
const labelsToAdd = targetLabels.filter(label => !currentLabels.includes(label));
const labelsToRemove = currentWorkspaceLabels.filter(label => !targetLabels.includes(label));
// Add new labels
if (labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: labelsToAdd
});
console.log(`Added labels: ${labelsToAdd.join(', ')}`);
}
// Remove old labels
for (const label of labelsToRemove) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: label
});
console.log(`Removed label: ${label}`);
} catch (error) {
if (error.status !== 404) {
console.error(`Failed to remove label ${label}:`, error.message);
}
}
}
if (labelsToAdd.length === 0 && labelsToRemove.length === 0) {
console.log(`✓ Labels already correct`);
}
} catch (error) {
console.error(`Error processing PR #${pr.number}:`, error.message);
// Continue with next PR instead of failing the entire workflow
}
}
console.log('Finished labeling workspace PRs');