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

Trigger when a new pull request is created in a repository.

Created by hugo697 425 days ago Viewed 9163 times
0
Submitted by hugo697 Bun
Verified 425 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 lastChecked: number = (await getState()) || 0;
14

15
  const response = await fetch(
16
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/pullrequests`,
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 newPullRequests = [];
32
  for (const pullRequest of data?.values || []) {
33
    if (new Date(pullRequest.created_on).getTime() > lastChecked) {
34
      newPullRequests.push(pullRequest);
35
    } else {
36
      break;
37
    }
38
  }
39

40
  if (newPullRequests.length > 0) {
41
    await setState(new Date(newPullRequests[0].created_on).getTime());
42
  }
43

44
  return newPullRequests;
45
}
46