66 - master
77 - main
88 pull_request :
9+
10+ # Cancel the workflow in progress in newer build is about to start.
11+ concurrency :
12+ group : ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+ cancel-in-progress : true
14+
915env :
1016 GO111MODULE : " on"
1117 RUN_BASE_COVERAGE : " on" # Runs test for PR base in case base test coverage is missing.
12- COV_GO_VERSION : 1.17.x # Version of Go to collect coverage
18+ COV_GO_VERSION : 1.18.x # Version of Go to collect coverage
19+ TARGET_DELTA_COV : 90 # Target coverage of changed lines, in percents
1320jobs :
1421 test :
1522 strategy :
1623 matrix :
17- go-version : [ 1.16.x, 1.17.x, tip ]
24+ go-version : [ 1.16.x, 1.17.x, 1.18.x ]
1825 runs-on : ubuntu-latest
1926 steps :
2027 - name : Install Go stable
2128 if : matrix.go-version != 'tip'
22- uses : actions/setup-go@v2
29+ uses : actions/setup-go@v3
2330 with :
2431 go-version : ${{ matrix.go-version }}
32+
2533 - name : Install Go tip
2634 if : matrix.go-version == 'tip'
2735 run : |
3139 tar -C ~/sdk/gotip -xzf gotip.tar.gz
3240 ~/sdk/gotip/bin/go version
3341 echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
42+
3443 - name : Checkout code
3544 uses : actions/checkout@v2
45+
3646 - name : Go cache
3747 uses : actions/cache@v2
3848 with :
@@ -45,58 +55,79 @@ jobs:
4555 key : ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
4656 restore-keys : |
4757 ${{ runner.os }}-go-cache
58+
4859 - name : Restore base test coverage
4960 id : base-coverage
50- if : matrix.go-version == env.COV_GO_VERSION
61+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
5162 uses : actions/cache@v2
5263 with :
5364 path : |
5465 unit-base.txt
5566 # Use base sha for PR or new commit hash for master/main push in test result key.
5667 key : ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
57- - name : Checkout base code
58- if : matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
59- uses : actions/checkout@v2
60- with :
61- ref : ${{ github.event.pull_request.base.sha }}
62- path : __base
68+
6369 - name : Run test for base code
6470 if : matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
6571 run : |
66- cd __base
67- make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
72+ git fetch origin master ${{ github.event.pull_request.base.sha }}
73+ HEAD=$(git rev-parse HEAD)
74+ git reset --hard ${{ github.event.pull_request.base.sha }}
75+ (make test-unit && go tool cover -func=./unit.coverprofile > unit-base.txt) || echo "No test-unit in base"
76+ git reset --hard $HEAD
77+
6878 - name : Test
6979 id : test
7080 run : |
7181 make test-unit
72- go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
73- OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
74- echo "${OUTPUT}"
75- OUTPUT="${OUTPUT//$'\n'/%0A}"
82+ go tool cover -func=./unit.coverprofile > unit.txt
7683 TOTAL=$(grep 'total:' unit.txt)
7784 echo "${TOTAL}"
78- echo "::set-output name=diff::$OUTPUT"
7985 echo "::set-output name=total::$TOTAL"
80- - name : Store base coverage
81- if : ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
82- run : cp unit.txt unit-base.txt
86+
87+ - name : Annotate missing test coverage
88+ id : annotate
89+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
90+ run : |
91+ git fetch origin master ${{ github.event.pull_request.base.sha }}
92+ curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.4/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && shasum -a 256 gocovdiff && echo "b351c67526eefeb0671c82e9271ae984875865eed19e911f40f78348cb98347c gocovdiff" | shasum -c
93+ REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
94+ echo "${REP}"
95+ REP="${REP//$'\n'/%0A}"
96+ cat gha-unit.txt
97+ DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
98+ DIFF="${DIFF//$'\n'/%0A}"
99+ TOTAL=$(cat delta-cov-unit.txt)
100+ echo "::set-output name=rep::$REP"
101+ echo "::set-output name=diff::$DIFF"
102+ echo "::set-output name=total::$TOTAL"
103+
83104 - name : Comment Test Coverage
84105 continue-on-error : true
85- if : matrix.go-version == env.COV_GO_VERSION
106+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
86107 uses : marocchino/sticky-pull-request-comment@v2
87108 with :
88109 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
89110 header : unit-test
90111 message : |
91112 ### Unit Test Coverage
92113 ${{ steps.test.outputs.total }}
114+ ${{ steps.annotate.outputs.total }}
115+ <details><summary>Coverage of changed lines</summary>
116+
117+ ${{ steps.annotate.outputs.rep }}
118+
119+ </details>
120+
93121 <details><summary>Coverage diff with base branch</summary>
94122
95- ```diff
96- ${{ steps.test.outputs.diff }}
97- ```
123+ ${{ steps.annotate.outputs.diff }}
124+
98125 </details>
99126
127+ - name : Store base coverage
128+ if : ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
129+ run : cp unit.txt unit-base.txt
130+
100131 - name : Upload code coverage
101132 if : matrix.go-version == env.COV_GO_VERSION
102133 uses : codecov/codecov-action@v1
0 commit comments