0
Compare two commit diff stats
One script reply has been approved by the moderators Verified

Produces a response in JSON format with a record for every path modified, including information on the type of the change and the number of lines added and removed.

Created by hugo697 277 days ago Viewed 8922 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 277 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Compare two commit diff stats
7
 * Produces a response in JSON format with a record for every path
8
modified, including information on the type of the change and the
9
number of lines added and removed.
10
 */
11
export async function main(
12
  auth: Bitbucket,
13
  repo_slug: string,
14
  spec: string,
15
  workspace: string,
16
  ignore_whitespace: string | undefined,
17
  merge: string | undefined,
18
  path: string | undefined,
19
  renames: string | undefined,
20
  topic: string | undefined
21
) {
22
  const url = new URL(
23
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/diffstat/${spec}`
24
  );
25
  for (const [k, v] of [
26
    ["ignore_whitespace", ignore_whitespace],
27
    ["merge", merge],
28
    ["path", path],
29
    ["renames", renames],
30
    ["topic", topic],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
40
    },
41
    body: undefined,
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