Notify Slack channel about upcoming releases #14
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow notifies Slack channel about upcoming releases. | |
name: Notify Slack channel about upcoming release | |
on: | |
pull_request: | |
# branches: | |
# - release | |
jobs: | |
notify: | |
runs-on: [ ubuntu-latest ] | |
steps: | |
- uses: actions/github-script@v6 | |
id: commitlist | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
retries: 5 | |
result-encoding: string | |
script: | | |
// Slack Message layout is created with https://app.slack.com/block-kit-builder | |
const urlForPr = function (prNumber) { | |
return `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/pull/${prNumber}` | |
} | |
const linkify = function (text) { | |
return text.replace(/#(\d+)/g, (match, prNumber) => { return `<${urlForPr(prNumber)}|#${prNumber}>` }) | |
} | |
const { data: pullRequest } = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
// pull_number: context.payload.number, | |
// TODO: revert back | |
pull_number: 5202 | |
}); | |
const header = `"${pullRequest.title}" is coming :tada:` | |
const { data: commits } = await github.rest.pulls.listCommits({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
// pull_number: context.payload.number, | |
// TODO: revert back | |
pull_number: 5202 | |
}); | |
const blocks = [] | |
blocks.push({ | |
type: "header", | |
text: { | |
type: "plain_text", | |
text: header, | |
}, | |
}, | |
{ | |
type: "context", | |
elements: [ | |
{ | |
type: "mrkdwn", | |
// text: `Release PR: ${urlForPr(context.payload.number)`, | |
// TODO: revert back | |
text: `Release PR: <${urlForPr(5202)}|${urlForPr(5202)}>`, | |
}, | |
], | |
}) | |
let messageBody = "" | |
for (const commit of commits) { | |
const message = commit.commit.message.replace(/\r\n/g, "\n") | |
const title = message.split("\n\n", 1)[0] | |
messageBody += `- ${linkify(title)}\n` | |
} | |
blocks.push({ | |
"type": "divider" | |
}, { | |
type: "section", | |
text: { | |
type: "mrkdwn", | |
text: messageBody, | |
}, | |
}, { | |
"type": "divider" | |
}, { | |
type: "context", | |
elements: [{ | |
type: "plain_text", | |
text: `Updated at <!date^${Date.now() / 1000}^{date_num} {time_secs}|${new Date().toISOString()}>`, | |
}], | |
}) | |
const slackMessage = { | |
text: header, | |
blocks, | |
} | |
// TODO: Remove debug logging | |
console.log(JSON.stringify(slackMessage, null, 2)) | |
return JSON.stringify(slackMessage) | |
- uses: actions/cache/restore@v3 | |
with: | |
path: release-notify.json | |
key: release-notify-${{ github.event.number }}.json | |
- id: message-id | |
run: | | |
if [ -f release-notify.json ]; then | |
UPDATE_TS=$(cat release-notify.json | jq --raw-output '.ts') | |
else | |
UPDATE_TS="" | |
fi | |
echo "update-ts=${UPDATE_TS}" >> $GITHUB_OUTPUT | |
- uses: slackapi/slack-github-action@v1 | |
id: slack | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | |
with: | |
channel-id: "C05QQ9J1BRC" # #test-release-notifications | |
update-ts: ${{ steps.message-id.outputs.update-ts }} | |
payload: ${{ steps.commitlist.outputs.result }} | |
- run: | | |
echo '{"ts": "${{ steps.slack.outputs.ts }}"}' > release-notify.json | |
- uses: actions/cache/save@v3 | |
with: | |
path: release-notify.json | |
key: release-notify-${{ github.event.number }}.json |