Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action output for matches #13

Open
stackptr opened this issue Oct 20, 2023 · 2 comments
Open

Add action output for matches #13

stackptr opened this issue Oct 20, 2023 · 2 comments

Comments

@stackptr
Copy link
Member

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
Copy link
Member

pbrisbin commented Oct 20, 2023

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
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants