freckle / grep-action

GitHub Action to run grep and attach results as annotations
MIT License
1 stars 0 forks source link

Add action output for matches #13

Open stackptr opened 11 months ago

stackptr commented 11 months ago

An output that specifies how many matches were found would make the action useful for scenarios outside of feedback in pull requests. For example, the number of occurrences of a pattern could be tracked as a kind of code quality metric:

on:
  pull_request:
  push:
    branches: main

jobs:
  grep:
    runs-on: ubuntu-latest
    steps:
      - id: grep
        uses: freckle/grep-action@v1
        with:
          only-changed: ${{ github.ref == 'refs/heads/main' && false || true }}
          patterns: |
            - pattern: "(TODO|FIXME)"
              syntax: extended
              paths:
                - "**/*"
              level: warning
              title: "TODO found"
      - uses: company/report-metric
        with:
          metric-name: todo-or-fixme-comments
          metric-value: ${{ steps.grep.outputs.total-matches }}

While the above supposes the existence of an output like total-matches, a more flexible output could be accomplished by extending the input patterns.pattern with an id, like:

on:
  pull_request:
  push:
    branches: main

jobs:
  grep:
    runs-on: ubuntu-latest
    steps:
      - id: grep
        uses: freckle/grep-action@v1
        with:
          only-changed: ${{ github.ref == 'refs/heads/main' && false || true }}
          patterns: |
            - pattern: "TODO"
              paths:
                - "**/*"
              level: warning
              title: "TODO found"
            - id: fixme
              pattern: "FIXME"
              paths:
                - "**/*"
              level: warning
              title: "FIXME found"
      - name: Log FIXME metric 
        run: |
          matches=${{ steps.grep.output.matches }}
          req_json=$(echo "$matches" | jq '{name: "todo-comments", value: .fixme.count}')

          curl --fail-with-body -X POST "https://api.example.com/metrics" \
            -H "Accept: application/json" \
            -H "Content-Type: application/json" \
            -H "DD-API-KEY: ${DATADOG_API_KEY}" \
            -d "$req_json"
pbrisbin commented 11 months ago

I like the id idea. I don't see why we couldn't set individual outputs, like steps.grep.outputs.fixme.count either. The dorny/paths-filter action seems able to output dyamic things like steps.changes.outputs.{my-filter-name}, so this would be similar to that.

stackptr commented 11 months ago

That's a good point about dorny/paths-filter. For some reason I assumed there would be a lot of frustrating pitfalls in using outputs without fixed names, such that it would be more trouble than it's worth. I haven't found that to be the case with that particular action.