Create or update an annotation

Creates or updates an individual annotation for the specified report.

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create or update an annotation
7
 * Creates or updates an individual annotation for the specified report.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  commit: string,
14
  reportId: string,
15
  annotationId: string,
16
  body: { type: string; [k: string]: unknown } & {
17
    external_id?: string;
18
    uuid?: string;
19
    annotation_type?: "VULNERABILITY" | "CODE_SMELL" | "BUG";
20
    path?: string;
21
    line?: number;
22
    summary?: string;
23
    details?: string;
24
    result?: "PASSED" | "FAILED" | "SKIPPED" | "IGNORED";
25
    severity?: "CRITICAL" | "HIGH" | "MEDIUM" | "LOW";
26
    link?: string;
27
    created_on?: string;
28
    updated_on?: string;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/commit/${commit}/reports/${reportId}/annotations/${annotationId}`
34
  );
35

36
  const response = await fetch(url, {
37
    method: "PUT",
38
    headers: {
39
      "Content-Type": "application/json",
40
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
41
    },
42
    body: JSON.stringify(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50