2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-02-09 15:39:14 -08:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Github
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function githubApiRequest(
|
2023-02-15 04:12:28 -08:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: object,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<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 {
|
2022-08-17 08:50:24 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter(
|
|
|
|
'authentication',
|
|
|
|
0,
|
|
|
|
'accessToken',
|
|
|
|
) as string;
|
2022-05-31 00:46:20 -07:00
|
|
|
let credentialType = '';
|
2020-04-04 08:34:10 -07:00
|
|
|
|
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');
|
2022-05-31 00:46:20 -07:00
|
|
|
credentialType = 'githubApi';
|
2020-04-04 08:34:10 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const baseUrl = credentials.server || 'https://api.github.com';
|
2020-04-04 08:34:10 -07:00
|
|
|
options.uri = `${baseUrl}${endpoint}`;
|
2020-02-09 15:39:14 -08:00
|
|
|
} else {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('githubOAuth2Api');
|
2022-05-31 00:46:20 -07:00
|
|
|
credentialType = '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-02-09 15:39:14 -08:00
|
|
|
}
|
2022-05-31 00:46:20 -07:00
|
|
|
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2019-06-23 03:35:23 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the SHA of the given file
|
|
|
|
*
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getFileSha(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
owner: string,
|
|
|
|
repository: string,
|
|
|
|
filePath: string,
|
|
|
|
branch?: string,
|
|
|
|
): Promise<any> {
|
2023-11-17 05:07:33 -08:00
|
|
|
const query: IDataObject = {};
|
2019-06-23 03:35:23 -07:00
|
|
|
if (branch !== undefined) {
|
2023-11-17 05:07:33 -08:00
|
|
|
query.ref = branch;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
2023-11-17 05:07:33 -08:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
const getEndpoint = `/repos/${owner}/${repository}/contents/${encodeURI(filePath)}`;
|
2023-11-17 05:07:33 -08:00
|
|
|
const responseData = await githubApiRequest.call(this, 'GET', getEndpoint, {}, query);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function githubApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-01-19 23:33:17 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.per_page = 100;
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = await githubApiRequest.call(this, method, endpoint, body as IDataObject, query, {
|
2022-08-17 08:50:24 -07:00
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2021-01-19 23:33:17 -08:00
|
|
|
query.page++;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData.body as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.headers.link?.includes('next'));
|
2021-01-19 23:33:17 -08:00
|
|
|
return returnData;
|
|
|
|
}
|