Hello , I want to restrict subtasks creation per issue type and when parent status is Done using forge and using the trigger avi:jira:created:issue .The restriction when parent is done works but restriction per issue type doesn’t work showing these logs : Parent issue key not found. Retrying… .
Here is the function:
export async function restrictSubtasksFunction(event) {
const { issue } = event;
let issueDeleted = false;
async function getParentIssueData(issue) {
const parentIssueKey = issue?.fields?.parent?.key;
if (!parentIssueKey) {
console.log('Parent issue key not found. Retrying...');
return null;
}
try {
const response = await api.asApp().requestJira(route`/rest/api/3/issue/${parentIssueKey}`);
if (!response.ok) {
const errorMessage = await response.text();
console.error('Erreur lors de la récupération des détails du parent :', errorMessage);
return null;
}
return await response.json();
} catch (error) {
console.error(`Failed to retrieve parent issue data: ${error.message}`);
return null;
}
}
if (issue?.fields?.issuetype?.subtask) {
try {
let parentData = await getParentIssueData(issue);
// Retry if parentData is null
if (!parentData) {
await new Promise(resolve => setTimeout(resolve, 3000)); // Attendre 3 secondes
parentData = await getParentIssueData(issue);
}
if (parentData) {
const parentStatus = parentData?.fields?.status?.name;
const parentIssueType = parentData?.fields?.issuetype?.name;
// Vérification du statut du parent
if (parentStatus === "Done") {
issueDeleted = true;
await api.store.set(`popup-${issue.key}`, {
show: true,
message: 'Sous-tâche supprimée car le parent est en statut "Done".',
});
return {
statusCode: 400,
body: `Impossible de créer une sous-tâche car le parent est en statut "Done".`,
};
}
// Vérification du type de l'issue parent
if (parentIssueType === "Task") {
issueDeleted = true;
await api.store.set(`popup-${issue.key}`, {
show: true,
message: 'Sous-tâche supprimée car le parent est de type "Task".',
});
return {
statusCode: 400,
body: `Impossible de créer une sous-tâche car le parent est de type "Task".`,
};
}
} else {
console.log('Parent issue details could not be retrieved after retrying.');
}
} catch (error) {
console.error(`Erreur dans consolidatedRestrictionFunction: ${error}`);
return {
statusCode: 500,
body: `Une erreur s'est produite lors de la vérification du parent : ${error.message}`,
};
}
}
return {
statusCode: issueDeleted ? 200 : 500,
body: issueDeleted ? 'Subtask is deleted because of parent status or type' : 'Subtasks allowed.',
};
}