ci: comment Go coverage report on PR

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-08-23 18:52:46 +02:00
parent 5272e7c86f
commit 11efc8d512
8 changed files with 723 additions and 24 deletions

View file

@ -450,6 +450,19 @@ sh_template(
template = "unused_gh_actions.sh.in",
)
sh_template(
name = "gocoverage_diff",
data = [
"//hack/gocoverage",
"@go_sdk//:bin/go",
],
substitutions = {
"@@GO@@": "$(rootpath @go_sdk//:bin/go)",
"@@GOCOVERAGE@@": "$(rootpath //hack/gocoverage:gocoverage)",
},
template = "gocoverage_diff.sh.in",
)
multirun(
name = "tidy",
commands = [

View file

@ -0,0 +1,96 @@
#!/usr/bin/env bash
###### script header ######
lib=$(realpath @@BASE_LIB@@) || exit 1
stat "${lib}" >> /dev/null || exit 1
# shellcheck source=../sh/lib.bash
if ! source "${lib}"; then
echo "Error: could not find import"
exit 1
fi
go=$(realpath @@GO@@)
stat "${go}" >> /dev/null
gocoverage=$(realpath @@GOCOVERAGE@@)
stat "${gocoverage}" >> /dev/null
cd "${BUILD_WORKSPACE_DIRECTORY}"
###### script body ######
readonly coverageResultPath="coverage_result.json"
readonly coverageMainBucketPath="s3://constellation-ci/gocoverage/coverage_main.json"
readonly coverageMainPath="coverage_main.json"
readonly diffMdPath="coverage_diff.md"
function goTestCover() {
local path=${1-.}
testResult=0
testout=$(CGO_ENABLED=0 ${go} test -C "${path}" -cover "./...") || testResult=$?
if [[ ${testResult} -ne 0 ]]; then
echo "go test failed with exit code ${testResult}:"
echo "${testout}"
exit "${testResult}"
fi
echo "${testout}"
}
function goTestCoverAllMods() {
readarray -t <<< "$(${go} list -f '{{.Dir}}' -m)"
modules=("${MAPFILE[@]}")
excludeMods=(
"hack/tools"
)
for exclude in "${excludeMods[@]}"; do
for i in "${!modules[@]}"; do
if [[ ${modules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}" ]]; then
unset 'modules[i]'
fi
done
done
out=""
for mod in "${modules[@]}"; do
out="${out}$(goTestCover "${mod}")\n"
done
echo "${out}"
}
function fetchMainReport() {
if ! aws s3 cp "${coverageMainBucketPath}" "${coverageMainPath}" > /dev/null; then
echo "Could not fetch main coverage report from ${coverageMainBucketPath}"
exit 1
fi
}
function findDiffPaths() {
diff=$(git diff --name-only origin/main...HEAD)
if [[ -z ${diff} ]]; then
echo "No diff found between main and HEAD"
exit 0
fi
echo "${diff}" | xargs dirname | sort | uniq | tr '\n' ','
}
function generateCoverageDiff() {
echo "Running go test with coverage for all modules..."
out=$(goTestCoverAllMods)
echo "Generating coverage report..."
${gocoverage} > "${coverageResultPath}" <<< "${out}"
echo "Fetch latest report from main..."
fetchMainReport
echo "Find package paths that changed..."
diffPaths=$(findDiffPaths)
echo "Generating diff report..."
${gocoverage} \
-touched "${diffPaths}" \
-diff "${coverageMainPath}" "${coverageResultPath}" \
> "${diffMdPath}"
}
generateCoverageDiff

View file

@ -27,31 +27,23 @@ excludeMods=(
"hack/tools"
)
check() {
echo "The following Go modules are excluded and won't be linted with golangci-lint:"
for exclude in "${excludeMods[@]}"; do
for i in "${!modules[@]}"; do
if [[ ${modules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}" ]]; then
echo " ${modules[i]}"
unset 'modules[i]'
fi
done
echo "The following Go modules are excluded and won't be linted with golangci-lint:"
for exclude in "${excludeMods[@]}"; do
for i in "${!modules[@]}"; do
if [[ ${modules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}" ]]; then
echo " ${modules[i]}"
unset 'modules[i]'
fi
done
done
statuscode=0
statuscode=0
echo "Linting the following Go modules with golangci-lint:"
for mod in "${modules[@]}"; do
echo " ${mod}"
PATH="$(dirname "${go}"):${PATH}" GOROOT=$(${go} env GOROOT) GOPATH=$(${go} env GOPATH) GOCACHE=$(${go} env GOCACHE) CGO_ENABLED=0 ${golangcilint} run --timeout=15m "${mod}/..." >&2
statuscode=$?
done
echo "Linting the following Go modules with golangci-lint:"
for mod in "${modules[@]}"; do
echo " ${mod}"
PATH="$(dirname "${go}"):${PATH}" GOROOT=$(${go} env GOROOT) GOPATH=$(${go} env GOPATH) GOCACHE=$(${go} env GOCACHE) CGO_ENABLED=0 ${golangcilint} run --timeout=15m "${mod}/..." >&2
statuscode=$?
done
exit "${statuscode}"
}
if test -v SILENT; then
check > /dev/null
else
check
fi
exit "${statuscode}"