Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/shared-slack-logic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Slack Auto-React (Approval & Merge)

on:
workflow_call:
inputs:
SLACK_CHANNEL_ID:
required: true
type: string

jobs:
auto_react:
if: |
(github.event_name == 'pull_request' && github.event.pull_request.merged == true) ||
(github.event_name == 'pull_request_review' && github.event.review.state == 'approved')

runs-on: ubuntu-latest
Comment thread
trumanfolkersTZ marked this conversation as resolved.

steps:
- name: Search and React to Slack Message
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ inputs.SLACK_CHANNEL_ID }}
PR_URL: ${{ github.event.pull_request.html_url }}
EMOJI: ${{ github.event_name == 'pull_request' && 'git-merged' || 'white_check_mark' }}
run: |
HISTORY=$(curl -s -X GET \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
"https://slack.com/api/conversations.history?channel=$SLACK_CHANNEL_ID&limit=100")

if [ "$(echo "$HISTORY" | jq -r '.ok')" != "true" ]; then
echo "Slack API Error: $(echo "$HISTORY" | jq -r '.error')"
exit 1
fi

TS=$(echo "$HISTORY" | jq -r --arg URL "$PR_URL" '.messages[]? | select(.text | contains($URL)) | .ts' | head -n 1)

if [ -n "$TS" ] && [ "$TS" != "null" ]; then
echo "Found matching message (TS: $TS). Adding :$EMOJI: reaction..."
curl -s -X POST \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"channel\":\"$SLACK_CHANNEL_ID\",\"timestamp\":\"$TS\",\"name\":\"$EMOJI\"}" \
https://slack.com/api/reactions.add > /dev/null
Comment thread
trumanfolkersTZ marked this conversation as resolved.
else
echo "No matching Slack message found for PR: $PR_URL"
fi
43 changes: 6 additions & 37 deletions workflow-templates/slack-react.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Slack Auto-React (Approval & Merge)
name: Slack Auto-React

on:
pull_request:
Expand All @@ -7,39 +7,8 @@ on:
types: [submitted]

jobs:
auto_react:
if: |
(github.event_name == 'pull_request' && github.event.pull_request.merged == true) ||
(github.event_name == 'pull_request_review' && github.event.review.state == 'approved')

runs-on: ubuntu-latest

steps:
- name: Search and React to Slack Message
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: "YOUR_SLACK_CHANNEL_ID_HERE"
PR_URL: ${{ github.event.pull_request.html_url }}
EMOJI: ${{ github.event_name == 'pull_request' && 'git-merged' || 'white_check_mark' }}
run: |
HISTORY=$(curl -s -X GET \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
"https://slack.com/api/conversations.history?channel=$SLACK_CHANNEL_ID&limit=100")

if [ "$(echo "$HISTORY" | jq -r '.ok')" != "true" ]; then
echo "Slack API Error: $(echo "$HISTORY" | jq -r '.error')"
exit 1
fi

TS=$(echo "$HISTORY" | jq -r --arg URL "$PR_URL" '.messages[]? | select(.text | contains($URL)) | .ts' | head -n 1)

if [ -n "$TS" ] && [ "$TS" != "null" ]; then
echo "Found matching message (TS: $TS). Adding :$EMOJI: reaction..."
curl -s -X POST \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"channel\":\"$SLACK_CHANNEL_ID\",\"timestamp\":\"$TS\",\"name\":\"$EMOJI\"}" \
https://slack.com/api/reactions.add > /dev/null
else
echo "No matching Slack message found for PR: $PR_URL"
fi
trigger-slack-reaction:
uses: tractorzoom/.github/.github/workflows/shared-slack-logic.yml@main
with:
SLACK_CHANNEL_ID: "SLACK_CHANNEL_ID_HERE"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assumes PRs for a given repo all go to same channel. Assumption is true probably 98% of the time but occasionally we'll cross different channels. Also, will this work for private channels? E.g. #the-room

secrets: inherit
Comment thread
trumanfolkersTZ marked this conversation as resolved.