0

Create Issue Note

by
Published 4 days ago

Add a comment (note) to a Wiz issue.

Script wiz Verified

The script

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

3
/**
4
 * Create Issue Note
5
 * Add a comment (note) to a Wiz issue.
6
 */
7
export async function main(auth: RT.Wiz, issue_id: string, text: string) {
8
  const tokenResponse = await fetch(
9
    auth.auth_url || "https://auth.app.wiz.io/oauth/token",
10
    {
11
      method: "POST",
12
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
13
      body: new URLSearchParams({
14
        grant_type: "client_credentials",
15
        audience: auth.audience || "wiz-api",
16
        client_id: auth.client_id,
17
        client_secret: auth.client_secret,
18
      }),
19
    }
20
  )
21
  if (!tokenResponse.ok) {
22
    throw new Error(`${tokenResponse.status} ${await tokenResponse.text()}`)
23
  }
24
  const { access_token } = (await tokenResponse.json()) as {
25
    access_token: string
26
  }
27

28
  const query = `
29
mutation CreateIssueNote($input: CreateIssueNoteInput!) {
30
  createIssueNote(input: $input) {
31
    issueNote {
32
      id
33
      text
34
      createdAt
35
      user { id email }
36
    }
37
  }
38
}`
39

40
  const response = await fetch(auth.api_endpoint, {
41
    method: "POST",
42
    headers: {
43
      Authorization: `Bearer ${access_token}`,
44
      "Content-Type": "application/json",
45
      Accept: "application/json",
46
    },
47
    body: JSON.stringify({
48
      query,
49
      variables: { input: { issueId: issue_id, text } },
50
    }),
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  const result = (await response.json()) as { data?: any; errors?: any }
58
  if (result.errors) {
59
    throw new Error(JSON.stringify(result.errors))
60
  }
61
  return result.data.createIssueNote.issueNote
62
}
63