This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEasyRedmine.node.ts
More file actions
333 lines (319 loc) · 8.5 KB
/
EasyRedmine.node.ts
File metadata and controls
333 lines (319 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* eslint-disable n8n-nodes-base/node-class-description-inputs-wrong-regular-node, n8n-nodes-base/node-class-description-outputs-wrong */
import {
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeOperationError,
INodeListSearchResult,
} from 'n8n-workflow';
import { EasyNodeOperationType, EasyNodeResourceType } from './Model';
import {
processGetManyOperation,
processGetOneOperation,
addCommentOperation,
updateOperation,
createOperation,
processSearchOperation,
} from './operations';
import { IssueFields } from './fields/IssueFields';
import { LeadFields } from './fields/LeadFields';
import { OpportunityFields } from './fields/OpportunityFields';
import { AccountFields } from './fields/AccountFields';
import { PersonalContactFields } from './fields/PersonalContactFields';
import { UserFields } from './fields/UserFields';
import { TimeEntryFields } from './fields/TimeEntryFields';
import { AttendanceFields } from './fields/AttendanceFields';
import { loadOptions } from './LoadOptions';
import { EasyRedmineClient } from './client';
import { ProjectFields } from './fields/ProjectFields';
/**
* Node that enables communication with EasyRedmine.
*/
export class EasyRedmine implements INodeType {
description: INodeTypeDescription = {
displayName: 'Easy Redmine',
subtitle: '={{$parameter["operation"]}}:{{$parameter["resource"]}}',
name: 'easyRedmine',
icon: {
light: 'file:EasyRedmine.svg',
dark: 'file:EasyRedmine.dark.svg',
} as const,
group: ['transform'],
version: 1,
description: 'Easy Redmine Operations',
defaults: {
name: 'Easy Redmine Node',
},
inputs: ['main'],
outputs: ['main'],
usableAsTool: true,
credentials: [
{
name: 'easyRedmineApi',
required: true,
},
],
requestDefaults: {
baseURL: '={{$credentials.domain}}',
url: '',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
},
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
default: 'issues',
noDataExpression: true,
options: [
{
name: 'Issue',
value: EasyNodeResourceType.issues,
},
{
name: 'Lead',
value: EasyNodeResourceType.leads,
},
{
name: 'Opportunity',
value: EasyNodeResourceType.opportunities,
},
{
name: 'Project',
value: EasyNodeResourceType.projects,
},
{
name: 'Account',
value: EasyNodeResourceType.accounts,
},
{
name: 'Attendance',
value: EasyNodeResourceType.attendances,
},
{
name: 'Personal Contact',
value: EasyNodeResourceType.personalContacts,
},
{
name: 'Time Entry',
value: EasyNodeResourceType.timeEntries,
},
{
name: 'User',
value: EasyNodeResourceType.users,
},
],
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
EasyNodeResourceType.leads,
EasyNodeResourceType.opportunities,
EasyNodeResourceType.accounts,
],
},
},
default: 'get-many',
options: [
{
name: 'Get One',
description: 'Get a single entity',
value: EasyNodeOperationType.getOne,
action: 'Get one',
},
{
name: 'Get Many',
description: 'Get multiple entities',
value: EasyNodeOperationType.getMany,
action: 'Get many',
},
{
name: 'Search',
description: 'Search entities',
value: EasyNodeOperationType.search,
action: 'Search',
},
{
name: 'Add Comment',
description: 'Add a comment to entity',
value: EasyNodeOperationType.addComment,
action: 'Add comment',
},
{
name: 'Create',
description: 'Create entity',
value: EasyNodeOperationType.create,
action: 'Create',
},
{
name: 'Update',
description: 'Update entity',
value: EasyNodeOperationType.update,
action: 'Update',
},
],
},
...AttendanceFields,
...IssueFields,
...LeadFields,
...OpportunityFields,
...ProjectFields,
...AccountFields,
...PersonalContactFields,
...TimeEntryFields,
...UserFields,
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
operation: [EasyNodeOperationType.getMany, EasyNodeOperationType.search],
},
},
},
{
displayName: 'Offset',
name: 'offset',
type: 'number',
default: 0,
description: 'Result offset',
typeOptions: {
minValue: 0,
},
displayOptions: {
show: {
operation: [EasyNodeOperationType.getMany, EasyNodeOperationType.search],
returnAll: [false],
},
},
},
{
displayName: 'Limit (1-100)',
name: 'limit',
type: 'number',
typeOptions: {
minValue: 1,
},
default: 50,
description: 'Max number of results to return',
displayOptions: {
show: {
operation: [EasyNodeOperationType.getMany, EasyNodeOperationType.search],
returnAll: [false],
},
},
},
{
displayName: 'Comment',
name: 'comment',
type: 'string',
displayOptions: {
show: {
operation: [EasyNodeOperationType.addComment],
},
},
default: '',
},
],
};
methods = {
loadOptions,
listSearch: {
getProjects: async function (
this: ILoadOptionsFunctions,
filter?: string,
paginationToken?: string,
): Promise<INodeListSearchResult> {
const client = new EasyRedmineClient(this, this.helpers);
const projects = (await client.listProjects()).sort((p0, p1) =>
p0.name.localeCompare(p1.name),
);
return {
results: projects.map((project) => ({
name: project.name,
value: project.id,
})),
paginationToken: undefined,
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const returnData: INodeExecutionData[] = [];
const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const operation = this.getNodeParameter('operation', itemIndex) as EasyNodeOperationType;
const resource = this.getNodeParameter('resource', itemIndex) as EasyNodeResourceType;
this.logger.debug(`Operation: ${operation}, resource: ${resource}`);
let responseData: any[] = [];
switch (operation) {
case EasyNodeOperationType.getMany:
responseData = await processGetManyOperation.call(this, resource, itemIndex);
break;
case EasyNodeOperationType.search:
responseData = await processSearchOperation.call(this, resource, itemIndex);
break;
case EasyNodeOperationType.getOne:
responseData = await processGetOneOperation.call(this, resource, itemIndex);
break;
case EasyNodeOperationType.addComment:
responseData = await addCommentOperation.call(this, resource, itemIndex);
break;
case EasyNodeOperationType.update:
responseData = await updateOperation.call(this, resource, itemIndex);
break;
case EasyNodeOperationType.create:
responseData = await createOperation.call(this, resource, itemIndex);
break;
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: itemIndex } },
);
returnData.push(...executionData);
} catch (error) {
// This node should never fail but we want to showcase how
// to handle errors.
if (this.continueOnFail()) {
let errorMessage = error.message;
if (error.context?.data?.errors && Array.isArray(error.context.data.errors)) {
const errors = error.context.data.errors;
errorMessage = errors.join(', ');
}
const executionErrorData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ error: errorMessage }),
{ itemData: { item: itemIndex } },
);
returnData.push(...executionErrorData);
} else {
// Adding `itemIndex` allows other workflows to handle this error
if (error.context) {
// If the error thrown already contains the context property,
// only append the itemIndex
error.context.itemIndex = itemIndex;
throw error;
}
throw new NodeOperationError(this.getNode(), error, {
itemIndex,
});
}
}
}
return [returnData];
}
}