diff --git a/index.js b/index.js index 9c04b74..f8e680b 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,31 @@ import core from '@actions/core'; import github from '@actions/github'; 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 () { 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} + */ async function run () { - try { - const webhookUrl = core.getInput('webhook_url') - const color = core.getInput('color') - const username = core.getInput('username') - const avatarUrl = core.getInput('avatar_url') - - if (!webhookUrl) { - return core.setFailed('webhook_url not set. Please set it.') - } + const webhookUrl = core.getInput('webhook_url') + const color = core.getInput('color') + const username = core.getInput('username') + const avatarUrl = core.getInput('avatar_url') - const {body, html_url, version} = await getContext() + if (!webhookUrl) return core.setFailed('webhook_url not set. Please set it.') - const description = 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 {body, html_url, version} = await getContext() - const embedMsg = { - title: `Release ${version}`, - url: html_url, - color: color, - description: description, - } + const description = formatDescription(body) - const requestBody = { username: username, avatar_url: avatarUrl, embeds: [embedMsg], } - - const url = `${webhookUrl}?wait=true` + const embedMsg = { + title: `Release ${version}`, + url: html_url, + color: color, + description: description + } - 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() \ No newline at end of file +run() + .then(() => {core.info('Action completed successfully')}) + .catch(err => {core.setFailed(err.message)}) \ No newline at end of file diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 35e45e3..d576188 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -1,6 +1,6 @@ { "name": "github-release-to-discord", - "version": "1.9.0", + "version": "1.12.1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package-lock.json b/package-lock.json index 7e8a509..82b392e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "github-release-to-discord", "version": "1.12.1", - "license": "ISC", + "license": "MIT", "dependencies": { "@actions/core": "^1.9.1", "@actions/github": "^5.0.3",