Added update token command (#22) #53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow will build a golang project | |
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | |
name: Check | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
TARGET_DELTA_COV: 90 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
path: pr | |
- name: Checkout base code | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.pull_request.base.sha }} | |
path: base | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '1.21' | |
- name: Count lines of code | |
id: loc | |
run: | | |
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz | |
sccdiff_hash=$(git hash-object ./sccdiff) | |
[ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1) | |
OUTPUT=$(cd pr && ../sccdiff -basedir ../base) | |
echo "${OUTPUT}" | |
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT | |
- name: Comment lines of code | |
continue-on-error: true | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
header: LOC | |
message: | | |
### Lines Of Code | |
${{ steps.loc.outputs.diff }} | |
- name: Build | |
working-directory: ./pr | |
run: go build -v ./... | |
- name: Lint | |
uses: golangci/golangci-lint-action@v3 | |
with: | |
version: latest | |
working-directory: ./pr | |
- name: Restore base test coverage | |
id: base-coverage | |
if: github.event.pull_request.base.sha != '' | |
uses: actions/cache@v2 | |
with: | |
path: | | |
unit-base.txt | |
# Use base sha for PR or new commit hash for master/main push in test result key. | |
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }} | |
- name: Run test for base code | |
if: steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != '' | |
working-directory: ./base | |
run: | | |
(go test -short -coverprofile=unit.coverprofile -covermode=atomic -race ./... && go tool cover -func=./unit.coverprofile > unit-base.txt) || echo "No test-unit in base" | |
- name: Test | |
id: test | |
working-directory: ./pr | |
run: | | |
go test -short -coverprofile=unit.coverprofile -covermode=atomic -race ./... | |
go tool cover -func=./unit.coverprofile > unit.txt | |
TOTAL=$(grep 'total:' unit.txt) | |
echo "${TOTAL}" | |
echo "total=$TOTAL" >> $GITHUB_OUTPUT | |
- name: Annotate missing test coverage | |
id: annotate | |
if: github.event.pull_request.base.sha != '' | |
working-directory: ./base | |
run: | | |
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.4.0/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz | |
gocovdiff_hash=$(git hash-object ./gocovdiff) | |
[ "$gocovdiff_hash" == "f191b45548bb65ec2c7d88909679a57116ff1ba1" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1) | |
REP=$(./gocovdiff -mod github.com/$GITHUB_REPOSITORY -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV}) | |
echo "${REP}" | |
cat gha-unit.txt | |
DIFF=$(test -e unit-base.txt && ./gocovdiff -mod github.com/$GITHUB_REPOSITORY -func-cov ../pr/unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file") | |
TOTAL=$(cat delta-cov-unit.txt) | |
echo "rep<<EOF" >> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT | |
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT | |
echo "total<<EOF" >> $GITHUB_OUTPUT && echo "$TOTAL" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT | |
- name: Comment test coverage | |
continue-on-error: true | |
if: github.event.pull_request.base.sha != '' | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
header: unit-test | |
message: | | |
### Unit Test Coverage | |
${{ steps.test.outputs.total }} | |
${{ steps.annotate.outputs.total }} | |
<details><summary>Coverage of changed lines</summary> | |
${{ steps.annotate.outputs.rep }} | |
</details> | |
<details><summary>Coverage diff with base branch</summary> | |
${{ steps.annotate.outputs.diff }} | |
</details> | |
- name: Store base coverage | |
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }} | |
working-directory: ./pr | |
run: cp unit.txt unit-base.txt | |
- name: Upload code coverage | |
uses: codecov/codecov-action@v1 | |
with: | |
file: ./pr/unit.coverprofile | |
flags: unittests |