forked from vitejs/vite-ecosystem-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-webhook.ts
148 lines (128 loc) · 3.53 KB
/
discord-webhook.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
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
import fetch from 'node-fetch'
import { getPermanentRef, setupEnvironment } from './utils'
type RefType = 'branch' | 'tag' | 'commit' | 'release'
type Status = 'success' | 'failure' | 'cancelled'
type Env = {
WORKFLOW_NAME?: string
REF_TYPE?: RefType
REF?: string
SUITE?: string
STATUS?: Status
DISCORD_WEBHOOK_URL?: string
}
const statusConfig = {
success: {
color: parseInt('57ab5a', 16),
emoji: ':white_check_mark:'
},
failure: {
color: parseInt('e5534b', 16),
emoji: ':x:'
},
cancelled: {
color: parseInt('768390', 16),
emoji: ':stop_button:'
}
}
async function run() {
if (!process.env.GITHUB_ACTIONS) {
throw new Error('This script can only run on GitHub Actions.')
}
if (!process.env.DISCORD_WEBHOOK_URL) {
console.warn(
"Skipped beacuse process.env.DISCORD_WEBHOOK_URL was empty or didn't exist"
)
return
}
const env = process.env as Env
assertEnv('WORKFLOW_NAME', env.WORKFLOW_NAME)
assertEnv('REF_TYPE', env.REF_TYPE)
assertEnv('REF', env.REF)
assertEnv('SUITE', env.SUITE)
assertEnv('STATUS', env.STATUS)
assertEnv('DISCORD_WEBHOOK_URL', env.DISCORD_WEBHOOK_URL)
await setupEnvironment()
const refType = env.REF_TYPE
// vite repo is not cloned when release
const permRef = refType === 'release' ? undefined : await getPermanentRef()
const targetText = createTargetText(refType, env.REF, permRef)
const webhookContent = {
username: `vite-ecosystem-ci (${env.WORKFLOW_NAME})`,
avatar_url: 'https://github.com/vitejs.png',
embeds: [
{
title: `${statusConfig[env.STATUS].emoji} ${env.SUITE}`,
description: await createDescription(env.SUITE, targetText),
color: statusConfig[env.STATUS].color
}
]
}
const res = await fetch(env.DISCORD_WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(webhookContent)
})
if (res.ok) {
console.log('Sent Webhook')
} else {
console.error(`Webhook failed ${res.status}:`, await res.text())
}
}
function assertEnv<T>(
name: string,
value: T
): asserts value is Exclude<T, undefined> {
if (!value) {
throw new Error(`process.env.${name} is empty or does not exist.`)
}
}
async function createRunUrl(suite: string) {
const result = await fetchJobs()
const job = result.jobs.find((job) => job.name === process.env.GITHUB_JOB)
if (job) {
return job.html_url
}
// when matrix
const jobM = result.jobs.find(
(job) => job.name === `${process.env.GITHUB_JOB} (${suite})`
)
return jobM?.html_url
}
interface GitHubActionsJob {
name: string
html_url: string
}
async function fetchJobs() {
const url = `${process.env.GITHUB_API_URL}/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/jobs`
const res = await fetch(url, {
headers: {
Accept: 'application/vnd.github.v3+json'
}
})
const result = await res.json()
return result as { jobs: GitHubActionsJob[] }
}
async function createDescription(suite: string, targetText: string) {
const runUrl = await createRunUrl(suite)
return `
:scroll:\u00a0\u00a0[Open](${runUrl})\u3000\u3000:zap:\u00a0\u00a0${targetText}
`.trim()
}
function createTargetText(
refType: RefType,
ref: string,
permRef: string | undefined
) {
if (refType === 'branch') {
const link = `https://github.com/vitejs/vite/commits/${permRef || ref}`
return `[${ref} (${permRef || 'unknown'})](${link})`
}
const refTypeText = refType === 'release' ? ' (release)' : ''
const link = `https://github.com/vitejs/vite/commits/${ref}`
return `[${ref}${refTypeText}](${link})`
}
run().catch((e) => {
console.error('Error sending webhook:', e)
})