generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 76
/
main.ts
79 lines (70 loc) · 2.21 KB
/
main.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
import * as core from '@actions/core'
import {Octokit} from '@octokit/rest'
import axios from 'axios'
import moment from 'moment-timezone'
import {createMessageCard} from './message-card'
const escapeMarkdownTokens = (text: string) =>
text
.replace(/\n\ {1,}/g, '\n ')
.replace(/\_/g, '\\_')
.replace(/\*/g, '\\*')
.replace(/\|/g, '\\|')
.replace(/#/g, '\\#')
.replace(/-/g, '\\-')
.replace(/>/g, '\\>')
async function run(): Promise<void> {
try {
const githubToken = core.getInput('github-token', {required: true})
const msTeamsWebhookUri: string = core.getInput('ms-teams-webhook-uri', {
required: true
})
const notificationSummary =
core.getInput('notification-summary') || 'GitHub Action Notification'
const notificationColor = core.getInput('notification-color') || '0b93ff'
const timezone = core.getInput('timezone') || 'UTC'
const verboseLogging = core.getInput('verbose-logging') == 'true'
const timestamp = moment()
.tz(timezone)
.format('dddd, MMMM Do YYYY, h:mm:ss a z')
const [owner, repo] = (process.env.GITHUB_REPOSITORY || '').split('/')
const sha = process.env.GITHUB_SHA || ''
const runId = process.env.GITHUB_RUN_ID || ''
const runNum = process.env.GITHUB_RUN_NUMBER || ''
const params = {owner, repo, ref: sha}
const repoName = params.owner + '/' + params.repo
const repoUrl = `https://github.com/${repoName}`
const octokit = new Octokit({auth: `token ${githubToken}`})
const commit = await octokit.repos.getCommit(params)
const author = commit.data.author
const messageCard = await createMessageCard(
notificationSummary,
notificationColor,
commit,
author,
runNum,
runId,
repoName,
sha,
repoUrl,
timestamp
)
if (verboseLogging) {
console.log(messageCard)
}
axios
.post(msTeamsWebhookUri, messageCard)
.then(function (response) {
if (verboseLogging) {
console.log(response)
}
core.debug(response.data)
})
.catch(function (error) {
core.debug(error)
})
} catch (error: any) {
console.log(error)
core.setFailed(error.message)
}
}
run()