Commit 0490a6e9 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'fix/sonar-report' into 'master'

Sonar report

See merge request to-be-continuous/golang!33
parents afb1e27f 0336474b
Loading
Loading
Loading
Loading
+30 −28
Original line number Diff line number Diff line
@@ -134,19 +134,37 @@ stages:
  }

  function go_test() {
    mkdir reports
    local go_tests_report="reports/tests-output.txt"
    mkdir -p reports
    local go_text_report="reports/tests-output.txt"
    local go_json_report="reports/sonar-tests-report.json"
    local junit_tests_report="reports/junit-tests-report.xml"
    local coverage_report_opts=-coverprofile=reports/coverage.out

    # maybe produce Sonar test report (not failing)
    sonar_tests_report || true

    set +e
    # shellcheck disable=SC2086
    go test $GO_TEST_FLAGS "$coverage_report_opts" $GO_TEST_PACKAGES | tee "$go_tests_report"
    go test $GO_TEST_FLAGS "$coverage_report_opts" $GO_TEST_PACKAGES > "$go_text_report"
    test_rc=$?
    set -e

    # dump text report in the console
    cat "$go_text_report"

    # compute and dump code coverage in the console
    output_coverage

    # produce JUnit report (for GitLab)
    install_go_junit_report
    # shellcheck disable=SC2002
    cat "$go_tests_report" | "$GOBIN/go-junit-report" > "$junit_tests_report"
    sonar_tests_report
    cat "$go_text_report" | "$GOBIN/go-junit-report" > "$junit_tests_report"

    # produce JSON report (for SonarQube)
    cat "$go_text_report" | go tool test2json > "$go_json_report"

    # maybe fail
    if [[ "$test_rc" != "0" ]]; then exit "$test_rc"; fi
  }

  function install_go_junit_report() {
@@ -156,26 +174,6 @@ stages:
    cd -
  }

  function sonar_tests_report() {
    if [[ -n "$SONAR_URL" ]]
    then
      log_info "SonarQube template detected (\$SONAR_URL): generate tests report for SonarQube (\\e[33;1mreports/sonar-tests-report.json\\e[0m)"
      mkdir -p reports
      # shellcheck disable=SC2086
      go test $GO_TEST_FLAGS -json $GO_TEST_PACKAGES > reports/sonar-tests-report.json
    fi
  }

  function sonar_lint_report() {
    if [[ -n "$SONAR_URL" ]]
    then
      mkdir -p reports
      log_info "SonarQube template detected (\$SONAR_URL): generate golangci-lint report for SonarQube (\\e[33;1mreports/golangci-lint-report.xml\\e[0m)"
      # shellcheck disable=SC2086
      golangci-lint run --out-format checkstyle $GO_CI_LINT_ARGS > reports/golangci-lint-report.xml
    fi
  }

  function install_go_mod_outdated() {
    cd "$(mktemp -d)"
    go mod init go-mod-outdated
@@ -371,14 +369,18 @@ go-ci-lint:
  stage: build
  image: $GO_CI_LINT_IMAGE
  script:
    - golangci-lint run $GO_CI_LINT_ARGS
    - sonar_lint_report
    - mkdir -p reports
    # produce all reports at once
    - golangci-lint run --out-format "colored-line-number:stdout,code-climate:reports/go-ci-lint.codeclimate.json,checkstyle:reports/golangci-lint-report.xml" $GO_CI_LINT_ARGS
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
    when: always
    paths:
      - $GO_PROJECT_DIR/reports/
    reports:
      codequality:
        - "reports/go-ci-lint.codeclimate.json"
  rules:
    # exclude if GO_CI_LINT_DISABLED set
    - if: '$GO_CI_LINT_DISABLED == "true"'
@@ -390,7 +392,7 @@ go-mod-outdated:
  stage: test
  dependencies: []
  script:
    - mkdir reports
    - mkdir -p reports
    - install_go_mod_outdated
    - go $GO_LIST_ARGS | $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS > reports/go-mod-outdated-report.md
  artifacts: