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

Trigger when a commit receives a comment.

Created by hugo697 424 days ago Viewed 9101 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(
9
  bitbucket: Bitbucket,
10
  workspace: string,
11
  repo: string,
12
  commit: 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}/commit/${commit}/comments?pagelen=100`,
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 newComments = [];
33
  for (const comment of data?.values || []) {
34
    if (new Date(comment.created_on).getTime() > lastChecked) {
35
      newComments.push(comment);
36
    } else {
37
      break;
38
    }
39
  }
40

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

45
  return newComments;
46
}
47