0

Get snippet changes between versions

by
Published Oct 24, 2023

Returns the diff of the specified commit against its first parent.

Script bitbucket Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get snippet changes between versions
7
 * Returns the diff of the specified commit against its first parent.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  encoded_id: string,
12
  revision: string,
13
  workspace: string,
14
  path: string | undefined
15
) {
16
  const url = new URL(
17
    `https://api.bitbucket.org/2.0/snippets/${workspace}/${encoded_id}/${revision}/diff`
18
  );
19
  for (const [k, v] of [["path", path]]) {
20
    if (v !== undefined && v !== "") {
21
      url.searchParams.append(k, v);
22
    }
23
  }
24
  const response = await fetch(url, {
25
    method: "GET",
26
    headers: {
27
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.text();
36
}
37