2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { pipelineFields, pipelineOperations } from './PipelineDescription';
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { circleciApiRequest, circleciApiRequestAllItems } from './GenericFunctions';
|
2020-06-30 15:46:45 -07:00
|
|
|
|
|
|
|
export class CircleCi implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'CircleCI',
|
|
|
|
name: 'circleCi',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-06-30 15:46:45 -07:00
|
|
|
icon: 'file:circleCi.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume CircleCI API',
|
|
|
|
defaults: {
|
|
|
|
name: 'CircleCI',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'circleCiApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-06-30 15:46:45 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-06-30 15:46:45 -07:00
|
|
|
options: [
|
|
|
|
{
|
2022-05-20 14:47:24 -07:00
|
|
|
name: 'Pipeline',
|
2020-06-30 15:46:45 -07:00
|
|
|
value: 'pipeline',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'pipeline',
|
|
|
|
},
|
|
|
|
...pipelineOperations,
|
|
|
|
...pipelineFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-06-30 15:46:45 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-06-30 15:46:45 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'pipeline') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const vcs = this.getNodeParameter('vcs', i) as string;
|
|
|
|
let slug = this.getNodeParameter('projectSlug', i) as string;
|
|
|
|
const pipelineNumber = this.getNodeParameter('pipelineNumber', i) as number;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
slug = slug.replace(new RegExp(/\//g), '%2F');
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/project/${vcs}/${slug}/pipeline/${pipelineNumber}`;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await circleciApiRequest.call(this, 'GET', endpoint, {}, qs);
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const vcs = this.getNodeParameter('vcs', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
let slug = this.getNodeParameter('projectSlug', i) as string;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
slug = slug.replace(new RegExp(/\//g), '%2F');
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.branch) {
|
|
|
|
qs.branch = filters.branch;
|
|
|
|
}
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/project/${vcs}/${slug}/pipeline`;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await circleciApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await circleciApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
responseData = responseData.items;
|
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2020-06-30 15:46:45 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'trigger') {
|
|
|
|
const vcs = this.getNodeParameter('vcs', i) as string;
|
|
|
|
let slug = this.getNodeParameter('projectSlug', i) as string;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
slug = slug.replace(new RegExp(/\//g), '%2F');
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/project/${vcs}/${slug}/pipeline`;
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.branch) {
|
|
|
|
body.branch = additionalFields.branch as string;
|
|
|
|
}
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.tag) {
|
|
|
|
body.tag = additionalFields.tag as string;
|
|
|
|
}
|
2020-06-30 15:46:45 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await circleciApiRequest.call(this, 'POST', endpoint, body, qs);
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-06-30 15:46:45 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push(...(responseData as INodeExecutionData[]));
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ error: error.message, json: {}, itemIndex: i });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-06-30 15:46:45 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-06-30 15:46:45 -07:00
|
|
|
}
|
|
|
|
}
|