2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-02-11 08:20:41 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-03-20 02:11:06 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestFunctions,
|
2022-02-11 08:20:41 -08:00
|
|
|
IDataObject,
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2022-02-11 08:20:41 -08:00
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
2022-03-20 02:11:06 -07:00
|
|
|
JsonObject,
|
2022-02-11 08:20:41 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-02-11 08:20:41 -08:00
|
|
|
|
2023-06-16 07:26:35 -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-02-11 08:20:41 -08:00
|
|
|
const endpoint = 'https://api.linear.app/graphql';
|
2023-09-20 02:19:12 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'apiToken') as string;
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
uri: endpoint,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
2023-09-20 02:19:12 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(
|
|
|
|
this,
|
|
|
|
authenticationMethod === 'apiToken' ? 'linearApi' : 'linearOAuth2Api',
|
|
|
|
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);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, get(responseData, `${propertyName}.nodes`) as IDataObject[]);
|
2022-03-20 02:11:06 -07:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.request(options);
|
2022-03-20 02:11:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//@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;
|
|
|
|
};
|