Skip to content
Open
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
191 changes: 108 additions & 83 deletions .github/workflows/pr_notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,122 @@ on:
pull_request:
types: [review_requested]

permissions:
contents: read

jobs:
notify-pull-request:
runs-on: ubuntu-latest
steps:
- name: Pull Request Details
run: |
echo "Pull Request: ${{ github.event.pull_request.number }}"
echo "Author: ${GITHUB_EVENT_PULL_REQUEST_USER_LOGIN}"
env:
GITHUB_EVENT_PULL_REQUEST_USER_LOGIN: ${{ github.event.pull_request.user.login }}
- name: Pull Request Details
run: |
echo "Pull Request: ${PR_NUMBER}"
echo "Author: ${AUTHOR}"
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
AUTHOR: ${{ github.event.pull_request.user.login }}

- name: Google Chat Notification
shell: bash
env:
TITLE: ${{ github.event.pull_request.title }}
LABELS: ${{ join(github.event.pull_request.labels.*.name, ', ') }}
GITHUB_EVENT_PULL_REQUEST_HEAD_REPO_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }}
GITHUB_EVENT_PULL_REQUEST_USER_LOGIN: ${{ github.event.pull_request.user.login }}
GITHUB_EVENT_PULL_REQUEST_HTML_URL: ${{ github.event.pull_request.html_url }}
run: |
curl --location --request POST '${{ secrets.WEBHOOK_URL }}' \
--header 'Content-Type: application/json' \
--data-raw '{
"cards": [
{
"header": {
"title": "Pull request notification",
"subtitle": "Pull request: #${{ github.event.pull_request.number }}"
},
"sections": [
- name: Google Chat Notification
shell: bash
env:
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this will always be nil on PRs that come in from forks, can you add a quick check for WEBHOOK_URL that will skip the workflow if nil?

PR_NUMBER: ${{ github.event.pull_request.number }}
TITLE: ${{ github.event.pull_request.title }}
LABELS: ${{ join(github.event.pull_request.labels.*.name, ', ') }}
REPO: ${{ github.event.pull_request.head.repo.full_name }}
CREATOR: ${{ github.event.pull_request.user.login }}
STATE: ${{ github.event.pull_request.state }}
ASSIGNEES: ${{ join(github.event.pull_request.assignees.*.login, ', ') }}
REVIEWERS: ${{ join(github.event.pull_request.requested_reviewers.*.login, ', ') }}
URL: ${{ github.event.pull_request.html_url }}
run: |
PAYLOAD=$(jq -n \
--arg pr_num "$PR_NUMBER" \
--arg title "$TITLE" \
--arg labels "$LABELS" \
--arg repo "$REPO" \
--arg creator "$CREATOR" \
--arg state "$STATE" \
--arg assignees "$ASSIGNEES" \
--arg reviewers "$REVIEWERS" \
--arg url "$URL" \
'{
cardsV2: [
{
"widgets": [
{
"keyValue": {
"topLabel": "Repo",
"content": "${GITHUB_EVENT_PULL_REQUEST_HEAD_REPO_FULL_NAME}"
}
},
{
"keyValue": {
"topLabel": "Title",
"content": "'"$TITLE"'"
}
},
{
"keyValue": {
"topLabel": "Creator",
"content": "${GITHUB_EVENT_PULL_REQUEST_USER_LOGIN}"
}
},
{
"keyValue": {
"topLabel": "State",
"content": "${{ github.event.pull_request.state }}"
}
},
{
"keyValue": {
"topLabel": "Assignees",
"content": "- ${{ join(github.event.pull_request.assignees.*.login, ', ') }}"
}
},
{
"keyValue": {
"topLabel": "Reviewers",
"content": "- ${{ join(github.event.pull_request.requested_reviewers.*.login, ', ') }}"
}
cardId: "prNotificationCard",
card: {
header: {
title: "Pull request notification",
subtitle: ("Pull request: #" + $pr_num)
},
{
"keyValue": {
"topLabel": "Labels",
"content": "- '"$LABELS"'"
}
},
{
"buttons": [
{
"textButton": {
"text": "Open Pull Request",
"onClick": {
"openLink": {
"url": "${GITHUB_EVENT_PULL_REQUEST_HTML_URL}"
}
sections: [
{
widgets: [
{
decoratedText: {
topLabel: "Repo",
text: $repo
}
},
{
decoratedText: {
topLabel: "Title",
text: $title
}
},
{
decoratedText: {
topLabel: "Creator",
text: $creator
}
},
{
decoratedText: {
topLabel: "State",
text: $state
}
},
{
decoratedText: {
topLabel: "Assignees",
text: (if $assignees != "" then "- " + $assignees else "None" end)
}
},
{
decoratedText: {
topLabel: "Reviewers",
text: (if $reviewers != "" then "- " + $reviewers else "None" end)
}
},
{
decoratedText: {
topLabel: "Labels",
text: (if $labels != "" then "- " + $labels else "None" end)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please do a little helper function to add this guard on every text in both files - for example, I think it's unlikely that "github.event.head_commit.author.username" will be nil, but it is possible, which would cause this to fail. text is required by v2.

}
},
{
buttonList: {
buttons: [
{
text: "Open Pull Request",
onClick: {
openLink: {
url: $url
}
}
}
]
}
}
}
]
}
]
]
}
]
}
}
]
}
]
}'
}')

curl --location --request POST "$WEBHOOK_URL" \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we want this to fail a PR, but I do think we want it to indicate that something failed. For both curl commands, you could do all of: a) add --fail-with-body b) Capture the error output, and echo it in the log and c) add continue-on-error: true

--header 'Content-Type: application/json; charset=UTF-8' \
--data "$PAYLOAD"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Why the change to --data instead of --data-raw?


123 changes: 69 additions & 54 deletions .github/workflows/push_notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,84 @@ on:
branches:
- main

permissions:
contents: read

jobs:
notify-push-main:
runs-on: ubuntu-latest
env:
COMMIT: ${{ github.event.head_commit.message }}
steps:
- name: Main Branch Push
run: |
echo "Workflow initiated by event with name: ${{ github.event_name }}"
echo "Pushing commit to main: ${GITHUB_EVENT_HEAD_COMMIT_ID}"
echo "Pushed by: ${GITHUB_EVENT_PUSHER_NAME}"
env:
GITHUB_EVENT_HEAD_COMMIT_ID: ${{ github.event.head_commit.id }}
GITHUB_EVENT_PUSHER_NAME: ${{ github.event.pusher.name }}
- name: Main Branch Push
run: |
echo "Workflow initiated by event with name: ${EVENT_NAME}"
echo "Pushing commit to main: ${HEAD_COMMIT_ID}"
echo "Pushed by: ${PUSHER_NAME}"
env:
EVENT_NAME: ${{ github.event_name }}
HEAD_COMMIT_ID: ${{ github.event.head_commit.id }}
PUSHER_NAME: ${{ github.event.pusher.name }}

- name: Push Notification to Google Chat
run: |
curl --location --request POST '${{ secrets.WEBHOOK_URL }}' \
--header 'Content-Type: application/json' \
--data-raw '{
"cards": [
{
"header": {
"title": "Push to main branch",
"subtitle": "'"$COMMIT"'"
},
"sections": [
- name: Push Notification to Google Chat
shell: bash
env:
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
COMMIT_MSG: ${{ github.event.head_commit.message }}
AUTHOR: ${{ github.event.head_commit.author.username }}
REPO: ${{ github.event.repository.full_name }}
COMPARE_URL: ${{ github.event.compare }}
run: |
PAYLOAD=$(jq -n \
--arg commit "$COMMIT_MSG" \
--arg author "$AUTHOR" \
--arg repo "$REPO" \
--arg compare "$COMPARE_URL" \
'{
cardsV2: [
{
"widgets": [
{
"keyValue": {
"topLabel": "Repo",
"content": "${GITHUB_EVENT_REPOSITORY_FULL_NAME}"
}
},
{
"keyValue": {
"topLabel": "Committed by",
"content": "${GITHUB_EVENT_HEAD_COMMIT_AUTHOR_USERNAME}"
}
cardId: "pushNotificationCard",
card: {
header: {
title: "Push to main branch",
subtitle: $commit
},
{
"buttons": [
{
"textButton": {
"text": "Ref comparison",
"onClick": {
"openLink": {
"url": "${GITHUB_EVENT_COMPARE}"
}
sections: [
{
widgets: [
{
decoratedText: {
topLabel: "Repo",
text: $repo
}
},
{
decoratedText: {
topLabel: "Committed by",
text: $author
}
},
{
buttonList: {
buttons: [
{
text: "Ref comparison",
onClick: {
openLink: {
url: $compare
}
}
}
]
}
}
}
]
}
]
]
}
]
}
}
]
}
]
}'
env:
GITHUB_EVENT_REPOSITORY_FULL_NAME: ${{ github.event.repository.full_name }}
GITHUB_EVENT_HEAD_COMMIT_AUTHOR_USERNAME: ${{ github.event.head_commit.author.username }}
GITHUB_EVENT_COMPARE: ${{ github.event.compare }}
}')

curl --location --request POST "$WEBHOOK_URL" \
--header 'Content-Type: application/json; charset=UTF-8' \
--data "$PAYLOAD"

Loading