0
Schedule success handler
One script reply has been approved by the moderators Verified
Created by hugo697 78 days ago Viewed 2783 times
0
Submitted by hugo697 Bun
Verified 78 days ago
1
import { WebClient } from '@slack/web-api';
2
import dayjs from "dayjs";
3

4
type Slack = {
5
  token: string;
6
};
7

8
export async function main(
9
  path: string, // The path of the script or flow
10
  is_flow: boolean, // Whether the runnable is a flow
11
  schedule_path: string, // The path of the schedule
12
  success_result: object, // The result of the latest successful job
13
  success_started_at: string, // The start datetime of the latest successful job
14
  slack: Slack,
15
  channel: string,
16
) {
17
  const baseUrl = process.env["WM_BASE_URL"];
18
  const scheduleUrl = baseUrl + "/runs?schedule_path=" +
19
    encodeURIComponent(schedule_path);
20
  const runnableUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
21
    path;
22
  const web = new WebClient(slack.token);
23

24
  await web.chat.postMessage({
25
    channel,
26
    text: `Schedule ${schedule_path} was successful`,
27
    blocks: [
28
      {
29
        "type": "section",
30
        "text": {
31
          "type": "mrkdwn",
32
          "text": `*Schedule <${scheduleUrl}|${schedule_path}> was successful*:\n- ${is_flow ? "Flow" : "Script"}: <${runnableUrl}|${path}>`,
33
        },
34
      },
35
    ],
36
    attachments: [
37
      {
38
        color: "#00ff00",
39
        "blocks": [
40
          {
41
            "type": "section",
42
            "text": {
43
              "type": "mrkdwn",
44
              "text": `Started at: ${dayjs(success_started_at).format("DD.MM.YYYY HH:mm (Z)")
45
                }\n\`\`\`\n${JSON.stringify(success_result)}\n\`\`\``,
46
            },
47
          },
48
        ],
49
      },
50
    ],
51
  });
52
}