transposeglobal
transposeglobal

Reputation: 531

In a Github Action, how to get diff of files from a Pull Request AFTER Closing/Merging?

I'm trying to run some code in a GitHub Action that depends on files that were merged in to our develop branch. I can't seem to find the right environment variables or event values to get the diff of the pull request after it has been merged. I have tried running this on push to the develop branch as well as on a pull request with type: closed and a conditional on whether the pull request was merged.

Here are two things I've tried:

closed pull request:

name: test_pr_action
on:
  pull_request:
    types: 
      - closed

jobs:
  job1:
    if: github.event.pull_request.merged == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}
      - name: one step
        run: |
          git fetch origin $GITHUB_BASE_REF --depth=1
          git diff --diff-filter=AM --name-only ${{ github.event.base.sha }} ${{ github.event.head.sha }}

For the pull request, the step runs but does not pick up the SHAs so does not output anything.

push:

name: test_pr_action

on:
  push:
    branches:
      - main

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}
      - name: one step
        run: |
          git fetch origin $GITHUB_BASE_REF --depth=1
          git diff --diff-filter=AM --name-only ${{ github.event.before }} ${{ github.event.after }}

For the push, the step fails because it doesn't like the first SHA.

I already have actions that run on push and on pull request BEFORE merge, so I have used these values before, but can't get it to work for this case.

Any advice helps! Thanks!

Upvotes: 3

Views: 5648

Answers (1)

transposeglobal
transposeglobal

Reputation: 531

Thanks to @GuiFalourd I was able to get this to work with the changed-files action.

My code ended up looking something like this:

name: action1
on:
  push:
    branches: [ develop ]

jobs:
  job1:
    runs-on: machine1
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}
      - name: Get changed files
        id: changed-files
        uses: tj-actions/[email protected]
        with:
          since_last_remote_commit: 'true'
      - name: get_modified_files
        id: diff
        working-directory: './diff_files'
        run: |
          git config diff.renameLimit 99000
          git fetch origin $GITHUB_BASE_REF --depth=1
          touch files.txt
          for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
            echo $file >> files.txt
          done
          cat files.txt | split -l 100 -a 8
          rm files.txt
          export filename_string=""
          for i in `ls`; do export filename_string=$filename_string"file!"$i; done
          echo "::set-output name=filenames::$filename_string"

For anyone wondering what all the extra stuff is, I'm reading each filename from the all_changed_files variable into a separate file, which I cat and split into several other files, which are left in the diff_files directory. This directory is then read by my python script that runs in subsequent steps. I break it up into separate files like this because the PRs in this repo tend to have thousands of file diffs.

Upvotes: 4

Related Questions