From d3bbce398cbaf0764ace3d105ef3aaa626dae70f Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 14 Nov 2023 21:36:07 +0100 Subject: [PATCH] Improve commit retrieval --- .github/workflows/linter.yml | 39 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 2c0d2b682..ac4d8c3ba 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -14,8 +14,29 @@ jobs: name: RuboCop runs-on: ubuntu-latest steps: - - name: Checkout sources - uses: actions/checkout@v4 + - name: Determine target branch + run: | + target_branch=${{ github.base_ref || 'mampf-next' }} + echo "Target branch is: $target_branch" + echo "TARGET_BRANCH=$target_branch" >> $GITHUB_ENV + + # for the " + 1", see: + # https://github.com/actions/checkout/issues/552#issuecomment-1167086216 + - name: Calculate fetch depth + run: | + depth=$(( ${{ github.event.pull_request.commits }} + 1 )) + echo "Fetch depth is: $depth" + echo "PR_FETCH_DEPTH=$depth" >> $GITHUB_ENV + + - name: 'Checkout PR branch and all PR commits' + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.ref }} + fetch-depth: ${{ env.PR_FETCH_DEPTH }} + + - name: Fetch target branch with enough history for a common merge-base commit + run: | + git fetch origin ${{ github.event.pull_request.base.ref }} - name: Set up Ruby 3 uses: ruby/setup-ruby@v1 @@ -28,20 +49,22 @@ jobs: # But in the end, one has to open a PR anyways and then the correct target # branch is identified. # TODO: rename to 'dev' when 'mampf-next' branch is renamed. - - name: Fetch target branch - run: | - targetBranch=${{ github.base_ref || 'mampf-next' }} - echo "Target branch is: $targetBranch" - git fetch origin $targetBranch:$targetBranch --depth=1 + # - name: Fetch target branch + # run: | + # targetBranch=${{ github.base_ref || 'mampf-next' }} + # echo "Target branch is: $targetBranch" + # git fetch origin $targetBranch:$targetBranch --depth=1 # adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721 # and: https://robertfaldo.medium.com/commands-to-run-rubocop-and-specs-you-changed-in-your-branch-e6d2f2e4110b # --diff-filter=d excludes deleted files # "|| test $? = 1;" is used to ignore the exit code of grep when no files # are found matching the pattern + # For the "three dots" ... syntax, see + # https://community.atlassian.com/t5/Bitbucket-questions/Git-diff-show-different-files-than-PR-Pull-Request/qaq-p/2331786 - name: Get changed ruby files (git diff) run: | - changedFiles=$(git diff --name-only --diff-filter=d -r $targetBranch | grep '\.rb$' || test $? = 1;) + changedFiles=$(git diff --name-only --diff-filter=d -r $targetBranch...HEAD | grep '\.rb$' || test $? = 1;) printf "Changed ruby files: \n$changedFiles" echo "CHANGED_FILES=\"$changedFiles\"" >> $GITHUB_ENV