This repository was archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (34 loc) · 1.28 KB
/
Copy pathindex.js
File metadata and controls
45 lines (34 loc) · 1.28 KB
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
const axios = require('axios')
const core = require('@actions/core')
const github = require('@actions/github')
const webhook = core.getInput('webhook')
if (!/https:\/\/discord(app|)\.com\/api\/webhooks\/\d+?\/.+/i.exec(webhook)) {
core.setFailed('The given discord webhook url is invalid. Please ensure you give a **full** url that start with "https://discordapp.com/api/webhooks"')
}
const shortSha = (i) => i.substr(0, 6)
const escapeMd = (str) => str.replace(/([\[\]\\`\(\)])/g, '\\$1')
const { payload: githubPayload } = github.context
const commits = githubPayload.commits.map(i => ` - [\`[${shortSha(i.id)}]\`](${i.url}) ${escapeMd(i.message)} - by ${i.author.name}`)
if (!commits.length) {
return
}
const beforeSha = githubPayload.before
const afterSha = githubPayload.after
const compareUrl = `${githubPayload.repository.url}/compare/${beforeSha}...${afterSha}`
const payload = {
content: '',
embeds: [
{
title: core.getInput('message-title') || 'Commits received',
description: `[\`\[${shortSha(beforeSha)}...${shortSha(afterSha)}\]\`](${compareUrl})\n${commits.join('\n')}`
}
]
}
axios
.post(webhook, payload)
.then((res) => {
core.setOutput('result', 'Webhook sent')
})
.catch((err) => {
core.setFailed(`Post to webhook failed, ${err}`)
})