Skip to content

Commit

Permalink
Fixup sonar, maybe?
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Jun 23, 2020
1 parent 02117a4 commit fb823f2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 56 deletions.
95 changes: 56 additions & 39 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,37 @@ jobs:
- checkout

# setup and prep fetch
- run: |
# Version info
GO_VER=1.14.4
CUE_VER=0.2.0
# Install Go
sudo rm -rf /usr/local/go
curl -LO https://dl.google.com/go/go${GO_VER}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${GO_VER}.linux-amd64.tar.gz
# Install Cue
curl -LO https://github.com/cuelang/cue/releases/download/v${CUE_VER}/cue_${CUE_VER}_Linux_x86_64.tar.gz
tar -xf cue_${CUE_VER}_Linux_x86_64.tar.gz
sudo mv cue /usr/local/bin/cue
# prep test related dir
- run: |
echo "$(id -u):$(id -g)"
sudo mkdir -p /no-home
sudo chown 1001:1002 -R /no-home
- run:
name: Prep system
command: |
sudo apt-get update -y
sudo apt-get install -y tree jq
# Version info
GO_VER=1.14.4
CUE_VER=0.2.0
# Install Go
sudo rm -rf /usr/local/go
curl -LO https://dl.google.com/go/go${GO_VER}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${GO_VER}.linux-amd64.tar.gz
# Install Cue
curl -LO https://github.com/cuelang/cue/releases/download/v${CUE_VER}/cue_${CUE_VER}_Linux_x86_64.tar.gz
tar -xf cue_${CUE_VER}_Linux_x86_64.tar.gz
sudo mv cue /usr/local/bin/cue
# fetch Go dependencies
- run: |
go mod vendor
- run:
name: Fetch deps
command: |
go mod vendor
# build Hof cli
- run: |
go build -o hof cmd/hof/main.go
sudo cp hof /usr/local/bin/hof
- run:
name: Build hof
command: |
go build -o hof cmd/hof/main.go
sudo cp hof /usr/local/bin/hof
# fetch Cue dependencies
Expand All @@ -57,24 +59,39 @@ jobs:
# en masse, regardless of the language, technology, or framework
# you can also script all of the processes that go around a
# complete testing setup with tasks like collectiong and reporting coverage statistics
- run: |
# run all tests
set +e
hof test test.cue -t cover
set -e

# prep test related dir
- run:
name: Test prep
command: |
echo "$(id -u):$(id -g)"
sudo mkdir -p /no-home
sudo chown 1001:1002 -R /no-home
- run:
name: Tests for coverage
command: |
# run all tests
set +e
hof test test.cue -t cover
set -e
# Build our custom SonarCloud image
- run: |
./ci/sc/build.sh
- run:
name: SonarCloud Custom Image
command: |
./ci/sc/build.sh
# Upload reports now that they have been generated and collected
- run: |
mkdir -p sonar-reports/go
./ci/sc/run.sh
- run:
name: SonarCloud Scan
command: |
./ci/sc/run.sh
# Rerun tests for failures, information, and inpsection purposes
- run: |
# run all tests
hof test test.cue -t test
- run:
name: Tests for humans
command: |
# run all tests
hof test test.cue -t test
6 changes: 6 additions & 0 deletions ci/sc/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
FROM sonarsource/sonar-scanner-cli

USER root
RUN apt-get update \
&& apt-get install -y --no-install-recommends tree \
&& rm -rf /var/lib/apt/lists/*
USER scanner-cli

COPY entrypoint.sh /usr/bin/entrypoint.sh
21 changes: 17 additions & 4 deletions ci/sc/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

echo "starting scan..."

declare -a args

add_env_var_as_env_prop() {
Expand All @@ -13,20 +15,31 @@ add_env_var_as_env_prop "${SONAR_LOGIN:-}" "sonar.login"
add_env_var_as_env_prop "${SONAR_PASSWORD:-}" "sonar.password"
add_env_var_as_env_prop "${SONAR_USER_HOME:-}" "sonar.userHome"
add_env_var_as_env_prop "${SONAR_PROJECT_BASE_DIR:-}" "sonar.projectBaseDir"
add_env_var_as_env_prop "${SONAR_BRANCH:-}" "sonar.branch.name"

# if not empty, setup pr scan
if [ ! -z $SONAR_PULL_REQUEST ]; then
add_env_var_as_env_prop "${SONAR_BRANCH:-}" "sonar.pullrequest.branch"
add_env_var_as_env_prop "${SONAR_PULL_REQUEST:-}" "sonar.pullrequest.key"
else
# otherwise branch scan
add_env_var_as_env_prop "${SONAR_BRANCH:-}" "sonar.branch.name"
fi

PROJECT_BASE_DIR="$PWD"
if [ "${SONAR_PROJECT_BASE_DIR:-}" ]; then
PROJECT_BASE_DIR="${SONAR_PROJECT_BASE_DIR}"
fi

# The grep inside the pipes is probably hiding errors
GO_TESTS="$(tree -f -i $PROJECT_BASE_DIR/{cmd,gen,lib,script} | grep "tests.json" | tr "\n" ",")sonar-reports/dummy.txt"
add_env_var_as_env_prop "${GO_TESTS:-}" "sonar.go.tests.reportPaths"

SONAR_CONFIG_FILE=${SONAR_CONFIG_FILE:-sonar-project.properties}
add_env_var_as_env_prop "${SONAR_CONFIG_FILE:-}" "project.settings"

echo "------- sonar args ------------"
echo "${args[@]}"
echo "------- sonar config ------------"
pwd
ls -lh .
echo "---------------------------------"
cat $SONAR_CONFIG_FILE
echo "---------------------------------"

Expand Down
61 changes: 54 additions & 7 deletions ci/sc/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,67 @@ set -euo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
ROOT="$DIR/../.."

SONAR_TOKEN=${1:-$SONAR_TOKEN}
USAGE="
./ci/sonar_cloud/run.sh [-ih] [token] [branch]
-h help
-i interactive
[token] as arg or SONAR_TOKEN
[branch] as arg or SONAR_BRANCH, defaults to current git branch
"

INTERACTIVE="-t"
while getopts "i" OPTION; do
case "${OPTION}" in
i)
INTERACTIVE="-it"
;;

h)
echo $USAGE
exit 0
;;

*)
echo $USAGE
exit 1
;;
esac
done
shift $((OPTIND - 1))

# This allows supplying value with ENV var, as arg, and a help message in one line
SONAR_TOKEN=${1:-${SONAR_TOKEN:?"missing SonarCloud API token, supply as next arg or SONAR_TOKEN"}}
GIT_BRANCH=${2:-`git rev-parse --abbrev-ref HEAD | tr -d "\n"`}

if [ -z $SONAR_TOKEN ]; then
echo please supply an SONAR_TOKEN env var or first argument to this script
echo or add it as FERRUM_SONAR_TOKEN in your .profile or similar
exit 1
# Extract the pull request id
PR_FLAG=""
if [ ! -z $CIRCLE_PULL_REQUEST ]; then
PR_ID=${CIRCLE_PULL_REQUEST##*/}
PR_FLAG="-e SONAR_PULL_REQUEST=$PR_ID"
fi

echo "================= env ======================="
env
echo "================= ======================="

# Run the scanning container
echo docker run \
$INTERACTIVE \
--user $(id -u):$(id -g) \
-e SONAR_TOKEN=$SONAR_TOKEN \
-e SONAR_BRANCH=$GIT_BRANCH \
$PR_FLAG \
-e SONAR_CONFIG_FILE="./ci/sc/sonar-project.properties" \
-v "$ROOT:/usr/src" \
hof/sonar-scanner-cli

docker run \
-it \
$INTERACTIVE \
--user $(id -u):$(id -g) \
-e SONAR_TOKEN=$SONAR_TOKEN \
-e SONAR_BRANCH=$GIT_BRANCH \
$PR_FLAG \
-e SONAR_CONFIG_FILE="./ci/sc/sonar-project.properties" \
-v "$PWD:/usr/src" \
-v "$ROOT:/usr/src" \
hof/sonar-scanner-cli
6 changes: 3 additions & 3 deletions ci/sc/sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**,**/cue.mod/**

# Golang
sonar.go.exclusions=**/*_test.go,**/vendor/**,**/cue.mod/**
sonar.go.tests.reportPaths=sonar-reports/go/**
sonar.go.coverage.reportPaths=sonar-reports/go/**
sonar.go.exclusions=**/vendor/**,**/cue.mod/**
# sonar.go.tests.reportPaths=sonar-reports/go/**/tests.json
sonar.go.coverage.reportPaths=cmd/**/cover.out,gen/**/cover.out,lib/**/cover.out,script/**/cover.out
4 changes: 1 addition & 3 deletions test.cue
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import "strings"
#GoBashCover: #GoBaseTest & {
dir: string
back: strings.Repeat("../", strings.Count(dir, "/") + 1)
scdir: "\(back)sonar-reports/go/\(dir)"
script: string | *"""
rm -rf .workdir
mkdir -p \(scdir)
go test -cover ./ -coverprofile \(scdir)/cover.out -json > \(scdir)/tests.out
go test -cover ./ -coverprofile cover.out -json > tests.json
"""
...
}
Expand Down

0 comments on commit fb823f2

Please sign in to comment.