Skip to content

Commit a2f4149

Browse files
committed
using correct state params
1 parent 3f4f7fe commit a2f4149

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

ado-importer.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,59 @@ async function createWorkItem(adoClient, adoOrg, project, workItemType, flaw, pa
762762
console.log('Payload:', JSON.stringify(payload, null, 2));
763763
}
764764

765+
// For Bug, we might need to adapt payload to process templates. Try candidates when needed.
766+
const isBug = String(workItemType).toLowerCase() === 'bug';
767+
if (isBug) {
768+
// Build candidate payloads derived from the primary Bug payload above
769+
const baseAdds = payload.filter(p => p.path !== '/fields/Microsoft.VSTS.TCM.ReproSteps' && p.path !== '/fields/System.Description');
770+
const repro = { op: 'add', path: '/fields/Microsoft.VSTS.TCM.ReproSteps', value: description };
771+
const desc = { op: 'add', path: '/fields/System.Description', value: description };
772+
const candidates = [
773+
[...baseAdds, repro, desc],
774+
[...baseAdds, repro],
775+
[...baseAdds, desc],
776+
[...baseAdds]
777+
];
778+
779+
for (let i = 0; i < candidates.length; i++) {
780+
try {
781+
if (debug === 'true') {
782+
console.log(`Posting Bug create with candidate #${i+1}`);
783+
}
784+
const response = await adoClient.post(url, candidates[i], {
785+
headers: { 'Content-Type': 'application/json-patch+json' }
786+
});
787+
if (debug === 'true') {
788+
console.log('Response:', JSON.stringify(response.data, null, 2));
789+
}
790+
// If last candidate (no description fields) was used, add description to Discussion
791+
if (i === candidates.length - 1) {
792+
try {
793+
const discussUrl = `/${adoOrg}/${project}/_apis/wit/workitems/${response.data.id}?api-version=7.0`;
794+
const discussPayload = [{ op: 'add', path: '/fields/System.History', value: description }];
795+
await adoClient.patch(discussUrl, discussPayload, { headers: { 'Content-Type': 'application/json-patch+json' } });
796+
} catch (e) {
797+
console.error(`Failed to backfill Discussion for Bug ${response.data?.id}: ${e.message}`);
798+
}
799+
}
800+
return response.data;
801+
} catch (error) {
802+
const status = error?.response?.status;
803+
console.error(`Bug create candidate #${i+1} failed${status ? ` (status ${status})` : ''}: ${error.message}`);
804+
if (error?.response?.data) {
805+
console.error('ADO response:', JSON.stringify(error.response.data));
806+
}
807+
if (!(status && (status === 400 || status === 422))) {
808+
throw error;
809+
}
810+
// try next
811+
}
812+
}
813+
// If all candidates failed, throw
814+
throw new Error('Failed to create Bug work item after trying all payload variants');
815+
}
816+
817+
// Non-Bug types: single post as before
765818
try {
766819
const response = await adoClient.post(url, payload, {
767820
headers: {
@@ -773,14 +826,10 @@ async function createWorkItem(adoClient, adoOrg, project, workItemType, flaw, pa
773826
}
774827
return response.data;
775828
} catch (error) {
776-
if (debug === 'true') {
777-
console.error('Error creating work item:');
778-
console.error('Status:', error.response?.status);
779-
console.error('Data:', error.response?.data);
780-
console.error('Headers:', error.response?.headers);
781-
console.error('Request URL:', error.config?.url);
782-
console.error('Request Method:', error.config?.method);
783-
console.error('Request Headers:', error.config?.headers);
829+
console.error('Error creating work item:', error.message);
830+
if (error.response) {
831+
console.error('Status:', error.response.status);
832+
console.error('Data:', error.response.data);
784833
}
785834
throw error;
786835
}
@@ -1226,7 +1275,7 @@ function processAnnotationsADO(annotations) {
12261275

12271276
// ADO-specific pipeline flaws processing
12281277
async function processPipelineFlawsADO(adoPatchClient, adoOrg, adoProject, adoWorkItemType, flawData, params) {
1229-
const { source_base_path_1, source_base_path_2, source_base_path_3, commit_hash, waitTime, fail_build, debug, existingWorkItems, processedFlawIds, duplicateDetectionData } = params;
1278+
const { source_base_path_1, source_base_path_2, source_base_path_3, commit_hash, waitTime, fail_build, debug, existingWorkItems, processedFlawIds, duplicateDetectionData, adoOpenState, adoReopenState } = params;
12301279

12311280
let createdCount = 0;
12321281
let reopenedCount = 0;
@@ -1314,7 +1363,7 @@ async function processPipelineFlawsADO(adoPatchClient, adoOrg, adoProject, adoWo
13141363

13151364
// ADO-specific policy flaws processing
13161365
async function processPolicyFlawsADO(adoPatchClient, adoOrg, adoProject, adoWorkItemType, flawData, params) {
1317-
const { source_base_path_1, source_base_path_2, source_base_path_3, commit_hash, waitTime, fail_build, debug, existingWorkItems, processedFlawIds, duplicateDetectionData } = params;
1366+
const { source_base_path_1, source_base_path_2, source_base_path_3, commit_hash, waitTime, fail_build, debug, existingWorkItems, processedFlawIds, duplicateDetectionData, adoOpenState, adoReopenState } = params;
13181367

13191368
let createdCount = 0;
13201369
let reopenedCount = 0;

0 commit comments

Comments
 (0)