mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
111 lines
2.6 KiB
TypeScript
111 lines
2.6 KiB
TypeScript
import { OptionsWithUri } from 'request';
|
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
|
|
|
/**
|
|
* Make an API request to Github
|
|
*
|
|
*/
|
|
export async function githubApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: object,
|
|
query?: object,
|
|
option: IDataObject = {},
|
|
): Promise<any> {
|
|
const options: OptionsWithUri = {
|
|
method,
|
|
headers: {
|
|
'User-Agent': 'n8n',
|
|
},
|
|
body,
|
|
qs: query,
|
|
uri: '',
|
|
json: true,
|
|
};
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
Object.assign(options, option);
|
|
}
|
|
|
|
try {
|
|
const authenticationMethod = this.getNodeParameter(
|
|
'authentication',
|
|
0,
|
|
'accessToken',
|
|
) as string;
|
|
let credentialType = '';
|
|
|
|
if (authenticationMethod === 'accessToken') {
|
|
const credentials = await this.getCredentials('githubApi');
|
|
credentialType = 'githubApi';
|
|
|
|
const baseUrl = credentials.server || 'https://api.github.com';
|
|
options.uri = `${baseUrl}${endpoint}`;
|
|
} else {
|
|
const credentials = await this.getCredentials('githubOAuth2Api');
|
|
credentialType = 'githubOAuth2Api';
|
|
|
|
const baseUrl = credentials.server || 'https://api.github.com';
|
|
options.uri = `${baseUrl}${endpoint}`;
|
|
}
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the SHA of the given file
|
|
*
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
*/
|
|
export async function getFileSha(
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
owner: string,
|
|
repository: string,
|
|
filePath: string,
|
|
branch?: string,
|
|
): Promise<any> {
|
|
const getBody: IDataObject = {};
|
|
if (branch !== undefined) {
|
|
getBody.branch = branch;
|
|
}
|
|
const getEndpoint = `/repos/${owner}/${repository}/contents/${encodeURI(filePath)}`;
|
|
const responseData = await githubApiRequest.call(this, 'GET', getEndpoint, getBody, {});
|
|
|
|
if (responseData.sha === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'Could not get the SHA of the file.');
|
|
}
|
|
return responseData.sha;
|
|
}
|
|
|
|
export async function githubApiRequestAllItems(
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
query: IDataObject = {},
|
|
): Promise<any> {
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
query.per_page = 100;
|
|
query.page = 1;
|
|
|
|
do {
|
|
responseData = await githubApiRequest.call(this, method, endpoint, body, query, {
|
|
resolveWithFullResponse: true,
|
|
});
|
|
query.page++;
|
|
returnData.push.apply(returnData, responseData.body);
|
|
} while (responseData.headers.link?.includes('next'));
|
|
return returnData;
|
|
}
|