Skip to content

Commit

Permalink
feat: add discord support (#33)
Browse files Browse the repository at this point in the history
* feat: add discord support

* fix: correct discord message

* fix: correct discord message

* fix: correct discord message
  • Loading branch information
henrycunh authored Jun 26, 2022
1 parent 74e533a commit 05b1ed4
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 1 deletion.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
slack-webhook-url:
required: false
description: A webhook URL of a Slack channel that if provided, will notify the release
discord-webhook-url:
required: false
description: A webhook URL of a Discord text channel that if provided, will notify the release
project-url:
required: false
description: A URL of the project that will be used in the slack notification
Expand Down
112 changes: 112 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions src/discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fetch from 'node-fetch'
import * as core from '@actions/core'

export const notifyDiscordChannel = async(webhookUrl: string, options: {
projectName: string
projectUrl: string
productionActionUrl: string
nextVersion: string
changelog: string
isReleaseCandidate: boolean
}) => {
core.startGroup('Notifying Discord channel')
const version = options.nextVersion + (options.isReleaseCandidate ? '-rc' : '')
const payload = {
username: '',
avatar_url: '',
content: `The project **${options.projectName}** has just released the version **${version}**!\n${
options.changelog
// replace headings with bold text
.replace(/#+ ([^\n]+)/g, '**$1**')
// replace links with discord link syntax
.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, '<$2|$1>')
.replace(/\- /g, '→')
}`,
embeds: [] as any[],
components: [] as any[],
}
payload.embeds.push({
title: 'See project',
url: options.projectUrl,
})
payload.embeds.push({
title: 'Deploy to production',
url: options.productionActionUrl,
})
payload.components.push({
type: '1',
components: [
{
type: 2,
style: 5,
label: 'See project',
url: options.projectUrl,
},
{
type: 2,
style: 5,
label: 'Deploy to production',
url: options.productionActionUrl,
},
],
})

core.info(`Sending payload to Discord\n${JSON.stringify(payload, null, 4)}`)
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
core.endGroup()
return response
}
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core'
import { context } from '@actions/github'
import { generateChangelog } from './changelog'
import { notifyDiscordChannel } from './discord'
import { getLastCommitMessage, getLastGitTag, tagCommit } from './git'
import { createGithubRelease } from './github'
import { notifySlackChannel } from './slack'
Expand All @@ -9,6 +10,7 @@ import { getNextVersion, getPureVersion, getReleaseTypeFromCommitMessage } from
async function run(): Promise<void> {
const isReleaseCandidate = core.getInput('release-candidate') === 'true'
const slackWebhookUrl = core.getInput('slack-webhook-url')
const discordWebhookUrl = core.getInput('discord-webhook-url')

try {
const currentVersion = await getLastGitTag({
Expand Down Expand Up @@ -47,6 +49,16 @@ async function run(): Promise<void> {
isReleaseCandidate,
})
}
if (discordWebhookUrl !== '') {
await notifyDiscordChannel(discordWebhookUrl, {
projectName: context.repo.repo,
projectUrl: core.getInput('project-url'),
productionActionUrl: core.getInput('production-action-url'),
nextVersion,
changelog,
isReleaseCandidate,
})
}

core.setOutput('next-version', nextVersion)
core.setOutput('release-type', releaseType)
Expand Down

0 comments on commit 05b1ed4

Please sign in to comment.