0

Update Issue

by
Published 4 days ago

Update an issue's title, body, state, assignees, labels or milestone. Only the fields you set change: empty text or an empty list keeps the current value. Assignees and labels you set replace the existing lists; turn on clear_assignees or clear_labels to remove them all. Turn on clear_milestone to remove the milestone.

Script github Verified

The script

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

3
/**
4
 * Update Issue
5
 * Update an issue's title, body, state, assignees, labels or milestone. Only the fields you set change: empty text or an empty list keeps the current value. Assignees and labels you set replace the existing lists; turn on clear_assignees or clear_labels to remove them all. Turn on clear_milestone to remove the milestone.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  issue_number: number,
12
  title: string | undefined,
13
  body: string | undefined,
14
  state: "open" | "closed" | undefined,
15
  state_reason: "completed" | "not_planned" | "reopened" | undefined,
16
  assignees: string[] | undefined,
17
  labels: string[] | undefined,
18
  milestone: number | undefined,
19
  clear_assignees: boolean = false,
20
  clear_labels: boolean = false,
21
  clear_milestone: boolean = false
22
) {
23
  const response = await fetch(
24
    `https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}`,
25
    {
26
      method: "PATCH",
27
      headers: {
28
        Authorization: `Bearer ${auth.token}`,
29
        "Content-Type": "application/json",
30
        Accept: "application/vnd.github+json",
31
        "X-GitHub-Api-Version": "2022-11-28",
32
      },
33
      body: JSON.stringify({
34
        title: title || undefined,
35
        body: body || undefined,
36
        state: state || undefined,
37
        state_reason: state_reason || undefined,
38
        assignees: clear_assignees
39
          ? []
40
          : assignees?.length
41
            ? assignees
42
            : undefined,
43
        labels: clear_labels ? [] : labels?.length ? labels : undefined,
44
        milestone: clear_milestone ? null : (milestone ?? undefined),
45
      }),
46
    }
47
  )
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55