2020-05-05 13:56:24 -07:00
|
|
|
import {
|
2020-04-23 22:59:19 -07:00
|
|
|
OptionsWithUri,
|
2021-01-29 11:08:27 -08:00
|
|
|
} from 'request';
|
2019-11-26 12:38:38 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-02-06 19:03:29 -08:00
|
|
|
IExecuteSingleFunctions,
|
2019-11-26 12:38:38 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-05-05 13:56:24 -07:00
|
|
|
import {
|
2020-05-04 21:07:19 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
2020-10-01 05:01:39 -07:00
|
|
|
IDataObject,
|
2022-03-27 02:49:47 -07:00
|
|
|
JsonObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2019-11-26 12:38:38 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2021-01-29 11:08:27 -08:00
|
|
|
export async function jiraSoftwareCloudApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-05-05 13:56:24 -07:00
|
|
|
|
|
|
|
const jiraVersion = this.getNodeParameter('jiraVersion', 0) as string;
|
|
|
|
|
2022-07-04 02:43:37 -07:00
|
|
|
let domain = '';
|
|
|
|
let credentialType: string;
|
2020-05-05 13:56:24 -07:00
|
|
|
|
|
|
|
if (jiraVersion === 'server') {
|
2022-07-04 02:43:37 -07:00
|
|
|
domain = (await this.getCredentials('jiraSoftwareServerApi')).domain as string;
|
|
|
|
credentialType = 'jiraSoftwareServerApi';
|
2020-02-02 07:01:56 -08:00
|
|
|
} else {
|
2022-07-04 02:43:37 -07:00
|
|
|
domain = (await this.getCredentials('jiraSoftwareCloudApi')).domain as string;
|
|
|
|
credentialType = 'jiraSoftwareCloudApi';
|
2020-02-02 07:01:56 -08:00
|
|
|
}
|
2020-05-05 13:56:24 -07:00
|
|
|
|
2020-01-31 07:21:14 -08:00
|
|
|
const options: OptionsWithUri = {
|
2020-02-06 19:03:29 -08:00
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2021-01-29 11:08:27 -08:00
|
|
|
'X-Atlassian-Token': 'no-check',
|
2020-02-06 19:03:29 -08:00
|
|
|
},
|
2020-01-31 07:21:14 -08:00
|
|
|
method,
|
|
|
|
qs: query,
|
2020-07-25 01:22:02 -07:00
|
|
|
uri: uri || `${domain}/rest${endpoint}`,
|
2020-01-31 07:21:14 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-01-31 07:21:14 -08:00
|
|
|
};
|
2019-11-26 12:38:38 -08:00
|
|
|
|
2021-01-29 11:08:27 -08:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(query || {}).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:21:14 -08:00
|
|
|
try {
|
2022-07-04 02:43:37 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2020-01-31 07:21:14 -08:00
|
|
|
} catch (error) {
|
2022-03-27 02:49:47 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-01-31 07:21:14 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 12:38:38 -08:00
|
|
|
|
2020-11-09 22:43:39 -08:00
|
|
|
export async function jiraSoftwareCloudApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2019-11-26 12:38:38 -08:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
2020-02-02 07:01:56 -08:00
|
|
|
query.startAt = 0;
|
|
|
|
body.startAt = 0;
|
2019-11-29 14:30:00 -08:00
|
|
|
query.maxResults = 100;
|
2021-01-29 11:08:27 -08:00
|
|
|
body.maxResults = 100;
|
2019-11-26 12:38:38 -08:00
|
|
|
|
|
|
|
do {
|
2020-02-02 07:01:56 -08:00
|
|
|
responseData = await jiraSoftwareCloudApiRequest.call(this, endpoint, method, body, query);
|
2019-11-26 12:38:38 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2020-02-02 07:01:56 -08:00
|
|
|
query.startAt = responseData.startAt + responseData.maxResults;
|
|
|
|
body.startAt = responseData.startAt + responseData.maxResults;
|
2019-11-26 12:38:38 -08:00
|
|
|
} while (
|
2020-02-02 07:01:56 -08:00
|
|
|
(responseData.startAt + responseData.maxResults < responseData.total)
|
2019-11-26 12:38:38 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-07-25 01:22:02 -07:00
|
|
|
|
2021-01-29 11:08:27 -08:00
|
|
|
export function eventExists(currentEvents: string[], webhookEvents: string[]) {
|
2020-07-25 01:22:02 -07:00
|
|
|
for (const currentEvent of currentEvents) {
|
|
|
|
if (!webhookEvents.includes(currentEvent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:08:27 -08:00
|
|
|
export function getId(url: string) {
|
2020-07-25 01:22:02 -07:00
|
|
|
return url.split('/').pop();
|
|
|
|
}
|
|
|
|
|
2022-04-14 23:00:47 -07:00
|
|
|
export function simplifyIssueOutput(responseData: {
|
2022-03-27 02:49:47 -07:00
|
|
|
names: { [key: string]: string },
|
|
|
|
fields: IDataObject,
|
|
|
|
id: string,
|
|
|
|
key: string,
|
|
|
|
self: string
|
|
|
|
}) {
|
|
|
|
const mappedFields: IDataObject = {
|
|
|
|
id: responseData.id,
|
|
|
|
key: responseData.key,
|
|
|
|
self: responseData.self,
|
|
|
|
};
|
|
|
|
// Sort custom fields last so we map them last
|
|
|
|
const customField = /^customfield_\d+$/;
|
|
|
|
const sortedFields: string[] = Object.keys(responseData.fields).sort((a, b) => {
|
|
|
|
if (customField.test(a) && customField.test(b)) {
|
|
|
|
return a > b ? 1 : -1;
|
|
|
|
}
|
|
|
|
if (customField.test(a)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (customField.test(b)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return a > b ? 1 : -1;
|
|
|
|
});
|
|
|
|
for (const field of sortedFields) {
|
|
|
|
if (responseData.names[field] in mappedFields) {
|
|
|
|
let newField: string = responseData.names[field];
|
|
|
|
let counter = 0;
|
|
|
|
while (newField in mappedFields) {
|
|
|
|
counter++;
|
|
|
|
newField = `${responseData.names[field]}_${counter}`;
|
|
|
|
}
|
|
|
|
mappedFields[newField] = responseData.fields[field];
|
|
|
|
} else {
|
|
|
|
mappedFields[responseData.names[field] || field] = responseData.fields[field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappedFields;
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:22:02 -07:00
|
|
|
export const allEvents = [
|
|
|
|
'board_created',
|
|
|
|
'board_updated',
|
|
|
|
'board_deleted',
|
|
|
|
'board_configuration_changed',
|
|
|
|
'comment_created',
|
|
|
|
'comment_updated',
|
|
|
|
'comment_deleted',
|
|
|
|
'jira:issue_created',
|
|
|
|
'jira:issue_updated',
|
|
|
|
'jira:issue_deleted',
|
|
|
|
'option_voting_changed',
|
|
|
|
'option_watching_changed',
|
|
|
|
'option_unassigned_issues_changed',
|
|
|
|
'option_subtasks_changed',
|
|
|
|
'option_attachments_changed',
|
|
|
|
'option_issuelinks_changed',
|
|
|
|
'option_timetracking_changed',
|
|
|
|
'project_created',
|
|
|
|
'project_updated',
|
|
|
|
'project_deleted',
|
|
|
|
'sprint_created',
|
|
|
|
'sprint_deleted',
|
|
|
|
'sprint_updated',
|
|
|
|
'sprint_started',
|
|
|
|
'sprint_closed',
|
|
|
|
'user_created',
|
|
|
|
'user_updated',
|
|
|
|
'user_deleted',
|
|
|
|
'jira:version_released',
|
|
|
|
'jira:version_unreleased',
|
|
|
|
'jira:version_created',
|
|
|
|
'jira:version_moved',
|
|
|
|
'jira:version_updated',
|
|
|
|
'jira:version_deleted',
|
|
|
|
'issuelink_created',
|
|
|
|
'issuelink_deleted',
|
|
|
|
'worklog_created',
|
|
|
|
'worklog_updated',
|
|
|
|
'worklog_deleted',
|
2021-04-16 09:33:36 -07:00
|
|
|
];
|