Improve commit retrieval #2325
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Linting | |
on: | |
push: | |
branches: | |
- '**' | |
pull_request: | |
branches: | |
- 'main' # Continuous Releases | |
jobs: | |
rubocop: | |
name: RuboCop | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the latest commit of the PR branch (at the time this event originally triggered) | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.pull_request.head.sha }} | |
- name: Get a list of changed files (fetching full PR commit history) | |
env: | |
branch_base: origin/${{ github.event.pull_request.base.ref }} | |
branch_pr: origin/${{ github.event.pull_request.head.ref }} | |
refspec_base: +${{ github.event.pull_request.base.sha }}:remotes/origin/${{ github.event.pull_request.base.ref }} | |
refspec_pr: +${{ github.event.pull_request.head.sha }}:remotes/origin/${{ github.event.pull_request.head.ref }} | |
run: | | |
# Fetch enough history to find a common ancestor commit (aka merge-base): | |
git fetch origin ${{ env.refspec_pr }} --depth=$(( ${{ github.event.pull_request.commits }} + 1 )) \ | |
--no-tags --prune --no-recurse-submodules | |
# `actions/checkout` fetched a specific commit, not a branch (ref), so that commit was checked out in a | |
# detached HEAD state. Depending on what you do, you may want to additionally switch to the branch | |
# the refspec assigned the commit to in the prior fetch command: | |
# git switch -c ${{ env.branch_pr }} | |
# This should get the oldest commit in the local fetched history (which may not be the commit the PR branched from): | |
COMMON_ANCESTOR=$( git rev-list --first-parent --max-parents=0 --max-count=1 ${{ env.branch_pr }} ) | |
DATE=$( git log --date=iso8601 --format=%cd "${COMMON_ANCESTOR}" ) | |
# Get all commits since that commit date from the base branch (eg: master or main): | |
git fetch origin ${{ env.refspec_base }} --shallow-since="${DATE}" \ | |
--no-tags --prune --no-recurse-submodules | |
# Show only files from the PR with content filtered by specific git status (Added or Modified): | |
git diff-tree --name-only --diff-filter 'AM' -r \ | |
--merge-base ${{ env.branch_base }} ${{ env.branch_pr }} | |
# - 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.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.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.pull_request.base.ref }} | |
# - name: Set up Ruby 3 | |
# uses: ruby/setup-ruby@v1 | |
# with: | |
# ruby-version: 3.1.4 | |
# bundler-cache: true | |
# If this action is not triggered from a PR, we use 'mampf-next' as default | |
# target branch to compare against. This might not cover all use cases. | |
# 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 | |
# 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...HEAD | grep '\.rb$' || test $? = 1;) | |
# printf "Changed ruby files: \n$changedFiles" | |
# echo "CHANGED_FILES=\"$changedFiles\"" >> $GITHUB_ENV | |
# See RuboCop command line flags here: | |
# https://docs.rubocop.org/rubocop/usage/basic_usage.html#command-line-flags | |
# - name: Run RuboCop | |
# if: env.CHANGED_FILES != '' | |
# run: $CHANGED_FILES | xargs bundle exec rubocop | |
# eslint: | |
# runs-on: ubuntu-latest | |
# name: A job to check eslint linter errors | |
# steps: | |
# - uses: actions/checkout@v2 | |
# - name: Linter count | |
# id: hello | |
# uses: henrixapp/[email protected] | |
# with: | |
# name: EsLint | |
# command: npx eslint | |
# total_regexp: \d+ problems | |
# errors_regexp: \d+ errors | |
# warnings_regexp: \d+ warnings | |
# compare_branch: mampf-next | |
# mode: changed | |
# include: .js | |
# coffee: | |
# runs-on: ubuntu-latest | |
# name: A job to check coffee linter errors | |
# steps: | |
# - uses: actions/checkout@v2 | |
# - name: Linter count | |
# id: hello | |
# uses: henrixapp/[email protected] | |
# with: | |
# name: Coffee | |
# command: npx coffeelint | |
# total_regexp: \d+ errors | |
# errors_regexp: \d+ errors | |
# warnings_regexp: \d+ warnings | |
# compare_branch: mampf-next | |
# mode: changed | |
# include: .coffee | |
# erblint: | |
# runs-on: ubuntu-latest | |
# name: A job to check erblint linter errors | |
# steps: | |
# - uses: actions/checkout@v2 | |
# - name: Set up Ruby 2.7 | |
# uses: ruby/setup-ruby@v1 | |
# with: | |
# ruby-version: 2.7 | |
# - name: Install gems # usual step to install the gems. | |
# run: | | |
# bin/bundle config path vendor/bundle | |
# bin/bundle config set without 'default doc job cable storage ujs test db' | |
# bin/bundle install --jobs 4 --retry 3 | |
# - name: Linter count | |
# id: hello | |
# uses: henrixapp/[email protected] | |
# with: | |
# name: Erblint | |
# command: bin/bundle exec erblint . | |
# total_regexp: \d+ error(s) | |
# errors_regexp: \d+ error(s) | |
# warnings_regexp: \d+ error(s) | |
# compare_branch: mampf-next |