Post PR to Slack #1677
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
name: Post PR to Slack | |
on: | |
pull_request: | |
types: | |
- opened | |
- converted_to_draft | |
- ready_for_review | |
- reopened | |
- closed | |
pull_request_review: | |
types: | |
- submitted | |
env: | |
CONFIG: | | |
{ | |
"opened": { | |
"icon": "${{ github.event.pull_request.draft && 'π' || 'π' }}", | |
"status": "${{ github.event.pull_request.draft && 'opened as draft' || 'opened' }}" | |
}, | |
"converted_to_draft": { | |
"icon": "π", | |
"status": "converted to draft" | |
}, | |
"ready_for_review": { | |
"icon": "π", | |
"status": "ready for review" | |
}, | |
"reopened": { | |
"icon": "π", | |
"status": "reopened" | |
}, | |
"submitted": { | |
"icon": "${{ github.event.review.state == 'approved' && 'β ' || 'π¬' }}", | |
"status": "${{ github.event.review.state == 'approved' && 'code review approved' || 'code review comments' }}" | |
}, | |
"closed": { | |
"icon": "${{ github.event.pull_request.merged && 'π' || 'ποΈ' }}", | |
"status": "${{ github.event.pull_request.merged && 'merged' || 'closed' }}" | |
} | |
} | |
jobs: | |
notify: | |
name: Slack notify | |
runs-on: ubuntu-24.04 | |
if: ${{ !contains(github.event.pull_request.head.ref || github.head_ref || github.ref, 'dependabot/') }} | |
steps: | |
- name: Build Slack JSON | |
uses: actions/github-script@v7 | |
id: payload | |
if: github.event.action != 'submitted' || github.event.review.state == 'approved' | |
env: | |
ACTION_ICON: ${{ fromJSON(env.CONFIG)[github.event.action].icon }} | |
ACTION_STATUS: ${{ fromJSON(env.CONFIG)[github.event.action].status }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const title = ${{ toJson(github.event.pull_request.title) }} | |
const username = ${{ toJson(github.event.pull_request.user.login) }} | |
const profileUrl = ${{ toJson(github.event.pull_request.user.html_url) }} | |
const { pull_request } = context.payload | |
const { owner, repo } = context.repo | |
// Format as Slack mrkdwn | |
const titleFormatted = `<${pull_request.html_url}|*${title}* #${pull_request.number}>` | |
const profileFormatted = `<${profileUrl}|@${username}>` | |
// Payload output is JSON encoded | |
return { | |
blocks: [ | |
{ | |
type: 'section', | |
text: { | |
type: 'mrkdwn', | |
text: `${owner}/*${repo}*: ${titleFormatted} byΒ ${profileFormatted} ${{ env.ACTION_ICON }}Β ${{ env.ACTION_STATUS }}` | |
} | |
} | |
], | |
unfurl_links: false, | |
unfurl_media: false | |
} | |
- name: Post to Slack | |
uses: slackapi/[email protected] | |
if: github.event.action != 'submitted' || github.event.review.state == 'approved' | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | |
with: | |
channel-id: ${{ vars.SLACK_CHANNEL_ID }} | |
payload: ${{ steps.payload.outputs.result }} |