Skip to content

Coding guidelines cibuild #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 1,
"isRoot": true,
"tools": {
"jetbrains.resharper.globaltools": {
"version": "2020.3.3",
"commands": [
"jb"
]
},
"regitlint": {
"version": "2.1.3",
"commands": [
"regitlint"
]
}
}
}
126 changes: 112 additions & 14 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,25 +1,123 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = true

[*.{csproj,props}]
[*.{csproj,json}]
indent_size = 2

[*.{cs,vb}]
dotnet_naming_rule.private_members_with_underscore.symbols = private_fields
dotnet_naming_rule.private_members_with_underscore.style = prefix_underscore
dotnet_naming_rule.private_members_with_underscore.severity = suggestion
[*.{cs}]
#### .NET Coding Conventions ####

# Organize usings
dotnet_sort_system_directives_first = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
csharp_style_pattern_local_over_anonymous_function = false:silent

# Expression-level preferences
dotnet_style_operator_placement_when_wrapping = end_of_line
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion

# Parameter preferences
dotnet_code_quality_unused_parameters = non_public:suggestion

# Expression-bodied members
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_constructors = false:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_lambdas = true:suggestion
csharp_style_expression_bodied_local_functions = false:suggestion
csharp_style_expression_bodied_methods = false:suggestion
csharp_style_expression_bodied_operators = false:suggestion
csharp_style_expression_bodied_properties = true:suggestion

# Code-block preferences
csharp_prefer_braces = true:suggestion

# Expression-level preferences
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:suggestion


#### C# Formatting Rules ####

# Indentation preferences
csharp_indent_case_contents_when_block = false

# Wrapping preferences
csharp_preserve_single_line_statements = false


#### Naming styles ####

dotnet_diagnostic.IDE1006.severity = warning

# Naming rules
dotnet_naming_rule.private_const_fields_should_be_pascal_case.symbols = private_const_fields
dotnet_naming_rule.private_const_fields_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.private_const_fields_should_be_pascal_case.severity = warning

dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.symbols = private_static_readonly_fields
dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.severity = warning

dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.symbols = private_static_or_readonly_fields
dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.style = camel_case_prefix_with_underscore
dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.severity = warning

dotnet_naming_rule.locals_and_parameters_should_be_camel_case.symbols = locals_and_parameters
dotnet_naming_rule.locals_and_parameters_should_be_camel_case.style = camel_case
dotnet_naming_rule.locals_and_parameters_should_be_camel_case.severity = warning

dotnet_naming_rule.types_and_members_should_be_pascal_case.symbols = types_and_members
dotnet_naming_rule.types_and_members_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.types_and_members_should_be_pascal_case.severity = warning

# Symbol specifications
dotnet_naming_symbols.private_const_fields.applicable_kinds = field
dotnet_naming_symbols.private_const_fields.applicable_accessibilities = private
dotnet_naming_symbols.private_const_fields.required_modifiers = const

dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities = private
dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = static,readonly

dotnet_naming_symbols.private_static_or_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.private_static_or_readonly_fields.applicable_accessibilities = private
dotnet_naming_symbols.private_static_or_readonly_fields.required_modifiers = static readonly

dotnet_naming_symbols.locals_and_parameters.applicable_kinds = local,parameter
dotnet_naming_symbols.locals_and_parameters.applicable_accessibilities = *

dotnet_naming_symbols.types_and_members.applicable_kinds = *
dotnet_naming_symbols.types_and_members.applicable_accessibilities = *

# Naming styles
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private
dotnet_naming_style.camel_case_prefix_with_underscore.required_prefix = _
dotnet_naming_style.camel_case_prefix_with_underscore.capitalization = camel_case

dotnet_naming_style.prefix_underscore.capitalization = camel_case
dotnet_naming_style.prefix_underscore.required_prefix = _
dotnet_naming_style.camel_case.capitalization = camel_case
41 changes: 0 additions & 41 deletions .github/CONTRIBUTING.MD

This file was deleted.

102 changes: 102 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# I don't want to read this whole thing I just have a question!!!

> Note: Please don't file an issue to ask a question.

You'll get faster results by using our official [Gitter channel](https://gitter.im/json-api-dotnet-core/Lobby) or [StackOverflow](https://stackoverflow.com/search?q=jsonapidotnetcore) where the community chimes in with helpful advice if you have questions.

# How can I contribute?

## Reporting bugs

This section guides you through submitting a bug report.
Following these guidelines helps maintainers and the community understand your report, reproduce the behavior and find related reports.

Before creating bug reports:
- Perform a search to see if the problem has already been reported. If it has and the issue is still open, add a comment to the existing issue instead of opening a new one. If you find a Closed issue that seems like it is the same thing that you're experiencing, open a new issue and include a link to the original issue in the body of your new one.
- Clone the source and run the project locally. You might be able to find the cause of the problem and fix things yourself. Most importantly, check if you can reproduce the problem in the latest version of the master branch.

When you are creating a bug report, please include as many details as possible.
Fill out the issue template, the information it asks for helps us resolve issues faster.

### How do I submit a (good) bug report?

Bugs are tracked as [GitHub issues](https://github.com/json-api-dotnet/JsonApiDotNetCore/issues). Create an issue and provide the following information by filling in the template.
Explain the problem and include additional details to help maintainers reproduce the problem:

- **Use a clear and descriptive title** for the issue to identify the problem.
- **Describe the exact steps which reproduce the problem** in as many details as possible. When listing steps, don't just say what you did, but explain how you did it.
- **Provide specific examples to demonstrate the steps.** Include links to files or GitHub projects, or copy/pasteable snippets, which you use in those examples. If you're providing snippets in the issue, use [Markdown code blocks](https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks).
- **Describe the behavior you observed after following the steps** and point out what exactly is the problem with that behavior. Explain which behavior you expected to see instead and why.
- **If you're reporting a crash**, include the full exception stack trace.

## Suggesting enhancements

This section guides you through submitting an enhancement suggestion, including completely new features and minor improvements to existing functionality. Following these guidelines helps maintainers and the community understand your suggestion and find related suggestions.

Before creating enhancement suggestions:
- Check the [documentation](https://www.jsonapi.net/usage/resources/index.html) and [integration tests](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/master/test/JsonApiDotNetCoreExampleTests/IntegrationTests) for existing features. You might discover the enhancement is already available.
- Perform a search to see if the feature has already been reported. If it has and the issue is still open, add a comment to the existing issue instead of opening a new one.

When you are creating an enhancement suggestion, please include as many details as possible. Fill in the template, including the steps that you imagine you would take if the feature you're requesting existed.

- **Use a clear and descriptive title** for the issue to identify the suggestion.
- **Provide a step-by-step description of the suggested enhancement** in as many details as possible.
- **Provide specific examples to demonstrate the usage.** Include copy/pasteable snippets which you use in those examples, as [Markdown code blocks](https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks).
- **Describe the current behavior and explain which behavior you expected to see instead** and why.
- **Explain why this enhancement would be useful** to most users and isn't something that can or should be implemented in your API project directly.
- **Verify that your enhancement does not conflict** with the [JSON:API specification](https://jsonapi.org/).

## Your first code contribution

Unsure where to begin contributing? You can start by looking through these [beginner](https://github.com/json-api-dotnet/JsonApiDotNetCore/labels/good%20first%20issue) and [help-wanted](https://github.com/json-api-dotnet/JsonApiDotNetCore/labels/help%20wanted) issues.

## Pull requests

Please follow these steps to have your contribution considered by the maintainers:

- **The worst thing in the world is opening a PR that gets rejected** after you've put a lot of effort in it. So for any non-trivial changes, open an issue first to discuss your approach and ensure it fits the product vision.
- Follow all instructions in the template. Don't forget to add tests and update documentation.
- After you submit your pull request, verify that all status checks are passing. In release builds, all compiler warnings are treated as errors, so you should address them before push.

We use [CSharpGuidelines](https://csharpcodingguidelines.com/) as our coding standard (with a few minor exceptions). Coding style is validated during PR build, where we inject an extra settings layer that promotes various suggestions to warning level. This ensures a high-quality codebase without interfering too much when editing code.
You can run the following [PowerShell scripts](https://github.com/PowerShell/PowerShell/releases) locally:
- `inspectcode.ps1`: Scans the code for style violations and opens the result in your web browser.
- `cleanupcode.ps1` Reformats the entire codebase to match with our configured style.

Code inspection violations can be addressed in several ways, depending on the situation:
- Types that are reported to be never instantiated (because the IoC container creates them dynamically) should be decorated with `[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]`.
- Exposed models that contain members never being read/written/assigned to should be decorated with `[UsedImplicitly(ImplicitUseTargetFlags.Members)]`.
- Types that are part of our public API surface can be decorated with `[PublicAPI]`. This suppresses messages such as "type can be marked sealed/internal", "virtual member is never overridden", "member is never used" etc.
- Incorrect violations can be [suppressed](https://www.jetbrains.com/help/resharper/Code_Analysis__Code_Inspections.html#ids-of-code-inspections) using a code comment.

In few cases, the automatic reformat decreases the readability of code. For example, when calling a Fluent API using chained method calls. This can be prevented using [formatter directives](https://www.jetbrains.com/help/resharper/Enforcing_Code_Formatting_Rules.html#configure):

```c#
public sealed class AppDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
// @formatter:wrap_chained_method_calls chop_always

builder.Entity<MusicTrack>()
.HasOne(musicTrack => musicTrack.Lyric)
.WithOne(lyric => lyric.Track)
.HasForeignKey<MusicTrack>();

// @formatter:wrap_chained_method_calls restore
}
}
```

## Backporting and hotfixes (for maintainers)

- Checkout the version you want to apply the feature on top of and create a new branch to release the new version:
```
git checkout tags/v2.5.1 -b release/2.5.2
```
- Cherrypick the merge commit: `git cherry-pick {git commit SHA}`
- Bump the package version in the csproj
- Make any other compatibility, documentation or tooling related changes
- Push the branch to origin and verify the build
- Once the build is verified, create a GitHub release, tagging the release branch
- Open a PR back to master with any other additions
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Closes #{ISSUE_NUMBER}

#### QUALITY CHECKLIST
- [ ] Changes implemented in code
- [ ] Complies with our [contributing guidelines](./.github/CONTRIBUTING.md)
- [ ] Adapted tests
- [ ] Documentation updated
- [ ] Created issue to update [Templates](https://github.com/json-api-dotnet/Templates/issues/new): {ISSUE_NUMBER}
51 changes: 50 additions & 1 deletion Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,64 @@ function CheckLastExitCode {
}
}

function RunInspectCode {
$outputPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'jetbrains-inspectcode-results.xml')
dotnet jb inspectcode JsonApiDotNetCore.sln --output="$outputPath" --properties:Configuration=Release --severity=WARNING --verbosity=WARN -dsl=GlobalAll -dsl=SolutionPersonal -dsl=ProjectPersonal
CheckLastExitCode

[xml]$xml = Get-Content "$outputPath"
if ($xml.report.Issues -and $xml.report.Issues.Project) {
foreach ($project in $xml.report.Issues.Project) {
if ($project.Issue.Count -gt 0) {
$project.ForEach({
Write-Output "`nProject $($project.Name)"
$failed = $true

$_.Issue.ForEach({
$issueType = $xml.report.IssueTypes.SelectSingleNode("IssueType[@Id='$($_.TypeId)']")
$severity = $_.Severity ?? $issueType.Severity

Write-Output "[$severity] $($_.File):$($_.Line) $($_.Message)"
})
})
}
}

if ($failed) {
throw "One or more projects failed code inspection.";
}
}
}

function RunCleanupCode {
# When running in cibuild for a pull request, this reformats only the files changed in the PR and fails if the reformat produces changes.

if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) {
Write-Output "Running code cleanup in cibuild for pull request"

$sourceCommitHash = $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT
$targetCommitHash = git rev-parse "$env:APPVEYOR_REPO_BRANCH"

Write-Output "Source commit hash = $sourceCommitHash"
Write-Output "Target commit hash = $targetCommitHash"

dotnet regitlint -s JsonApiDotNetCore.sln --print-command --jb --profile --jb --profile='\"JADNC Full Cleanup\"' --jb --properties:Configuration=Release --jb --verbosity=WARN -f commits -a $sourceCommitHash -b $targetCommitHash --fail-on-diff --print-diff
CheckLastExitCode
}
}

$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
$revision = "{0:D4}" -f [convert]::ToInt32($revision, 10)

dotnet restore
dotnet tool restore
CheckLastExitCode

dotnet build -c Release
CheckLastExitCode

RunInspectCode
RunCleanupCode

dotnet test -c Release --no-build
CheckLastExitCode

Expand Down
Loading