Add links to license and source code in footer (#909)

* Display link to source code and license in footer

* Remove " (fetch)" suffix in remote URL

* Use split instead of replace for getting the clean remote URL

* Open license link in new tab as well

* LINQ-ify things and handle potential errors

* Properly detect remote URL type and parse accordingly

Squashed 3 commits:

* Properly detect remote URL type and parse accordingly
* Actually fix broken source code link...maybe
* Attempt to fix broken source code link

* Fix a kokoism

Perhaps we should actually utilize the firstRemoteUnparsedUrl variable instead of getting the FirstOrDefault remote over and over

* Fix missing slash when using an SSH remote

* Assign null to parsed URL variable by default

Thank you clyde discord

* Only perform the null check on the unparsed URL once

Thank you again clyde discord

* Handle exception when there are no defined remotes

* Move remote determination to VersionHelper and format file

Squashed 4 commits:

* Nitpick variable naming and add XML doc
* Use preferred braces style
* Return null directly if unparsed URL is null
* Move remote determination to VersionHelper and format file

* Add param to include HTTPS prefix in DetermineRemoteUrl method

* Remove warnings from XML doc

Not needed as of c63284cb37

* Properly insert HTTPS prefix into SSH remotes

* Fix license layout and improve git remote parsing

---------

Co-authored-by: Slendy <josh@slendy.pw>
This commit is contained in:
Molly Phillips 2023-10-03 20:08:23 -04:00 committed by GitHub
commit 248f141c65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 24 deletions

View file

@ -191,8 +191,28 @@
<footer> <footer>
<div class="ui black attached inverted segment"> <div class="ui black attached inverted segment">
<div class="ui container"> <div class="ui container">
@{
<p>@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)</p> string? remoteUrl = VersionHelper.DetermineRemoteUrl();
}
<span>
@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)
</span>
<span style="float: right;">
<a href="https://github.com/LBPUnion/ProjectLighthouse/blob/main/LICENSE" target="_blank">
AGPL-3.0 License
</a>
&centerdot;
@if (!string.IsNullOrWhiteSpace(remoteUrl))
{
<a href="https://@remoteUrl" target="_blank">
Source Code
</a>
}
else
{
<span class="ui text red">Cannot Detect Source Code</span>
}
</span>
@if (VersionHelper.IsDirty) @if (VersionHelper.IsDirty)
{ {
<p>@Model.Translate(BaseLayoutStrings.GeneratedModified)</p> <p>@Model.Translate(BaseLayoutStrings.GeneratedModified)</p>

View file

@ -1,11 +1,12 @@
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using LBPUnion.ProjectLighthouse.Configuration; using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Logging; using LBPUnion.ProjectLighthouse.Types.Logging;
namespace LBPUnion.ProjectLighthouse.Helpers; namespace LBPUnion.ProjectLighthouse.Helpers;
public static class VersionHelper public static partial class VersionHelper
{ {
static VersionHelper() static VersionHelper()
{ {
@ -32,11 +33,8 @@ public static class VersionHelper
} }
catch catch
{ {
Logger.Error Logger.Error("Project Lighthouse was built incorrectly. Please make sure git is available when building.",
( LogArea.Startup);
"Project Lighthouse was built incorrectly. Please make sure git is available when building.",
LogArea.Startup
);
CommitHash = "invalid"; CommitHash = "invalid";
Branch = "invalid"; Branch = "invalid";
CanCheckForUpdates = false; CanCheckForUpdates = false;
@ -44,37 +42,59 @@ public static class VersionHelper
if (!IsDirty) return; if (!IsDirty) return;
Logger.Warn Logger.Warn("This is a modified version of Project Lighthouse. " +
( "Please make sure you are properly disclosing the source code to any users who may be using this instance.",
"This is a modified version of Project Lighthouse. " + LogArea.Startup);
"Please make sure you are properly disclosing the source code to any users who may be using this instance.",
LogArea.Startup
);
CanCheckForUpdates = false; CanCheckForUpdates = false;
} }
[GeneratedRegex(@"((git|ssh|http(s)?)|(git@[\w\.-]+))(:(\/\/)?)([\w\.@\:\/\-~]+)((\.git)(\/))?( .+)?")]
private static partial Regex GitRemoteRegex();
#nullable enable
/// <summary>
/// Determines the URL of the git remote.
/// </summary>
public static string? DetermineRemoteUrl()
{
string? remote = Remotes?.FirstOrDefault();
if (remote == null) return null;
Match match = GitRemoteRegex().Match(remote);
if (!match.Success || match.Groups.Count != 12) return null;
return match.Groups[7].Value;
}
#nullable disable
public static string CommitHash { get; set; } public static string CommitHash { get; set; }
public static string Branch { get; set; } public static string Branch { get; set; }
/// <summary> /// <summary>
/// The full revision string. States current revision hash and, if not main, the branch. /// The full revision string. States current revision hash and, if not main, the branch.
/// </summary> /// </summary>
public static string FullRevision { get; set; } private static string FullRevision { get; set; }
/// <summary> /// <summary>
/// The server's branding (environment version) to show to LBP clients. Shows the environment name next to the revision. /// The server's branding (environment version) to show to LBP clients. Shows the environment name next to the
/// revision.
/// </summary> /// </summary>
public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}"; public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}";
public static string FullVersion => $"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}"; public static string FullVersion =>
public static bool IsDirty => CommitHash.EndsWith("-dirty") || CommitsOutOfDate != 1 || CommitHash == "invalid" || Branch == "invalid"; $"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}";
public static bool IsDirty => CommitHash.EndsWith("-dirty") ||
CommitsOutOfDate != 1 ||
CommitHash == "invalid" ||
Branch == "invalid";
public static int CommitsOutOfDate { get; set; } public static int CommitsOutOfDate { get; set; }
public static bool CanCheckForUpdates { get; set; } public static bool CanCheckForUpdates { get; set; }
public static string[] Remotes { get; set; } public static string[] Remotes { get; set; }
public const string Build = public const string Build =
#if DEBUG #if DEBUG
"Debug"; "Debug";
#elif RELEASE #elif RELEASE
"Release"; "Release";
#else #else
"Unknown"; "Unknown";
#endif #endif
} }