Skip to content

Commit

Permalink
prepare the repo for codecov.io representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dee-6777 committed Mar 7, 2023
1 parent b5fb50b commit 200d0db
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
31 changes: 31 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
codecov:
notify:
require_ci_to_pass: no

coverage:
precision: 2
round: down
range: "20...100"

status:
project: no
patch: no
changes: no

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "reach,diff,flags,tree"
behavior: default
require_changes: no

ignore:
- "**/mocks"
- "**/zz_generated*.go"

23 changes: 5 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unexport GOFLAGS

GOOS?=linux
TESTOPTS ?=
GOARCH?=amd64
GOENV=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 GOFLAGS=
GOPATH := $(shell go env GOPATH)
Expand Down Expand Up @@ -34,25 +35,11 @@ tools:

.PHONY: test
test:
go test ./... -covermode=atomic -coverpkg=./... -v
go test ./... -v $(TESTOPTS)

test-cover:
go test -covermode=atomic -coverpkg=./... -coverprofile=coverage.out ./...

.PHONY: coverage-html
cover-html:
go tool cover -html=coverage.out

.PHONY:coverage
coverage:test-cover
@{ \
set -e ;\
REQ_COV=15.5 ;\
TEST_COV=$$(go tool cover -func coverage.out | grep -F total | awk '{print substr($$3, 1, length($$3)-1)}' ) ;\
if (( $$(echo "$$REQ_COV > $$TEST_COV" |bc -l) )); then echo "Error: Code Coverage is less"; exit 1 ;\
else echo "Code Coverage Test Passed"; exit 0; fi ;\
}
@rm -rf coverage.out
.PHONY: coverage
coverage:
hack/codecov.sh

# Installed using instructions from: https://golangci-lint.run/usage/install/#linux-and-windows
getlint:
Expand Down
55 changes: 55 additions & 0 deletions hack/codecov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

REPO_ROOT=$(git rev-parse --show-toplevel)
CI_SERVER_URL=https://prow.svc.ci.openshift.org/view/gcs/origin-ci-test
COVER_PROFILE=${COVER_PROFILE:-coverage.out}
JOB_TYPE=${JOB_TYPE:-"local"}

# Default concurrency to four threads. By default it's the number of procs,
# which seems to be 16 in the CI env. Some consumers' coverage jobs were
# regularly getting OOM-killed; so do this rather than boost the pod resources
# unreasonably.
COV_THREAD_COUNT=${COV_THREAD_COUNT:-4}
make -C "${REPO_ROOT}" test TESTOPTS="-coverprofile=${COVER_PROFILE}.tmp -covermode=atomic -coverpkg=./... -p ${COV_THREAD_COUNT}"

# Remove generated files from coverage profile
grep -v "zz_generated" "${COVER_PROFILE}.tmp" > "${COVER_PROFILE}"
rm -f "${COVER_PROFILE}.tmp"

# Configure the git refs and job link based on how the job was triggered via prow
if [[ "${JOB_TYPE}" == "presubmit" ]]; then
echo "detected PR code coverage job for #${PULL_NUMBER}"
REF_FLAGS="-P ${PULL_NUMBER} -C ${PULL_PULL_SHA}"
JOB_LINK="${CI_SERVER_URL}/pr-logs/pull/${REPO_OWNER}_${REPO_NAME}/${PULL_NUMBER}/${JOB_NAME}/${BUILD_ID}"
elif [[ "${JOB_TYPE}" == "postsubmit" ]]; then
echo "detected branch code coverage job for ${PULL_BASE_REF}"
REF_FLAGS="-B ${PULL_BASE_REF} -C ${PULL_BASE_SHA}"
JOB_LINK="${CI_SERVER_URL}/logs/${JOB_NAME}/${BUILD_ID}"
elif [[ "${JOB_TYPE}" == "local" ]]; then
echo "coverage report available at ${COVER_PROFILE}"
exit 0
else
echo "${JOB_TYPE} jobs not supported" >&2
exit 1
fi

# Configure certain internal codecov variables with values from prow.
export CI_BUILD_URL="${JOB_LINK}"
export CI_BUILD_ID="${JOB_NAME}"
export CI_JOB_ID="${BUILD_ID}"

if [[ "${JOB_TYPE}" != "local" ]]; then
if [[ -z "${ARTIFACT_DIR:-}" ]] || [[ ! -d "${ARTIFACT_DIR}" ]] || [[ ! -w "${ARTIFACT_DIR}" ]]; then
echo '${ARTIFACT_DIR} must be set for non-local jobs, and must point to a writable directory' >&2
exit 1
fi
curl -sS https://codecov.io/bash -o "${ARTIFACT_DIR}/codecov.sh"
bash <(cat "${ARTIFACT_DIR}/codecov.sh") -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" ${REF_FLAGS}
else
bash <(curl -s https://codecov.io/bash) -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" ${REF_FLAGS}
fi

0 comments on commit 200d0db

Please sign in to comment.