Prevent converting a Test Execution to another issue type when there is a test run
Hi All using below code I saw its working in log but how can I restrict the move if I have test runs
I shared the below code that's not restricting the move click but issue got reset after final changes
async function checkTestRuns(issueId, token) {
const query = `
query ($issueId: String!) {
getTestExecution(issueId: $issueId) {
testRuns(limit: 10) {
total
results {
id
status { name }
test { jira(fields: ["key", "summary"]) }
}
}
}
}
`;
const response = await api.fetch('https://xray.cloud.getxray.app/api/v2/graphql', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ query, variables: { issueId } })
});
if (!response.ok) {
console.error('❌ Failed to fetch test runs:', await response.text());
return false;
}
const data = await response.json();
console.log("data",JSON.stringify(data))
const total = data?.data?.getTestExecution?.testRuns?.total || 0;
return total > 0;
}
// Check if issue type changed from Test Execution while test runs exist
export async function validateIssueTypeChange(event, token) {
const issueId = event?.issue?.id;
const changelog = event?.changelog?.items || [];
const issueTypeChange = changelog.find(i => i.field === 'issuetype');
const fromTypeId = issueTypeChange?.from;
const toTypeId = issueTypeChange?.to;
const currentTypeName = event.issue.fields.issuetype?.name;
console.log('Type change:', { fromTypeId, toTypeId, currentTypeName });
// Only validate if moving away from Test Execution
if (fromTypeId && toTypeId && currentTypeName !== 'Xray Test Execution') {
const hasRuns = await checkTestRuns(issueId, token);
if (hasRuns) {
console.log('🚫 Issue type conversion blocked due to existing test runs.');
await api.asApp().requestJira(route`/rest/api/3/issue/${issueId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
fields: {
issuetype: { name: 'Xray Test Execution' }
}
})
});
return {
success: false,
message: '❌ Cannot convert Test Execution to another issue type while test runs exist.'
};
}
else {
return { success: true };
}
}
// Main validator entry point
export async function run(event) {
console.log('Incoming event:', JSON.stringify(event, null, 2));
//const issueId = event?.issue?.id;
const token = XRAY_TOKEN;
const typeChangeResult = await validateIssueTypeChange(event, token);
console.log("typeChangeResult",typeChangeResult)
if (!typeChangeResult) return typeChangeResult;
}