diff --git a/.github/workflows/release-notification.yaml b/.github/workflows/release-notification.yaml index 29ef8fa..d2db3c0 100644 --- a/.github/workflows/release-notification.yaml +++ b/.github/workflows/release-notification.yaml @@ -1,4 +1,4 @@ -name: release-notification +name: On PR Closed on: pull_request: @@ -18,10 +18,8 @@ jobs: runs-on: ubuntu-latest environment: cc-test-release env: - PR_BODY: ${{ github.event.pull_request.body }} + EVENT_PR: ${{ github.event.pull_request }} steps: - - name: Print Body - run: echo "$PR_BODY" - name: Checkout repository uses: actions/checkout@v4 - name: Sending Notification - Slack @@ -29,4 +27,4 @@ jobs: with: script: | const sendSlackMessage = require('./.github/workflows/send-slack.js') - sendSlackMessage(process.env.PR_BODY,process.env.CC_SLACK_WEBHOOK) + sendSlackMessage(process.env.EVENT_PR,process.env.CC_SLACK_WEBHOOK) diff --git a/.github/workflows/send-slack.js b/.github/workflows/send-slack.js deleted file mode 100644 index 029c39f..0000000 --- a/.github/workflows/send-slack.js +++ /dev/null @@ -1,11 +0,0 @@ -const sendSlackNotification = async (slackMessage, slack_webhook_url) => { - console.log("slackMessage", slackMessage); - const result = await fetch(slack_webhook_url, { - method: "POST", - body: JSON.stringify({ text: slackMessage }), - }).catch(console.error); - console.log("API Response", result); - return result; -}; - -module.exports = sendSlackNotification; diff --git a/.github/workflows/src/helpers.js b/.github/workflows/src/helpers.js new file mode 100644 index 0000000..ad34091 --- /dev/null +++ b/.github/workflows/src/helpers.js @@ -0,0 +1,24 @@ +function convertMarkdownToSlackFormat(markdown) { + // Convert headings + markdown = markdown.replace(/^(#+) (.*$)/gim, "*$2*"); + // Convert bold text + markdown = markdown.replace(/\*\*(.*?)\*\*/gim, "*$1*"); + // Convert italic text + markdown = markdown.replace(/\*(.*?)\*/gim, "_$1_"); + // Convert inline code + markdown = markdown.replace(/`([^`]+)`/g, "```$1```"); + // Convert block quotes + markdown = markdown.replace(/^> (.*$)/gim, "_$1_"); + // Convert unordered lists + markdown = markdown.replace(/^\s*[-+*]\s+(.*$)/gim, "• $1"); + // Convert ordered lists + markdown = markdown.replace(/^\s*\d+\.\s+(.*$)/gim, "1. $1"); + // Convert newlines to break lines + markdown = markdown.replace(/\n/g, "\n"); + + return markdown.trim(); +} + +module.exports = { + convertMarkdownToSlackFormat, +}; diff --git a/.github/workflows/src/send-slack.js b/.github/workflows/src/send-slack.js new file mode 100644 index 0000000..9b7b488 --- /dev/null +++ b/.github/workflows/src/send-slack.js @@ -0,0 +1,18 @@ +const { convertMarkdownToSlackFormat } = require("./helpers"); + +const sendSlackMessage = async (event_pr, slack_webhook_url) => { + const { number, html_url, title, body } = event_pr; + + const text = `:rocket: *Production Release*\n*Title:* ${title} | <${html_url}|#${number}>\n*PR Description:* ${convertMarkdownToSlackFormat( + body + )}`; + console.log("text", text); + + const result = await fetch(slack_webhook_url, { + method: "POST", + body: JSON.stringify({ text }), + }).catch(console.error); + return result; +}; + +module.exports = sendSlackMessage;