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

Trigger when a new repository is created in a workspace.

Created by hugo697 424 days ago Viewed 9133 times
0
Submitted by hugo697 Bun
Verified 424 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(bitbucket: Bitbucket, workspace: string) {
9
  const lastChecked: number = (await getState()) || 0;
10

11
  const response = await fetch(
12
    `https://api.bitbucket.org/2.0/repositories/${workspace}?pagelen=100`,
13
    {
14
      headers: {
15
        Authorization:
16
          "Basic " +
17
          Buffer.from(bitbucket.username + ":" + bitbucket.password).toString(
18
            "base64"
19
          ),
20
      },
21
    }
22
  );
23
  const data = await response.json();
24
  if (!response.ok) {
25
    throw new Error(data.error.message);
26
  }
27
  const newRepos = [];
28
  for (const repo of data?.values || []) {
29
    if (new Date(repo.created_on).getTime() > lastChecked) {
30
      newRepos.push(repo);
31
    } else {
32
      break;
33
    }
34
  }
35

36
  if (newRepos.length > 0) {
37
    await setState(new Date(newRepos[0].created_on).getTime());
38
  }
39

40
  return newRepos;
41
}
42