0
Create a commit by uploading a file
One script reply has been approved by the moderators Verified

This endpoint is used to create new commits in the repository by uploading files.

Created by hugo697 455 days ago Viewed 12000 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 455 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a commit by uploading a file
7
 * This endpoint is used to create new commits in the repository by
8
uploading files.
9
 */
10
export async function main(
11
  auth: Bitbucket,
12
  repo_slug: string,
13
  workspace: string,
14
  message: string | undefined,
15
  author: string | undefined,
16
  parents: string | undefined,
17
  files: string | undefined,
18
  branch: string | undefined
19
) {
20
  const url = new URL(
21
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/src`
22
  );
23
  for (const [k, v] of [
24
    ["message", message],
25
    ["author", author],
26
    ["parents", parents],
27
    ["files", files],
28
    ["branch", branch],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.text();
46
}
47