Introduces integration-based webhook registration, message parsing, dry-run monitoring, JSM ticket creation, and OAuth token refresh for DC Ops spaces. Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
import fetch from 'node-fetch';
|
|
|
|
function buildAuthHeader(config) {
|
|
const jira = config.jiraCloud;
|
|
|
|
if (jira.authType === 'bearer') {
|
|
return `Bearer ${jira.token}`;
|
|
}
|
|
|
|
return `Basic ${Buffer.from(`${jira.email}:${jira.token}`).toString('base64')}`;
|
|
}
|
|
|
|
export function createJiraTicketsService(config, log) {
|
|
const { logError } = log;
|
|
|
|
async function createJsmRequest(payload) {
|
|
const response = await fetch(`${config.jiraCloud.host}/rest/servicedeskapi/request`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
Authorization: buildAuthHeader(config),
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
const responseText = await response.text();
|
|
let data;
|
|
|
|
try {
|
|
data = responseText ? JSON.parse(responseText) : {};
|
|
} catch {
|
|
data = { raw: responseText };
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const message = data.errorMessage || data.message || responseText || 'Unknown JSM error';
|
|
const error = new Error(message);
|
|
error.status = response.status;
|
|
error.details = data;
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
async function createJobRequest(payload) {
|
|
try {
|
|
const data = await createJsmRequest(payload);
|
|
return data;
|
|
} catch (error) {
|
|
logError('jiraTickets', 'Failed to create JSM request', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return {
|
|
createJsmRequest,
|
|
createJobRequest,
|
|
};
|
|
}
|