Search code examples
github-actions

How to make a Github Actions workflow update a Check on a Pull Request comment?


I am trying to use GitHub Actions to validate the book-keeping side of pull requests. Basically, the idea is that merging should be blocked unless certain tags, milestones, and other information is present in the PR. The logic I am currently struggling with is this: The PR needs to have one of two labels, "no release notes" or "public release notes" and if the "public release notes" label is present, then a specially formatted comment should be present with the release notes in question.

I have succeeded in getting the action to fire and update the check when the PR is created, or a label is added or removed. These paths modify the check status on the PR itself. A PR with a successful check, showing the check was triggered by pull_request events

However, while I can get the Action to run when I add a PR comment (issue comment) this does not seem to update the check status. Is it possible to use an issue comment event to modify the check status of the PR directly?

The YML for the action is:

name: Github PR Audit

on:
  pull_request:
    types:
      - opened
      - edited
      - labeled
      - unlabeled
      
  issue_comment:
    types:
      - created
      - edited
      - deleted
jobs:
  Audit-Pull-Request:
    runs-on: ubuntu-latest
    steps:

Solution

  • You can use the GitHub Script GH Action + Branch protection rules configuration.

    GitHub Script Action provides an easy and elegant way to run scripts in your workflow and Branch protection rules allow configuring which status checks must pass before branches can be merged.

    Example workflow:

    name: Github PR Audit
    
    on:
      pull_request:
        types:
          - opened
          - edited
          - labeled
          - unlabeled
    
      issue_comment:
        types:
          - created
          - edited
          - deleted
    
    jobs:
      Audit-Pull-Request:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/github-script@v6
            with:
              script: |
                const requiredLabels = ['no release notes', 'public release notes'];
                let labels = [];
    
                if (context.payload.pull_request) {
                  labels = context.payload.pull_request.labels;
                } else if (context.payload.issue) {
                  labels = context.payload.issue.labels;
                }
    
                if (labels.filter(l => requiredLabels.includes(l.name)).length === 0) {
                  throw new Error(`Required labels: ${requiredLabels.join(', ')}`);
                }
    
    

    This script will check if its context has the corresponding labels and will fail if not.

    Example failing run:

    run

    Branch protection rule configuration:

    rule