1
0
mirror of https://github.com/bitwarden/server synced 2025-12-06 00:03:34 +00:00

Switch to using built in source link feature (#6297)

* Switch to using built in source link feature

* Switch to using types assembly

* Formatting

* Make version retrieval safer

* Add debug message

* Apply suggestions from code review

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>

---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
This commit is contained in:
Justin Baur
2025-10-10 09:14:27 -04:00
committed by GitHub
parent 3bef57259d
commit b39e486e08
3 changed files with 45 additions and 44 deletions

View File

@@ -7,8 +7,6 @@
<RootNamespace>Bit.$(MSBuildProjectName)</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<IsTestProject Condition="'$(IsTestProject)' == '' and ($(MSBuildProjectName.EndsWith('.Test')) or $(MSBuildProjectName.EndsWith('.IntegrationTest')))">true</IsTestProject>
<Nullable Condition="'$(Nullable)' == '' and '$(IsTestProject)' == 'true'">annotations</Nullable>
<Nullable Condition="'$(Nullable)' == '' and '$(IsTestProject)' != 'true'">enable</Nullable>
@@ -32,19 +30,4 @@
<AutoFixtureAutoNSubstituteVersion>4.18.1</AutoFixtureAutoNSubstituteVersion>
</PropertyGroup>
<Target Name="SetSourceRevisionId" BeforeTargets="CoreGenerateAssemblyInfo">
<Exec Command="git describe --long --always --dirty --exclude=* --abbrev=8" ConsoleToMSBuild="True" IgnoreExitCode="False">
<Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput" />
</Exec>
</Target>
<Target Name="WriteRevision" AfterTargets="SetSourceRevisionId">
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(SourceRevisionId)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>
</Target>
</Project>

View File

@@ -1,46 +1,46 @@
// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
using System.Diagnostics;
using System.Reflection;
namespace Bit.Core.Utilities;
public static class AssemblyHelpers
{
private static readonly IEnumerable<AssemblyMetadataAttribute> _assemblyMetadataAttributes;
private static readonly AssemblyInformationalVersionAttribute _assemblyInformationalVersionAttributes;
private const string GIT_HASH_ASSEMBLY_KEY = "GitHash";
private static string _version;
private static string _gitHash;
private static string? _version;
private static string? _gitHash;
static AssemblyHelpers()
{
_assemblyMetadataAttributes = Assembly.GetEntryAssembly().GetCustomAttributes<AssemblyMetadataAttribute>();
_assemblyInformationalVersionAttributes = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
}
public static string GetVersion()
{
if (string.IsNullOrWhiteSpace(_version))
var assemblyInformationalVersionAttribute = typeof(AssemblyHelpers).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (assemblyInformationalVersionAttribute == null)
{
_version = _assemblyInformationalVersionAttributes.InformationalVersion;
Debug.Fail("The AssemblyInformationalVersionAttribute is expected to exist in this assembly, possibly its generation was turned off.");
return;
}
var informationalVersion = assemblyInformationalVersionAttribute.InformationalVersion.AsSpan();
if (!informationalVersion.TrySplitBy('+', out var version, out var gitHash))
{
// Treat the whole thing as the version
_version = informationalVersion.ToString();
return;
}
_version = version.ToString();
if (gitHash.Length < 8)
{
return;
}
_gitHash = gitHash[..8].ToString();
}
public static string? GetVersion()
{
return _version;
}
public static string GetGitHash()
public static string? GetGitHash()
{
if (string.IsNullOrWhiteSpace(_gitHash))
{
try
{
_gitHash = _assemblyMetadataAttributes.Where(i => i.Key == GIT_HASH_ASSEMBLY_KEY).First().Value;
}
catch (Exception)
{ }
}
return _gitHash;
}
}

View File

@@ -0,0 +1,18 @@
using Bit.Core.Utilities;
using Xunit;
namespace Bit.Core.Test.Utilities;
public class AssemblyHelpersTests
{
[Fact]
public void ReturnsValidVersionAndGitHash()
{
var version = AssemblyHelpers.GetVersion();
_ = Version.Parse(version);
var gitHash = AssemblyHelpers.GetGitHash();
Assert.NotNull(gitHash);
Assert.Equal(8, gitHash.Length);
}
}