-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use GitHub script to handle JSON escaping in Slack message (#3851)
- Loading branch information
Showing
1 changed file
with
25 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,50 +18,39 @@ jobs: | |
github.repository_owner == 'WordPress' | ||
runs-on: ubuntu-latest | ||
env: | ||
pr_url: ${{ github.event.pull_request.html_url }} | ||
pr_number: ${{ github.event.pull_request.number }} | ||
pr_title: ${{ format('{0}', github.event.pull_request.title) }} | ||
pr_author: ${{ github.event.pull_request.user.login }} | ||
pr_repo: ${{ github.event.pull_request.base.repo.full_name }} | ||
EVENT_ACTION: ${{ github.event.action }} | ||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | ||
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK | ||
steps: | ||
- name: Send notification for new PR | ||
if: github.event.action == 'opened' | ||
id: slack | ||
uses: slackapi/[email protected] | ||
env: | ||
pr_status: ${{ github.event.pull_request.draft && ' draft ' || ' ' }} | ||
pr_icon: ${{ github.event.pull_request.draft && ':pr-draft:' || ':pull-request:' }} | ||
- name: Write payload to file | ||
uses: actions/github-script@v7 | ||
with: | ||
payload: | | ||
{ | ||
"text": "New${{ env.pr_status }}PR opened by ${{ env.pr_author }} in ${{ env.pr_repo }}: #${{ env.pr_number }} - ${{ env.pr_title }}", | ||
"blocks": [ | ||
script: | | ||
let { writeFileSync } = await import('fs') | ||
const repoName = `${repo.repo.owner}/${context.repo.repo}` | ||
const { pull_request: pr } = context.payload | ||
const isNew = process.env.EVENT_ACTION === 'opened' | ||
const emoji = pr.draft ? ':pr-draft:' : ':pull-request:' | ||
const adjective = isNew ? pr.draft ? 'New draft ' : 'New ' : '' | ||
const verb = isNew ? 'opened' : 'marked ready for review' | ||
const payload = { | ||
text: `${adjective}PR ${verb} by ${pr.user.login} in ${repoName}: #${pr.number} - ${pr.title}`, | ||
blocks: [ | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "${{ env.pr_icon }} New${{ env.pr_status }}PR opened by *${{ env.pr_author }}* in `${{ env.pr_repo }}`:\n<${{ env.pr_url }}|#${{ env.pr_number }} - ${{ env.pr_title }}>" | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `${emoji} ${adjective}PR ${verb} by *${pr.user.login}* in \`${repoName}\`:\n<${pr.html_url}|#${pr.number} - ${pr.title}>` | ||
} | ||
} | ||
] | ||
} | ||
- name: Send notification for PR marked ready | ||
if: github.event.action == 'ready_for_review' | ||
id: slack-notifications | ||
console.log(JSON.stringify(payload)) | ||
writeFileSync('/tmp/pr_ping_payload.json', JSON.stringify(payload)) | ||
- name: Send Slack notification | ||
uses: slackapi/[email protected] | ||
with: | ||
payload: | | ||
{ | ||
"text": "PR by ${{ env.pr_author }} in ${{ env.pr_repo }} marked ready for review: #${{ env.pr_number }} - ${{ env.pr_title }}", | ||
"blocks": [ | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": ":pull-request: PR by *${{ env.pr_author }}* in `${{ env.pr_repo }}` marked ready for review:\n<${{ env.pr_url }}|#${{ env.pr_number }} - ${{ env.pr_title }}>" | ||
} | ||
} | ||
] | ||
} | ||
payload-file-path: /tmp/pr_ping_payload.json |