From e2225fd3785749035e2ae6c36160477c8d2e48a3 Mon Sep 17 00:00:00 2001 From: Thomas Duft Date: Mon, 19 Feb 2024 09:29:22 +0100 Subject: [PATCH] Added utility class UserNameProvider --- notes.txt | 1 + releasy.sln | 19 ++++++++------- samples/.gitkeep | 0 src/releasy/Changelog/ChangelogEntry.cs | 2 +- src/releasy/Releasenotes/ReleaseNoteEntry.cs | 2 +- src/releasy/Utils/UserNameProvider.cs | 25 ++++++++++++++++++++ 6 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 samples/.gitkeep create mode 100644 src/releasy/Utils/UserNameProvider.cs diff --git a/notes.txt b/notes.txt index f49daf8..552660d 100644 --- a/notes.txt +++ b/notes.txt @@ -1,5 +1,6 @@ TODO: +- ... ---------------------------------------------------------------------------------------------------- Links: diff --git a/releasy.sln b/releasy.sln index 7ed5d12..42912b4 100644 --- a/releasy.sln +++ b/releasy.sln @@ -7,27 +7,30 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5B165FC2-791 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "releasy", "src\releasy\releasy.csproj", "{465C7145-7B4D-4476-B49A-E0BE796FEDFF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "targets", "build\targets.csproj", "{BFC41007-C126-4638-ABE3-7AF6ECB99448}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{17E01B48-449A-4AF6-8B34-4F80A343BC69}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "targets", "build\targets.csproj", "{68F97F92-9ED2-4BC7-BBA7-C2A63911FD26}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {465C7145-7B4D-4476-B49A-E0BE796FEDFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {465C7145-7B4D-4476-B49A-E0BE796FEDFF}.Debug|Any CPU.Build.0 = Debug|Any CPU {465C7145-7B4D-4476-B49A-E0BE796FEDFF}.Release|Any CPU.ActiveCfg = Release|Any CPU {465C7145-7B4D-4476-B49A-E0BE796FEDFF}.Release|Any CPU.Build.0 = Release|Any CPU - {BFC41007-C126-4638-ABE3-7AF6ECB99448}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BFC41007-C126-4638-ABE3-7AF6ECB99448}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BFC41007-C126-4638-ABE3-7AF6ECB99448}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BFC41007-C126-4638-ABE3-7AF6ECB99448}.Release|Any CPU.Build.0 = Release|Any CPU + {68F97F92-9ED2-4BC7-BBA7-C2A63911FD26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68F97F92-9ED2-4BC7-BBA7-C2A63911FD26}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68F97F92-9ED2-4BC7-BBA7-C2A63911FD26}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68F97F92-9ED2-4BC7-BBA7-C2A63911FD26}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {465C7145-7B4D-4476-B49A-E0BE796FEDFF} = {5B165FC2-791D-48B7-8999-024A385AB207} + {68F97F92-9ED2-4BC7-BBA7-C2A63911FD26} = {17E01B48-449A-4AF6-8B34-4F80A343BC69} EndGlobalSection EndGlobal diff --git a/samples/.gitkeep b/samples/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/releasy/Changelog/ChangelogEntry.cs b/src/releasy/Changelog/ChangelogEntry.cs index 97d455a..6540407 100644 --- a/src/releasy/Changelog/ChangelogEntry.cs +++ b/src/releasy/Changelog/ChangelogEntry.cs @@ -8,7 +8,7 @@ internal sealed class ChangelogEntry public string Tag { get; set; } = string.Empty; public string Message { get; set; } = string.Empty; public DateTime CreatedAt { get; set; } = DateTime.Now; - public string CreatedBy { get; set; } = Environment.UserName; + public string CreatedBy { get; set; } = UserNameProvider.GetUserName(); public static ChangelogEntry Create( string issueId, diff --git a/src/releasy/Releasenotes/ReleaseNoteEntry.cs b/src/releasy/Releasenotes/ReleaseNoteEntry.cs index e186b45..367b7ae 100644 --- a/src/releasy/Releasenotes/ReleaseNoteEntry.cs +++ b/src/releasy/Releasenotes/ReleaseNoteEntry.cs @@ -9,7 +9,7 @@ internal sealed class ReleaseNoteEntry public string Message { get; set; } = string.Empty; public string[] Instructions { get; set; } = []; public DateTime CreatedAt { get; set; } = DateTime.Now; - public string CreatedBy { get; set; } = Environment.UserName; + public string CreatedBy { get; set; } = UserNameProvider.GetUserName(); public static ReleaseNoteEntry Create( string issueId, diff --git a/src/releasy/Utils/UserNameProvider.cs b/src/releasy/Utils/UserNameProvider.cs new file mode 100644 index 0000000..1d1d22e --- /dev/null +++ b/src/releasy/Utils/UserNameProvider.cs @@ -0,0 +1,25 @@ +using System.Diagnostics; + +namespace tomware.Releasy; + +public static class UserNameProvider +{ + public static string GetUserName() + { + // 1. try to read the user name from the git username config + using Process proc = new(); + proc.StartInfo.FileName = "git"; + proc.StartInfo.Arguments = "config user.name"; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.Start(); + string? userName = proc.StandardOutput + .ReadToEnd() + .Replace(Environment.NewLine, string.Empty) + .Replace("\n", string.Empty); + proc.WaitForExit(); + + // 2. try to read the user name from the environment + return userName ?? Environment.UserName; + } +}