2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2022-03-20 02:11:06 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeCredentialTestResult,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
linearApiRequest,
|
|
|
|
linearApiRequestAllItems,
|
|
|
|
sort,
|
2022-06-13 12:41:18 -07:00
|
|
|
validateCredentials,
|
2022-03-20 02:11:06 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { issueFields, issueOperations } from './IssueDescription';
|
2022-03-20 02:11:06 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { query } from './Queries';
|
2022-03-20 02:11:06 -07:00
|
|
|
interface IGraphqlBody {
|
|
|
|
query: string;
|
|
|
|
variables: IDataObject;
|
|
|
|
}
|
|
|
|
export class Linear implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Linear',
|
|
|
|
name: 'linear',
|
|
|
|
icon: 'file:linear.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Linear API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Linear',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'linearApi',
|
|
|
|
required: true,
|
|
|
|
testedBy: 'linearApiTest',
|
2023-09-20 02:19:12 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: ['apiToken'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'linearOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: ['oAuth2'],
|
|
|
|
},
|
|
|
|
},
|
2022-03-20 02:11:06 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2023-09-20 02:19:12 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'API Token',
|
|
|
|
value: 'apiToken',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'apiToken',
|
|
|
|
},
|
2022-03-20 02:11:06 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2022-03-20 02:11:06 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Issue',
|
|
|
|
value: 'issue',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'issue',
|
|
|
|
},
|
|
|
|
...issueOperations,
|
|
|
|
...issueFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async linearApiTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2022-03-20 02:11:06 -07:00
|
|
|
try {
|
2022-06-13 12:41:18 -07:00
|
|
|
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
|
2022-03-20 02:11:06 -07:00
|
|
|
} catch (error) {
|
|
|
|
const { error: err } = error as JsonObject;
|
|
|
|
const errors = (err as IDataObject).errors as [{ extensions: { code: string } }];
|
2022-08-17 08:50:24 -07:00
|
|
|
const authenticationError = Boolean(
|
|
|
|
errors.filter((e) => e.extensions.code === 'AUTHENTICATION_ERROR').length,
|
|
|
|
);
|
2022-03-20 02:11:06 -07:00
|
|
|
if (authenticationError) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: 'The security token included in the request is invalid',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
loadOptions: {
|
|
|
|
async getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const body = {
|
|
|
|
query: query.getTeams(),
|
|
|
|
variables: {
|
|
|
|
$first: 10,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const teams = await linearApiRequestAllItems.call(this, 'data.teams', body);
|
|
|
|
|
|
|
|
for (const team of teams) {
|
|
|
|
returnData.push({
|
|
|
|
name: team.name,
|
|
|
|
value: team.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const body = {
|
|
|
|
query: query.getUsers(),
|
|
|
|
variables: {
|
|
|
|
$first: 10,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const users = await linearApiRequestAllItems.call(this, 'data.users', body);
|
|
|
|
|
|
|
|
for (const user of users) {
|
|
|
|
returnData.push({
|
|
|
|
name: user.name,
|
|
|
|
value: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getStates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2023-02-09 07:58:52 -08:00
|
|
|
let teamId = this.getNodeParameter('teamId', null) as string;
|
|
|
|
// Handle Updates
|
|
|
|
if (!teamId) {
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', null) as IDataObject;
|
|
|
|
// If not updating the team look up the current team
|
|
|
|
if (!updateFields.teamId) {
|
|
|
|
const issueId = this.getNodeParameter('issueId');
|
|
|
|
const body = {
|
|
|
|
query: query.getIssueTeam(),
|
|
|
|
variables: {
|
|
|
|
issueId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const responseData = await linearApiRequest.call(this, body);
|
|
|
|
teamId = responseData?.data?.issue?.team?.id;
|
|
|
|
} else {
|
|
|
|
teamId = updateFields.teamId as string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:11:06 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const body = {
|
|
|
|
query: query.getStates(),
|
|
|
|
variables: {
|
|
|
|
$first: 10,
|
2023-02-09 07:58:52 -08:00
|
|
|
filter: {
|
|
|
|
team: {
|
|
|
|
id: {
|
|
|
|
eq: teamId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-20 02:11:06 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const states = await linearApiRequestAllItems.call(this, 'data.workflowStates', body);
|
|
|
|
|
|
|
|
for (const state of states) {
|
|
|
|
returnData.push({
|
|
|
|
name: state.name,
|
|
|
|
value: state.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData.sort(sort);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2022-03-20 02:11:06 -07:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-03-20 02:11:06 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'issue') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const teamId = this.getNodeParameter('teamId', i) as string;
|
|
|
|
const title = this.getNodeParameter('title', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-03-20 02:11:06 -07:00
|
|
|
const body: IGraphqlBody = {
|
|
|
|
query: query.createIssue(),
|
|
|
|
variables: {
|
|
|
|
teamId,
|
|
|
|
title,
|
|
|
|
...additionalFields,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await linearApiRequest.call(this, body);
|
|
|
|
responseData = responseData.data.issueCreate?.issue;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const issueId = this.getNodeParameter('issueId', i) as string;
|
|
|
|
const body: IGraphqlBody = {
|
|
|
|
query: query.deleteIssue(),
|
|
|
|
variables: {
|
|
|
|
issueId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await linearApiRequest.call(this, body);
|
|
|
|
responseData = responseData?.data?.issueDelete;
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const issueId = this.getNodeParameter('issueId', i) as string;
|
|
|
|
const body: IGraphqlBody = {
|
|
|
|
query: query.getIssue(),
|
|
|
|
variables: {
|
|
|
|
issueId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await linearApiRequest.call(this, body);
|
2023-01-20 04:59:47 -08:00
|
|
|
responseData = responseData.data.issue;
|
2022-03-20 02:11:06 -07:00
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-03-20 02:11:06 -07:00
|
|
|
const body: IGraphqlBody = {
|
|
|
|
query: query.getIssues(),
|
|
|
|
variables: {
|
|
|
|
first: 50,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await linearApiRequestAllItems.call(this, 'data.issues', body);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2024-05-08 02:24:08 -07:00
|
|
|
responseData = await linearApiRequestAllItems.call(this, 'data.issues', body, limit);
|
2022-03-20 02:11:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
|
|
const issueId = this.getNodeParameter('issueId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2022-03-20 02:11:06 -07:00
|
|
|
const body: IGraphqlBody = {
|
|
|
|
query: query.updateIssue(),
|
|
|
|
variables: {
|
|
|
|
issueId,
|
|
|
|
...updateFields,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await linearApiRequest.call(this, body);
|
|
|
|
responseData = responseData?.data?.issueUpdate?.issue;
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2022-03-20 02:11:06 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2022-03-20 02:11:06 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2022-03-20 02:11:06 -07:00
|
|
|
}
|
|
|
|
}
|