Skip to content

Commit 49671dd

Browse files
committed
refactor: improve robustness based on Copilot review
- Add dynamic column detection for Issue Number field with fallback - Use optional chaining for error handling to prevent undefined access - Add comment explaining the default spreadsheet ID is public and shared - Improve error message handling throughout the script
1 parent 9caaa34 commit 49671dd

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

.github/scripts/remove-rca-needed-label-sheets.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ async function main(): Promise<void> {
132132
);
133133
skippedCount++;
134134
}
135-
} catch (error) {
135+
} catch (error: any) {
136136
console.error(
137-
`❌ Failed to process issue #${issue.number}: ${error.message}`,
137+
`❌ Failed to process issue #${issue.number}: ${error?.message || error}`,
138138
);
139139
failedCount++;
140140
failedIssues.push(issue.number);
@@ -167,9 +167,9 @@ async function main(): Promise<void> {
167167
} else {
168168
console.log(`\n✅ All operations completed successfully!`);
169169
}
170-
} catch (error) {
170+
} catch (error: any) {
171171
core.setFailed(
172-
`Error in Google Sheets RCA label removal: ${error.message}`,
172+
`Error in Google Sheets RCA label removal: ${error?.message || error}`,
173173
);
174174
process.exit(1);
175175
}
@@ -206,10 +206,39 @@ async function fetchRcaResponses(sheets: any): Promise<RcaFormResponse[]> {
206206
return [];
207207
}
208208

209-
// Process data rows (skip header row at index 0)
210-
// Column indices based on actual sheet:
211-
// 0: Timestamp, 1: Email, 2: Github Repository, 3: Github Issue URL, 4: Issue Number
212-
const ISSUE_NUMBER_COLUMN = 4;
209+
// Dynamically determine the column index for "Issue Number" from the header row
210+
const headerRow = rows[0] || [];
211+
const ISSUE_NUMBER_HEADER = 'Issue Number';
212+
const issueNumberColumnIndex = headerRow.findIndex(
213+
(col: string) => col && col.trim() === ISSUE_NUMBER_HEADER,
214+
);
215+
216+
if (issueNumberColumnIndex === -1) {
217+
console.warn(
218+
`Could not find "${ISSUE_NUMBER_HEADER}" column in sheet headers. Falling back to column E (index 4)`,
219+
);
220+
// Fallback to known column position for backwards compatibility
221+
const ISSUE_NUMBER_COLUMN = 4;
222+
const responses: RcaFormResponse[] = [];
223+
for (let i = 1; i < rows.length; i++) {
224+
const row = rows[i];
225+
if (!row || row.length === 0) continue;
226+
const issueNumberValue = row[ISSUE_NUMBER_COLUMN];
227+
if (issueNumberValue) {
228+
const issueMatch = issueNumberValue.toString().match(/\d+/);
229+
if (issueMatch) {
230+
responses.push({
231+
issueNumber: issueMatch[0],
232+
timestamp: row[0] || '',
233+
});
234+
console.log(
235+
` Found RCA for issue #${issueMatch[0]} submitted on ${row[0]}`,
236+
);
237+
}
238+
}
239+
}
240+
return responses;
241+
}
213242

214243
const responses: RcaFormResponse[] = [];
215244
for (let i = 1; i < rows.length; i++) {
@@ -220,8 +249,8 @@ async function fetchRcaResponses(sheets: any): Promise<RcaFormResponse[]> {
220249
continue;
221250
}
222251

223-
// Get issue number from column E (index 4)
224-
const issueNumberValue = row[ISSUE_NUMBER_COLUMN];
252+
// Get issue number from dynamically determined column
253+
const issueNumberValue = row[issueNumberColumnIndex];
225254

226255
if (issueNumberValue) {
227256
// Extract just the numeric part from the issue number
@@ -243,8 +272,11 @@ async function fetchRcaResponses(sheets: any): Promise<RcaFormResponse[]> {
243272
}
244273

245274
return responses;
246-
} catch (error) {
247-
console.error('Error fetching Google Sheets data:', error);
275+
} catch (error: any) {
276+
console.error(
277+
'Error fetching Google Sheets data:',
278+
error?.message || error,
279+
);
248280
throw error;
249281
}
250282
}
@@ -315,10 +347,10 @@ async function removeLabelFromIssue(
315347
issue_number: issueNumber,
316348
name: labelName,
317349
});
318-
} catch (error) {
350+
} catch (error: any) {
319351
// If label doesn't exist on issue, the API will throw 404
320352
// This is not an error for our use case, so we can safely ignore it
321-
if (error.status !== 404) {
353+
if (error?.status !== 404) {
322354
throw error;
323355
}
324356
}

.github/workflows/remove-rca-needed-label-sheets.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ on:
1111
spreadsheet_id:
1212
description: 'Google Spreadsheet ID (uses default if not provided)'
1313
required: false
14+
# Default is the shared MetaMask RCA tracking spreadsheet
15+
# This is public and documented in the PR descriptions
1416
default: '1Y16QEnDwZuR3DAQIe3T5LTWy1ye07GNYqxIei_cMg24'
1517
type: string
1618
sheet_name:

0 commit comments

Comments
 (0)