Skip to content

Commit f9c48ce

Browse files
committed
fixing reopen function
1 parent 1d18dd4 commit f9c48ce

File tree

1 file changed

+66
-37
lines changed

1 file changed

+66
-37
lines changed

ado-importer.js

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -560,37 +560,56 @@ async function reopenWorkItem(adoClient, adoOrg, adoProject, workItemId, params)
560560
const { source_base_path_1, source_base_path_2, source_base_path_3, commit_hash, debug } = params;
561561

562562
const url = `/${adoOrg}/${adoProject}/_apis/wit/workitems/${workItemId}?api-version=7.0`;
563-
const payload = [
564-
{
565-
op: 'replace',
566-
path: '/fields/System.State',
567-
value: systemState
568-
},
569-
{
570-
op: 'add',
571-
path: '/fields/System.History',
572-
value: `Reopened by Veracode scan - Commit: ${commit_hash || 'Unknown'}`
573-
}
574-
];
563+
564+
// Try different "open" states for different ADO processes
565+
const candidateStates = ['To Do', 'Active', 'New', 'Open'];
566+
567+
for (const state of candidateStates) {
568+
const payload = [
569+
{
570+
op: 'replace',
571+
path: '/fields/System.State',
572+
value: state
573+
},
574+
{
575+
op: 'add',
576+
path: '/fields/System.History',
577+
value: `Reopened by Veracode scan - Commit: ${commit_hash || 'Unknown'}`
578+
}
579+
];
575580

576-
if (debug === 'true') {
577-
console.log('Reopening work item with payload:', JSON.stringify(payload, null, 2));
578-
}
581+
if (debug === 'true') {
582+
console.log(`Attempting to reopen work item ${workItemId} using state "${state}" with payload:`, JSON.stringify(payload, null, 2));
583+
}
579584

580-
try {
581-
const response = await adoClient.patch(url, payload, {
582-
headers: {
583-
'Content-Type': 'application/json-patch+json'
585+
try {
586+
const response = await adoClient.patch(url, payload, {
587+
headers: {
588+
'Content-Type': 'application/json-patch+json'
589+
}
590+
});
591+
if (debug === 'true') {
592+
console.log(`Work item ${workItemId} reopened successfully with state "${state}"`);
584593
}
585-
});
586-
if (debug === 'true') {
587-
console.log('Work item reopened successfully:', response.data.id);
594+
return response.data;
595+
} catch (error) {
596+
const status = error?.response?.status;
597+
if (debug === 'true') {
598+
console.log(`Reopening with state "${state}" failed${status ? ` (status ${status})` : ''}. Trying next candidate...`);
599+
if (error?.response?.data) {
600+
console.log('ADO error response:', JSON.stringify(error.response.data));
601+
}
602+
}
603+
// Try next candidate state on 400/422 errors, otherwise rethrow
604+
if (status && (status === 400 || status === 422)) {
605+
continue;
606+
}
607+
throw error;
588608
}
589-
return response.data;
590-
} catch (error) {
591-
console.error(`Failed to reopen work item ${workItemId}:`, error.message);
592-
throw error;
593609
}
610+
611+
// If none of the states worked, throw a clear error
612+
throw new Error(`Failed to reopen work item ${workItemId}: none of the candidate states were accepted (${candidateStates.join(', ')})`);
594613
}
595614

596615
async function createWorkItem(adoClient, adoOrg, project, workItemType, flaw, params) {
@@ -1338,17 +1357,27 @@ async function processPolicyFlawsADO(adoPatchClient, adoOrg, adoProject, adoWork
13381357

13391358
// Reopen the work item if it's closed
13401359
if (workItemState === 'Closed' || workItemState === 'Resolved' || workItemState === 'Done') {
1341-
await reopenWorkItem(adoPatchClient, adoOrg, adoProject, workItemId, {
1342-
source_base_path_1,
1343-
source_base_path_2,
1344-
source_base_path_3,
1345-
commit_hash,
1346-
debug
1347-
});
1348-
reopenedCount++;
1349-
1350-
// Wait between API calls to avoid rate limiting
1351-
await new Promise(resolve => setTimeout(resolve, waitTime * 1000));
1360+
try {
1361+
await reopenWorkItem(adoPatchClient, adoOrg, adoProject, workItemId, {
1362+
source_base_path_1,
1363+
source_base_path_2,
1364+
source_base_path_3,
1365+
commit_hash,
1366+
debug
1367+
});
1368+
reopenedCount++;
1369+
console.log(`✅ Successfully reopened work item ${workItemId} for flaw ${flawId}`);
1370+
1371+
// Wait between API calls to avoid rate limiting
1372+
await new Promise(resolve => setTimeout(resolve, waitTime * 1000));
1373+
} catch (reopenError) {
1374+
console.error(`❌ Failed to reopen work item ${workItemId} for flaw ${flawId}:`, reopenError.message);
1375+
if (debug === 'true') {
1376+
console.error('Reopen error details:', reopenError);
1377+
}
1378+
}
1379+
} else {
1380+
console.log(`Work item ${workItemId} is already open (State: ${workItemState}), no need to reopen`);
13521381
}
13531382
}
13541383

0 commit comments

Comments
 (0)