-
Notifications
You must be signed in to change notification settings - Fork 3.8k
124 lines (113 loc) · 4.86 KB
/
code-cover-gen.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: PR code coverage (generate)
on:
pull_request:
types: [ opened, reopened, synchronize ]
branches: [ master ]
jobs:
code-cover-gen:
runs-on: ubuntu-latest
env:
PR: ${{ github.event.pull_request.number }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GH_TOKEN: ${{ github.token }}
FETCH_DEPTH: 15
steps:
- uses: actions/checkout@v3
with:
# By default, checkout merges the PR into the current master.
# Instead, we want to check out the PR as is.
ref: ${{ github.event.pull_request.head.sha }}
# Fetching the entire history is much slower; we only fetch the last
# 15 commits. As such, we don't support PRs with 15 commits or more
# (we cannot get to the "base" commit).
fetch-depth: ${{ env.FETCH_DEPTH }}
- name: Set up Bazel cache
uses: actions/cache@v3
with:
path: |
~/.cache/bazel
key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel') }}
restore-keys: |
${{ runner.os }}-bazel-
- name: Get list of changed packages
continue-on-error: true
shell: bash
run: |
set -euxo pipefail
MAX_CHANGED_PKGS=20
FETCH_DEPTH=${{ env.FETCH_DEPTH }}
mkdir -p artifacts
skip() {
echo "Skipping code coverage on PR #$PR: $1"
# Generate the json files with an error (which will show up in Reviewable).
msg="$1; see $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID."
jq -n --arg err "$msg" '{error: $err}' > artifacts/cover-${PR}-${HEAD_SHA}.json
if [ -n "${BASE_SHA:-}" ]; then
jq -n --arg err "$msg" '{error: $err}' > artifacts/cover-${PR}-${BASE_SHA}.json
fi
echo "SKIP=true" >> "${GITHUB_ENV}"
exit 1
}
# To get the base commit, we get the number of commits in the PR.
# Note that github.event.pull_request.base.sha is not what we want,
# that is the tip of master and not necessarily the PR fork point.
NUM_COMMITS=$(gh pr view $PR --json commits --jq '.commits | length')
# The number of commits bust be below the checkout fetch-depth.
if [ ${NUM_COMMITS} -ge ${FETCH_DEPTH} ]; then
echo "ERROR=too many commits (${NUM_COMMITS})" >> ${GITHUB_ENV}
exit 1
fi
BASE_SHA=$(git rev-parse HEAD~${NUM_COMMITS})
CHANGED_PKGS=$(build/ghactions/changed-go-pkgs.sh ${BASE_SHA} ${HEAD_SHA})
NUM_CHANGED_PKGS=$(echo "${CHANGED_PKGS}" | wc -w)
if [ ${NUM_CHANGED_PKGS} -gt ${MAX_CHANGED_PKGS} ]; then
echo "ERROR=too many changed packages (${NUM_CHANGED_PKGS})" >> ${GITHUB_ENV}
exit 1
fi
echo "BASE_SHA=${BASE_SHA}" >> "${GITHUB_ENV}"
echo "CHANGED_PKGS=${CHANGED_PKGS}" >> "${GITHUB_ENV}"
- name: Run "after" test coverage
if: env.ERROR == ''
continue-on-error: true
shell: bash
run: |
set -euxo pipefail
CHANGED_PKGS='${{ env.CHANGED_PKGS }}'
# Make a copy of the script so that the "before" run below uses the
# same version.
cp build/ghactions/pr-codecov-run-tests.sh ${RUNNER_TEMP}/
if ! ${RUNNER_TEMP}/pr-codecov-run-tests.sh artifacts/cover-${PR}-${HEAD_SHA}.json "${CHANGED_PKGS}"; then
echo "ERROR=tests failed" >> ${GITHUB_ENV}
exit 1
fi
- name: Run "before" test coverage
if: env.ERROR == ''
continue-on-error: true
shell: bash
run: |
set -euxo pipefail
BASE_SHA='${{ env.BASE_SHA }}'
CHANGED_PKGS='${{ env.CHANGED_PKGS }}'
git checkout -f ${BASE_SHA}
if ! ${RUNNER_TEMP}/pr-codecov-run-tests.sh artifacts/cover-${PR}-${BASE_SHA}.json "${CHANGED_PKGS}"; then
echo "ERROR=tests failed on base branch" >> ${GITHUB_ENV}
exit 1
fi
- name: Finalize
shell: bash
run: |
ERROR='${{ env.ERROR }}'
if [ -n "$ERROR" ]; then
# Generate the json files with an error (which will show up in Reviewable).
msg="$ERROR; see [run]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)."
jq -n --arg err "$msg" '{error: $err}' > artifacts/cover-${PR}-${HEAD_SHA}.json
BASE_SHA=${{ env.BASE_SHA }}
if [ -n "$BASE_SHA" ]; then
jq -n --arg err "$msg" '{error: $err}' > artifacts/cover-${PR}-${BASE_SHA}.json
fi
fi
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: cover
path: artifacts/cover-*.json