0

Variable expiration handler

by
Published today

Workspace variable expiration handler sending a message to a Slack channel an hour before the value of a variable expires, so it can be rotated before it stops working

Script slack Verified

The script

Submitted by hugo989 Bun
Verified 3 hours ago
1
import { WebClient } from '@slack/web-api';
2
import dayjs from "dayjs";
3

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

8
// The variable's value is deliberately not a parameter: job arguments and results are
9
// stored in cleartext and shown in Runs, so this handler is given a path to rotate and
10
// never a value to handle. Do not add one.
11
export async function main(
12
  workspace_id: string, // The workspace the variable belongs to
13
  variable_path: string, // The path of the variable whose value is expiring
14
  description: string, // The variable's description
15
  value_expires_at: string, // The datetime at which the value expires
16
  is_secret: boolean, // Whether the variable is a secret
17
  slack: Slack,
18
  channel: string,
19
) {
20
  const baseUrl = process.env["WM_BASE_URL"];
21
  const variablesUrl = `${baseUrl}/variables?workspace=${encodeURIComponent(workspace_id)}`;
22
  const expiry = dayjs(value_expires_at);
23
  const web = new WebClient(slack.token);
24

25
  const kind = is_secret ? "Secret" : "Variable";
26
  const expiresAt = expiry.format("DD.MM.YYYY HH:mm (Z)");
27
  const mdText =
28
    `*${kind} <${variablesUrl}|${variable_path}> is about to expire*\n- Expires at: ${expiresAt}`;
29

30
  await web.chat.postMessage({
31
    channel,
32
    text: `${kind} ${variable_path} expires at ${expiresAt}`,
33
    blocks: [
34
      {
35
        "type": "section",
36
        "text": {
37
          "type": "mrkdwn",
38
          "text": mdText,
39
        },
40
      },
41
    ],
42
    attachments: [
43
      {
44
        color: "#eab308",
45
        "blocks": [
46
          {
47
            "type": "section",
48
            "text": {
49
              "type": "mrkdwn",
50
              "text": description
51
                ? `Rotate it before it stops working.\n>${description}`
52
                : "Rotate it before it stops working.",
53
            },
54
          },
55
        ],
56
        fallback: `Rotate ${variable_path} before it stops working.`,
57
      },
58
    ],
59
  });
60
}
61