Skip to content

Commit abb06c7

Browse files
committed
Initial code
0 parents  commit abb06c7

File tree

8 files changed

+651
-0
lines changed

8 files changed

+651
-0
lines changed

.gitignore

+406
Large diffs are not rendered by default.

WrapThat.Version.Tests/InfoTest.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace WrapThat.Version.Tests;
4+
5+
internal class InfoTest
6+
{
7+
[Test]
8+
public void ShieldsIoTest()
9+
{
10+
var sut = new InfoController();
11+
var result = sut.Info();
12+
var value = result.Result.AssertObjectResult<ShieldsIo>();
13+
Assert.Multiple(() =>
14+
{
15+
Assert.That(value.label, Is.EqualTo("Version"));
16+
Assert.That(value.message.Length, Is.GreaterThan(0));
17+
});
18+
TestContext.WriteLine($"Version is {value.message}");
19+
}
20+
21+
[Test]
22+
public void VersionTest()
23+
{
24+
var sut = new InfoController();
25+
var result = sut.Version();
26+
var value = result.Result.AssertObjectResult<string>();
27+
Assert.Multiple(() =>
28+
{
29+
Assert.That(value.Count(c => c == '.'), Is.EqualTo(2));
30+
Assert.That(value.All(c => c == '.' || char.IsDigit(c)));
31+
});
32+
33+
}
34+
35+
[Test]
36+
public void VersionExtensionTest()
37+
{
38+
var v = new System.Version(1,2,3,4);
39+
var result = v.ToSemver();
40+
Assert.That(result, Is.EqualTo("1.2.3"));
41+
}
42+
}
43+
44+
public static class ConvertObjectResult
45+
{
46+
47+
public static T AssertObjectResult<T>(this ActionResult? result)
48+
{
49+
var realResult = result as OkObjectResult;
50+
51+
Assert.That(realResult, Is.Not.Null, "Returnerer ikke Ok result");
52+
Assert.That(realResult!.Value, Is.Not.Null, "Returnerer null value");
53+
54+
return (T)realResult.Value!;
55+
56+
}
57+
}

WrapThat.Version.Tests/Usings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using NUnit.Framework;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
13+
<PackageReference Include="NUnit" Version="3.13.2" />
14+
<PackageReference Include="NUnit3TestAdapter" version="4.1.0" />
15+
<PackageReference Include="coverlet.collector" Version="3.1.0" />
16+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" >
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\WrapThat.Version\WrapThat.Version.csproj" />
23+
</ItemGroup>
24+
25+
</Project>

WrapThat.Version.sln

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32112.339
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WrapThat.Version", "WrapThat.Version\WrapThat.Version.csproj", "{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WrapThat.Version.Tests", "WrapThat.Version.Tests\WrapThat.Version.Tests.csproj", "{118A3838-89B4-4E31-BF20-63A2125E852E}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7B7CA0BF-0D79-4AEA-90C2-2EE54EE2FDCD}"
11+
ProjectSection(SolutionItems) = preProject
12+
readme.md = readme.md
13+
EndProjectSection
14+
EndProject
15+
Global
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|Any CPU = Debug|Any CPU
18+
Release|Any CPU = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{D4F00FD1-7B4A-4E9F-B611-78EB51F89A76}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{118A3838-89B4-4E31-BF20-63A2125E852E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{118A3838-89B4-4E31-BF20-63A2125E852E}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{118A3838-89B4-4E31-BF20-63A2125E852E}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{118A3838-89B4-4E31-BF20-63A2125E852E}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {750D2F4F-541E-4EAA-AD05-9081107967B3}
35+
EndGlobalSection
36+
EndGlobal

WrapThat.Version/InfoController.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Reflection;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace WrapThat.Version;
6+
7+
[Route("api/[controller]")]
8+
[ApiController]
9+
[ApiExplorerSettings(GroupName = "v1")]
10+
public class InfoController : ControllerBase
11+
{
12+
[HttpGet]
13+
[AllowAnonymous]
14+
[Route("version")]
15+
public ActionResult<string> Version()
16+
{
17+
var version = Assembly.GetEntryAssembly()!.GetName().Version;
18+
return Ok(version?.ToSemver());
19+
}
20+
21+
[HttpGet]
22+
[AllowAnonymous]
23+
public ActionResult<string> Info()
24+
{
25+
var version = Assembly.GetEntryAssembly()!.GetName().Version;
26+
var shields = new ShieldsIo("Version", version!.ToSemver());
27+
return Ok(shields);
28+
}
29+
30+
[HttpGet]
31+
[Route("status")]
32+
public ActionResult<string> Status()
33+
{
34+
var version = Assembly.GetEntryAssembly()!.GetName().Version;
35+
return Ok(version?.ToSemver());
36+
}
37+
38+
}
39+
40+
public static class VersionExtensions
41+
{
42+
public static string ToSemver(this System.Version version) => $"{version.Major}.{version.Minor}.{version.Build}";
43+
}
44+
45+
46+
47+
/// <summary>
48+
/// To be used with Badges in Shields.io, ref https://shields.io/endpoint
49+
/// </summary>
50+
public class ShieldsIo
51+
{
52+
public int schemaVersion => 1;
53+
public string label { get; set; }
54+
public string message { get; set; }
55+
56+
public string color { get; set; } = "lightgrey";
57+
58+
public ShieldsIo(string label, string message)
59+
{
60+
this.label = label;
61+
this.message = message;
62+
}
63+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Title>Add a Info endpoint that gives out the component version number in etiher text or shields.io format</Title>
5+
<Authors>Terje Sandstrom</Authors>
6+
<Copyright>2022 Terje Sandstrom</Copyright>
7+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
8+
<IncludeSymbols>true</IncludeSymbols>
9+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
10+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
11+
<RepositoryUrl>https://github.com/WrapThat/Version</RepositoryUrl>
12+
<TargetFramework>net6.0</TargetFramework>
13+
<ImplicitUsings>enable</ImplicitUsings>
14+
<Nullable>enable</Nullable>
15+
<PackageId>WrapThat.Version</PackageId>
16+
<Version>0.9.3</Version>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" >
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
</Project>

readme.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# WrapThat.Version
2+
3+
## Usage:
4+
5+
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.
6+
7+
### Get version as a string
8+
9+
```cs
10+
Route: /api/info/version
11+
12+
returns
13+
1.2.3
14+
```
15+
16+
### Get version as a [shields.io](https://shields.io/) structure
17+
18+
```cs
19+
Route: /api/info
20+
21+
returns shields io structure for use in a [shields endpoint](https://shields.io/endpoint) call.
22+
23+
```
24+
25+
You call this using shields like:
26+
27+
```
28+
https://img.shields.io/endpoint?url=https://yourwebapi/api/info
29+
```
30+
31+
And you get a lightgrey shields badge back, with label Version and your version number.
32+
33+
### Get version as a string authenticated
34+
35+
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.
36+
37+
If it is, you will be returned the version string, if not, you will get a 403 response.
38+

0 commit comments

Comments
 (0)