yuzutech / annotations-action

GitHub action to create annotations from a JSON file
MIT License
36 stars 14 forks source link

Annotations file not found by the action #55

Open stumelius opened 2 years ago

stumelius commented 2 years ago

I'm getting a Ignoring missing file at 'annotations.json' because 'ignore-missing-file' is true error in my workflow:

- name: Lint SQL
  run: sqlfluff lint --format github-annotation | tee annotations.json || echo "::warning::Linter error ignored!"

- name: Print annotations (for testing purposes)
  run: cat annotations.json

- name: Annotate SQL linting errors
  uses: yuzutech/annotations-action@v0.4.0
  with:
    repo-token: "${{ secrets.GITHUB_TOKEN }}"
    title: lint
    input: './annotations.json'

The "Print annotations" step is printing out the annotations but the "Annotate SQL linting errors" step does not find the file. What am I missing here? I have configured the working-directory at the workflow level if that's of any importance here.

ggrossetie commented 2 years ago

I have configured the working-directory at the workflow level if that's of any importance here.

Might be the reason as we do not use this variable:

https://github.com/yuzutech/annotations-action/blob/79c9f5eb5f4f2195af56fdb68f75fcfd9039fff0/index.js#L123

ggrossetie commented 2 years ago

Having said that, I expect GitHub Action to execute the annotations-action in the working-directory so fs.readFile('./annotations.json', { encoding: 'utf8' }) should work...

Did you try to explicitly set the working-directory on the task? or use the relative path from the root in input?

stumelius commented 2 years ago

Thanks! I just fixed it by including my working directory in the input path.

stumelius commented 2 years ago

For some reason this action is not run in the working directory or there's some other reason why the working-directory is not applied to it

ggrossetie commented 2 years ago

For some reason this action is not run in the working directory or there's some other reason why the working-directory is not applied to it

It sounds like a bug/limitation of GitHub Actions.

In my opinion, actions should not have to explicitly set the working directory, so the code seems correct: fs.readFile(inputPath, { encoding: 'utf8' }). Does it work if you explicitly define the working-directory on the step?

- name: Annotate SQL linting errors
  uses: yuzutech/annotations-action@v0.4.0
  with:
    repo-token: "${{ secrets.GITHUB_TOKEN }}"
    title: lint
    input: './annotations.json'
  working-directory: 'some/path'

You should open a support ticket at: https://support.github.com/features/actions

stumelius commented 2 years ago

It looks like jobs with uses don't support the working-directory argument. I got an error from the Github actions validator but didn't find much from quick googling. Just this https://github.community/t/use-working-directory-for-entire-job/16747/4

orestesgaolin commented 2 years ago

I think that it also doesn't take into account that annotations paths can be nested relative to working directory as well.

Consider following workflow

name: example_app

on:
  pull_request:
    paths:
      - ".github/workflows/example_app.yaml"
      - "example/lib/**"

jobs:
  build:
    defaults:
      run:
        working-directory: ./example

    runs-on: ubuntu-latest

    steps:
      - name: 📚 Git Checkout
        uses: actions/checkout@v3
...
      - name: 🧪 Evaluate
        run: generate_annotations > ./annotations.json

      - name: 🕵️ Debug
        run: cat ./annotations.json

      - name: Annotate
        uses: yuzutech/annotations-action@v0.4.0
        with:
          repo-token: "${{ secrets.GITHUB_TOKEN }}"
          title: "lint"
          ignore-missing-file: false
          input: "./example/annotations.json"

The output can be:

[
    {
        "path": "lib/main_development.dart",
        "start_line": 9,
        "end_line": 9,
        "annotation_level": "notice",
        "message": "My message"
    }
]

So the real path would be example/lib/main_development.dart.

I wonder what is valid and what would be expected by users:

ggrossetie commented 2 years ago

the annotations should always be generated with full paths, not relative to a working directory

I think that's the safer approach.

the annotations should be generated with paths relative to working directory, and the annotations-action should take that into account while calling GH apis

Not sure if the GitHub Action has access to the working-directory configuration...