Schneegans/dynamic-badges-action

Can this be used for code coverage?

TheJaredWilcurt opened this issue · 12 comments

It looks like maybe? with the env usage, setting a environment variable to the coverage percent? but I'm not sure.

If it is possible, can you show an example in the README.

Preferably using Jest (most popular testing tool for JavaScript).

Yes, this is definitely possible! Everything you can store in an environment variable can be shown in the badges. I do not use Jest, but reading it's documentation, I think it could work along the following lines. I haven't tested it - please tell me whether you get this working, then I can add an example to the README.

The idea is to extract the coverage percentage from jest's output and store it in an environment variable.

#!/bin/bash

# Get the last-but-one line of the text summary. This contains the line coverage
# percentage. It may look something like that:
# Lines        : 100% ( 161/161 )
SUMMARY="$(jest --coverageReporters='text-summary' | tail -2 | head -1)"

# Split the line by white space.
TOKENS=($SUMMARY)

# The third token is our 100% above.
echo ${TOKENS[2]}

If this works, you could set an environment variable in your workflow file like this:

echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})"

And use this later in the action's configuration:

- name: Create the Badge
  uses: schneegans/dynamic-badges-action@v1.0.0
  with:
    auth: ${{ secrets.GIST_SECRET }}
    gistID: <gist-ID>
    filename: coverage.json
    label: Coverage
    message: ${{ env.COVERAGE }}
    color: green

Thanks!

I was hoping I could keep all the logic in Node, so it would be cross-platform and easier to manage. But it looks like when node creates/modifies an environment variable, it doesn't effect the CI globally. So I couldn't get my Node script to communicate with the action.

This is a clever hack, but it feels like a lot of work to set up each badge, especially since I wanted each branch to have its own coverage badge. But now I don't even know how to just make the main branch have its own that isn't effected by other branches.

I really hope GitHub releases a built in way of handling this, like every other CI does.

Yep, it's kind of verbose. But if you use bash, it's not too complex, I think. Maybe you can add one file for each branch to your gist with filename: ${{ github.ref }}.json. Then you could add a table to your README with a badge for each relevant branch.

Not tested:

- run: |
    SUMMARY="$(npm test -- --coverageReporters='text-summary' | tail -2 | head -1)"
    TOKENS=($SUMMARY)
    echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})"
- name: Create the Badge
  uses: schneegans/dynamic-badges-action@v1.0.0
  with:
    auth: ${{ secrets.GIST_SECRET }}
    gistID: 9c5d16fe3fa8f8ef414fe8b0eff17f7f
    filename: ${{ github.ref }}.json
    label: Coverage
    message: ${{ env.COVERAGE }}
    color: green

Sadly GitHub's Actions API doesn't have a way of getting the branch name, so I used the closest thing.

That seems to work pretty well.

Though now I'm getting this error message:

The set-env command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands

It seems like you have to replace echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})" with something like echo "COVERAGE=$(echo ${TOKENS[2]})" >> $GITHUB_ENV. Does this work?

Reference: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files

Edit: I updated the example in the README accordingly.

I couldn't get it to work:

The closest thing I could find related to the error, but not sure what to do about it

Thanks for the help

Have you missed the leading echo in the command I posted above? It should be something like (now without a line break)

echo "COVERAGE=$(echo ${TOKENS[2]})" >> $GITHUB_ENV

Ayyyyyyyyyy! It woooooooooooorks!

😄

Great article! Hopefully it helps someone discover this project! Thank you!