Commit 95f5a560 authored by Christian Ceelen's avatar Christian Ceelen Committed by Pierre Smeyers
Browse files

feat: add dotnet format job

parent 33c8ace3
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -226,6 +226,17 @@ In order to implement the best GitLab and SonarQube integration, the .NET templa
  
  :information_source: the `coverlet.msbuild` package shall be added to your test projects ([see NuGet](https://www.nuget.org/packages/coverlet.msbuild))

### `dotnet-format` job

This job performs code formatting validation using [`dotnet format`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format) of the **whole repository**. It verifies that the code follows the formatting rules defined in `.editorconfig`.

It uses the following variables:

| Input / Variable | Description | Default value |
| ---------------- | ----------- | ------------- |
| `format-disabled` / `DOTNET_FORMAT_DISABLED` | Set to true to disable the [Dotnet Format](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format) code formatting check (enabled by default) | `false` |

The job will **fail the pipeline** if the project's `.editorconfig` styling rules are not respected.

### `dotnet-sonar` job

+6 −0
Original line number Diff line number Diff line
@@ -111,6 +111,12 @@
          "advanced": true
        }
      ]
    },
    {
      "id": "format",
      "name": "dotnet format",
      "description": "Run a [dotnet format](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format) check",
      "disable_with": "DOTNET_FORMAT_DISABLED"
    }
  ]
}
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
@@ -64,6 +64,10 @@ spec:
    sonar-exclusions:
      description: Files and directories to be excluded from analysis, as a comma-separated list of paths. See [documentation](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) for the format.
      default: '**/bin/**,**/obj/**,**/packages/**,**/*.g.cs,**/*.g.i.cs,**/*.designer.cs,**/*AssemblyInfo.cs,.sonarqube'
    format-disabled:
      description: Set to true to disable the [Dotnet Format](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format) code formatting check (enabled by default)
      type: boolean
      default: false
    package-configuration:
      description: The build configuration to use for packaging (Debug or Release).
      default: Release
@@ -161,6 +165,9 @@ variables:
  DOTNET_SONAR_EXTRA_ARGS: $[[ inputs.sonar-extra-args ]]
  DOTNET_SONAR_EXCLUSIONS: $[[ inputs.sonar-exclusions ]]

  # Format
  DOTNET_FORMAT_DISABLED: $[[ inputs.format-disabled ]]

  # default production ref name (pattern)
  PROD_REF: '/^(master|main)$/'
  # default integration ref name (pattern)
@@ -1345,6 +1352,35 @@ stages:
    fi 
  }

  # Format function
  function dotnet_run_format() {
    _dotnet_run_restore
    log_info "Checking code formatting: ${DOTNET_PROJECT_DIR}"
    log_debug "dotnet format whitespace / style / analyzers ${DOTNET_PROJECT_DIR}/${DOTNET_BUILD_FILE} --verify-no-changes ${DOTNET_RESTORE_OPTS}"
    local whitespace_rc
    whitespace_rc=0
    dotnet format whitespace "${DOTNET_PROJECT_DIR}/${DOTNET_BUILD_FILE}" --verify-no-changes ${DOTNET_RESTORE_OPTS} || whitespace_rc=$?
    if [[ "${whitespace_rc}" -ne "0" ]]; then
      log_warn "Whitespace issues found"
    fi
    local style_rc
    style_rc=0
    dotnet format style "${DOTNET_PROJECT_DIR}/${DOTNET_BUILD_FILE}" --verify-no-changes ${DOTNET_RESTORE_OPTS} || style_rc=$?
    if [[ "${style_rc}" -ne "0" ]]; then
      log_warn "Code style issues found"
    fi
    local analyzers_rc
    analyzers_rc=0
    dotnet format analyzers "${DOTNET_PROJECT_DIR}/${DOTNET_BUILD_FILE}" --verify-no-changes ${DOTNET_RESTORE_OPTS} || analyzers_rc=$?
    if [[ "${analyzers_rc}" -ne "0" ]]; then
      log_warn "Code analyzer issues found"
    fi
    if [[ "${whitespace_rc}" -ne "0" || "${style_rc}" -ne "0" || "${analyzers_rc}" -ne "0" ]]; then
      fail "Code issues detected"
    fi
    log_info "Code formatting check completed"
  }

  unscope_variables
  eval_all_secrets

@@ -1428,3 +1464,13 @@ dotnet-sonar:
    - if: '$SONAR_HOST_URL == null || $SONAR_HOST_URL == "" || $SONAR_TOKEN == null || $SONAR_TOKEN == ""'
      when: never
    - !reference [.test-policy, rules]

dotnet-format:
  extends: .dotnet-env-base
  stage: test
  script:
    - dotnet_run_format
  rules:
    - if: '$DOTNET_FORMAT_DISABLED == "true"'
      when: never
    - !reference [.test-policy, rules]