Skip to content

Commit 1163430

Browse files
committed
wip
1 parent e66dbef commit 1163430

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

.github/workflows/build.yml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
name: Build
2+
3+
permissions:
4+
contents: write
5+
6+
on:
7+
push:
8+
branches: [main]
9+
pull_request:
10+
branches: [main]
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
go:
19+
- "stable"
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
fetch-tags: true
25+
persist-credentials: true
26+
27+
- name: Set up Go
28+
uses: actions/setup-go@v5
29+
with:
30+
go-version: ${{ matrix.go }}
31+
32+
- name: Go generate
33+
run: go generate ./...
34+
35+
- name: Test
36+
run: go test -tags debug -bench=. -coverprofile=coverage.out ./...
37+
38+
- name: Run Gosec Security Scanner
39+
uses: securego/gosec@master
40+
with:
41+
args: ./...
42+
43+
- name: Build
44+
run: go build .
45+
46+
- name: Go report card
47+
uses: creekorful/[email protected]
48+
49+
# Generate code coverage badge and push it to the 'coverage' branch.
50+
# Reference it in your README.md like this:
51+
#
52+
# [![coverage](https://github.com/USERNAME/REPO/blob/coverage/BRANCH/badge.svg)](#)
53+
#
54+
# If you have a detailed HTML report of the coverage (here called report.html), replace the '#' with:
55+
#
56+
# https://htmlpreview.github.io/?https://github.com/USERNAME/REPO/blob/coverage/BRANCH/report.html
57+
#
58+
# You need to have given write permissions for the for the workflow:
59+
#
60+
# permissions:
61+
# contents: write
62+
#
63+
# To create the 'coverage' branch for the repository (replace 'main' with the branch you want to publish for):
64+
#
65+
# git checkout main
66+
# git checkout --orphan coverage && git rm --cached $(git ls-files) && echo '# Coverage branch' > README.md
67+
# git add README.md && git commit -m 'Add README.md' && git push origin coverage
68+
# git checkout --force main
69+
- name: Extract branch name
70+
run: echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
71+
id: extract_branch
72+
- uses: actions/checkout@v3
73+
with:
74+
ref: coverage
75+
path: coverage
76+
- name: Ensure directories for this branch exist
77+
env:
78+
BRANCH: ${{ steps.extract_branch.outputs.branch }}
79+
run: mkdir -p coverage/${BRANCH}
80+
- name: Extract code coverage
81+
id: coverage
82+
env:
83+
BRANCH: ${{ steps.extract_branch.outputs.branch }}
84+
# Change this to extract the coverage percentage (without the percent sign) for your language into COVERAGE.
85+
# Here we are using a 'coverage.out' file generated by 'go test -coverprofile=coverage.out ./...'
86+
# You may also generate a HTML report and place at 'coverage/${BRANCH}/report.html'.
87+
run: |
88+
echo "COVERAGE=$(go tool cover -func=coverage.out | tail -n 1 | tr -s '\t' | cut -f 3 | rev | cut -c2- | rev)" >> $GITHUB_OUTPUT
89+
go tool cover -html=coverage.out -o=coverage/${BRANCH}/report.html
90+
- name: Generate the badge SVG image
91+
uses: emibcn/[email protected]
92+
with:
93+
label: 'coverage'
94+
status: ${{ steps.coverage.outputs.coverage }}%
95+
path: coverage/${{ steps.extract_branch.outputs.branch }}/badge.svg
96+
color: ${{
97+
steps.coverage.outputs.coverage > 95 && 'green' ||
98+
steps.coverage.outputs.coverage > 90 && 'yellow,green,green' ||
99+
steps.coverage.outputs.coverage > 80 && 'yellow,green' ||
100+
steps.coverage.outputs.coverage > 70 && 'yellow' ||
101+
steps.coverage.outputs.coverage > 60 && 'orange,yellow' ||
102+
steps.coverage.outputs.coverage > 50 && 'orange' ||
103+
steps.coverage.outputs.coverage > 40 && 'red,orange' ||
104+
steps.coverage.outputs.coverage > 30 && 'red,red,orange' ||
105+
steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' ||
106+
'red' }}
107+
- name: Commit coverage files
108+
env:
109+
BRANCH: ${{ steps.extract_branch.outputs.branch }}
110+
run: |
111+
pushd coverage
112+
git config --local user.email "[email protected]"
113+
git config --local user.name "GitHub Action"
114+
test ! -f "${BRANCH}/badge.svg" || git add "${BRANCH}/badge.svg"
115+
test ! -f "${BRANCH}/report.html" || git add "${BRANCH}/report.html"
116+
test -z "$(git status --porcelain)" || git commit -m "update"
117+
git push
118+
popd

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
[![build](https://github.com/linkdata/gitcoverage/actions/workflows/build.yml/badge.svg)](https://github.com/linkdata/gitcoverage/actions/workflows/build.yml)
2+
[![coverage](https://github.com/linkdata/gitcoverage/blob/coverage/main/badge.svg)](https://htmlpreview.github.io/?https://github.com/linkdata/gitcoverage/blob/coverage/main/report.html)
3+
[![goreport](https://goreportcard.com/badge/github.com/linkdata/gitcoverage)](https://goreportcard.com/report/github.com/linkdata/gitcoverage)
4+
[![Docs](https://godoc.org/github.com/linkdata/gitcoverage?status.svg)](https://godoc.org/github.com/linkdata/gitcoverage)
5+
16
# gitcoverage
27
Manage code coverage badges and reports

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/linkdata/gitcoverage
2+
3+
go 1.25.0
4+
5+
require github.com/linkdata/gitsemver v1.8.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/linkdata/gitsemver v1.8.0 h1:lrXwFLSbQxnLFxxXSDB30c3Ja64XLlcmwFnMhNt6jYs=
2+
github.com/linkdata/gitsemver v1.8.0/go.mod h1:aVbGwlu8kT+EBQU+uvwgw2DA1qvGKugFvXUEM67hpe4=

main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"syscall"
9+
10+
gitsemver "github.com/linkdata/gitsemver/pkg"
11+
)
12+
13+
var (
14+
flagGit = flag.String("git", "git", "path to Git executable")
15+
flagDebug = flag.Bool("debug", false, "write debug info to stderr")
16+
flagPct = flag.String("pct", "", "coverage percentage (required)")
17+
)
18+
19+
func mainfn() int {
20+
var err error
21+
if *flagPct != "" {
22+
repoDir := os.ExpandEnv(flag.Arg(0))
23+
if repoDir == "" {
24+
repoDir = "."
25+
}
26+
var vs *gitsemver.GitSemVer
27+
if vs, err = gitsemver.New(*flagGit); err == nil {
28+
if *flagDebug {
29+
vs.DebugOut = os.Stderr
30+
}
31+
if repoDir, err = vs.Git.CheckGitRepo(repoDir); err == nil {
32+
var vi gitsemver.VersionInfo
33+
if vi, err = vs.GetVersion(repoDir); err == nil {
34+
35+
fmt.Println(vi.Branch)
36+
return 0
37+
}
38+
}
39+
}
40+
}
41+
42+
retv := 125
43+
if err != nil {
44+
fmt.Fprintln(os.Stderr, err.Error())
45+
if e := errors.Unwrap(err); e != nil {
46+
if errno, ok := e.(syscall.Errno); ok {
47+
retv = int(errno)
48+
}
49+
}
50+
}
51+
return retv
52+
}
53+
54+
var exitFn func(int) = os.Exit
55+
56+
func main() {
57+
flag.Parse()
58+
exitFn(mainfn())
59+
}

main_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"testing"
6+
)
7+
8+
func TestMainFn(t *testing.T) {
9+
flag.Parse()
10+
mainfn()
11+
}
12+
13+
func TestMainError(t *testing.T) {
14+
exitFn = func(i int) {
15+
if i == 0 {
16+
t.Error(i)
17+
}
18+
if i == 125 {
19+
t.Log("didn't get a syscall.Errno")
20+
}
21+
}
22+
flag.Parse()
23+
*flagDebug = true
24+
main()
25+
}

0 commit comments

Comments
 (0)