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

Trigger when a new commit is pushed to a branch.

Created by hugo697 198 days ago Viewed 5934 times
0
Submitted by hugo697 Bun
Verified 198 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
  branch: string
13
) {
14
  const lastChecked: number = (await getState()) || 0;
15

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

41
  if (newCommits.length > 0) {
42
    await setState(new Date(newCommits[0].date).getTime());
43
  }
44

45
  return newCommits;
46
}
47