0
Compare two commits
One script reply has been approved by the moderators Verified

Produces a raw git-style diff.

Created by hugo697 407 days ago Viewed 9010 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 407 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Compare two commits
7
 * Produces a raw git-style diff.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  repo_slug: string,
12
  spec: string,
13
  workspace: string,
14
  context: string | undefined,
15
  path: string | undefined,
16
  ignore_whitespace: string | undefined,
17
  binary: string | undefined,
18
  renames: string | undefined,
19
  merge: string | undefined,
20
  topic: string | undefined
21
) {
22
  const url = new URL(
23
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/diff/${spec}`
24
  );
25
  for (const [k, v] of [
26
    ["context", context],
27
    ["path", path],
28
    ["ignore_whitespace", ignore_whitespace],
29
    ["binary", binary],
30
    ["renames", renames],
31
    ["merge", merge],
32
    ["topic", topic],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.text();
50
}
51