Commit 823a5aa7 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: normalize reports

BREAKING CHANGE: generated reports have changed (see doc). It is a breaking change if you're using SonarQube.
parent b771f636
Loading
Loading
Loading
Loading
+22 −9
Original line number Diff line number Diff line
@@ -62,7 +62,6 @@ go-build:
        GO_TARGET_ARCH: "arm"
```


These jobs use the following variable:

| Name                    | description                              | default value     |
@@ -75,6 +74,15 @@ These jobs use the following variable:
| `GO_TARGET_OS`          | The `GOOS` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)         | _none_ (fallback to go docker image `GOOS`)
| `GO_TARGET_ARCH`          | The `GOARCH` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)         | _none_ (fallback to go docker image `GOARCH`)

In addition to a textual report in the console, the test jobs produce the following reports, kept for one day:

| Report         | Format                                                                       | Usage             |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$GO_PROJECT_DIR/reports/go-test.native.txt` | native Go test report (text) | N/A |
| `$GO_PROJECT_DIR/reports/go-test.native.json` | native Go test report (json) | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/test-coverage/test-execution-parameters/#header-8) |
| `$GO_PROJECT_DIR/reports/go-test.xunit.xml` | [xUnit](https://en.wikipedia.org/wiki/XUnit) test report(s) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsjunit) |
| `$GO_PROJECT_DIR/reports/go-coverage.native.out` | native Go coverage | N/A |
| `$GO_PROJECT_DIR/reports/go-coverage.cobertura.xml` | [Cobertura XML](https://gcovr.com/en/stable/output/cobertura.html) coverage report | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscoverage_report) |

### `go-ci-lint` job

@@ -88,7 +96,12 @@ It is bound to the `build` stage, and uses the following variables:
| `GO_CI_LINT_ARGS`     | `golangci-lint` [command line arguments](https://github.com/golangci/golangci-lint#command-line-options) | `-E gosec,goimports ./...`       |
| `GO_CI_LINT_DISABLED` | Set to `true` to disable this job            | _none_(enabled)       |

Golang Security Checker can be a long operation and therefore the job is configured to be ran **manually** by default (overridable).
In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report         | Format                                                                       | Usage             |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$GO_PROJECT_DIR/reports/go-ci-lint.codeclimate.json` | [Code Climate](https://docs.codeclimate.com/docs/pylint) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscodequality) |
| `$GO_PROJECT_DIR/reports/go-ci-lint.checkstyle.xml` | Checkstyle | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/external-issues/) |

### `go-mod-outdated` job

@@ -98,7 +111,7 @@ It is bound to the `test` stage, and uses the following variables:

| Name                  | description                                  | default value     |
| --------------------- | -------------------------------------------- | ----------------- |
| `GO_MOD_OUTDATED_ARGS`     | `god-mod-outdated` [command line arguments](https://github.com/psampaz/go-mod-outdated#usage) | `-update -direct -style markdown -ci`       |
| `GO_MOD_OUTDATED_ARGS`     | `god-mod-outdated` [command line arguments](https://github.com/psampaz/go-mod-outdated#usage) | `-update -direct` |

Checking outdated modules can be a long operation and therefore the job is configured to be ran **manually** by default (overridable).

@@ -118,12 +131,12 @@ sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**

# tests report: JSON format
sonar.go.tests.reportPaths=reports/sonar-tests-report.json
# coverage report
sonar.go.coverage.reportPaths=reports/coverage.out
# golanci-lint report
sonar.go.golangci-lint.reportPaths=reports/golangci-lint-report.xml
# tests report: JSON native format
sonar.go.tests.reportPaths=reports/go-test.native.json
# coverage report: native format
sonar.go.coverage.reportPaths=reports/go-coverage.native.out
# golanci-lint: checkstyle report (if enabled)
sonar.go.golangci-lint.reportPaths=reports/go-ci-lint.checkstyle.xml
```

More info:
+1 −1
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@
        {
          "name": "GO_MOD_OUTDATED_ARGS",
          "description": "`god-mod-outdated` [command line arguments](https://github.com/psampaz/go-mod-outdated#usage",
          "default": "-update -direct -style markdown -ci",
          "default": "-update -direct",
          "advanced": true
        }
      ]
+35 −21
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ variables:
    -json all

  # Default arguments for go-mod-outdated command
  GO_MOD_OUTDATED_ARGS: '-update -direct -style markdown -ci'
  GO_MOD_OUTDATED_ARGS: '-update -direct'

  # Default golangci-lint Docker image (can be overridden)
  GO_CI_LINT_IMAGE: "golangci/golangci-lint:latest-alpine"
@@ -112,13 +112,15 @@ stages:
  }

  function output_coverage() {
    coverage_out=reports/coverage.out
    coverage_out=reports/go-coverage.native.out
    if [[ -f "$coverage_out" ]]
    then
      log_info "--- \\e[32mCoverage report(s) found\\e[0m (\\e[33;1m${coverage_out}\\e[0m): output"
      percent=$(go tool cover -func="$coverage_out" | tail -1 | awk -F" " '{print $NF}')
      echo "${percent} covered"
      go tool cover -html="$coverage_out" -o "reports/coverage.html"

      go get github.com/boumenot/gocover-cobertura
      go run github.com/boumenot/gocover-cobertura < "$coverage_out" > reports/go-coverage.cobertura.xml
    else
      log_info "--- \\e[32mCoverage report(s) not found\\e[0m: skip"
    fi
@@ -134,15 +136,12 @@ stages:
  }

  function go_test() {
    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
    mkdir -p -m 777 reports
    local go_text_report="reports/go-test.native.txt"

    set +e
    # shellcheck disable=SC2086
    go test $GO_TEST_FLAGS "$coverage_report_opts" $GO_TEST_PACKAGES > "$go_text_report"
    go test $GO_TEST_FLAGS "-coverprofile=reports/go-coverage.native.out" $GO_TEST_PACKAGES > "$go_text_report"
    test_rc=$?
    set -e

@@ -154,10 +153,10 @@ stages:

    # produce JUnit report (for GitLab)
    install_go_junit_report
    "$GOBIN/go-junit-report" < "$go_text_report" > "$junit_tests_report"
    "$GOBIN/go-junit-report" < "$go_text_report" > reports/go-test.xunit.xml

    # produce JSON report (for SonarQube)
    go tool test2json < "$go_text_report" > "$go_json_report"
    go tool test2json < "$go_text_report" > reports/go-test.native.json

    # maybe fail
    if [[ "$test_rc" != "0" ]]; then exit "$test_rc"; fi
@@ -330,9 +329,13 @@ go-test:
    when: always
    reports:
      junit:
        - "$GO_PROJECT_DIR/reports/junit-*.xml"
        - "$GO_PROJECT_DIR/reports/go-test.xunit.xml"
      coverage_report:
        coverage_format: cobertura
        path: "$GO_PROJECT_DIR/reports/go-coverage.cobertura.xml"
    paths:
      - $GO_PROJECT_DIR/reports/
      - "$GO_PROJECT_DIR/reports/go-test.*"
      - "$GO_PROJECT_DIR/reports/go-coverage.*"
  rules:
    # if $GO_TEST_IMAGE set
    - if: '$GO_TEST_IMAGE == null'
@@ -352,10 +355,15 @@ go-build-test:
    expire_in: 1 day
    reports:
      junit:
        - "$GO_PROJECT_DIR/reports/junit-*.xml"
        - "$GO_PROJECT_DIR/reports/go-test.xunit.xml"
      coverage_report:
        coverage_format: cobertura
        path: "$GO_PROJECT_DIR/reports/go-coverage.cobertura.xml"
    paths:
      - $GO_PROJECT_DIR/bin/
      - $GO_PROJECT_DIR/reports/
      - "$GO_PROJECT_DIR/reports/go-test.*"
      - "$GO_PROJECT_DIR/reports/go-coverage.*"
  rules:
    # if $GO_TEST_IMAGE not set
    - if: '$GO_TEST_IMAGE == null'
@@ -365,18 +373,18 @@ go-ci-lint:
  stage: build
  image: $GO_CI_LINT_IMAGE
  script:
    - mkdir -p reports
    - mkdir -p -m 777 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
    - golangci-lint run --out-format "colored-line-number:stdout,code-climate:reports/go-ci-lint.codeclimate.json,checkstyle:reports/go-ci-lint.checkstyle.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/
      - "$GO_PROJECT_DIR/reports/go-ci-lint.*"
    reports:
      codequality:
        - "reports/go-ci-lint.codeclimate.json"
        - "$GO_PROJECT_DIR/reports/go-ci-lint.codeclimate.json"
  rules:
    # exclude if GO_CI_LINT_DISABLED set
    - if: '$GO_CI_LINT_DISABLED == "true"'
@@ -388,15 +396,21 @@ go-mod-outdated:
  stage: test
  dependencies: []
  script:
    - mkdir -p reports
    - mkdir -p -m 777 reports
    # go list
    - go $GO_LIST_ARGS > reports/go-list.native.json
    - install_go_mod_outdated
    - go $GO_LIST_ARGS | $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS > reports/go-mod-outdated-report.md
    # console output (no fail)
    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS < reports/go-list.native.json
    # text report (-ci fails)
    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS -ci < reports/go-list.native.json > reports/go-mod-outdated.native.txt
  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/
      - "$GO_PROJECT_DIR/reports/go-list.native.json"
      - "$GO_PROJECT_DIR/reports/go-mod-outdated.native.txt"
  rules:
    # on schedule: auto
    - if: '$CI_PIPELINE_SOURCE == "schedule"'