4

I'm trying to setup GitHub action to check for lint errors and fail the pull request if any error/ warnings detected.

Currently my logic works locally but when I try to run it via GitHub action, I get an error:

fatal: ambiguous argument 'origin/master...HEAD': unknown revision or path not in the working tree.

I believe it's something to do with checkout@v2 not fetching the right amount of data, But I cant get my head around what

Code Sample

name: Initiate PR
on: push
jobs:
  STEPS:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 100
      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 14.18.0
      - name: Install Node.js dependencies
        run: npm ci --ignore-scripts
      - name: lint-on-PR
        shell: bash
        run: |
          npx eslint --max-warnings 0 $(git diff origin/master...HEAD --name-only --relative --diff-filter=MATR '***.js' '***.jsx' '***.ts' '***.tsx' | xargs)

torek
  • 448,244
  • 59
  • 642
  • 775
Ash
  • 564
  • 2
  • 5
  • 23
  • Maybe you can replace `HEAD` with `$GITHUB_REF`. The git error you provided indicated problems with finding HEAD: https://stackoverflow.com/a/12887486/2182703 By using `$GITHUB_REF` you're sure the right commit hash is used to compare with `master`. – Martijn Gastkemper Oct 31 '21 at 21:03
  • 1
    The `checkout@v2` action does a shallow clone by default. Shallow clones leave commits out (that's what "shallow clone" means), and by leaving commits out, you've instructed the system to leave out the commits and names you'd need in order to run the `git diff` command you want to run. So you'll need a non-shallow non-single-branch clone. The easiest way to do that is to use the non-v2 checkout action. – torek Oct 31 '21 at 23:58
  • 1
    @MartijnGastkemper I have already done $GITHUB_REF there is no difference, if anything HEAD is a more generic. – Ash Nov 01 '21 at 09:05
  • @torek l am trying to be efficient here and not just download the entire repo... however I think you are pointing in the right direction, but how do I load only commits between master and current branch. I added `fetch-depth: 100` to test, but that didnt work... – Ash Nov 01 '21 at 09:09
  • Then I think @torek is in the right direction. Does it work when you set `fetch-depth: 0`? The second paragraph of the docs (https://github.com/actions/checkout) suggests that. – Martijn Gastkemper Nov 01 '21 at 09:53
  • Unfortunately a `--depth` clone is also a `--single-branch` clone, and you need a non-single-branch clone of unknown depth. – torek Nov 01 '21 at 10:33
  • `fetch-depth: 0` worked to my surprise, @torek Do you think its a bad idea ? btw I tried checkout@non-v2 but that version dont exist... did you mean checkout@v1 ? – Ash Nov 01 '21 at 10:58
  • 1
    Using zero makes GitHub actions do a full clone (turns off both `--depth` and hence `--single-branch`). The effect is similar to the v1 version, yes. The "non-v2" phrase is short for "I don't remember how to spell the other one" :-) (Note that "the non-v2 checkout action" has no `literal text` in it and means you *cannot* just copy-paste this.) – torek Nov 01 '21 at 11:06
  • I dont understand, how do I get "the non-v2 checkout action", would you mind posting that and I mark it as correct answer. ideally I rather not use v1 when there is v2. – Ash Nov 01 '21 at 16:00
  • 1
    I'm pretty sure `checkout@v1` *is* the non-v2 checkout torek is referring to. – joanis Nov 01 '21 at 20:36

1 Answers1

2

You would probably need to do a checkout@v1 as in this example to get all the files.

- uses: actions/checkout@v1
...
- run: git diff ${{ github.event.pull_request.base.sha }} ${{ github.sha }}

v2 by default only fetches the sha that triggered the action.

tm243
  • 109
  • 2