-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (75 loc) · 3.08 KB
/
index.js
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
const core = require('@actions/core')
const github = require('@actions/github')
const fetch = require('node-fetch')
async function run () {
const webhookId = core.getInput('webhook_id')
const webhookToken = core.getInput('webhook_token')
const webhookIdToken = core.getInput('webhook_id_token')
if ((!webhookId || !webhookToken) && !webhookIdToken)
return core.setFailed('webhook_id_token, webhook_id, or webhook_token is missing')
if (webhookId.indexOf('E') > -1)
return core.setFailed('webhook_id must be a string')
const embedMsg = { }
const title = core.getInput('title')
embedMsg['color'] = core.getInput('color')
const description = core.getInput('description')
const url = core.getInput('url')
const isRelease = core.getInput('release')
const isCommits = core.getInput('commits')
if (isRelease == 'true') {
const release = github.context.payload.release
embedMsg['title'] = title.replace('VERSION', release.tag_name)
const release_des = release.body
const extra = ` ([...](${release.html_url}))`
embedMsg['description'] = release_des.length < 4096 - extra.length ? release_des : release_des.substring(0, 4096 - extra.length) + extra
embedMsg['url'] = url ? url : release.html_url
}
else if (isCommits == 'true') {
const commits = github.context.payload.commits
embedMsg['title'] = title
embedMsg['url'] = url ? url : github.context.payload.compare
let text = ''
for (let i = 0; i < commits.length; i++)
text += '[`' + commits[i].id.substring(0, 7) + '`](' + commits[i].url + ') ' + commits[i].message + '\n';
embedMsg['description'] = text
}
else {
embedMsg['title'] = title
if (description)
embedMsg['description'] = description
if (url)
embedMsg['url'] = url
}
const author = core.getInput('author')
const author_url = core.getInput('author_url')
const author_icon_url = core.getInput('author_icon_url')
if (author || author_url || author_icon_url)
embedMsg['author'] = { }
if (author)
embedMsg['author']['name'] = author
if (author_url)
embedMsg['author']['url'] = author_url
if (author_icon_url)
embedMsg['author']['icon_url'] = author_icon_url
const body = { embeds: [embedMsg] }
const username = core.getInput('username')
if (username)
body['username'] = username
const avatar_url = core.getInput('avatar_url')
if (avatar_url)
body['avatar_url'] = avatar_url
const message = core.getInput('message')
if (message)
body['content'] = message
console.log(JSON.stringify(body))
const webhook = 'https://discord.com/api/webhooks/' + (webhookIdToken ? webhookIdToken : (webhookId + '/' + webhookToken)) + '?wait=true'
fetch(webhook, {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(data => core.info(JSON.stringify(data)))
.catch(err => core.info(err))
}
run()