0

As a part of PR validation, I need to detect what files have changed in PRs created from forked repositories to my main repository.

Currently, I am using this code:

- uses: actions/checkout@v3
  with:
    fetch-depth: 0
- name: Get changed files
  id: changed-files
  uses: tj-actions/changed-files@v34

But the problem is that it also returns all files modified inside my main repository that were done after PR was opened on top of just Pull Request changes as checkout is doing a merge there. More detailed description of this problem here

I am looking for the best option to get ONLY the files listed in PR "Files changes" tab. I cannot work with GitHub CLI (which will be the easiest) as I don't want to share secrets with forked repositories for that, so I have to rely only on git commands.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • Can you run a git command? That is done with a diff with `...` (3 dots). So, `git diff --name-only target-branch...feature-branch` – eftshift0 Oct 27 '22 at 10:12
  • @eftshift0 it's not between branches - both branches are "main" but those are separate repository - main one and forked one – Grzegorz Krukowski Oct 27 '22 at 13:26
  • That doesn't matter. As long as you have visibility of both branches on a repo (as remote branches, or one local and the other remote), it can be done: `git diff --name-only main-remote/main...forked-remote/main` or `git diff --name-only main-remote/main...main` – eftshift0 Oct 27 '22 at 13:32
  • @eftshift0 I will give it a try - does it make sense then to checkout PR head and add main repository as remote? By default `actions/checkout` is doing merge which I think will mess up everything... – Grzegorz Krukowski Oct 27 '22 at 17:08

1 Answers1

1

I ended up using just this for PRs:

- name: Checkout
  uses: actions/checkout@v3
  with:
     fetch-depth: 2
- name: Get changes
  run: git diff --name-only -r HEAD^1 HEAD | xargs

Seems like it's enough, as checkout is doing a merge commit when checking out pull requests.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • What about using `${{ github.base_ref }}` instead of `HEAD^1` ? https://docs.github.com/en/actions/learn-github-actions/contexts#github-context – ITChap Nov 02 '22 at 15:33