List commits that modified a file

Returns a paginated list of commits that modified the specified file.

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
 * List commits that modified a file
7
 * Returns a paginated list of commits that modified the specified file.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  commit: string,
12
  path: string,
13
  repo_slug: string,
14
  workspace: string,
15
  renames: string | undefined,
16
  q: string | undefined,
17
  sort: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/filehistory/${commit}/${path}`
21
  );
22
  for (const [k, v] of [
23
    ["renames", renames],
24
    ["q", q],
25
    ["sort", sort],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44