Search code examples
github-actions

How to get all the changes of a Pull Request when triggering on pull_request_review?


I currently have a GitHub Action that triggers on:

  pull_request_review:
    types: [submitted]

I then want to run a command, which expects the contents of changes of the Pull Request.

Previously, I was using

on: 
  push

and I had no issues with the contents of the files being available in the Action context.

However, my command is failing now, and I think it's because the context only includes the commit that the action was triggered on (no file changes.) Previously I was running this action on push and that was always successful, with the file changes being available in the context.

I'm using:

    steps:
      - uses: actions/checkout@v2

(https://github.com/actions/checkout)

Is it possible to use this to have all the file changes on the Pull Request within the Action context?

Any help on this would be appreciated!


Solution

  • You can do that by using an open source Action available on marketplace:

    jobs:
      build:
        runs-on: ubuntu-latest  # windows-latest | macos-latest
        name: Test changed-files
        steps:
          - uses: actions/checkout@v2
            with:
              fetch-depth: 0  # OR "2" -> To retrieve the preceding commit.
    
          - name: Get changed files
            id: changed-files
            uses: tj-actions/[email protected]
    
          - name: List all changed files
            run: |
              for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
                echo "$file was changed"
              done
    

    The solution above uses git checkout and git diff to get files changed by PR. Alternatively if you really need just information about paths changed and you don't really need files themselves (no checkout) - you can do it without checkout using gh CLI:

    gh pr view XXX --json files -q '.files[].path'
    

    You can run it like this:

    jobs:
      comment:
        runs-on: ubuntu-latest
        steps:
          - run: gh pr view XXX --json files -q '.files[].path'
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}