...
1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15name: code coverage
16on:
17 pull_request:
18 pull_request_target:
19 types: [labeled]
20
21# Declare default permissions as read only.
22permissions: read-all
23
24jobs:
25 build:
26 if: "${{ github.event.action != 'labeled' || github.event.label.name == 'tests: run' }}"
27 runs-on: ubuntu-latest
28 permissions:
29 issues: write
30 pull-requests: write
31 steps:
32 - name: Remove PR Label
33 if: "${{ github.event.action == 'labeled' && github.event.label.name == 'tests: run' }}"
34 uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
35 with:
36 github-token: ${{ secrets.GITHUB_TOKEN }}
37 script: |
38 try {
39 await github.rest.issues.removeLabel({
40 name: 'tests: run',
41 owner: context.repo.owner,
42 repo: context.repo.repo,
43 issue_number: context.payload.pull_request.number
44 });
45 } catch (e) {
46 console.log('Failed to remove label. Another job may have already removed it!');
47 }
48 - name: Setup Go
49 uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
50 with:
51 go-version: "1.22"
52
53 - name: Checkout base branch
54 uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
55 with:
56 ref: ${{ github.base_ref }}
57 - name: Calculate base code coverage
58 run: |
59 go test -short -coverprofile current_cover.out ./... || true
60 export CUR_COVER=$(go tool cover -func current_cover.out | grep total | awk '{print substr($3, 1, length($3)-1)}')
61 echo "CUR_COVER=$CUR_COVER" >> $GITHUB_ENV
62
63 - name: Checkout PR branch
64 uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
65 with:
66 ref: ${{ github.event.pull_request.head.sha }}
67 repository: ${{ github.event.pull_request.head.repo.full_name }}
68 - name: Calculate PR code coverage
69 run: |
70 go test -short -coverprofile pr_cover.out ./... || true
71 export PR_COVER=$(go tool cover -func pr_cover.out | grep total | awk '{print substr($3, 1, length($3)-1)}')
72 echo "PR_COVER=$PR_COVER" >> $GITHUB_ENV
73
74 - name: Verify code coverage. If your reading this and the step has failed, please add tests to cover your changes.
75 run: |
76 go tool cover -func pr_cover.out
77 if [ "${{ env.PR_COVER }}" -lt "${{ env.CUR_COVER }}" ]; then
78 exit 1;
79 fi
View as plain text