generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
113 lines (107 loc) · 3.08 KB
/
index.ts
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
import axios from 'axios';
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 } from './lib/helpers.js';
void (async () => {
try {
const {
GITHUB_REPOSITORY,
GITHUB_SHA,
GITHUB_RUN_ID,
GITHUB_RUN_NUMBER,
GITHUB_REF,
} = 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 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 timezoneString =
core.getInput('timezone', {
required: false,
trimWhitespace: true,
}) || 'America/New_York';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault(timezoneString);
const colorString = getAdaptiveCardColorString(color);
let messageToPost;
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 = `https://github.com/${repoName}`;
const octokit = new Octokit({ auth: `token ${githubToken}` });
const commit = await octokit.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({
title,
color: colorString,
commit,
branch,
author,
runNum,
runId,
repoName,
sha,
repoUrl,
timestamp,
});
} else {
const { populateCard } = await import('./lib/cards/simple.js');
messageToPost = populateCard({
title,
text: message,
color: colorString,
});
}
await axios
.post(teamsWebhookUrl, messageToPost)
.then(function (response) {
// console.log(response);
core.debug(response.data);
})
.catch(function (error) {
// console.log(error);
core.debug(error);
throw new Error(error);
});
} catch (error: any) {
console.error(error);
const { message } = error;
core.setFailed(message);
}
})();