0
Bulk create or update annotations
One script reply has been approved by the moderators Verified

Bulk upload of annotations.

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

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