Skip to content

Commit 00552e5

Browse files
committed
Add test reports to PRs
This patch updates GitHub actions to provide more information about tests executed as part of pull requests.
1 parent 58a56ec commit 00552e5

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
# sometimes we need to explicitly override the Go version, ex. CVEs,
77
# when it is not possible to update the go.mod version yet, ex. the
88
# internal builds do not yet support that version of Go.
9-
GO_VERSION:
9+
GO_VERSION: ""
1010

1111
on:
1212
pull_request:
@@ -239,17 +239,21 @@ jobs:
239239
indicators: true
240240
output: both
241241
thresholds: '79 89' # really '80 90', but the values are not inclusive
242-
- name: Save pull request ID
242+
- name: Save commit and pull request ID
243243
if: github.event_name == 'pull_request'
244244
env:
245245
PR_NUMBER: ${{ github.event.number }}
246246
run: |
247+
echo "${GITHUB_SHA}" >commit_id
247248
echo "${PR_NUMBER}" >pull_request_id
248-
- name: Store code coverage results
249+
- name: Store pull request reports
249250
if: github.event_name == 'pull_request'
250251
uses: actions/upload-artifact@v4
251252
with:
252-
name: code-coverage-results
253+
name: pull-request-reports
253254
path: |
254255
code-coverage-results.md
256+
commit_id
255257
pull_request_id
258+
test-results.json
259+
test-results.xml

.github/workflows/post-coverage-to-pr.yml renamed to .github/workflows/pr-reports.yml

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: post-coverage-to-pr
1+
name: pr-reports
22

33
on:
44
workflow_run:
@@ -12,9 +12,9 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414

15-
- name: Fetch code coverage results
15+
- name: Fetch pull request reports
1616
uses: actions/github-script@v7
17-
id: fetch-code-coverage-results
17+
id: fetch-pr-reports
1818
with:
1919
script: |
2020
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
@@ -24,7 +24,7 @@ jobs:
2424
});
2525
let is_pull_request = 'false';
2626
let matchedArtifacts = allArtifacts.data.artifacts.filter((artifact) => {
27-
return artifact.name == "code-coverage-results"
27+
return artifact.name == "pull-request-reports"
2828
});
2929
if (matchedArtifacts.length > 0) {
3030
let download = await github.rest.actions.downloadArtifact({
@@ -35,22 +35,40 @@ jobs:
3535
});
3636
let fs = require('fs');
3737
fs.writeFileSync(
38-
`${process.env.GITHUB_WORKSPACE}/code-coverage-results.zip`,
38+
`${process.env.GITHUB_WORKSPACE}/pull-request-reports.zip`,
3939
Buffer.from(download.data)
4040
);
4141
is_pull_request = 'true';
4242
}
4343
core.info('Setting output: is_pull_request=' + is_pull_request);
4444
core.setOutput('is_pull_request', is_pull_request);
4545
46-
- name: Unzip code coverage results
47-
if: steps.fetch-code-coverage-results.outputs.is_pull_request == 'true'
48-
run: unzip code-coverage-results.zip
46+
- name: Unzip pull request reports
47+
if: steps.fetch-pr-reports.outputs.is_pull_request == 'true'
48+
run: unzip pull-request-reports.zip
49+
50+
- name: Load commit ID
51+
uses: actions/github-script@v7
52+
id: load-commit-id
53+
if: steps.fetch-pr-reports.outputs.is_pull_request == 'true'
54+
with:
55+
script: |
56+
let fs = require('fs');
57+
let pull_request_id = fs.readFileSync(
58+
`${process.env.GITHUB_WORKSPACE}/commit_id`
59+
);
60+
core.info('commit_id=' + commit_id);
61+
if (commit_id == '') {
62+
core.error('commit_id is empty');
63+
} else {
64+
core.info('Setting output: commit_id=' + commit_id);
65+
core.setOutput('commit_id', Number(commit_id));
66+
}
4967
5068
- name: Load pull request ID
5169
uses: actions/github-script@v7
5270
id: load-pull-request-id
53-
if: steps.fetch-code-coverage-results.outputs.is_pull_request == 'true'
71+
if: steps.fetch-pr-reports.outputs.is_pull_request == 'true'
5472
with:
5573
script: |
5674
let fs = require('fs');
@@ -65,10 +83,21 @@ jobs:
6583
core.setOutput('pull_request_id', Number(pull_request_id));
6684
}
6785
68-
- name: Update PR with code coverage results
86+
- name: Update pull request with test report
87+
uses: mikepenz/action-junit-report@v5
88+
if: steps.fetch-pr-reports.outputs.is_pull_request == 'true'
89+
with:
90+
report_paths: test-report.xml
91+
commit: ${{ steps.load-commit-id.outputs.commit_id }}
92+
pr_id: ${{ steps.load-pull-request-id.outputs.pull_request_id }}
93+
comment: true
94+
updateComment: true
95+
include_time_in_summary: true
96+
97+
- name: Update pull request with code coverage
6998
uses: marocchino/sticky-pull-request-comment@v2
70-
if: steps.fetch-code-coverage-results.outputs.is_pull_request == 'true'
99+
if: steps.fetch-pr-reports.outputs.is_pull_request == 'true'
71100
with:
72101
number: ${{ steps.load-pull-request-id.outputs.pull_request_id }}
73102
recreate: true
74-
path: code-coverage-results.md
103+
path: pull-request-reports.md

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Ignore the test reports.
2+
junit*.xml
3+
test-results.json
4+
test-results.xml
5+
16
# Only vendor the net-op CRD for networkinterfaces.
27
config/crd/external-crds/netoperator.vmware.com_*
38
!config/crd/external-crds/netoperator.vmware.com_networkinterfaces.yaml

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ IMAGE ?= vmoperator-controller
105105
IMAGE_TAG ?= latest
106106
IMG ?= ${IMAGE}:${IMAGE_TAG}
107107

108-
# Code coverage files
108+
# Code coverage files.
109109
COVERAGE_FILE := cover.out
110110

111+
# Test result file name (sans extension).
112+
TEST_RESULTS_FILE := test-results
113+
111114
# Gather a set of root packages that have at least one file that matches
112115
# the pattern *_test.go as a child or descendent in that directory.
113116
# However, given this is not a cheap operation, only gather these packages if
@@ -217,7 +220,7 @@ help: ## Display this help
217220
.PHONY: test-api
218221
test-api: | $(GINKGO)
219222
test-api: ## Run API tests
220-
COVERAGE_FILE="" hack/test.sh ./api
223+
COVERAGE_FILE="" TEST_RESULTS_FILE="" hack/test.sh ./api
221224

222225
.PHONY: test-nocover
223226
test-nocover: | $(GINKGO)
@@ -228,8 +231,8 @@ test-nocover: ## Run tests sans coverage
228231
.PHONY: test
229232
test: | $(GINKGO) $(ETCD) $(KUBE_APISERVER)
230233
test: ## Run tests
231-
@rm -f "$(COVERAGE_FILE)"
232-
COVERAGE_FILE="$(COVERAGE_FILE)" $(MAKE) test-nocover
234+
@rm -f "$(COVERAGE_FILE)" "$(TEST_RESULTS_FILE).*"
235+
COVERAGE_FILE="$(COVERAGE_FILE)" TEST_RESULTS_FILE="$(TEST_RESULTS_FILE)" $(MAKE) test-nocover
233236

234237
.PHONY: coverage-xml
235238
coverage-xml: $(GOCOV) $(GOCOV_XML)

hack/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ if [ -n "${COVERAGE_FILE:-}" ]; then
3232
GO_TEST_FLAGS+=("--coverprofile=${COVERAGE_FILE:-}")
3333
fi
3434

35+
# Create test report(s).
36+
if [ -n "${TEST_RESULTS_FILE:-}" ]; then
37+
GO_TEST_FLAGS+=("--output-dir='$(pwd)'")
38+
GO_TEST_FLAGS+=("--junit-report=${TEST_RESULTS_FILE}.xml")
39+
GO_TEST_FLAGS+=("--json-report=${TEST_RESULTS_FILE}.json")
40+
fi
41+
42+
# If run on GitHub then use the Ginkgo GitHub flag.
43+
if [ -n "${GITHUB_RUN_ID:-}" ]; then
44+
GO_TEST_FLAGS+=("--github-output")
45+
fi
46+
3547
# Run the tests.
3648
# shellcheck disable=SC2086
3749
ginkgo "${GO_TEST_FLAGS[@]+"${GO_TEST_FLAGS[@]}"}" "${@:-}" || TEST_CMD_EXIT_CODE="${?}"

0 commit comments

Comments
 (0)