Merge pull request #676 from n8n-io/Gitlab-OAuth2-support

Gitlab OAuth2 support
This commit is contained in:
Ricardo Espinoza 2020-07-23 15:05:35 -04:00 committed by GitHub
commit 81fd5da5e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 21 deletions

View file

@ -0,0 +1,53 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class GitlabOAuth2Api implements ICredentialType {
name = 'gitlabOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Gitlab OAuth2 API';
properties = [
{
displayName: 'Gitlab Server',
name: 'server',
type: 'string' as NodePropertyTypes,
default: 'https://gitlab.com'
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://gitlab.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://gitlab.com/oauth/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: 'api',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body',
},
];
}

View file

@ -7,6 +7,7 @@ import {
import {
IDataObject,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
/**
* Make an API request to Gitlab
@ -17,27 +18,44 @@ import {
* @param {object} body
* @returns {Promise<any>}
*/
export async function gitlabApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
export async function gitlabApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
const options : OptionsWithUri = {
method,
headers: {},
body,
qs: query,
uri: '',
json: true
};
if (query === undefined) {
delete options.qs;
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('gitlabApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const options = {
method,
headers: {
'Private-Token': `${credentials.accessToken}`,
},
body,
qs: query,
uri: `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`,
json: true
};
options.headers!['Private-Token'] = `${credentials.accessToken}`;
try {
//@ts-ignore
return await this.helpers?.request(options);
options.uri = `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`;
return await this.helpers.request(options);
} else {
const credentials = this.getCredentials('gitlabOAuth2Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.uri = `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`;
return await this.helpers.requestOAuth2!.call(this, 'gitlabOAuth2Api', options);
}
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error

View file

@ -13,7 +13,6 @@ import {
gitlabApiRequest,
} from './GenericFunctions';
export class Gitlab implements INodeType {
description: INodeTypeDescription = {
displayName: 'Gitlab',
@ -33,9 +32,44 @@ export class Gitlab implements INodeType {
{
name: 'gitlabApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'gitlabOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.',
},
{
displayName: 'Resource',
name: 'resource',
@ -793,11 +827,27 @@ export class Gitlab implements INodeType {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const credentials = this.getCredentials('gitlabApi');
let credentials;
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
if (authenticationMethod === 'accessToken') {
credentials = this.getCredentials('gitlabApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
} else {
credentials = this.getCredentials('gitlabOAuth2Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
}
} catch (error) {
throw new Error(error);
}
// Operations which overwrite the returned data
const overwriteDataOperations = [

View file

@ -14,7 +14,6 @@ import {
gitlabApiRequest,
} from './GenericFunctions';
export class GitlabTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Gitlab Trigger',
@ -34,7 +33,25 @@ export class GitlabTrigger implements INodeType {
{
name: 'gitlabApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'gitlabOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
{
@ -45,6 +62,23 @@ export class GitlabTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.',
},
{
displayName: 'Repository Owner',
name: 'owner',

View file

@ -60,6 +60,7 @@
"dist/credentials/GithubApi.credentials.js",
"dist/credentials/GithubOAuth2Api.credentials.js",
"dist/credentials/GitlabApi.credentials.js",
"dist/credentials/GitlabOAuth2Api.credentials.js",
"dist/credentials/GoogleApi.credentials.js",
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",