Drive approval card details from requestTypeFields.json, fix Service Request type 382→194, and add discovery tooling plus tests. Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
#!/usr/bin/env node
|
|
// Dump JSM request-type field definitions for approval card mapping.
|
|
//
|
|
// Usage:
|
|
// node scripts/discover-request-type-fields.js
|
|
// Writes config/requestTypeFields.discovered.json
|
|
|
|
import dotenv from 'dotenv';
|
|
import fetch from 'node-fetch';
|
|
import fs from 'fs';
|
|
|
|
dotenv.config();
|
|
|
|
const cloudId = process.env.JIRA_CLOUD_ID?.trim();
|
|
const host = cloudId
|
|
? `https://api.atlassian.com/ex/jira/${cloudId}`
|
|
: (process.env.JIRA_BASE_URL || 'https://aeo.atlassian.net').replace(/\/$/, '');
|
|
const email = process.env.JIRA_EMAIL;
|
|
const token = process.env.JIRA_API_TOKEN;
|
|
|
|
if (!email || !token) {
|
|
console.error('JIRA_EMAIL and JIRA_API_TOKEN must be set in .env');
|
|
process.exit(1);
|
|
}
|
|
|
|
const headers = {
|
|
Accept: 'application/json',
|
|
Authorization: `Basic ${Buffer.from(`${email}:${token}`).toString('base64')}`,
|
|
};
|
|
|
|
const deskMap = {
|
|
177: '135',
|
|
179: '135',
|
|
183: '135',
|
|
};
|
|
const defaultDesk = '136';
|
|
|
|
const requests = JSON.parse(fs.readFileSync('./config/requests.json', 'utf8'));
|
|
|
|
function summarizeField(field) {
|
|
return {
|
|
fieldId: field.fieldId,
|
|
name: field.name,
|
|
required: field.required,
|
|
type: field.jiraSchema?.type,
|
|
custom: field.jiraSchema?.custom,
|
|
};
|
|
}
|
|
|
|
const output = {
|
|
_meta: {
|
|
generatedAt: new Date().toISOString(),
|
|
serviceDesks: { CHANGE: '135', REQUEST: '136' },
|
|
source: 'GET /rest/servicedeskapi/servicedesk/{deskId}/requesttype/{id}/field',
|
|
},
|
|
requestTypes: {},
|
|
};
|
|
|
|
for (const [id, cfg] of Object.entries(requests)) {
|
|
const deskId = deskMap[id] || defaultDesk;
|
|
const url = `${host}/rest/servicedeskapi/servicedesk/${deskId}/requesttype/${id}/field`;
|
|
const response = await fetch(url, { headers });
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
output.requestTypes[id] = {
|
|
name: cfg.name,
|
|
serviceDeskId: deskId,
|
|
error: data.errorMessage || String(response.status),
|
|
};
|
|
continue;
|
|
}
|
|
|
|
output.requestTypes[id] = {
|
|
name: cfg.name,
|
|
serviceDeskId: deskId,
|
|
fields: (data.requestTypeFields || []).map(summarizeField),
|
|
};
|
|
}
|
|
|
|
const outputPath = './config/requestTypeFields.discovered.json';
|
|
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2));
|
|
console.log(`Wrote ${outputPath}`);
|
|
|
|
for (const [id, rt] of Object.entries(output.requestTypes)) {
|
|
const count = rt.fields?.length ?? 0;
|
|
console.log(`${id} ${rt.name}: ${rt.error || `${count} fields`}`);
|
|
}
|