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

Trigger when a pipeline event occurs.

Created by hugo697 488 days ago Viewed 15555 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}/pipelines?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 newPipelines = [];
32
  for (const pipeline of data?.values || []) {
33
    if (new Date(pipeline.created_on).getTime() > lastChecked) {
34
      newPipelines.push(pipeline);
35
    } else {
36
      break;
37
    }
38
  }
39

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

44
  return newPipelines;
45
}
46