Skip to content

Commit

Permalink
Add "create project" action
Browse files Browse the repository at this point in the history
  • Loading branch information
leelasn committed Oct 10, 2024
1 parent 657e5d6 commit 75ac14f
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linear-zapier",
"version": "4.2.0",
"version": "4.3.0",
"description": "Linear's Zapier integration",
"main": "index.js",
"license": "MIT",
Expand Down
158 changes: 158 additions & 0 deletions src/creates/createProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Bundle, ZObject } from "zapier-platform-core";
import { fetchFromLinear } from "../fetchFromLinear";
import { omitBy } from "lodash";

interface ProjectCreateResponse {
data?: { projectCreate: { project: { id: string; url: string; name: string }; success: boolean } };
errors?: {
message: string;
extensions?: {
userPresentableMessage?: string;
};
}[];
}

const createProjectRequest = async (z: ZObject, bundle: Bundle) => {
const teamIds = bundle.inputData.teams || [];
if (teamIds.length === 0) {
throw new z.errors.HaltedError("You must select at least one team to add to the project");
}
const variables = omitBy(
{
name: bundle.inputData.name,
description: bundle.inputData.description || "",
statusId: bundle.inputData.statusId,
teamIds,
memberIds: bundle.inputData.members || [],
leadId: bundle.inputData.lead,
priority: bundle.inputData.priority ? Number(bundle.inputData.priority) : undefined,
},
(v) => v === undefined
);
const query = `
mutation ZapierProjectCreate(
$name: String!,
$description: String!,
$statusId: String,
$teamIds: [String!]!,
$memberIds: [String!],
$leadId: String,
$priority: Int
) {
projectCreate(input: {
name: $name,
description: $description,
statusId: $statusId,
teamIds: $teamIds,
memberIds: $memberIds,
leadId: $leadId,
priority: $priority
}) {
project {
id
url
name
}
success
}
}`;

const response = await fetchFromLinear(z, bundle, query, variables);
const data = response.json as ProjectCreateResponse;

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.projectCreate && data.data.projectCreate.success) {
return data.data.projectCreate.project;
} else {
const error = data.errors ? data.errors[0].message : "Something went wrong";
throw new z.errors.Error("Failed to create a project", error, 400);
}
};

export const createProject = {
key: "createProject",
display: {
hidden: false,
description: "Create a new project in Linear",
label: "Create Project",
},
noun: "Project",
operation: {
perform: createProjectRequest,
inputFields: [
{
required: true,
label: "Name",
helpText: "The name of the project",
key: "name",
},
{
required: false,
label: "Summary",
helpText: "A short summary of the project",
key: "description",
type: "text",
},
{
required: false,
label: "Status",
key: "statusId",
helpText: "The status of the project",
dynamic: "projectStatus.id.name",
altersDynamicFields: true,
},
{
required: true,
label: "Teams",
helpText: "The teams to add to this project",
key: "teams",
dynamic: "team.id.name",
list: true,
},
{
required: false,
label: "Members",
helpText: "The users to add as project members",
key: "members",
dynamic: "user.id.name",
list: true,
},
{
required: false,
label: "Lead",
helpText: "The user to assign as the project lead",
key: "lead",
dynamic: "user.id.name",
},
{
required: false,
label: "Priority",
helpText: "The priority of the project",
key: "priority",
choices: [
{ value: "0", sample: "0", label: "No priority" },
{ value: "1", sample: "1", label: "Urgent" },
{ value: "2", sample: "2", label: "High" },
{ value: "3", sample: "3", label: "Medium" },
{ value: "4", sample: "4", label: "Low" },
],
},
],
sample: {
data: {
projectCreate: {
project: { id: "a25cce1b-510d-433f-af50-1373efc05a4a", url: "https://www.example.com", name: "My title" },
success: true,
},
},
},
},
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { initiative } from "./triggers/initiative";
import { projectStatus } from "./triggers/projectStatus";
import { newProjectInstant } from "./triggers/newProject";
import { createIssueAttachment } from "./creates/createIssueAttachment";
import { createProject } from "./creates/createProject";

const handleErrors = (response: HttpResponse, z: ZObject) => {
if (response.request.url !== "https://api.linear.app/graphql") {
Expand All @@ -50,6 +51,7 @@ const App = {
[createIssue.key]: createIssue,
[createComment.key]: createComment,
[createIssueAttachment.key]: createIssueAttachment,
[createProject.key]: createProject,
},
triggers: {
[newIssue.key]: newIssue,
Expand Down

0 comments on commit 75ac14f

Please sign in to comment.