Skip to content

Commit

Permalink
Omit pr author from comment notification on slack (#4)
Browse files Browse the repository at this point in the history
* find author

* try with curl

* add last

* replace scretes with token

* debug more

* add issue author

* add new step

* add new step

* add debug

* refactor

* fix syntax

* debug
  • Loading branch information
rahulpatidar0191 authored May 3, 2024
1 parent d46797f commit 156cbed
Showing 1 changed file with 91 additions and 64 deletions.
155 changes: 91 additions & 64 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,101 @@ inputs:
SLACK_WEBHOOK_URL:
description: Webhook for the slack app
required: true

runs:
using: "composite"
steps:
- name: Find slack user email
run: |
github_username=${{ github.event.pull_request.user.login }}${{ github.event.issue.user.login }} #only one of this value is accessible at a time
# Define mapping for frontend devs
declare -A mapping=(
[ZoeElizabeth][email protected]
[shallow-and-limited][email protected]
[CaseyQWood][email protected]
)
# Get Slack username
slack_user_email=${mapping[$github_username]:-Unknown}
if [ "$slack_user_email" != "Unknown" ]; then
echo "slack_user_email_found=true" >> $GITHUB_ENV
fi
- name: Check event type and set variables
id: check_event_type
run: |
event_type="${{ github.event_name }}"
if [ "$event_type" == "issue_comment" ]; then
issue_number="${{ github.event.issue.number }}"
event_name='issues'
username="${{ github.event.issue.user.login }}"
else
issue_number="${{ github.event.pull_request.number }}"
event_name='pulls'
username="${{ github.event.pull_request.user.login }}"
fi
echo "issue_number=$issue_number" >> $GITHUB_ENV
echo "event_name=$event_name" >> $GITHUB_ENV
echo "username=$username" >> $GITHUB_ENV
shell: bash

- name: Check for comment author
id: check_comment_author
run: |
last_comment_author=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ github.repository }}/${{ env.event_name }}/${{ env.issue_number }}/comments" --jq '.[-1].user.login')
echo "last_comment_author=$last_comment_author" >> $GITHUB_ENV
if [ "${{ env.username }}" != "$last_comment_author" ]; then
echo "slack_trigger=true" >> $GITHUB_ENV
fi
env:
GH_TOKEN: ${{ github.token }}
shell: bash

- name: Find slack user email
if: env.slack_trigger == 'true'
id: find_slack_user_email
run: |
# Define mapping for frontend devs
declare -A mapping=(
[ZoeElizabeth][email protected]
[shallow-and-limited][email protected]
[CaseyQWood][email protected]
)
github_username="${{ env.username }}"
echo "github_username" $github_username
slack_user_email=${mapping[$github_username]:-Unknown}
echo "slack_user_email" $slack_user_email
if [ "$slack_user_email" != "Unknown" ]; then
echo "slack_user_email_found=true" >> $GITHUB_ENV
echo "slack_user_email=$slack_user_email" >> $GITHUB_ENV
shell: bash

- name: Find Slack user based on the emails
id: find-slack-user
if: ${{ env.slack_user_email_found == 'true' }}
uses: scribd/[email protected]
with:
slack-token: ${{ inputs.SLACK_API_TOKEN }}
email: ${{ env.slack_user_email }}
include-at-symbol: true
fi
shell: bash

- name: Find Slack user based on the emails
if: env.slack_user_email_found == 'true'
id: find_slack_user
uses: scribd/[email protected]
with:
slack-token: ${{ inputs.SLACK_API_TOKEN }}
email: ${{ env.slack_user_email }}
include-at-symbol: true

- name: Get PR comment URL
if: ${{ env.slack_user_email_found == 'true' }}
id: get-comment
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "${{ github.event_name }}" == "issue_comment" ]; then
ISSUE_NUMBER="${{ github.event.issue.number }}"
event_name='issues'
else
ISSUE_NUMBER="${{ github.event.pull_request.number }}"
event_name='pulls'
fi
last_comment_url=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ github.repository }}/$event_name/$ISSUE_NUMBER/comments" --jq '.[-1].html_url')
echo "last_comment_url=$last_comment_url" >> $GITHUB_ENV
shell: bash
- name: Get PR comment URL
if: env.slack_user_email_found == 'true'
id: get_comment
run: |
last_comment_url=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ github.repository }}/${{ env.event_name }}/${{ env.issue_number }}/comments" --jq '.[-1].html_url')
echo "last_comment_url=$last_comment_url" >> $GITHUB_ENV
env:
GH_TOKEN: ${{ github.token }}
shell: bash

- name: Slack notification
if: ${{ env.slack_user_email_found == 'true' }}
uses: 8398a7/action-slack@v3
with:
status: custom
custom_payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi ${{ steps.find-slack-user.outputs.username }}, there's a comment on your PR. Please review it. Here's the <${{ env.last_comment_url }}|link>"
}
- name: Slack notification
if: env.slack_user_email_found == 'true'
uses: 8398a7/action-slack@v3
with:
status: custom
custom_payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi ${{ steps.find_slack_user.outputs.username }}, there's a comment on your PR. Please review it. Here's the <${{ env.last_comment_url }}|link>"
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ inputs.SLACK_WEBHOOK_URL }}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ inputs.SLACK_WEBHOOK_URL }}

0 comments on commit 156cbed

Please sign in to comment.