2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-09-17 14:20:27 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-09-17 14:20:12 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { createHash } from 'crypto';
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
export async function getAuthorization(
|
2022-08-17 08:50:24 -07:00
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IWebhookFunctions,
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials?: ICredentialDataDecryptedObject,
|
2020-09-17 14:20:12 -07:00
|
|
|
): Promise<string> {
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-09-17 14:20:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const { password, username } = credentials;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
type: 'normal',
|
|
|
|
password,
|
|
|
|
username,
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
uri: credentials.url ? `${credentials.url}/api/v1/auth` : 'https://api.taiga.io/api/v1/auth',
|
2020-09-17 14:20:12 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request!(options);
|
|
|
|
|
|
|
|
return response.auth_token;
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-09-17 14:20:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function taigaApiRequest(
|
2022-08-17 08:50:24 -07:00
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IWebhookFunctions,
|
2020-09-17 14:20:12 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body = {},
|
|
|
|
query = {},
|
|
|
|
uri?: string | undefined,
|
2020-10-22 09:00:28 -07:00
|
|
|
option = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('taigaApi');
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
const authToken = await getAuthorization.call(this, credentials);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
bearer: authToken,
|
|
|
|
},
|
|
|
|
qs: query,
|
|
|
|
method,
|
|
|
|
body,
|
2022-08-17 08:50:24 -07:00
|
|
|
uri:
|
|
|
|
uri || credentials.url
|
|
|
|
? `${credentials.url}/api/v1${resource}`
|
|
|
|
: `https://api.taiga.io/api/v1${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-09-17 14:20:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-09-17 14:20:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function taigaApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-09-17 14:20:12 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await taigaApiRequest.call(this, method, resource, body, query, uri, {
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2020-09-17 14:20:12 -07:00
|
|
|
returnData.push.apply(returnData, responseData.body);
|
|
|
|
uri = responseData.headers['x-pagination-next'];
|
|
|
|
if (query.limit && returnData.length >= query.limit) {
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
} while (
|
|
|
|
responseData.headers['x-pagination-next'] !== undefined &&
|
|
|
|
responseData.headers['x-pagination-next'] !== ''
|
|
|
|
);
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
|
|
|
|
const data = `${credentials.username},${credentials.password}`;
|
|
|
|
return createHash('md5').update(data).digest('hex');
|
|
|
|
}
|
2021-07-14 15:02:30 -07:00
|
|
|
|
|
|
|
export async function handleListing(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
i: number,
|
|
|
|
) {
|
|
|
|
let responseData;
|
|
|
|
qs.project = this.getNodeParameter('projectId', i) as number;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
return await taigaApiRequestAllItems.call(this, method, endpoint, body, qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await taigaApiRequestAllItems.call(this, method, endpoint, body, qs);
|
|
|
|
return responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const toOptions = (items: LoadedResource[]) =>
|
|
|
|
items.map(({ name, id }) => ({ name, value: id }));
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function throwOnEmptyUpdate(this: IExecuteFunctions, resource: Resource) {
|
2021-07-14 15:02:30 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getVersionForUpdate(this: IExecuteFunctions, endpoint: string) {
|
|
|
|
return await taigaApiRequest.call(this, 'GET', endpoint).then((response) => response.version);
|
2021-07-14 15:02:30 -07:00
|
|
|
}
|