mirror of
https://github.com/SethCohen/github-releases-to-discord
synced 2026-09-14 20:13:23 +00:00
refactor: added JSDocs, created more descriptive functions
This commit is contained in:
@@ -2,6 +2,31 @@ import core from '@actions/core';
|
|||||||
import github from '@actions/github';
|
import github from '@actions/github';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stylizes a markdown body into an appropriate embed message style.
|
||||||
|
* H3s converted to bold and underlined.
|
||||||
|
* H2s converted to bold.
|
||||||
|
* Redundant whitespace and newlines removed.
|
||||||
|
* @param description
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
const formatDescription = (description) => {
|
||||||
|
return description
|
||||||
|
.replace(/### (.*?)\n/g,function (substring) {
|
||||||
|
const newString = substring.slice(4).replace(/(\r\n|\n|\r)/gm, "")
|
||||||
|
return `**__${newString}__**`
|
||||||
|
})
|
||||||
|
.replace(/## (.*?)\n/g,function (substring) {
|
||||||
|
const newString = substring.slice(3).replace(/(\r\n|\n|\r)/gm, "")
|
||||||
|
return `**${newString}**`
|
||||||
|
})
|
||||||
|
.replace(/\n\s*\n/g, '\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the context of the action, returns a GitHub Release payload.
|
||||||
|
* @returns {Promise<{html_url, body: (*|string), version: string}>}
|
||||||
|
*/
|
||||||
async function getContext () {
|
async function getContext () {
|
||||||
const payload = github.context.payload
|
const payload = github.context.payload
|
||||||
|
|
||||||
@@ -14,52 +39,47 @@ async function getContext () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the action.
|
||||||
|
* Get inputs, creates a stylized response webhook, and sends it to the channel.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async function run () {
|
async function run () {
|
||||||
try {
|
const webhookUrl = core.getInput('webhook_url')
|
||||||
const webhookUrl = core.getInput('webhook_url')
|
const color = core.getInput('color')
|
||||||
const color = core.getInput('color')
|
const username = core.getInput('username')
|
||||||
const username = core.getInput('username')
|
const avatarUrl = core.getInput('avatar_url')
|
||||||
const avatarUrl = core.getInput('avatar_url')
|
|
||||||
|
|
||||||
if (!webhookUrl) {
|
if (!webhookUrl) return core.setFailed('webhook_url not set. Please set it.')
|
||||||
return core.setFailed('webhook_url not set. Please set it.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const {body, html_url, version} = await getContext()
|
const {body, html_url, version} = await getContext()
|
||||||
|
|
||||||
const description = body
|
const description = formatDescription(body)
|
||||||
.replace(/### (.*?)\n/g,function (substring) {
|
|
||||||
const newString = substring.slice(4).replace(/(\r\n|\n|\r)/gm, "")
|
|
||||||
return `**__${newString}__**`
|
|
||||||
})
|
|
||||||
.replace(/## (.*?)\n/g,function (substring) {
|
|
||||||
const newString = substring.slice(3).replace(/(\r\n|\n|\r)/gm, "")
|
|
||||||
return `**${newString}**`
|
|
||||||
})
|
|
||||||
.replace(/\n\s*\n/g, '\n')
|
|
||||||
|
|
||||||
const embedMsg = {
|
const embedMsg = {
|
||||||
title: `Release ${version}`,
|
title: `Release ${version}`,
|
||||||
url: html_url,
|
url: html_url,
|
||||||
color: color,
|
color: color,
|
||||||
description: description,
|
description: description
|
||||||
}
|
|
||||||
|
|
||||||
const requestBody = { username: username, avatar_url: avatarUrl, embeds: [embedMsg], }
|
|
||||||
|
|
||||||
const url = `${webhookUrl}?wait=true`
|
|
||||||
|
|
||||||
fetch(url, {
|
|
||||||
method: 'post',
|
|
||||||
body: JSON.stringify(requestBody),
|
|
||||||
headers: { 'Content-Type': 'application/json' }
|
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(data => core.info(JSON.stringify(data)))
|
|
||||||
.catch(err => core.info(err))
|
|
||||||
} catch (error) {
|
|
||||||
core.setFailed(error.message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requestBody = {
|
||||||
|
username: username,
|
||||||
|
avatar_url: avatarUrl,
|
||||||
|
embeds: [embedMsg]
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${webhookUrl}?wait=true`
|
||||||
|
fetch(url, {
|
||||||
|
method: 'post',
|
||||||
|
body: JSON.stringify(requestBody),
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => core.info(JSON.stringify(data)))
|
||||||
|
.catch(err => core.info(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
run()
|
run()
|
||||||
|
.then(() => {core.info('Action completed successfully')})
|
||||||
|
.catch(err => {core.setFailed(err.message)})
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "github-release-to-discord",
|
"name": "github-release-to-discord",
|
||||||
"version": "1.9.0",
|
"version": "1.12.1",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
|
|||||||
Generated
+1
-1
@@ -7,7 +7,7 @@
|
|||||||
"": {
|
"": {
|
||||||
"name": "github-release-to-discord",
|
"name": "github-release-to-discord",
|
||||||
"version": "1.12.1",
|
"version": "1.12.1",
|
||||||
"license": "ISC",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.9.1",
|
"@actions/core": "^1.9.1",
|
||||||
"@actions/github": "^5.0.3",
|
"@actions/github": "^5.0.3",
|
||||||
|
|||||||
Reference in New Issue
Block a user