Skip to content

Commit 05b1ed4

Browse files
authored
feat: add discord support (#33)
* feat: add discord support * fix: correct discord message * fix: correct discord message * fix: correct discord message
1 parent 74e533a commit 05b1ed4

File tree

5 files changed

+193
-1
lines changed

5 files changed

+193
-1
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ inputs:
2020
slack-webhook-url:
2121
required: false
2222
description: A webhook URL of a Slack channel that if provided, will notify the release
23+
discord-webhook-url:
24+
required: false
25+
description: A webhook URL of a Discord text channel that if provided, will notify the release
2326
project-url:
2427
required: false
2528
description: A URL of the project that will be used in the slack notification

dist/index.js

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/discord.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import fetch from 'node-fetch'
2+
import * as core from '@actions/core'
3+
4+
export const notifyDiscordChannel = async(webhookUrl: string, options: {
5+
projectName: string
6+
projectUrl: string
7+
productionActionUrl: string
8+
nextVersion: string
9+
changelog: string
10+
isReleaseCandidate: boolean
11+
}) => {
12+
core.startGroup('Notifying Discord channel')
13+
const version = options.nextVersion + (options.isReleaseCandidate ? '-rc' : '')
14+
const payload = {
15+
username: '',
16+
avatar_url: '',
17+
content: `The project **${options.projectName}** has just released the version **${version}**!\n${
18+
options.changelog
19+
// replace headings with bold text
20+
.replace(/#+ ([^\n]+)/g, '**$1**')
21+
// replace links with discord link syntax
22+
.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, '<$2|$1>')
23+
.replace(/\- /g, '→')
24+
25+
}`,
26+
embeds: [] as any[],
27+
components: [] as any[],
28+
}
29+
payload.embeds.push({
30+
title: 'See project',
31+
url: options.projectUrl,
32+
})
33+
payload.embeds.push({
34+
title: 'Deploy to production',
35+
url: options.productionActionUrl,
36+
})
37+
payload.components.push({
38+
type: '1',
39+
components: [
40+
{
41+
type: 2,
42+
style: 5,
43+
label: 'See project',
44+
url: options.projectUrl,
45+
},
46+
{
47+
type: 2,
48+
style: 5,
49+
label: 'Deploy to production',
50+
url: options.productionActionUrl,
51+
},
52+
],
53+
})
54+
55+
core.info(`Sending payload to Discord\n${JSON.stringify(payload, null, 4)}`)
56+
const response = await fetch(webhookUrl, {
57+
method: 'POST',
58+
headers: {
59+
'Content-Type': 'application/json',
60+
},
61+
body: JSON.stringify(payload),
62+
})
63+
core.endGroup()
64+
return response
65+
}

src/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core'
22
import { context } from '@actions/github'
33
import { generateChangelog } from './changelog'
4+
import { notifyDiscordChannel } from './discord'
45
import { getLastCommitMessage, getLastGitTag, tagCommit } from './git'
56
import { createGithubRelease } from './github'
67
import { notifySlackChannel } from './slack'
@@ -9,6 +10,7 @@ import { getNextVersion, getPureVersion, getReleaseTypeFromCommitMessage } from
910
async function run(): Promise<void> {
1011
const isReleaseCandidate = core.getInput('release-candidate') === 'true'
1112
const slackWebhookUrl = core.getInput('slack-webhook-url')
13+
const discordWebhookUrl = core.getInput('discord-webhook-url')
1214

1315
try {
1416
const currentVersion = await getLastGitTag({
@@ -47,6 +49,16 @@ async function run(): Promise<void> {
4749
isReleaseCandidate,
4850
})
4951
}
52+
if (discordWebhookUrl !== '') {
53+
await notifyDiscordChannel(discordWebhookUrl, {
54+
projectName: context.repo.repo,
55+
projectUrl: core.getInput('project-url'),
56+
productionActionUrl: core.getInput('production-action-url'),
57+
nextVersion,
58+
changelog,
59+
isReleaseCandidate,
60+
})
61+
}
5062

5163
core.setOutput('next-version', nextVersion)
5264
core.setOutput('release-type', releaseType)

0 commit comments

Comments
 (0)