From f228faeb5e12d45f334ed8d458a6d2e581ddc8e6 Mon Sep 17 00:00:00 2001 From: Shon <13995143+ShonP40@users.noreply.github.com> Date: Sat, 23 Sep 2023 19:18:00 +0300 Subject: [PATCH] Implement more functions (#20) * Send the release title instead of version & remove the `Release` text * Ability to use the existing Discord user & avatar * fix * Support for adding a footer * Update index.js * Update index.js * Update index.js * Updated docs * Support for footer timestamps * Support for sending normal text above the embed useful for pings --- README.md | 20 ++++++++++++++------ action.yml | 14 ++++++++++++-- index.js | 45 ++++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 11f9702..ee9d835 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,16 @@ A GitHub action that parses a GitHub release and posts it to a Discord channel a ## Configuration -| Variable | Required | Default | Description | -|------------|----------|----------------------------------------------------------------------------------------------------------------|--------------------------------------------| -| webhook_url | ✔ | | Discord's webhook url. Use GH repo secrets. | -| color | ❌ | "2105893" | Decimal color value for embed. | -| username | ❌ | "Release Changelog" | String username for webhook. | -| avatar_url | ❌ | ["Profile Picture"](https://cdn.discordapp.com/avatars/487431320314576937/bd64361e4ba6313d561d54e78c9e7171.png) | String url to webhook avatar picture. | +| Variable | Required | Default | Description | +|-----------------|----------|----------------------------------------------------------------------------------------------------------------|--------------------------------------------| +| webhook_url | ✔ | | Discord's webhook url. Use GH repo secrets.| +| color | ❌ | "2105893" | Decimal color value for embed. | +| username | ❌ | | String username for webhook. | +| avatar_url | ❌ | | String url to webhook avatar picture. | +| content | ❌ | | String content for webhook. | +| footer_title | ❌ | | String title for the webhook footer. | +| footer_icon_url | ❌ | | String url for the webhook footer picture. | +| footer_timestamp| ❌ | | Boolean to enable footer timestamp. | ## Example Usage @@ -35,6 +39,10 @@ jobs: color: "2105893" username: "Release Changelog" avatar_url: "https://cdn.discordapp.com/avatars/487431320314576937/bd64361e4ba6313d561d54e78c9e7171.png" + content: "||@everyone||" + footer_title: "Changelog" + footer_icon_url: "https://cdn.discordapp.com/avatars/487431320314576937/bd64361e4ba6313d561d54e78c9e7171.png" + footer_timestamp: true ``` ## Setup Instructions diff --git a/action.yml b/action.yml index 451506e..e59516a 100644 --- a/action.yml +++ b/action.yml @@ -12,11 +12,21 @@ inputs: username: description: String username for webhook. required: false - default: 'Release Changelog' avatar_url: description: String url to webhook avatar picture. required: false - default: 'https://cdn.discordapp.com/avatars/487431320314576937/bd64361e4ba6313d561d54e78c9e7171.png' + content: + description: String content for webhook. + required: false + footer_title: + description: Title for the footer. + required: false + footer_icon_url: + description: Icon url for the footer. + required: false + footer_timestamp: + description: Timestamp for the footer. + required: false runs: using: 'node16' main: 'index.js' diff --git a/index.js b/index.js index f8e680b..e14ce96 100644 --- a/index.js +++ b/index.js @@ -25,16 +25,16 @@ const formatDescription = (description) => { /** * Get the context of the action, returns a GitHub Release payload. - * @returns {Promise<{html_url, body: (*|string), version: string}>} + * @returns {Promise<{html_url, body: (*|string), name: string}>} */ async function getContext () { - const payload = github.context.payload + const payload = github.context.payload; return { body: payload.release.body.length < 1500 ? payload.release.body : payload.release.body.substring(0, 1500) + ` ([...](${payload.release.html_url}))`, - version: payload.release.tag_name, + name: payload.release.name, html_url: payload.release.html_url } } @@ -45,31 +45,42 @@ async function getContext () { * @returns {Promise} */ async function run () { - const webhookUrl = core.getInput('webhook_url') - const color = core.getInput('color') - const username = core.getInput('username') - const avatarUrl = core.getInput('avatar_url') + const webhookUrl = core.getInput('webhook_url'); + const color = core.getInput('color'); + const username = core.getInput('username'); + const avatarUrl = core.getInput('avatar_url'); + const content = core.getInput('content'); + const footerTitle = core.getInput('footer_title'); + const footerIconUrl = core.getInput('footer_icon_url'); + const footerTimestamp = core.getInput('footer_timestamp'); - if (!webhookUrl) return core.setFailed('webhook_url not set. Please set it.') + if (!webhookUrl) return core.setFailed('webhook_url not set. Please set it.'); - const {body, html_url, version} = await getContext() + const {body, html_url, name} = await getContext(); - const description = formatDescription(body) + const description = formatDescription(body); - const embedMsg = { - title: `Release ${version}`, + let embedMsg = { + title: name, url: html_url, color: color, - description: description + description: description, + footer: {} } - const requestBody = { - username: username, - avatar_url: avatarUrl, + if (footerTitle != '') embedMsg.footer.text = footerTitle; + if (footerIconUrl != '') embedMsg.footer.icon_url = footerIconUrl; + if (footerTimestamp == 'true') embedMsg.timestamp = new Date().toISOString(); + + let requestBody = { embeds: [embedMsg] } - const url = `${webhookUrl}?wait=true` + if (username != '') requestBody.username = username; + if (avatarUrl != '') requestBody.avatar_url = avatarUrl; + if (content != '') requestBody.content = content; + + const url = `${webhookUrl}?wait=true`; fetch(url, { method: 'post', body: JSON.stringify(requestBody),