Get list of files on pull request merge

If I have the following workflow to trigger on a pull request merge:

name: deployment

on:
  pull_request:
    types:
      - closed

jobs:
  job1:
    runs-on: ubuntu-latest

    steps:
    - name: Check out
      uses: actions/checkout@v1

    - name: get list of files in PR
      if: github.event.pull_request.merged
      run: |
        ???

What is the best way to get a list of files in the PR?

You can use rest api to get the file list of pull request. Please refer to the link for more details.

Code sample as below, use jq to filter the filename.

- name: list files
        run: |
          URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${{ github.event.pull_request.number }}/files"
          FILES=$(curl -s -X GET -G $URL | jq -r '.[] | .filename')
          echo $FILES

2 Likes

Hi @weide-zhou. Your snippet was very helpful, but I’m having some trouble and I don’t understand why, could you help me?

1 Like

Hi @luchiago ,

The error is due to no json file changes in your pull request, which caused command ’FILES=$(curl … | | grep -E ‘.(json)$’)‘ to be failed.

You can check in this way:

- id: changes
        run: |
          URL="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files"
          FILES=$(curl -s -X GET -G $URL | jq -r '.[] | .filename')
          if echo $FILES | grep -q ".json"; then
            echo "json file changed!!"
          else
            echo "no json file changed!!"
          fi

Thanks.

1 Like

Thanks @weide-zhou. It worked

Hi @weide-zhou Thanks for your reply. It’s very helpful but I’m getting error like
Cannot index string with string "filename"

Thanks
Khanh

Hi @self-coding-crab,

I notice you parsed ‘.json’ in your jq filter, it should be ‘.filename’ from the curl command. I confirmed the script works on my side.

Basically you can check the curl command without jq, check the response body, and then use jq to filter the file.

If i checked the incorrect workflow, please let me know.

Thanks