< Summary

Information
Class: ClutterStock.Api.Extensions.ApiBuildMetadata
Assembly: Api
File(s): /home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Extensions/ApiBuildMetadata.cs
Tag: 58_25416222083
Line coverage
75%
Covered lines: 33
Uncovered lines: 11
Coverable lines: 44
Total lines: 83
Line coverage: 75%
Branch coverage
40%
Covered branches: 9
Total branches: 22
Branch coverage: 40.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildOpenApiDescription(...)60%101085.71%
ParseInformationalVersion(...)50%6681.81%
GetSourceRevisionId(...)0%4260%

File(s)

/home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Extensions/ApiBuildMetadata.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Text;
 3
 4namespace ClutterStock.Api.Extensions;
 5
 6/// <summary>
 7///     Reads version info baked in at publish (<c>InformationalVersion</c>, <c>SourceRevisionId</c>).
 8/// </summary>
 9internal static class ApiBuildMetadata
 10{
 11    public static string BuildOpenApiDescription(string introduction)
 12    {
 313        var asm = Assembly.GetExecutingAssembly();
 314        var informational =
 315            asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
 316               ?.InformationalVersion;
 17
 318        var (semver, commitFromInfo) = ParseInformationalVersion(informational);
 319        var commit = !string.IsNullOrWhiteSpace(commitFromInfo)
 320            ? commitFromInfo
 321            : GetSourceRevisionId(asm);
 22
 323        var sb = new StringBuilder();
 324        sb.AppendLine(introduction.TrimEnd());
 325        sb.AppendLine();
 326        sb.AppendLine("---");
 327        sb.AppendLine();
 28
 329        if (!string.IsNullOrWhiteSpace(semver))
 30        {
 331            sb.Append("**Version:** `");
 332            sb.Append(semver);
 333            sb.AppendLine("`");
 34        }
 035        else if (!string.IsNullOrWhiteSpace(informational))
 36        {
 037            sb.Append("**Version:** `");
 038            sb.Append(informational);
 039            sb.AppendLine("`");
 40        }
 41
 342        if (!string.IsNullOrWhiteSpace(commit))
 43        {
 344            sb.Append("**Git SHA:** `");
 345            sb.Append(commit);
 346            sb.AppendLine("`");
 47        }
 48
 349        return sb.ToString()
 350                 .Replace("\r\n", "\n")
 351                 .TrimEnd();
 52    }
 53
 54    private static (string SemVer, string? Commit) ParseInformationalVersion(string? informational)
 55    {
 356        if (string.IsNullOrWhiteSpace(informational))
 057            return ("", null);
 58
 359        var trimmed = informational.Trim();
 360        var plus = trimmed.IndexOf('+');
 361        if (plus < 0)
 062            return (trimmed, null);
 63
 364        var semver = trimmed[..plus]
 365            .Trim();
 66
 367        var commit = trimmed[(plus + 1)..]
 368            .Trim();
 69
 370        return (semver, string.IsNullOrEmpty(commit) ? null : commit);
 71    }
 72
 73    private static string? GetSourceRevisionId(Assembly asm)
 74    {
 075        foreach (var meta in asm.GetCustomAttributes<AssemblyMetadataAttribute>())
 76        {
 077            if (string.Equals(meta.Key, "SourceRevisionId", StringComparison.OrdinalIgnoreCase))
 078                return string.IsNullOrWhiteSpace(meta.Value) ? null : meta.Value.Trim();
 79        }
 80
 081        return null;
 082    }
 83}