mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Merge pull request #676 from n8n-io/Gitlab-OAuth2-support
Gitlab OAuth2 support
This commit is contained in:
commit
81fd5da5e9
|
@ -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',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import {
|
||||||
import {
|
import {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
import { OptionsWithUri } from 'request';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make an API request to Gitlab
|
* Make an API request to Gitlab
|
||||||
|
@ -17,27 +18,44 @@ import {
|
||||||
* @param {object} body
|
* @param {object} body
|
||||||
* @returns {Promise<any>}
|
* @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 credentials = this.getCredentials('gitlabApi');
|
const options : OptionsWithUri = {
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {},
|
||||||
'Private-Token': `${credentials.accessToken}`,
|
|
||||||
},
|
|
||||||
body,
|
body,
|
||||||
qs: query,
|
qs: query,
|
||||||
uri: `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`,
|
uri: '',
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
if (query === undefined) {
|
||||||
//@ts-ignore
|
delete options.qs;
|
||||||
return await this.helpers?.request(options);
|
}
|
||||||
|
|
||||||
|
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!');
|
||||||
|
}
|
||||||
|
|
||||||
|
options.headers!['Private-Token'] = `${credentials.accessToken}`;
|
||||||
|
|
||||||
|
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) {
|
} catch (error) {
|
||||||
if (error.statusCode === 401) {
|
if (error.statusCode === 401) {
|
||||||
// Return a clear error
|
// Return a clear error
|
||||||
|
|
|
@ -13,7 +13,6 @@ import {
|
||||||
gitlabApiRequest,
|
gitlabApiRequest,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
|
|
||||||
export class Gitlab implements INodeType {
|
export class Gitlab implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'Gitlab',
|
displayName: 'Gitlab',
|
||||||
|
@ -33,9 +32,44 @@ export class Gitlab implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'gitlabApi',
|
name: 'gitlabApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'gitlabOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
@ -793,10 +827,26 @@ export class Gitlab implements INodeType {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
const credentials = this.getCredentials('gitlabApi');
|
let credentials;
|
||||||
|
|
||||||
if (credentials === undefined) {
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
|
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
|
// Operations which overwrite the returned data
|
||||||
|
|
|
@ -14,7 +14,6 @@ import {
|
||||||
gitlabApiRequest,
|
gitlabApiRequest,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
|
|
||||||
export class GitlabTrigger implements INodeType {
|
export class GitlabTrigger implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'Gitlab Trigger',
|
displayName: 'Gitlab Trigger',
|
||||||
|
@ -34,7 +33,25 @@ export class GitlabTrigger implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'gitlabApi',
|
name: 'gitlabApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'gitlabOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
webhooks: [
|
webhooks: [
|
||||||
{
|
{
|
||||||
|
@ -45,6 +62,23 @@ export class GitlabTrigger implements INodeType {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Repository Owner',
|
||||||
name: 'owner',
|
name: 'owner',
|
||||||
|
|
|
@ -60,6 +60,7 @@
|
||||||
"dist/credentials/GithubApi.credentials.js",
|
"dist/credentials/GithubApi.credentials.js",
|
||||||
"dist/credentials/GithubOAuth2Api.credentials.js",
|
"dist/credentials/GithubOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GitlabApi.credentials.js",
|
"dist/credentials/GitlabApi.credentials.js",
|
||||||
|
"dist/credentials/GitlabOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GoogleApi.credentials.js",
|
"dist/credentials/GoogleApi.credentials.js",
|
||||||
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
|
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",
|
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",
|
||||||
|
|
Loading…
Reference in a new issue