Skip to content

Commit

Permalink
Initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Feb 20, 2022
0 parents commit abb06c7
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 0 deletions.
406 changes: 406 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions WrapThat.Version.Tests/InfoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Mvc;

namespace WrapThat.Version.Tests;

internal class InfoTest
{
[Test]
public void ShieldsIoTest()
{
var sut = new InfoController();
var result = sut.Info();
var value = result.Result.AssertObjectResult<ShieldsIo>();
Assert.Multiple(() =>
{
Assert.That(value.label, Is.EqualTo("Version"));
Assert.That(value.message.Length, Is.GreaterThan(0));
});
TestContext.WriteLine($"Version is {value.message}");
}

[Test]
public void VersionTest()
{
var sut = new InfoController();
var result = sut.Version();
var value = result.Result.AssertObjectResult<string>();
Assert.Multiple(() =>
{
Assert.That(value.Count(c => c == '.'), Is.EqualTo(2));
Assert.That(value.All(c => c == '.' || char.IsDigit(c)));
});

}

[Test]
public void VersionExtensionTest()
{
var v = new System.Version(1,2,3,4);
var result = v.ToSemver();
Assert.That(result, Is.EqualTo("1.2.3"));
}
}

public static class ConvertObjectResult
{

public static T AssertObjectResult<T>(this ActionResult? result)
{
var realResult = result as OkObjectResult;

Assert.That(realResult, Is.Not.Null, "Returnerer ikke Ok result");
Assert.That(realResult!.Value, Is.Not.Null, "Returnerer null value");

return (T)realResult.Value!;

}
}
1 change: 1 addition & 0 deletions WrapThat.Version.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
25 changes: 25 additions & 0 deletions WrapThat.Version.Tests/WrapThat.Version.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" version="4.1.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" >
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WrapThat.Version\WrapThat.Version.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions WrapThat.Version.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WrapThat.Version", "WrapThat.Version\WrapThat.Version.csproj", "{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WrapThat.Version.Tests", "WrapThat.Version.Tests\WrapThat.Version.Tests.csproj", "{118A3838-89B4-4E31-BF20-63A2125E852E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7B7CA0BF-0D79-4AEA-90C2-2EE54EE2FDCD}"
ProjectSection(SolutionItems) = preProject
readme.md = readme.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Release|Any CPU.Build.0 = Release|Any CPU
{118A3838-89B4-4E31-BF20-63A2125E852E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{118A3838-89B4-4E31-BF20-63A2125E852E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{118A3838-89B4-4E31-BF20-63A2125E852E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{118A3838-89B4-4E31-BF20-63A2125E852E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {750D2F4F-541E-4EAA-AD05-9081107967B3}
EndGlobalSection
EndGlobal
63 changes: 63 additions & 0 deletions WrapThat.Version/InfoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Reflection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WrapThat.Version;

[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = "v1")]
public class InfoController : ControllerBase
{
[HttpGet]
[AllowAnonymous]
[Route("version")]
public ActionResult<string> Version()
{
var version = Assembly.GetEntryAssembly()!.GetName().Version;
return Ok(version?.ToSemver());
}

[HttpGet]
[AllowAnonymous]
public ActionResult<string> Info()
{
var version = Assembly.GetEntryAssembly()!.GetName().Version;
var shields = new ShieldsIo("Version", version!.ToSemver());
return Ok(shields);
}

[HttpGet]
[Route("status")]
public ActionResult<string> Status()
{
var version = Assembly.GetEntryAssembly()!.GetName().Version;
return Ok(version?.ToSemver());
}

}

public static class VersionExtensions
{
public static string ToSemver(this System.Version version) => $"{version.Major}.{version.Minor}.{version.Build}";
}



/// <summary>
/// To be used with Badges in Shields.io, ref https://shields.io/endpoint
/// </summary>
public class ShieldsIo
{
public int schemaVersion => 1;
public string label { get; set; }
public string message { get; set; }

public string color { get; set; } = "lightgrey";

public ShieldsIo(string label, string message)
{
this.label = label;
this.message = message;
}
}
25 changes: 25 additions & 0 deletions WrapThat.Version/WrapThat.Version.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Title>Add a Info endpoint that gives out the component version number in etiher text or shields.io format</Title>
<Authors>Terje Sandstrom</Authors>
<Copyright>2022 Terje Sandstrom</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryUrl>https://github.com/WrapThat/Version</RepositoryUrl>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>WrapThat.Version</PackageId>
<Version>0.9.3</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" >
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# WrapThat.Version

## Usage:

Add this package to your main ASP.Net project, and it will add an Info endpoint with three methods, giving you the version of the main assembly in semver format.

### Get version as a string

```cs
Route: /api/info/version

returns
1.2.3
```

### Get version as a [shields.io](https://shields.io/) structure

```cs
Route: /api/info

returns shields io structure for use in a [shields endpoint](https://shields.io/endpoint) call.
```

You call this using shields like:

```
https://img.shields.io/endpoint?url=https://yourwebapi/api/info
```

And you get a lightgrey shields badge back, with label Version and your version number.

### Get version as a string authenticated

This is the same as the first method, except that it require authentication, if your API/app is set up for that. The intention is that you can use this to check if the authentication is actually working.

If it is, you will be returned the version string, if not, you will get a 403 response.

0 comments on commit abb06c7

Please sign in to comment.