2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
2022-02-11 08:20:41 -08:00
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
IWebhookResponseData,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { capitalizeFirstLetter, linearApiRequest } from './GenericFunctions';
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
export class LinearTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Linear Trigger',
|
|
|
|
name: 'linearTrigger',
|
|
|
|
icon: 'file:linear.svg',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["triggerOn"]}}',
|
|
|
|
description: 'Starts the workflow when Linear events occur',
|
|
|
|
defaults: {
|
|
|
|
name: 'Linear Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'linearApi',
|
|
|
|
required: true,
|
2022-03-20 02:11:06 -07:00
|
|
|
testedBy: 'linearApiTest',
|
2022-02-11 08:20:41 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Team Name or ID',
|
|
|
|
name: 'teamId',
|
|
|
|
type: 'options',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2022-02-11 08:20:41 -08:00
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'getTeams',
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Listen to Resources',
|
|
|
|
name: 'resources',
|
|
|
|
type: 'multiOptions',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Comment Reaction',
|
|
|
|
value: 'reaction',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Cycle',
|
|
|
|
value: 'cycle',
|
|
|
|
},
|
|
|
|
/* It's still on Alpha stage
|
|
|
|
{
|
|
|
|
name: 'Issue Attachment',
|
|
|
|
value: 'attachment',
|
|
|
|
},*/
|
|
|
|
{
|
|
|
|
name: 'Issue',
|
|
|
|
value: 'issue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Issue Comment',
|
|
|
|
value: 'comment',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Issue Label',
|
|
|
|
value: 'issueLabel',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Project',
|
|
|
|
value: 'project',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: [],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const body = {
|
2022-08-17 08:50:24 -07:00
|
|
|
query: `query Teams {
|
2022-02-11 08:20:41 -08:00
|
|
|
teams {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
teams: { nodes },
|
|
|
|
},
|
|
|
|
} = await linearApiRequest.call(this, body);
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
for (const node of nodes) {
|
|
|
|
returnData.push({
|
|
|
|
name: node.name,
|
|
|
|
value: node.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const teamId = this.getNodeParameter('teamId') as string;
|
|
|
|
const body = {
|
2022-08-17 08:50:24 -07:00
|
|
|
query: `query {
|
2022-02-11 08:20:41 -08:00
|
|
|
webhooks {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
url
|
|
|
|
enabled
|
|
|
|
team {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
};
|
|
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
|
|
// one that is supposed to get created.
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
webhooks: { nodes },
|
|
|
|
},
|
|
|
|
} = await linearApiRequest.call(this, body);
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
for (const node of nodes) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (node.url === webhookUrl && node.team.id === teamId && node.enabled === true) {
|
2022-02-11 08:20:41 -08:00
|
|
|
webhookData.webhookId = node.id as string;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const teamId = this.getNodeParameter('teamId') as string;
|
|
|
|
const resources = this.getNodeParameter('resources') as string[];
|
|
|
|
const body = {
|
|
|
|
query: `
|
|
|
|
mutation webhookCreate($url: String!, $teamId: String!, $resources: [String!]!) {
|
|
|
|
webhookCreate(
|
|
|
|
input: {
|
|
|
|
url: $url
|
|
|
|
teamId: $teamId
|
|
|
|
resourceTypes: $resources
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
success
|
|
|
|
webhook {
|
|
|
|
id
|
|
|
|
enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
variables: {
|
|
|
|
url: webhookUrl,
|
|
|
|
teamId,
|
|
|
|
resources: resources.map(capitalizeFirstLetter),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
webhookCreate: {
|
|
|
|
success,
|
|
|
|
webhook: { id },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} = await linearApiRequest.call(this, body);
|
2022-02-11 08:20:41 -08:00
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
webhookData.webhookId = id as string;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
|
|
|
const body = {
|
|
|
|
query: `
|
|
|
|
mutation webhookDelete($id: String!){
|
|
|
|
webhookDelete(
|
|
|
|
id: $id
|
|
|
|
) {
|
|
|
|
success
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
variables: {
|
|
|
|
id: webhookData.webhookId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await linearApiRequest.call(this, body);
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Remove from the static workflow data so that it is clear
|
2023-03-03 09:49:19 -08:00
|
|
|
// that no webhooks are registered anymore
|
2022-02-11 08:20:41 -08:00
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const bodyData = this.getBodyData();
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(bodyData)],
|
2022-02-11 08:20:41 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|