Create a schedule

Create a schedule for the given repository.

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
 * Create a schedule
7
 * Create a schedule for the given repository.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  body: { type: string; [k: string]: unknown } & {
14
    target: {
15
      selector: { type: string; [k: string]: unknown } & {
16
        type?: "branches" | "tags" | "bookmarks" | "default" | "custom";
17
        pattern?: string;
18
        [k: string]: unknown;
19
      };
20
      ref_name: string;
21
      ref_type: "branch";
22
      [k: string]: unknown;
23
    };
24
    enabled?: boolean;
25
    cron_pattern: string;
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines_config/schedules`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47