Add Trello List getAll/getCards functionality (#1350)

* Fix to add List.getCards

*  Small improvements to #1347

*  Minor formatting improvements

*  Small fix

Co-authored-by: tumf <y.takahara@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2021-01-19 09:09:03 -05:00 committed by GitHub
parent 9911348166
commit 4336088741
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 283 additions and 4 deletions

View file

@ -53,3 +53,26 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
throw error;
}
}
export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
query.limit = 30;
query.sort = '-id';
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await apiRequest.call(this, method, endpoint, body, query);
returnData.push.apply(returnData, responseData);
if (responseData.length !== 0) {
query.before = responseData[responseData.length - 1].id;
}
} while (
query.limit <= responseData.length
);
return returnData;
}

View file

@ -33,6 +33,16 @@ export const listOperations = [
value: 'get',
description: 'Get the data of a list',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all the lists',
},
{
name: 'Get Cards',
value: 'getCards',
description: 'Get all the cards in a list',
},
{
name: 'Update',
value: 'update',
@ -159,6 +169,89 @@ export const listFields = [
],
},
// ----------------------------------
// list:getCards
// ----------------------------------
{
displayName: 'List ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
operation: [
'getCards',
],
resource: [
'list',
],
},
},
description: 'The ID of the list to get cards.',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'list',
],
operation: [
'getCards',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 20,
displayOptions: {
show: {
resource: [
'list',
],
operation: [
'getCards',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
operation: [
'getCards',
],
resource: [
'list',
],
},
},
default: {},
options: [
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: 'all',
description: 'Fields to return. Either "all" or a comma-separated list of fields.',
},
],
},
// ----------------------------------
// list:get
// ----------------------------------
@ -207,6 +300,90 @@ export const listFields = [
],
},
// ----------------------------------
// list:getAll
// ----------------------------------
{
displayName: 'Board ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'list',
],
},
},
description: 'The ID of the board',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'list',
],
operation: [
'getAll',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 20,
displayOptions: {
show: {
resource: [
'list',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'list',
],
},
},
default: {},
options: [
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: 'all',
description: 'Fields to return. Either "all" or a comma-separated list of fields.',
},
],
},
// ----------------------------------
// list:update
// ----------------------------------

View file

@ -11,6 +11,7 @@ import {
import {
apiRequest,
apiRequestAllItems,
} from './GenericFunctions';
import {
@ -52,14 +53,14 @@ export class Trello implements INodeType {
description: INodeTypeDescription = {
displayName: 'Trello',
name: 'trello',
icon: 'file:trello.png',
icon: 'file:trello.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Create, change and delete boards and cards',
defaults: {
name: 'Trello',
color: '#026aa7',
color: '#0079bf',
},
inputs: ['main'],
outputs: ['main'],
@ -147,6 +148,8 @@ export class Trello implements INodeType {
let requestMethod: string;
let endpoint: string;
let returnAll = false;
let responseData;
for (let i = 0; i < items.length; i++) {
requestMethod = 'GET';
@ -365,6 +368,46 @@ export class Trello implements INodeType {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
Object.assign(qs, additionalFields);
} else if (operation === 'getAll') {
// ----------------------------------
// getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
const id = this.getNodeParameter('id', i) as string;
endpoint = `boards/${id}/lists`;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
Object.assign(qs, additionalFields);
} else if (operation === 'getCards') {
// ----------------------------------
// getCards
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
const id = this.getNodeParameter('id', i) as string;
endpoint = `lists/${id}/cards`;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
Object.assign(qs, additionalFields);
} else if (operation === 'update') {
// ----------------------------------
// update
@ -549,7 +592,7 @@ export class Trello implements INodeType {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
Object.assign(qs, additionalFields);
} else if (operation ==='completedCheckItems') {
} else if (operation === 'completedCheckItems') {
// ----------------------------------
// completedCheckItems
// ----------------------------------
@ -673,7 +716,21 @@ export class Trello implements INodeType {
throw new Error(`The resource "${resource}" is not known!`);
}
const responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs);
// resources listed here do not support pagination so
// paginate them 'manually'
const skipPagination = [
'list:getAll',
];
if (returnAll === true && !skipPagination.includes(`${resource}:${operation}`)) {
responseData = await apiRequestAllItems.call(this, requestMethod, endpoint, body, qs);
} else {
responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs);
if (returnAll === false && qs.limit) {
responseData = responseData.splice(0, qs.limit);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 541 B

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="200px" height="200px" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 41 (35326) - http://www.bohemiancoding.com/sketch -->
<title>trello-mark-blue-flat</title>
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Logos" transform="translate(-1579.000000, -521.000000)">
<g id="Group" transform="translate(-9.000000, 1.000000)">
<g id="Trello-Logo" transform="translate(468.000000, 0.000000)">
<g id="Trello-Mark---Blue---Flat" transform="translate(1020.000000, 420.000000)">
<g id="Mark" transform="translate(100.000000, 100.000000)">
<rect id="Board" fill="#0079BF" x="0" y="0" width="200" height="200" rx="25"></rect>
<rect id="Right-List" fill="#FFFFFF" x="113" y="26" width="61" height="87.5" rx="12"></rect>
<rect id="Left-List" fill="#FFFFFF" x="26" y="26" width="61" height="137.5" rx="12"></rect>
</g>
</g>
</g>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB