Create or update a report

Creates or updates a report for the specified commit.

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 a report
7
 * Creates or updates a report for the specified commit.
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
    uuid?: string;
17
    title?: string;
18
    details?: string;
19
    external_id?: string;
20
    reporter?: string;
21
    link?: string;
22
    remote_link_enabled?: boolean;
23
    logo_url?: string;
24
    report_type?: "SECURITY" | "COVERAGE" | "TEST" | "BUG";
25
    result?: "PASSED" | "FAILED" | "PENDING";
26
    data?: {
27
      type?:
28
        | "BOOLEAN"
29
        | "DATE"
30
        | "DURATION"
31
        | "LINK"
32
        | "NUMBER"
33
        | "PERCENTAGE"
34
        | "TEXT";
35
      title?: string;
36
      value?: { [k: string]: unknown };
37
      [k: string]: unknown;
38
    }[];
39
    created_on?: string;
40
    updated_on?: string;
41
    [k: string]: unknown;
42
  }
43
) {
44
  const url = new URL(
45
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/commit/${commit}/reports/${reportId}`
46
  );
47

48
  const response = await fetch(url, {
49
    method: "PUT",
50
    headers: {
51
      "Content-Type": "application/json",
52
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62