Skip to content

Commit

Permalink
maint: Introduce formatting and analyzers (#36)
Browse files Browse the repository at this point in the history
* Use dotnet format to lint locally and in CI

* Results of running dotnet format

* Add code analysis workflow

* <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> and <EnableNETAnalyzers>true</EnableNETAnalyzers>

* Give full commit hash instead of v1 for code-analysis action

* Install .NET before running code analysis

* Rip out unmaintained analysis Action

* Do not error on CS0618; error on all other warnings
  • Loading branch information
kfcampbell committed Jan 31, 2024
1 parent 820a5e8 commit 24f96a2
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
with:
dotnet-version: '8.x.x'

- name: Format
run: dotnet format --verify-no-changes

- name: Build and strong name sign
run: dotnet build --no-incremental /p:SignAssembly=true /p:AssemblyOriginatorKeyFile=../key.snk

Expand Down
30 changes: 15 additions & 15 deletions Tests/Client/ClientFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using Xunit;
using GitHub.Octokit.Client;
using Xunit;

public class ClientFactoryTests
{

[Fact]
public void Creates_Client_With_Default_Timeout()
{
var clientFactory = ClientFactory.Create();
Assert.Equal(TimeSpan.FromSeconds(100), clientFactory.Timeout);
}
[Fact]
public void Creates_Client_With_Default_Timeout()
{
var clientFactory = ClientFactory.Create();
Assert.Equal(TimeSpan.FromSeconds(100), clientFactory.Timeout);
}

[Fact]
public void Creates_Client_Persists_Set_Timeout()
{
var clientFactory = ClientFactory.Create();
clientFactory.Timeout = TimeSpan.FromSeconds(5);
Assert.Equal(TimeSpan.FromSeconds(5), clientFactory.Timeout);
}
[Fact]
public void Creates_Client_Persists_Set_Timeout()
{
var clientFactory = ClientFactory.Create();
clientFactory.Timeout = TimeSpan.FromSeconds(5);
Assert.Equal(TimeSpan.FromSeconds(5), clientFactory.Timeout);
}

}
}
46 changes: 23 additions & 23 deletions Tests/Client/RequestAdaptorTest.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using Xunit;
using GitHub.Octokit.Client;
using GitHub.Octokit.Authentication;
using GitHub.Octokit.Client;
using NSubstitute;
using Xunit;

public class RequestAdapterTests
{

[Fact]
public void Creates_RequestAdaptor_With_Defaults()
{
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"));
Assert.NotNull(requestAdapter);
}
[Fact]
public void Creates_RequestAdaptor_With_Defaults()
{
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"));
Assert.NotNull(requestAdapter);
}

[Fact]
public void Creates_RequestAdaptor_With_GenericHttpClient()
{
var httpClient = Substitute.For<HttpClient>();
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"), httpClient);
Assert.NotNull(requestAdapter);
}
[Fact]
public void Creates_RequestAdaptor_With_GenericHttpClient()
{
var httpClient = Substitute.For<HttpClient>();
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"), httpClient);
Assert.NotNull(requestAdapter);
}

[Fact]
public void Creates_RequestAdaptor_With_ClientFactory()
{
var clientFactory = ClientFactory.Create();
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"), clientFactory);
Assert.NotNull(requestAdapter);
}
[Fact]
public void Creates_RequestAdaptor_With_ClientFactory()
{
var clientFactory = ClientFactory.Create();
var requestAdapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", "JRRTOLKIEN"), clientFactory);
Assert.NotNull(requestAdapter);
}

}
}
6 changes: 6 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- The below NoWarn is due to Kiota generating code marked as Obsolete.
This is expected, but we still want to error on other warnings from analyzers. -->
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions script/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

# GitHub Actions performs formatting in .github/workflows/build.yml.
# This script may be used to update files before pushing to a PR check.
dotnet format
2 changes: 1 addition & 1 deletion src/Authentication/TokenAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TokenAuthenticationProvider : IAuthenticationProvider
/// Gets or sets the client identifier.
/// </summary>
public string ClientId { get; set; }

/// <summary>
/// Gets or sets the token.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Client/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace GitHub.Octokit.Client;
/// </summary>
public static class ClientFactory
{
private static readonly Lazy<List<DelegatingHandler>> s_handlers =
new(() =>
private static readonly Lazy<List<DelegatingHandler>> s_handlers =
new(() =>
[
new APIVersionHandler(),
new UserAgentHandler(),
Expand Down Expand Up @@ -79,7 +79,7 @@ public static HttpClient Create(HttpMessageHandler? finalHandler = null)
/// <param name="handlers">The collection of <see cref="DelegatingHandler"/> instances to chain.</param>
/// <returns>The first link in the chain of <see cref="DelegatingHandler"/> instances.</returns>
public static DelegatingHandler? ChainHandlersCollectionAndGetFirstLink(
params DelegatingHandler[] handlers) =>
params DelegatingHandler[] handlers) =>
ChainHandlersCollectionAndGetFirstLink(null, handlers);

/// <summary>
Expand Down
34 changes: 17 additions & 17 deletions src/Client/RequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace GitHub.Octokit.Client;

public static class RequestAdapter
{
/// <summary>
/// Represents an adapter for making HTTP requests using HttpClient.
/// </summary>
/// TODO: Implement the missing props and methods
public static HttpClientRequestAdapter Create(IAuthenticationProvider authenticationProvider, HttpClient? clientFactory = null)
{
clientFactory ??= ClientFactory.Create();
/// <summary>
/// Represents an adapter for making HTTP requests using HttpClient.
/// </summary>
/// TODO: Implement the missing props and methods
public static HttpClientRequestAdapter Create(IAuthenticationProvider authenticationProvider, HttpClient? clientFactory = null)
{
clientFactory ??= ClientFactory.Create();

var gitHubRequestAdapter =
new HttpClientRequestAdapter(
authenticationProvider,
null, // Node Parser
null, // Serializer
clientFactory,
null);
return gitHubRequestAdapter;
}
var gitHubRequestAdapter =
new HttpClientRequestAdapter(
authenticationProvider,
null, // Node Parser
null, // Serializer
clientFactory,
null);

return gitHubRequestAdapter;
}
}
88 changes: 47 additions & 41 deletions src/GitHub.Octokit.SDK.csproj
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>GitHub.Octokit.SDK</PackageId>
<Version>0.0.1-alpha</Version>
<NuGetVersion>0.0.1-alpha</NuGetVersion>
<Title>Octokit</Title>
<Description>
This is an auto-generated SDK for GitHub's REST API, built on Kiota.
</Description>
<Authors>GitHub</Authors>
<PackageTags>GitHub API Octokit dotnet-core</PackageTags>
<RepositoryUrl>https://github.com/octokit/dotnet-sdk</RepositoryUrl>
<PackageProjectUrl>https://github.com/octokit/dotnet-sdk</PackageProjectUrl>
<PackageIcon>octokit.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>NugetREADME.md</PackageReadmeFile>
<Copyright>Copyright (c) GitHub 2023</Copyright>
</PropertyGroup>

<PropertyGroup>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.7.2" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.3.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.1.2" />
<None Include="NugetREADME.md" Pack="true" PackagePath="\"/>
<None Include="octokit.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>GitHub.Octokit.SDK</PackageId>
<Version>0.0.1-alpha</Version>
<NuGetVersion>0.0.1-alpha</NuGetVersion>
<Title>Octokit</Title>
<Description>
This is an auto-generated SDK for GitHub's REST API, built on Kiota.
</Description>
<Authors>GitHub</Authors>
<PackageTags>GitHub API Octokit dotnet-core</PackageTags>
<RepositoryUrl>https://github.com/octokit/dotnet-sdk</RepositoryUrl>
<PackageProjectUrl>https://github.com/octokit/dotnet-sdk</PackageProjectUrl>
<PackageIcon>octokit.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>NugetREADME.md</PackageReadmeFile>
<Copyright>Copyright (c) GitHub 2023</Copyright>
</PropertyGroup>

<PropertyGroup>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- The below NoWarn is due to Kiota generating code marked as Obsolete.
This is expected, but we still want to error on other warnings from analyzers. -->
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.7.2" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.3.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.1.2" />
<None Include="NugetREADME.md" Pack="true" PackagePath="\"/>
<None Include="octokit.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Middleware/Options/APIVersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class APIVersionOptions : IRequestOption
private static string GetAPIVersion() =>
//TODO : Get the version from the generator
"2022-11-28";
}
}
2 changes: 1 addition & 1 deletion src/Middleware/Options/UserAgentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ public class UserAgentOptions : IRequestOption
public string ProductVersion { get; set; } = GetProductVersion();

private static string GetProductVersion() => "0.0.0";
}
}
2 changes: 1 addition & 1 deletion src/Middleware/UserAgentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques

var userAgentHandlerOption = request.GetRequestOption<UserAgentOptions>() ?? _userAgentOption;

if(!request.Headers.UserAgent.Any(x => userAgentHandlerOption.ProductName.Equals(x.Product?.Name, StringComparison.OrdinalIgnoreCase)))
if (!request.Headers.UserAgent.Any(x => userAgentHandlerOption.ProductName.Equals(x.Product?.Name, StringComparison.OrdinalIgnoreCase)))
{
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(userAgentHandlerOption.ProductName, userAgentHandlerOption.ProductVersion));
}
Expand Down

0 comments on commit 24f96a2

Please sign in to comment.