0

Create Issue

by
Published today

Create an issue in a team. Set parent_id to create a sub-issue.

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 Issue
32
 * Create an issue in a team. Set parent_id to create it as a sub-issue. Priority: 0 none, 1 urgent, 2 high, 3 medium, 4 low. Description is Markdown.
33
 */
34
export async function main(
35
  auth: RT.Linear,
36
  team_id: DynSelect_team_id,
37
  title: string,
38
  description: string | undefined,
39
  assignee_id: string | undefined,
40
  state_id: string | undefined,
41
  priority: number | undefined,
42
  due_date: string | undefined,
43
  label_ids: string[] | undefined,
44
  project_id: string | undefined,
45
  cycle_id: string | undefined,
46
  parent_id: string | undefined
47
) {
48
  const response = await fetch("https://api.linear.app/graphql", {
49
    method: "POST",
50
    headers: {
51
      Authorization: auth.apiKey,
52
      "Content-Type": "application/json",
53
      Accept: "application/json",
54
    },
55
    body: JSON.stringify({
56
      query: `mutation ($input: IssueCreateInput!) { issueCreate(input: $input) { success issue { id identifier title description url priority priorityLabel estimate dueDate createdAt updatedAt archivedAt state { id name type } assignee { id name email } team { id key name } project { id name } cycle { id number } parent { id identifier } labels { nodes { id name } } } } }`,
57
      variables: {
58
        input: {
59
          teamId: team_id,
60
          title,
61
          description: description || undefined,
62
          assigneeId: assignee_id || undefined,
63
          stateId: state_id || undefined,
64
          priority,
65
          dueDate: due_date || undefined,
66
          labelIds: label_ids?.length ? label_ids : undefined,
67
          projectId: project_id || undefined,
68
          cycleId: cycle_id || undefined,
69
          parentId: parent_id || undefined,
70
        },
71
      },
72
    }),
73
  })
74

75
  if (!response.ok) {
76
    throw new Error(`${response.status} ${await response.text()}`)
77
  }
78

79
  const { data, errors } = await response.json()
80
  if (errors) {
81
    throw new Error(JSON.stringify(errors))
82
  }
83

84
  return data.issueCreate
85
}
86