Skip to content

Commit

Permalink
Codecov experiment (StyraInc#1045)
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored and srenatus committed Oct 1, 2024
1 parent 80bdedb commit 456c3b2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ jobs:
with:
name: regal-${{ matrix.os.name }}
path: regal

code_coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version-file: go.mod
- uses: open-policy-agent/setup-opa@34a30e8a924d1b03ce2cf7abe97250bbb1f332b5 # v2.2.0
with:
version: v0.68.0
- run: |
go run main.go test --coverage bundle \
| opa eval -f raw -I -d build/simplecov/simplecov.rego data.build.simplecov.from_opa \
> coverage.json
- uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
with:
fail_ci_if_error: false
files: ./coverage.json
name: regal
token: ${{ secrets.CODECOV_TOKEN }} # required
44 changes: 44 additions & 0 deletions build/simplecov/simplecov.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package build.simplecov

import rego.v1

from_opa := {"coverage": coverage}

coverage[file] := {"lines": to_lines(report)} if some file, report in input.files

covered_map(report) := cm if {
covered := object.get(report, "covered", [])
cm := {line: 1 |
some item in covered
some line in numbers.range(item.start.row, item.end.row)
}
}

not_covered_map(report) := ncm if {
not_covered := object.get(report, "not_covered", [])
ncm := {line: 0 |
some item in not_covered
some line in numbers.range(item.start.row, item.end.row)
}
}

to_lines(report) := lines if {
cm := covered_map(report)
ncm := not_covered_map(report)
keys := sort([line | some line, _ in object.union(cm, ncm)])
last := keys[count(keys) - 1]

lines := [value |
some i in numbers.range(1, last)
value := to_value(cm, ncm, i)
]
}

to_value(cm, _, line) := 1 if cm[line]

to_value(_, ncm, line) := 0 if ncm[line]

to_value(cm, ncm, line) := null if {
not cm[line]
not ncm[line]
}

0 comments on commit 456c3b2

Please sign in to comment.