Edits history of script submission #22991 for ' Variable expiration handler (slack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { WebClient } from '@slack/web-api';
    import dayjs from "dayjs";
    
    type Slack = {
      token: string;
    };
    
    // The variable's value is deliberately not a parameter: job arguments and results are
    // stored in cleartext and shown in Runs, so this handler is given a path to rotate and
    // never a value to handle. Do not add one.
    export async function main(
      workspace_id: string, // The workspace the variable belongs to
      variable_path: string, // The path of the variable whose value is expiring
      description: string, // The variable's description
      value_expires_at: string, // The datetime at which the value expires
      is_secret: boolean, // Whether the variable is a secret
      slack: Slack,
      channel: string,
    ) {
      const baseUrl = process.env["WM_BASE_URL"];
      const variablesUrl = `${baseUrl}/variables?workspace=${encodeURIComponent(workspace_id)}`;
      const expiry = dayjs(value_expires_at);
      const web = new WebClient(slack.token);
    
      const kind = is_secret ? "Secret" : "Variable";
      const expiresAt = expiry.format("DD.MM.YYYY HH:mm (Z)");
      const mdText =
        `*${kind} <${variablesUrl}|${variable_path}> is about to expire*\n- Expires at: ${expiresAt}`;
    
      await web.chat.postMessage({
        channel,
        text: `${kind} ${variable_path} expires at ${expiresAt}`,
        blocks: [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": mdText,
            },
          },
        ],
        attachments: [
          {
            color: "#eab308",
            "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": description
                    ? `Rotate it before it stops working.\n>${description}`
                    : "Rotate it before it stops working.",
                },
              },
            ],
            fallback: `Rotate ${variable_path} before it stops working.`,
          },
        ],
      });
    }
    

    Submitted by hugo989 3 hours ago