forked from rapidsai/cudf
-
Notifications
You must be signed in to change notification settings - Fork 0
319 lines (272 loc) · 12.7 KB
/
issue-release-scheduled.yml
File metadata and controls
319 lines (272 loc) · 12.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: Issue Release Field Scheduled Sync
on:
schedule:
- cron: '0 * * * *'
workflow_dispatch:
jobs:
sync-release-fields:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
with:
persist-credentials: false
- name: Restore priority cache
id: cache-priority
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: .github/priority-cache.json
key: priority-cache-${{ github.run_id }}
restore-keys: |
priority-cache-
- name: Sync release fields for all project issues
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
with:
github-token: ${{ secrets.PROJECT_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const fs = require('fs');
const path = require('path');
// Only operate on this specific project
const TARGET_PROJECT_ID = 'PVT_kwDOAp2shc4AiNzl';
console.log('Starting scheduled release field sync...');
try {
// Load previous priority cache
const cacheFile = '.github/priority-cache.json';
let previousPriorities = {};
if (fs.existsSync(cacheFile)) {
const cacheContent = fs.readFileSync(cacheFile, 'utf8');
previousPriorities = JSON.parse(cacheContent);
console.log(`Loaded ${Object.keys(previousPriorities).length} cached priority values`);
} else {
console.log('No previous priority cache found, treating all as new');
}
// Current priorities (will be saved at the end)
const currentPriorities = {};
// Read VERSION file to get current release
const versionContent = fs.readFileSync('VERSION', 'utf8').trim();
const versionParts = versionContent.split('.');
const currentRelease = `${versionParts[0]}.${versionParts[1]}`;
console.log(`Current release from VERSION: ${currentRelease}`);
// Query the target project directly
console.log(`Querying project: ${TARGET_PROJECT_ID}`);
const projectQuery = `
query($projectId: ID!) {
node(id: $projectId) {
... on ProjectV2 {
title
items(first: 100) {
nodes {
id
content {
__typename
... on Issue {
number
repository {
owner { login }
name
}
}
... on PullRequest {
number
}
}
fieldValues(first: 20) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
name
field {
... on ProjectV2SingleSelectField {
id
name
}
}
}
... on ProjectV2ItemFieldTextValue {
text
field {
... on ProjectV2Field {
id
name
}
}
}
}
}
}
}
fields(first: 20) {
nodes {
... on ProjectV2Field {
id
name
dataType
}
... on ProjectV2SingleSelectField {
id
name
dataType
options {
id
name
}
}
}
}
}
}
}
`;
const projectResult = await github.graphql(projectQuery, {
projectId: TARGET_PROJECT_ID
});
const project = projectResult.node;
if (!project) {
throw new Error(`Project with ID ${TARGET_PROJECT_ID} not found`);
}
const projectTitle = project.title;
const projectItems = project.items?.nodes || [];
const projectFields = project.fields?.nodes || [];
console.log(`Found project: ${projectTitle}`);
console.log(`Found ${projectItems.length} items in project`);
// Get Release field information
const releaseField = projectFields.find(f => f.name === 'Release');
if (!releaseField) {
throw new Error(`Release field not found in project "${projectTitle}"`);
}
const releaseFieldId = releaseField.id;
const releaseFieldType = releaseField.dataType;
const releaseFieldOptions = releaseField.options || [];
console.log(`Release field type: ${releaseFieldType}`);
let updatedCount = 0;
let skippedCount = 0;
let errorCount = 0;
// Process each item in the project
for (const projectItem of projectItems) {
const itemId = projectItem.id;
const content = projectItem.content;
// Skip if no content (item was deleted)
if (!content) {
continue;
}
// Only process issues (skip PRs, draft issues, etc.)
if (content.__typename !== 'Issue') {
continue;
}
const issueNumber = content.number;
const issueOwner = content.repository.owner.login;
const issueRepo = content.repository.name;
// Skip if issue is not from this repository
if (issueOwner !== owner || issueRepo !== repo) {
continue;
}
console.log(`\nChecking issue #${issueNumber}`);
try {
// Find Priority and Release field values
let priorityValue = null;
let currentReleaseValue = null;
for (const fieldValue of projectItem.fieldValues.nodes) {
if (fieldValue.field?.name === 'Priority') {
priorityValue = fieldValue.name;
console.log(` Found Priority: ${priorityValue}`);
}
if (fieldValue.field?.name === 'Release') {
currentReleaseValue = fieldValue.text || fieldValue.name;
console.log(` Found Release: ${currentReleaseValue}`);
}
}
// Skip if no Priority value
if (!priorityValue) {
console.log(` No Priority value found, skipping`);
skippedCount++;
continue;
}
// Store current priority for this issue
const cacheKey = `${TARGET_PROJECT_ID}:${issueNumber}`;
currentPriorities[cacheKey] = priorityValue;
// Check if Priority has changed since last run
const previousPriority = previousPriorities[cacheKey];
const priorityChanged = previousPriority !== priorityValue;
if (!priorityChanged) {
console.log(` Priority unchanged (${priorityValue}), skipping Release update (preserving manual changes)`);
continue;
}
console.log(` Priority changed: "${previousPriority || '(new)'}" → "${priorityValue}"`);
// Determine what the release value should be based on priority
let expectedReleaseValue;
if (priorityValue === '0' || priorityValue === '1') {
expectedReleaseValue = currentRelease;
} else if (priorityValue.match(/^[2-9]$/) || priorityValue.match(/^\d{2,}$/)) {
expectedReleaseValue = 'Backlog';
} else {
console.log(` Unknown priority format: ${priorityValue}, skipping`);
continue;
}
console.log(` Updating Release field to: "${expectedReleaseValue}"`);
// Find the option ID for the expected release value
const option = releaseFieldOptions.find(o => o.name === expectedReleaseValue);
if (!option) {
console.log(` ERROR: Option "${expectedReleaseValue}" not found in Release field options`);
console.log(` Available options: ${releaseFieldOptions.map(o => o.name).join(', ')}`);
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: `⚠️ **Release Automation Error**: Cannot set Release to "${expectedReleaseValue}" - this option does not exist in the project.\n\nAvailable options: ${releaseFieldOptions.map(o => o.name).join(', ')}\n\nPlease add "${expectedReleaseValue}" as an option to the Release field in your project.`
});
errorCount++;
continue;
}
console.log(` Using option ID: ${option.id} for "${expectedReleaseValue}"`);
// Update the Release field with the single select option ID
const updateMutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $optionId }
}
) {
projectV2Item {
id
}
}
}
`;
await github.graphql(updateMutation, {
projectId: TARGET_PROJECT_ID,
itemId: itemId,
fieldId: releaseFieldId,
optionId: option.id
});
console.log(` ✓ Successfully updated Release field to "${expectedReleaseValue}"`);
updatedCount++;
} catch (error) {
console.error(`Error processing issue #${issueNumber}:`, error.message);
errorCount++;
// Continue with next issue rather than failing the entire workflow
}
}
// Save current priorities cache for next run
const cacheDir = '.github';
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
fs.writeFileSync(cacheFile, JSON.stringify(currentPriorities, null, 2));
console.log(`\nSaved ${Object.keys(currentPriorities).length} priority values to cache`);
// Summary
console.log('\n=== Sync Summary ===');
console.log(`Total items in project: ${projectItems.length}`);
console.log(`Issues updated: ${updatedCount}`);
console.log(`Issues skipped (no Priority or unchanged): ${skippedCount}`);
console.log(`Errors encountered: ${errorCount}`);
} catch (error) {
console.error('Fatal error in scheduled sync:', error);
throw error;
}