mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 13:14:07 -08:00
7ce7285f7a
* Changes to types so that credentials can be always loaded from DB This first commit changes all return types from the execute functions and calls to get credentials to be async so we can use await. This is a first step as previously credentials were loaded in memory and always available. We will now be loading them from the DB which requires turning the whole call chain async. * Fix updated files * Removed unnecessary credential loading to improve performance * Fix typo * ⚡ Fix issue * Updated new nodes to load credentials async * ⚡ Remove not needed comment Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
110 lines
3.2 KiB
TypeScript
110 lines
3.2 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
|
|
*
|
|
* @param {IHookFunctions} this
|
|
* @param {string} method
|
|
* @param {string} url
|
|
* @param {object} body
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-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;
|
|
|
|
if (authenticationMethod === 'accessToken') {
|
|
const credentials = await this.getCredentials('githubApi');
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
}
|
|
|
|
const baseUrl = credentials!.server || 'https://api.github.com';
|
|
options.uri = `${baseUrl}${endpoint}`;
|
|
|
|
options.headers!.Authorization = `token ${credentials.accessToken}`;
|
|
return await this.helpers.request(options);
|
|
} else {
|
|
const credentials = await this.getCredentials('githubOAuth2Api');
|
|
|
|
const baseUrl = credentials!.server || 'https://api.github.com';
|
|
options.uri = `${baseUrl}${endpoint}`;
|
|
//@ts-ignore
|
|
return await this.helpers.requestOAuth2.call(this, 'githubOAuth2Api', options);
|
|
}
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 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) {
|
|
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> { // 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;
|
|
}
|