Last active
March 6, 2025 11:53
-
-
Save lgersman/a0954b5e714feea9ce3190369031d176 to your computer and use it in GitHub Desktop.
github workflow to create pr messages in a google chat
This file contains hidden or 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 is triggered by pull requests and will notify a google chat room about pull request events | |
# | |
# this workflow requires a secret "GCHAT_PR_ANNOUNCEMENTS_WEBHOOK" to point to the google chat room | |
# if not declared his workflow will set a warning and will exit gracefully (=> without an error) | |
# | |
# TODO: we cannot react to single review comments in github pipelines as of now | |
# See "Add events for pull request review comment resolved/unresolved" https://github.com/orgs/community/discussions/12811 | |
# | |
name: 'gchat-notify-pull-request' | |
on: | |
pull_request: | |
types: | |
- opened | |
- closed | |
- reopened | |
- converted_to_draft | |
- ready_for_review | |
- review_requested | |
- review_request_removed | |
- edited | |
pull_request_review: | |
types: | |
- submitted | |
- edited | |
- dismissed | |
pull_request_review_comment: | |
types: | |
- created | |
- edited | |
- deleted | |
issue_comment: | |
jobs: | |
send_notification: | |
runs-on: ubuntu-latest | |
name: Send message to gchat | |
steps: | |
- id: gchat_url | |
run: | | |
if [[ -z "${{ secrets.GCHAT_PR_ANNOUNCEMENTS_WEBHOOK }}" ]]; then | |
echo "::warning::skip sending google chat pull request message : secret GCHAT_PR_ANNOUNCEMENTS_WEBHOOK is not defined" | |
else | |
# compute google chat url | |
THREAD_KEY=$(echo '${{ github.event.pull_request.number || github.event.issue.number }}' | jq -sRr @uri) | |
echo "GCHAT_URL=${{ secrets.GCHAT_PR_ANNOUNCEMENTS_WEBHOOK }}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD&thread_key=$THREAD_KEY" \ | |
>> $GITHUB_OUTPUT | |
fi | |
- name: send pr open/close/edit message | |
if: ${{ github.event_name == 'pull_request' && steps.gchat_url.outputs.GCHAT_URL }} | |
run: | | |
TEXT=$(cat -s <<'X!X' | jq -Rsr @json | |
${{ github.triggering_actor}} ${{ github.event.action}} pull request *'${{ github.event.pull_request.title }} ${{ github.event.pull_request.draft && '(draft)' || '' }}'* | |
${{ (github.event.action=='opened' || github.event.action=='edited') && github.event.pull_request.body || '' }} | |
${{ github.event.pull_request._links.html.href }} | |
X!X | |
) | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "{\"text\": $TEXT }" \ | |
'${{ steps.gchat_url.outputs.GCHAT_URL }}' | |
- name: send pr review | |
# this step will send a message if a review comment is made on a pull request review | |
if: ${{ github.event_name == 'pull_request_review' && steps.gchat_url.outputs.GCHAT_URL }} | |
run: | | |
TEXT=$(cat -s <<'X!X' | jq -Rsr @json | |
${{ github.triggering_actor }} ${{ github.event.action }} a review comment | |
${{ github.event.review.body || github.event.comment.body || '' }} | |
${{ github.event.review._links.html.href }} | |
X!X | |
) | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "{\"text\": $TEXT }" \ | |
'${{ steps.gchat_url.outputs.GCHAT_URL }}' | |
- name: send pr review comment | |
# this step will send a message if a comment is made on a pull request review | |
if: ${{ github.event_name == 'pull_request_review_comment' && steps.gchat_url.outputs.GCHAT_URL }} | |
run: | | |
TEXT=$(cat -s <<'X!X' | jq -Rsr @json | |
${{ github.triggering_actor }} ${{github.event.action }} a review comment | |
${{ github.event.review.body || github.event.comment.body || '' }} | |
${{ github.event.comment._links.html.href }} | |
X!X | |
) | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "{\"text\": $TEXT }" \ | |
'${{ steps.gchat_url.outputs.GCHAT_URL }}' | |
- name: send pr comment message | |
# this step will send a message if a comment is made on a pull request review | |
if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request && steps.gchat_url.outputs.GCHAT_URL }} | |
run: | | |
TEXT=$(cat -s <<'X!X' | jq -Rsr @json | |
${{ github.triggering_actor }} ${{ github.event.action }} a pr comment | |
${{ github.event.comment.body || ''}} | |
${{ github.event.comment.html_url }} | |
X!X | |
) | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "{\"text\": $TEXT }" \ | |
'${{ steps.gchat_url.outputs.GCHAT_URL }}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment