Search code examples
githubgithub-actionsgithub-clisuper-linter

Unable to edit and capture Super Linter error into GitHub Issue


I am using Superlinter actions in GitHub Actions for code linting. I would like to trigger action once issue is created, and capture error that is raised by SQLFluff (Linter in SuperLiner) into the issue that was raised by the user. Currently, my main workflow file looks like below. However, for some reason I am unable to capture the error that is raised by linter into the issue. What am I missing?

Main.yml (Updated code after jonrsharpe comment)

name: close-issue-after-lint-open
on:
  issues:
    types: [opened]
jobs:
  close-issue:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      contents: read
      packages: read
      statuses: write
    steps:
      - name: Checkout code
        uses: actions/[email protected]
        with:
          fetch-depth: 0
      - name: Super-linter
        uses: super-linter/[email protected]
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VALIDATE_SQLFLUFF: true
          ENABLE_GITHUB_ACTIONS_STEP_SUMMARY: true
          SAVE_SUPER_LINTER_OUTPUT: true

      - name: Log-error-to-the-issue
        if: failure()
        env: 
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ISSUE: ${{ github.event.issue.number }}
          ERROR_LOG_PATH: ${{ github.workspace }}
        run: 
          gh run view --log "$ERROR_LOG_PATH/error/super-linter"
      
          
      - name: Close
        env: 
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO: ${{ github.repository }}
          ISSUE: ${{ github.event.issue.number }}
        run:
          gh issue close --repo "$REPO" --comment "Autoclosing issue $ISSUE"
          "$ISSUE"

Solution

  • For Future Stackoverflow reader,

    Below is the code I use to resolve my issue. First, I wanted to see what are all the files I have? After that ,I narrowed down to the path I wanted and was able to add the JSON to the issue. Note, I added entire JSON to the issue, but was able to capture what I was looking for. Thanks @jonrsharpe for pointers.

    name: close-issue-after-lint-open
    
    on:
      issues:
        types: [opened]
    
    jobs:
      close-issue:
        runs-on: ubuntu-latest
        permissions:
          issues: write
          contents: read
          packages: read
          statuses: write
        
        steps:
          - name: Checkout code
            uses: actions/[email protected]
            with:
              fetch-depth: 0
    
          - name: Run Super-linter
            uses: super-linter/[email protected]
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              VALIDATE_SQLFLUFF: true
              ENABLE_GITHUB_ACTIONS_STEP_SUMMARY: true
              
              
          - name: Log error to the issue
            if: failure()
            env: 
              GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              ISSUE: ${{ github.event.issue.number }}
              ERROR_LOG_PATH: ${{ github.workspace }}/super-linter-output/super-linter/super-linter-worker-results-SQLFLUFF.json
            run: |
              echo "Listing all files and directories in workspace:"
              ls -R $GITHUB_WORKSPACE  # List everything recursively in the workspace
              
              # Optionally, list the contents of the specific super-linter output directory
              if [ -d "${GITHUB_WORKSPACE}/${{ env.SUPER_LINTER_OUTPUT_DIRECTORY_NAME }}/super-linter" ]; then
                echo "Listing contents of the Super Linter output directory:"
                ls -R "${GITHUB_WORKSPACE}/${{ env.SUPER_LINTER_OUTPUT_DIRECTORY_NAME }}/super-linter"
              else
                echo "Super Linter output directory not found."
              fi
              
              # Check if the error log exists
              if [ -f "$ERROR_LOG_PATH" ]; then
                ERROR_LOG=$(cat "$ERROR_LOG_PATH")
              else
                ERROR_LOG="No errors found."
              fi
    
              # Post the error log to the issue
              gh issue comment $ISSUE --body "Super Linter found the following issues:$ERROR_LOG"
    
          - name: Close issue
            env: 
              GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              REPO: ${{ github.repository }}
              ISSUE: ${{ github.event.issue.number }}
            run: |
              # Close the issue and add a comment
              gh issue close --repo "$REPO" --comment "Autoclosing issue $ISSUE due to linting errors."