Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

feat: addLabel #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addBearerHeader, authentication } from "./authentication";
import { addLabel } from "./updates/addLabel";
import { createIssue } from "./creates/createIssue";
import { createIssueMove } from "./creates/createIssueMove";
import { newComment } from "./triggers/comment";
Expand Down Expand Up @@ -44,6 +45,9 @@ const App = {
[createAttachmentLinkIntercom.key]: createAttachmentLinkIntercom,
[createAttachmentLinkURL.key]: createAttachmentLinkURL,
},
updates: {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure I can do that.

[addLabel.key]: addLabel,
}
triggers: {
[newIssue.key]: newIssue,
[updatedIssue.key]: updatedIssue,
Expand Down
105 changes: 105 additions & 0 deletions src/updates/addLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Bundle, ZObject } from "zapier-platform-core";

interface AddLabelRequestResponse {
data?: { issueUpdate: { issue: { url: string }; success: boolean } };
errors?: {
message: string;
extensions?: {
userPresentableMessage?: string;
};
}[];
}

const addLabelRequest = async (z: ZObject, bundle: Bundle) => {
const variables = {
issueId: bundle.inputData.issue_id,
labelId: bundle.inputData.label_id,
};

const query = `
mutation IssueUpdate(
$issueId: String!,
$labelId: String!
) {
issueUpdate(
id: $issueId,
input: {
labelIds: [$labelId]
}
) {
success
issue {
id
title
labels {
nodes { id, name }
}
}
}
}`;

const response = await z.request({
url: "https://api.linear.app/graphql",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
authorization: bundle.authData.api_key,
},
body: {
query,
variables,
},
method: "POST",
});

const data = response.json as AddLabelRequestResponse;

if (data.errors && data.errors.length) {
const error = data.errors[0];
throw new z.errors.Error(
(error.extensions && error.extensions.userPresentableMessage) || error.message,
"invalid_input",
400
);
}

if (data.data && data.data.issueUpdate && data.data.issueUpdate.success) {
return data.data.issueUpdate.issue;
} else {
const error = data.errors ? data.errors[0].message : "Something went wrong";
throw new z.errors.Error(`Failed to create an issue`, error, 400);
}
};

export const addLabel = {
key: "add_label",

display: {
hidden: false,
important: true,
description: "Add label to Linear issue",
label: "Add label",
},

noun: "Issue",

operation: {
perform: addLabelRequest,

inputFields: [
{
required: true,
label: "Issue",
key: "issue_id",
helpText: "The issue to add the label to",
},
{
required: true,
label: "Label",
helpText: "The label to add",
key: "label_id",
},
],
sample: { data: { issueUpdate: { success: true } } },
},
};