Add Linear Trigger node (#2767)

*  Linear Trigger

* 🎨 Replace PNG with SVG icon

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
This commit is contained in:
Ricardo Espinoza 2022-02-11 11:20:41 -05:00 committed by GitHub
parent 9335ee5deb
commit f35d123776
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 308 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class LinearApi implements ICredentialType {
name = 'linearApi';
displayName = 'Linear API';
documentationUrl = 'linear';
properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
default: '',
},
];
}

View file

@ -0,0 +1,45 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
IHookFunctions,
IWebhookFunctions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
export async function linearApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, body: any = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('linearApi') as IDataObject;
const endpoint = 'https://api.linear.app/graphql';
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
Authorization: credentials.apiKey,
},
method: 'POST',
body,
uri: endpoint,
json: true,
};
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export function capitalizeFirstLetter(data: string) {
return data.charAt(0).toUpperCase() + data.slice(1);
}

View file

@ -0,0 +1,242 @@
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
capitalizeFirstLetter,
linearApiRequest,
} from './GenericFunctions';
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',
color: '#D9DCF8',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'linearApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Team Name or ID',
name: 'teamId',
type: 'options',
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 = {
query:
`query Teams {
teams {
nodes {
id
name
}
}
}`,
};
const { data: { teams: { nodes } } } = await linearApiRequest.call(this, body);
for (const node of nodes) {
returnData.push({
name: node.name,
value: node.id,
});
}
return returnData;
},
},
};
//@ts-ignore (because of request)
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 = {
query:
`query {
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.
const { data: { webhooks: { nodes } } } = await linearApiRequest.call(this, body);
for (const node of nodes) {
if (node.url === webhookUrl &&
node.team.id === teamId &&
node.enabled === true) {
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),
},
};
const { data: { webhookCreate: { success, webhook: { id } } } } = await linearApiRequest.call(this, body);
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
// that no webhooks are registred anymore
delete webhookData.webhookId;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const bodyData = this.getBodyData();
return {
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
}
}

View file

@ -0,0 +1 @@
<svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px;margin-right:12px"><path d="M0.403013 37.3991L26.6009 63.597C13.2225 61.3356 2.66442 50.7775 0.403013 37.3991Z" fill="#5E6AD2"></path><path d="M0 30.2868L33.7132 64C35.7182 63.8929 37.6742 63.6013 39.5645 63.142L0.85799 24.4355C0.398679 26.3259 0.10713 28.2818 0 30.2868Z" fill="#5E6AD2"></path><path d="M2.53593 19.4042L44.5958 61.4641C46.1277 60.8066 47.598 60.0331 48.9956 59.1546L4.84543 15.0044C3.96691 16.402 3.19339 17.8723 2.53593 19.4042Z" fill="#5E6AD2"></path><path d="M7.69501 11.1447C13.5677 4.32093 22.2677 0 31.9769 0C49.6628 0 64 14.3372 64 32.0231C64 41.7323 59.6791 50.4323 52.8553 56.305L7.69501 11.1447Z" fill="#5E6AD2"></path></svg>

After

Width:  |  Height:  |  Size: 779 B

View file

@ -158,6 +158,7 @@
"dist/credentials/KeapOAuth2Api.credentials.js", "dist/credentials/KeapOAuth2Api.credentials.js",
"dist/credentials/KitemakerApi.credentials.js", "dist/credentials/KitemakerApi.credentials.js",
"dist/credentials/LemlistApi.credentials.js", "dist/credentials/LemlistApi.credentials.js",
"dist/credentials/LinearApi.credentials.js",
"dist/credentials/LineNotifyOAuth2Api.credentials.js", "dist/credentials/LineNotifyOAuth2Api.credentials.js",
"dist/credentials/LingvaNexApi.credentials.js", "dist/credentials/LingvaNexApi.credentials.js",
"dist/credentials/LinkedInOAuth2Api.credentials.js", "dist/credentials/LinkedInOAuth2Api.credentials.js",
@ -487,6 +488,7 @@
"dist/nodes/Lemlist/Lemlist.node.js", "dist/nodes/Lemlist/Lemlist.node.js",
"dist/nodes/Lemlist/LemlistTrigger.node.js", "dist/nodes/Lemlist/LemlistTrigger.node.js",
"dist/nodes/Line/Line.node.js", "dist/nodes/Line/Line.node.js",
"dist/nodes/Linear/LinearTrigger.node.js",
"dist/nodes/LingvaNex/LingvaNex.node.js", "dist/nodes/LingvaNex/LingvaNex.node.js",
"dist/nodes/LinkedIn/LinkedIn.node.js", "dist/nodes/LinkedIn/LinkedIn.node.js",
"dist/nodes/LocalFileTrigger/LocalFileTrigger.node.js", "dist/nodes/LocalFileTrigger/LocalFileTrigger.node.js",