Skip to content

Commit

Permalink
Slack notification support implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
palani-sayari committed Nov 18, 2024
1 parent 4ef38c4 commit 698d7d3
Showing 1 changed file with 98 additions and 4 deletions.
102 changes: 98 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}

on: [push]
permissions:
contents: read # Allows read access to the code in the repository

on:
push:
schedule:
- cron: '0 0 * * *' # Runs every midnight (00:00 UTC)
workflow_dispatch: # Allows manual triggering

jobs:
compile:
Expand All @@ -25,7 +32,7 @@ jobs:
- name: Set up node
uses: actions/setup-node@v4
- name: Compile & Test
run: yarn && yarn test
run: yarn && yarn test

run-examples:
name: Run examples (to look for regressions)
Expand All @@ -39,7 +46,7 @@ jobs:
node-version: 21
- name: Build
run: yarn && yarn build
- name: test examples
- name: Test examples
run: |
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Running hello world"
Expand All @@ -52,7 +59,7 @@ jobs:
node examples/trade-search.mjs
publish:
needs: [ compile, test, run-examples ]
needs: [compile, test, run-examples]
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
Expand All @@ -70,3 +77,90 @@ jobs:
npm publish --access public
env:
NPM_TOKEN: ${{ secrets.SDK_NPM_TOKEN }}

notify-slack-ci:
name: Notify Slack - CI Jobs Completed
needs: [compile, test, run-examples]
runs-on: ubuntu-latest
if: success()
env:
GITHUB_TOKEN: ${{ github.token }}
WEBHOOK_URL: ${{ secrets.GH_ACTION_WEBHOOK_URL }}
GITHUB_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
REPOSITORY: ${{ github.repository }}
steps:
- name: Check for PR on the branch and get reviewers
id: pr_check
run: |
# Fetch open PRs for the branch using GitHub API
PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${{ github.ref_name }}&state=open" \
| jq '.[0].number // empty')
if [ -n "$PR_NUMBER" ]; then
echo "This branch has an open pull request: #$PR_NUMBER"
echo "::set-output name=pr_number::$PR_NUMBER"
# Fetch reviewers for the PR
REVIEWERS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/requested_reviewers" \
| jq -r '.users[].login' | sed 's/^/@/g' | paste -sd ", ")
echo "::set-output name=reviewers::$REVIEWERS"
else
echo "No open pull requests for this branch."
echo "::set-output name=pr_number::"
echo "::set-output name=reviewers::"
fi
- name: Send Slack Notification
run: |
# Branch, PR, and reviewers info
BRANCH=${{ github.ref_name }}
PR_NUMBER="${{ steps.pr_check.outputs.pr_number }}"
REVIEWERS="${{ steps.pr_check.outputs.reviewers }}"
# Construct PR link if PR_NUMBER is set
if [ -n "$PR_NUMBER" ]; then
PULL_REQUEST_INFO="Pull-Request: <https://github.com/${REPOSITORY}/pull/${PR_NUMBER}|#${PR_NUMBER}>"
else
PULL_REQUEST_INFO=""
fi
# Include reviewers if available
if [ -n "$REVIEWERS" ]; then
REVIEWERS_INFO="Reviewers: $REVIEWERS"
else
REVIEWERS_INFO=""
fi
# Slack message format
MESSAGE="CI Build for Branch : *${BRANCH}* (${REPOSITORY})\nCommit Author : @${{ github.actor }}\n*Click <${GITHUB_RUN_URL}|here> for the job URL*"
# Append pull request info and reviewers if they exist
if [ -n "$PULL_REQUEST_INFO" ]; then
MESSAGE="$MESSAGE\n${PULL_REQUEST_INFO}"
fi
if [ -n "$REVIEWERS_INFO" ]; then
MESSAGE="$MESSAGE\n${REVIEWERS_INFO}"
fi
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$MESSAGE\"}" $WEBHOOK_URL
notify-slack-publish:
name: Notify Slack - Publish Job Completed
needs: [publish]
runs-on: ubuntu-latest
if: success()
env:
WEBHOOK_URL: ${{ secrets.GH_ACTION_WEBHOOK_URL }}
GITHUB_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
steps:
- name: Send Slack Notification
run: |
BRANCH=${{ github.ref_name }}
MESSAGE="Publish job for branch *${BRANCH}* completed successfully. Package is now available. See details here: <${GITHUB_RUN_URL}|Click here>"
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$MESSAGE\"}" $WEBHOOK_URL

0 comments on commit 698d7d3

Please sign in to comment.