0

Create Project

by
Published today

Create a project for a team.

Script linear Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
export type DynSelect_team_id = string
4

5
// Dropdown of the workspace's teams.
6
export async function team_id(auth: RT.Linear) {
7
  const response = await fetch("https://api.linear.app/graphql", {
8
    method: "POST",
9
    headers: {
10
      Authorization: auth.apiKey,
11
      "Content-Type": "application/json",
12
      Accept: "application/json",
13
    },
14
    body: JSON.stringify({
15
      query: "{ teams(first: 250) { nodes { id key name } } }",
16
    }),
17
  })
18
  if (!response.ok) {
19
    throw new Error(`${response.status} ${await response.text()}`)
20
  }
21
  const { data } = (await response.json()) as {
22
    data: { teams: { nodes: { id: string; key: string; name: string }[] } }
23
  }
24
  return data.teams.nodes.map((t) => ({
25
    value: t.id,
26
    label: `${t.name} (${t.key})`,
27
  }))
28
}
29

30
/**
31
 * Create Project
32
 * Create a project owned by a team. Dates are YYYY-MM-DD. Priority: 0 none, 1 urgent, 2 high, 3 medium, 4 low.
33
 */
34
export async function main(
35
  auth: RT.Linear,
36
  team_id: DynSelect_team_id,
37
  name: string,
38
  description: string | undefined,
39
  lead_id: string | undefined,
40
  member_ids: string[] | undefined,
41
  priority: number | undefined,
42
  start_date: string | undefined,
43
  target_date: string | undefined
44
) {
45
  const response = await fetch("https://api.linear.app/graphql", {
46
    method: "POST",
47
    headers: {
48
      Authorization: auth.apiKey,
49
      "Content-Type": "application/json",
50
      Accept: "application/json",
51
    },
52
    body: JSON.stringify({
53
      query: `mutation ($input: ProjectCreateInput!) { projectCreate(input: $input) { success project { id name description url startDate targetDate status { id name type } lead { id name } } } }`,
54
      variables: {
55
        input: {
56
          teamIds: [team_id],
57
          name,
58
          description: description || undefined,
59
          leadId: lead_id || undefined,
60
          memberIds: member_ids?.length ? member_ids : undefined,
61
          priority,
62
          startDate: start_date || undefined,
63
          targetDate: target_date || undefined,
64
        },
65
      },
66
    }),
67
  })
68

69
  if (!response.ok) {
70
    throw new Error(`${response.status} ${await response.text()}`)
71
  }
72

73
  const { data, errors } = await response.json()
74
  if (errors) {
75
    throw new Error(JSON.stringify(errors))
76
  }
77

78
  return data.projectCreate
79
}
80