2020-02-09 15:39:14 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Github
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2021-01-19 23:33:17 -08:00
|
|
|
export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-02-09 15:39:14 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2019-06-23 03:35:23 -07:00
|
|
|
method,
|
2020-02-09 15:39:14 -08:00
|
|
|
headers: {
|
|
|
|
'User-Agent': 'n8n',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
body,
|
|
|
|
qs: query,
|
2020-04-04 08:34:10 -07:00
|
|
|
uri: '',
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2021-01-19 23:33:17 -08:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
try {
|
2020-04-04 08:34:10 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken') as string;
|
|
|
|
|
2020-02-09 15:39:14 -08:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('githubApi');
|
2020-04-04 08:34:10 -07:00
|
|
|
|
|
|
|
const baseUrl = credentials!.server || 'https://api.github.com';
|
|
|
|
options.uri = `${baseUrl}${endpoint}`;
|
|
|
|
|
2020-02-09 15:39:14 -08:00
|
|
|
options.headers!.Authorization = `token ${credentials.accessToken}`;
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} else {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('githubOAuth2Api');
|
2020-04-04 08:34:10 -07:00
|
|
|
|
2022-04-14 23:00:47 -07:00
|
|
|
const baseUrl = credentials.server || 'https://api.github.com';
|
2020-04-04 08:34:10 -07:00
|
|
|
options.uri = `${baseUrl}${endpoint}`;
|
2020-06-07 14:29:13 -07:00
|
|
|
//@ts-ignore
|
2020-06-01 17:42:38 -07:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'githubOAuth2Api', options);
|
2020-02-09 15:39:14 -08:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the SHA of the given file
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
* @param {string} owner
|
|
|
|
* @param {string} repository
|
|
|
|
* @param {string} filePath
|
|
|
|
* @param {string} [branch]
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function getFileSha(this: IHookFunctions | IExecuteFunctions, owner: string, repository: string, filePath: string, branch?: string): Promise<any> { // tslint:disable-line:no-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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Could not get the SHA of the file.');
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
return responseData.sha;
|
|
|
|
}
|
2021-01-19 23:33:17 -08:00
|
|
|
|
|
|
|
export async function githubApiRequestAllItems(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-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 && responseData.headers.link.includes('next')
|
|
|
|
);
|
|
|
|
return returnData;
|
|
|
|
}
|