Github OAuth support

This commit is contained in:
Ricardo Espinoza
2020-02-09 10:11:15 -05:00
parent 928bf4dc68
commit 96741460e3
3 changed files with 52 additions and 16 deletions

View File

@@ -17,17 +17,24 @@ import {
* @returns {Promise<any>}
*/
export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('githubApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
const githubApiCredentials = this.getCredentials('githubApi');
const oAuth2ApiCrendetials = this.getCredentials('oAuth2Api');
let headers = {}
if (githubApiCredentials !== undefined) {
headers = {
Authorization: `token ${githubApiCredentials.accessToken}`,
'User-Agent': githubApiCredentials.user,
};
} else {
const { access_token } = oAuth2ApiCrendetials!.oauthTokenData as IDataObject;
headers = {
Authorization: `token ${access_token}`,
'User-Agent': 'Node js',
};
}
const options = {
method,
headers: {
'Authorization': `token ${credentials.accessToken}`,
'User-Agent': credentials.user,
},
headers,
body,
qs: query,
uri: `https://api.github.com${endpoint}`,