Search code examples
githubgithub-actionspushgithub-apipull-request

GitHub Actions, Send Notification When Bypassing Branch Protections


I'm trying to setup a GitHub action that triggers when a pull request is merged that didn't pass all status checks

This is what I have at the moment

name: Alert Merged Failed Checks
on: 
  push:
    branches:
      - main
      - develop
jobs:
  alert-if-merged-failed-checks:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      # Get the commit that checks were run on (non-merge commit)
      - name: Get commit 
        id: get_commit
        run: |
          commit_hash=$(git log -n 1 ${{ github.event.after }}^2 --format='%H')
          echo "commit_hash=$commit_hash" >> $GITHUB_OUTPUT
          echo "commit_hash=$commit_hash"
      # Get the status of the commit
      - name: GitHub API Request
        uses: octokit/[email protected]
        id: get_check_status
        with:
          route: GET /repos/{owner}/{repo}/commits/{ref}/status
          owner: ***
          repo: ***
          ref: ${{ steps.get_commit.outputs.commit_hash }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - run: "echo check status: ${{ steps.get_check_status.outputs.data }}"
      - run: "echo help"

but the result of the GitHub API Request was

{
    "state": "pending",
    "statuses": [],
    "total_count": 0,
    ...
}

and when I look at the relevant commit with the same SHA in GitHub I see

commit with failed checks

I was expecting the "state" to be "failure" per https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28


Solution

  • I believe what you are looking for is to list check-runs for a git reference instead: /repos/{owner}/{repo}/commits/{ref}/check-runs

    For instance:

    $ curl -s https://api.github.com/repos/rffontenelle/test-gh-workflow/commits/0b542d5e61957f8799450ba9fcfbd435d9811888/check-runs | jq .check_runs[].conclusion
    "success"