Skip to content

Commit

Permalink
fix(Discourse Node): Fix issue with not all posts getting returned an…
Browse files Browse the repository at this point in the history
…d add credential test (#3007)

* 🔨 fix for not all posts returning

* ⚡ added credential test

* ⚡ Improvements

* ⚡ Improvements

* ⚡ Define test the new way

* ⚡ Remove not needed imports

* ⚡ Fix auth test problem

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Ricardo Espinoza <ricardo@n8n.io>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
  • Loading branch information
4 people authored Apr 18, 2022
1 parent a9653c2 commit d68b7a4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
24 changes: 24 additions & 0 deletions packages/nodes-base/credentials/DiscourseApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';

Expand Down Expand Up @@ -30,4 +33,25 @@ export class DiscourseApi implements ICredentialType {
default: '',
},
];

async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
requestOptions.headers = {
'Api-Key': credentials.apiKey,
'Api-Username': credentials.username,
};

if (requestOptions.method === 'GET') {
delete requestOptions.body;
}

return requestOptions;
}

test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.url}}',
url: '/admin/groups.json',
method: 'GET',
},
};
}
2 changes: 1 addition & 1 deletion packages/nodes-base/credentials/ZendeskApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ZendeskApi implements ICredentialType {
password: credentials.apiToken as string,
};
return requestOptions;
}
}
test: ICredentialTestRequest = {
request: {
baseURL: '=https://{{$credentials.subdomain}}.zendesk.com/api/v2',
Expand Down
31 changes: 28 additions & 3 deletions packages/nodes-base/nodes/Discourse/Discourse.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import {
} from 'n8n-core';

import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';

import {
Expand Down Expand Up @@ -44,6 +48,7 @@ import {
userGroupFields,
userGroupOperations,
} from './UserGroupDescription';
import { OptionsWithUri } from 'request';

//import moment from 'moment';

Expand Down Expand Up @@ -117,6 +122,7 @@ export class Discourse implements INodeType {
};

methods = {

loadOptions: {
// Get all the calendars to display them to user so that he can
// select them easily
Expand Down Expand Up @@ -321,6 +327,7 @@ export class Discourse implements INodeType {
//https://docs.discourse.org/#tag/Posts/paths/~1posts.json/get
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const limit = this.getNodeParameter('limit', i, 0) as number;

responseData = await discourseApiRequest.call(
this,
Expand All @@ -329,11 +336,29 @@ export class Discourse implements INodeType {
{},
qs,
);

responseData = responseData.latest_posts;

//Getting all posts relying on https://github.com/discourse/discourse_api/blob/main/spec/discourse_api/api/posts_spec.rb
let lastPost = responseData.pop();
let previousLastPostID;
while (lastPost.id !== previousLastPostID) {
if (limit && responseData.length > limit) {
break;
}
const chunk = await discourseApiRequest.call(
this,
'GET',
`/posts.json?before=${lastPost.id}`,
{},
qs,
);
responseData = responseData.concat(chunk.latest_posts);
previousLastPostID = lastPost.id;
lastPost = responseData.pop();
}
responseData.push(lastPost);

if (returnAll === false) {
const limit = this.getNodeParameter('limit', i) as number;
responseData = responseData.splice(0, limit);
}
}
Expand Down Expand Up @@ -495,7 +520,7 @@ export class Discourse implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
Expand Down
13 changes: 4 additions & 9 deletions packages/nodes-base/nodes/Discourse/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import {
} from 'n8n-core';

import {
IDataObject, NodeApiError,
IDataObject, JsonObject, NodeApiError,
} from 'n8n-workflow';

export async function discourseApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, qs: IDataObject = {}, option = {}): Promise<any> { // tslint:disable-line:no-any

const credentials = await this.getCredentials('discourseApi');
const credentials = await this.getCredentials('discourseApi') as { url: string };

const options: OptionsWithUri = {
headers: {
'Api-Key': credentials.apiKey,
'Api-Username': credentials.username,
},
method,
body,
qs,
Expand All @@ -32,10 +28,9 @@ export async function discourseApiRequest(this: IExecuteFunctions | IExecuteSing
if (Object.keys(body).length === 0) {
delete options.body;
}
//@ts-ignore
return await this.helpers.request.call(this, options);
return await this.helpers.requestWithAuthentication.call(this, 'discourseApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

Expand Down
7 changes: 0 additions & 7 deletions packages/nodes-base/nodes/Zendesk/Zendesk.node.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import {
OptionsWithUri,
} from 'request';

import {
IExecuteFunctions,
} from 'n8n-core';

import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
Expand Down

0 comments on commit d68b7a4

Please sign in to comment.