0
New Branch Trigger
One script reply has been approved by the moderators Verified

Trigger when a new branch is created.

Created by hugo697 434 days ago Viewed 9933 times
0
Submitted by hugo697 Bun
Verified 434 days ago
1
import { getState, setState } from "windmill-client@1";
2

3
type Bitbucket = {
4
  username: string;
5
  password: string;
6
};
7

8
export async function main(
9
  bitbucket: Bitbucket,
10
  workspace: string,
11
  repo: string
12
) {
13
  const state: string[] = (await getState()) || [];
14

15
  const response = await fetch(
16
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/refs/branches?pagelen=100`,
17
    {
18
      headers: {
19
        Authorization:
20
          "Basic " +
21
          Buffer.from(bitbucket.username + ":" + bitbucket.password).toString(
22
            "base64"
23
          ),
24
      },
25
    }
26
  );
27
  const data = await response.json();
28
  if (!response.ok) {
29
    throw new Error(data.error.message);
30
  }
31
  const branches = data.values;
32
  const newBranches = branches.filter(
33
    (branch: any) => !state.includes(branch.name)
34
  );
35
  await setState([...state, ...newBranches.map((branch: any) => branch.name)]);
36
  return newBranches;
37
}
38