2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2022-02-11 08:20:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
import {
|
2022-03-20 02:11:06 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestFunctions,
|
2022-02-11 08:20:41 -08:00
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
2022-03-20 02:11:06 -07:00
|
|
|
JsonObject,
|
2022-02-11 08:20:41 -08:00
|
|
|
NodeApiError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import get from 'lodash.get';
|
2022-03-20 02:11:06 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { query } from './Queries';
|
2022-03-20 02:11:06 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function linearApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('linearApi');
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
const endpoint = 'https://api.linear.app/graphql';
|
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: credentials.apiKey,
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
uri: endpoint,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
return this.helpers.request!(options);
|
2022-02-11 08:20:41 -08:00
|
|
|
} catch (error) {
|
2022-03-20 02:11:06 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-02-11 08:20:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function capitalizeFirstLetter(data: string) {
|
|
|
|
return data.charAt(0).toUpperCase() + data.slice(1);
|
2022-03-20 02:11:06 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function linearApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
): Promise<any> {
|
2022-03-20 02:11:06 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
body.variables.first = 50;
|
|
|
|
body.variables.after = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await linearApiRequest.call(this, body);
|
|
|
|
returnData.push.apply(returnData, get(responseData, `${propertyName}.nodes`));
|
|
|
|
body.variables.after = get(responseData, `${propertyName}.pageInfo.endCursor`);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (get(responseData, `${propertyName}.pageInfo.hasNextPage`));
|
2022-03-20 02:11:06 -07:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function validateCredentials(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
decryptedCredentials: ICredentialDataDecryptedObject,
|
|
|
|
): Promise<any> {
|
2022-03-20 02:11:06 -07:00
|
|
|
const credentials = decryptedCredentials;
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: credentials.apiKey,
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
query: query.getIssues(),
|
|
|
|
variables: {
|
|
|
|
first: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
uri: 'https://api.linear.app/graphql',
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.helpers.request!(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
export const sort = (a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-03-20 02:11:06 -07:00
|
|
|
return 0;
|
|
|
|
};
|