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

Trigger when a new issue receives is created in a repository.

Created by hugo697 488 days ago Viewed 14862 times
In Windmill, a trigger script is designed to pull data from an external source and return all the new items since the last run. It operates without resorting to external webhooks and is typically used with schedules and states to compare the current execution to the previous one.
0
Submitted by hugo697 Bun
Verified 488 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}/issues?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 newIssues = [];
32
  for (const issue of data?.values || []) {
33
    if (new Date(issue.created_on).getTime() > lastChecked) {
34
      newIssues.push(issue);
35
    } else {
36
      break;
37
    }
38
  }
39

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

44
  return newIssues;
45
}
46