All files / src index.ts

100% Statements 51/51
100% Branches 34/34
100% Functions 0/0
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                                      7x                 7x 7x       7x       7x         7x         7x         7x         7x         7x         7x           7x         7x     7x 1x 1x     7x 7x 7x   7x     7x 2x 2x 2x 2x 2x 2x 2x 2x 2x         2x 2x 2x   2x       2x                                   5x 5x               7x               7x 7x 7x 4x 4x   3x       7x 2x 2x     5x   2x 2x 2x    
import * as core from '@actions/core';
import { Octokit } from '@octokit/rest';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
 
import {
  getAdaptiveCardColorString,
  parseUserMentions,
} from './lib/helpers.js';
 
interface TeamsMessage {
  type: string;
  attachments: Array<{
    contentType: string;
    content: Record<string, unknown>;
  }>;
}
 
try {
  const {
    GITHUB_API_URL,
    GITHUB_REF,
    GITHUB_REPOSITORY,
    GITHUB_RUN_ID,
    GITHUB_RUN_NUMBER,
    GITHUB_SERVER_URL,
    GITHUB_SHA,
  } = process.env;
  const githubToken = core.getInput('github-token', {
    required: true,
    trimWhitespace: true,
  });
  const teamsWebhookUrl = core.getInput('webhook-url', {
    required: true,
    trimWhitespace: true,
  });
  const title = core.getInput('title', {
    required: true,
    trimWhitespace: true,
  });
  const titleSize =
    (core.getInput('title-size', {
      required: false,
      trimWhitespace: true,
    }) as 'Default' | 'Large') || 'Default';
  const message =
    core.getInput('message', {
      required: false,
      trimWhitespace: true,
    }) || '';
  const color =
    core.getInput('color', {
      required: false,
      trimWhitespace: true,
    }) || 'default';
  const isDeployCard =
    core.getBooleanInput('deploy-card', {
      required: false,
      trimWhitespace: true,
    }) || false;
  const showCommitMessage =
    core.getBooleanInput('show-commit-message', {
      required: false,
      trimWhitespace: true,
    }) || false;
  const timezoneString =
    core.getInput('timezone', {
      required: false,
      trimWhitespace: true,
    }) || 'America/New_York';
 
  const userMentionsInput =
    core.getInput('user-mentions', {
      required: false,
      trimWhitespace: true,
    }) || '';
 
  const { invalidEntries, mentions: userMentions } =
    parseUserMentions(userMentionsInput);
 
  if (invalidEntries.length > 0) {
    const invalidMessage = `Ignoring invalid user-mentions entries: ${invalidEntries.join(', ')}`;
    core.warning(invalidMessage);
  }
 
  dayjs.extend(utc);
  dayjs.extend(timezone);
  dayjs.tz.setDefault(timezoneString);
 
  const colorString = getAdaptiveCardColorString(color);
 
  let messageToPost: TeamsMessage;
  if (isDeployCard) {
    const { populateCard } = await import('./lib/cards/deploy.js');
    const [owner, repo] = (GITHUB_REPOSITORY ?? '').split('/');
    const sha = GITHUB_SHA ?? '';
    const params = { owner, repo, ref: sha };
    const runId = GITHUB_RUN_ID ?? '';
    const runNum = GITHUB_RUN_NUMBER ?? '';
    const repoName = `${owner}/${repo}`;
    const repoUrl = `${GITHUB_SERVER_URL}/${repoName}`;
    const octokit = new Octokit({
      auth: `token ${githubToken}`,
      baseUrl: GITHUB_API_URL || 'https://api.github.com',
      request: { fetch },
    });
    const commit = await octokit.rest.repos.getCommit(params);
    const branch = GITHUB_REF?.split('/')[GITHUB_REF.split('/').length - 1];
    const { author } = commit.data;
 
    const timestamp = dayjs()
      .tz(timezoneString)
      .format('ddd, D MMM YYYY hh:mm:ss Z');
 
    messageToPost = populateCard({
      author,
      branch,
      color: colorString,
      commit,
      message,
      repoName,
      repoUrl,
      runId,
      runNum,
      sha,
      showCommitMessage,
      timestamp,
      title,
      titleSize,
      userMentions,
    });
  } else {
    const { populateCard } = await import('./lib/cards/simple.js');
    messageToPost = populateCard({
      color: colorString,
      text: message,
      title,
      titleSize,
      userMentions,
    });
  }
  const response = await fetch(teamsWebhookUrl, {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(messageToPost),
  });
 
  const responseText = await response.text();
  let responseData: unknown = responseText;
  if (responseText) {
    try {
      responseData = JSON.parse(responseText) as unknown;
    } catch {
      responseData = responseText;
    }
  }
 
  if (!response.ok) {
    core.debug(responseData as string);
    throw new Error(responseText || `Request failed: ${response.status}`);
  }
 
  core.debug(responseData as string);
} catch (error: unknown) {
  console.error(error);
  const { message } = error as Error;
  core.setFailed(message);
}