-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "new project" trigger to Zapier #73
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4a798ca
Add "new project" trigger
leelasn da4e894
Name
leelasn 096e2cd
Sample
leelasn e006c06
Update project.json
leelasn 1a152fa
Update documentComment.json
leelasn 31113d1
Update issue.json
leelasn df4808e
Update issueComment.json
leelasn 40eb426
Update projectUpdate.json
leelasn 51ec17c
Update projectUpdateComment.json
leelasn 8728b5d
4.1.0
leelasn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { ZObject, Bundle } from "zapier-platform-core"; | ||
|
||
interface InitiativesResponse { | ||
data: { | ||
initiatives: { | ||
nodes: { | ||
id: string; | ||
name: string; | ||
}[]; | ||
pageInfo: { | ||
hasNextPage: boolean; | ||
endCursor: string; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
const getInitiativesList = async (z: ZObject, bundle: Bundle) => { | ||
const cursor = bundle.meta.page ? await z.cursor.get() : undefined; | ||
|
||
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: ` | ||
query Initiatives($after: String) { | ||
initiatives( | ||
first: 50 | ||
after: $after | ||
) { | ||
nodes { | ||
id | ||
name | ||
} | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
} | ||
}`, | ||
variables: { | ||
after: cursor, | ||
}, | ||
}, | ||
method: "POST", | ||
}); | ||
|
||
const data = (response.json as InitiativesResponse).data; | ||
const initiatives = data.initiatives.nodes; | ||
|
||
// Set cursor for pagination | ||
if (data.initiatives.pageInfo.hasNextPage) { | ||
await z.cursor.set(data.initiatives.pageInfo.endCursor); | ||
} | ||
|
||
return initiatives; | ||
}; | ||
|
||
export const initiative = { | ||
key: "initiative", | ||
noun: "Initiative", | ||
display: { | ||
label: "Get initiative", | ||
hidden: true, | ||
description: | ||
"The only purpose of this trigger is to populate the dropdown list of initiatives in the UI, thus, it's hidden.", | ||
}, | ||
|
||
operation: { | ||
perform: getInitiativesList, | ||
canPaginate: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
import { omit, omitBy, pick } from "lodash"; | ||
import { ZObject, Bundle } from "zapier-platform-core"; | ||
import sample from "../samples/issue.json"; | ||
import { getWebhookData, unsubscribeHook } from "../handleWebhook"; | ||
import { jsonToGraphQLQuery, VariableType } from "json-to-graphql-query"; | ||
import { fetchFromLinear } from "../fetchFromLinear"; | ||
|
||
interface IdAndName { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
interface ProjectCommon { | ||
id: string; | ||
url: string; | ||
name: string; | ||
description: string; | ||
priority: number; | ||
createdAt: Date; | ||
startDate?: Date; | ||
targetDate?: Date; | ||
status: { | ||
id: string; | ||
name: string; | ||
type: string; | ||
}; | ||
} | ||
|
||
interface ProjectApi extends ProjectCommon { | ||
teams: { | ||
nodes: IdAndName[]; | ||
}; | ||
initiatives: { | ||
nodes: IdAndName[]; | ||
}; | ||
projectMilestones: { | ||
nodes: IdAndName[]; | ||
}; | ||
} | ||
|
||
interface ProjectsResponse { | ||
data: { | ||
projects: { | ||
nodes: ProjectApi[]; | ||
}; | ||
}; | ||
} | ||
|
||
interface ProjectWebhook extends ProjectCommon { | ||
teamIds: string[]; | ||
milestones: IdAndName[]; | ||
initiatives: IdAndName[]; | ||
} | ||
|
||
const subscribeHook = async (z: ZObject, bundle: Bundle) => { | ||
const inputData = | ||
bundle.inputData && Object.keys(bundle.inputData).length > 0 | ||
? omitBy( | ||
{ | ||
...pick(bundle.inputData, ["teamId", "statusId", "leadId", "initiativeId"]), | ||
}, | ||
(v) => v === undefined | ||
) | ||
: undefined; | ||
|
||
const data = { | ||
url: bundle.targetUrl, | ||
inputData, | ||
}; | ||
|
||
return z | ||
.request({ | ||
url: "https://client-api.linear.app/connect/zapier/subscribe/createProject", | ||
method: "POST", | ||
body: data, | ||
}) | ||
.then((response) => response.data); | ||
}; | ||
|
||
const getProjectList = | ||
() => | ||
async (z: ZObject, bundle: Bundle): Promise<ProjectWebhook[]> => { | ||
const variables: Record<string, string> = {}; | ||
const variableSchema: Record<string, string> = {}; | ||
const filters: unknown[] = []; | ||
if (bundle.inputData.statusId) { | ||
variableSchema.statusId = "ID"; | ||
variables.statusId = bundle.inputData.statusId; | ||
filters.push({ status: { id: { eq: new VariableType("statusId") } } }); | ||
} | ||
if (bundle.inputData.leadId) { | ||
variableSchema.leadId = "ID"; | ||
variables.leadId = bundle.inputData.leadId; | ||
filters.push({ lead: { id: { eq: new VariableType("leadId") } } }); | ||
} | ||
if (bundle.inputData.teamId) { | ||
variableSchema.teamId = "ID!"; | ||
variables.teamId = bundle.inputData.teamId; | ||
filters.push({ accessibleTeams: { and: [{ id: { in: [new VariableType("teamId")] } }] } }); | ||
} | ||
if (bundle.inputData.initiativeId) { | ||
variableSchema.initiativeId = "ID!"; | ||
variables.initiativeId = bundle.inputData.initiativeId; | ||
filters.push({ initiatives: { and: [{ id: { in: [new VariableType("initiativeId")] } }] } }); | ||
} | ||
const filter = { and: filters }; | ||
|
||
const jsonQuery = { | ||
query: { | ||
__variables: variableSchema, | ||
projects: { | ||
__args: { | ||
first: 25, | ||
filter, | ||
}, | ||
nodes: { | ||
id: true, | ||
url: true, | ||
name: true, | ||
description: true, | ||
priority: true, | ||
createdAt: true, | ||
startDate: true, | ||
targetDate: true, | ||
status: { | ||
id: true, | ||
name: true, | ||
type: true, | ||
}, | ||
teams: { | ||
nodes: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
initiatives: { | ||
nodes: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
projectMilestones: { | ||
nodes: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const query = jsonToGraphQLQuery(jsonQuery); | ||
const response = await fetchFromLinear(z, bundle, query, variables); | ||
const data = (response.json as ProjectsResponse).data; | ||
const projectsRaw = data.projects.nodes; | ||
// We need to map the API schema to the webhook schema | ||
return projectsRaw.map((projectRaw) => | ||
omit( | ||
{ | ||
...projectRaw, | ||
teamIds: projectRaw.teams.nodes.map((team) => team.id), | ||
milestones: projectRaw.projectMilestones.nodes, | ||
initiatives: projectRaw.initiatives.nodes, | ||
}, | ||
["teams", "projectMilestones"] | ||
) | ||
); | ||
}; | ||
|
||
export const newProjectInstant = { | ||
noun: "Project", | ||
key: "newProjectInstant", | ||
display: { | ||
label: "New Project", | ||
description: "Triggers when a new project is created.", | ||
}, | ||
operation: { | ||
inputFields: [ | ||
{ | ||
required: false, | ||
label: "Team", | ||
key: "teamId", | ||
helpText: "The team associated with the project.", | ||
dynamic: "team.id.name", | ||
altersDynamicFields: true, | ||
}, | ||
{ | ||
required: false, | ||
label: "Status", | ||
key: "statusId", | ||
helpText: "The project status.", | ||
dynamic: "projectStatus.id.name", | ||
altersDynamicFields: true, | ||
}, | ||
{ | ||
required: false, | ||
label: "Lead", | ||
key: "leadId", | ||
helpText: "The user who is the lead of the project.", | ||
dynamic: "user.id.name", | ||
altersDynamicFields: true, | ||
}, | ||
{ | ||
required: false, | ||
label: "Initiative", | ||
key: "initiativeId", | ||
helpText: "The initiative this project belongs to.", | ||
dynamic: "initiative.id.name", | ||
altersDynamicFields: true, | ||
}, | ||
], | ||
type: "hook", | ||
perform: getWebhookData, | ||
performUnsubscribe: unsubscribeHook, | ||
performList: getProjectList(), | ||
sample, | ||
performSubscribe: subscribeHook, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ZObject, Bundle } from "zapier-platform-core"; | ||
|
||
interface ProjectStatusesResponse { | ||
data: { | ||
organization: { | ||
projectStatuses: { | ||
id: string; | ||
name: string; | ||
}[]; | ||
}; | ||
}; | ||
} | ||
|
||
const getStatusList = async (z: ZObject, bundle: Bundle) => { | ||
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: ` | ||
query ProjectStatuses { | ||
organization { | ||
projectStatuses { | ||
id | ||
name | ||
} | ||
} | ||
}`, | ||
}, | ||
method: "POST", | ||
}); | ||
const data = (response.json as ProjectStatusesResponse).data; | ||
return data.organization.projectStatuses; | ||
}; | ||
|
||
export const projectStatus = { | ||
key: "projectStatus", | ||
noun: "Project Status", | ||
display: { | ||
label: "Get project status", | ||
hidden: true, | ||
description: | ||
"The only purpose of this trigger is to populate the dropdown list of project statuses in the UI, thus, it's hidden.", | ||
}, | ||
operation: { | ||
perform: getStatusList, | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor version bump is justified when it's a new trigger fwiw