From ee042a15dd33503f36a4cee428ac4255d3915a02 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 13 Jan 2024 16:52:44 +0800 Subject: [PATCH 1/6] Follow the OsuIcon and HexaconsIcons, this class should be better to placed into graphics namespace. --- osu.Game.Rulesets.Karaoke/Graphics/{Sprites => }/KaraokeIcon.cs | 2 +- osu.Game.Rulesets.Karaoke/Mods/KaraokeModAutoplayBySinger.cs | 2 +- osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs | 2 +- osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs | 2 +- osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename osu.Game.Rulesets.Karaoke/Graphics/{Sprites => }/KaraokeIcon.cs (93%) diff --git a/osu.Game.Rulesets.Karaoke/Graphics/Sprites/KaraokeIcon.cs b/osu.Game.Rulesets.Karaoke/Graphics/KaraokeIcon.cs similarity index 93% rename from osu.Game.Rulesets.Karaoke/Graphics/Sprites/KaraokeIcon.cs rename to osu.Game.Rulesets.Karaoke/Graphics/KaraokeIcon.cs index 49aca0b3e..6afedda1d 100644 --- a/osu.Game.Rulesets.Karaoke/Graphics/Sprites/KaraokeIcon.cs +++ b/osu.Game.Rulesets.Karaoke/Graphics/KaraokeIcon.cs @@ -4,7 +4,7 @@ using osu.Framework.Graphics.Sprites; using osu.Game.Graphics; -namespace osu.Game.Rulesets.Karaoke.Graphics.Sprites; +namespace osu.Game.Rulesets.Karaoke.Graphics; public static class KaraokeIcon { diff --git a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModAutoplayBySinger.cs b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModAutoplayBySinger.cs index a2b75084e..f32253260 100644 --- a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModAutoplayBySinger.cs +++ b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModAutoplayBySinger.cs @@ -9,7 +9,7 @@ using osu.Framework.Localisation; using osu.Game.Beatmaps; using osu.Game.Extensions; -using osu.Game.Rulesets.Karaoke.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Graphics; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Replays; using osu.Game.Rulesets.Karaoke.UI; diff --git a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs index 77fc8f847..033fe0d6e 100644 --- a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs +++ b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs @@ -3,7 +3,7 @@ using osu.Framework.Graphics.Sprites; using osu.Framework.Localisation; -using osu.Game.Rulesets.Karaoke.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Graphics; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Objects; diff --git a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs index 0ac9e41a1..99659a093 100644 --- a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs +++ b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs @@ -5,7 +5,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Sprites; using osu.Framework.Localisation; -using osu.Game.Rulesets.Karaoke.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Graphics; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Objects.Drawables; using osu.Game.Rulesets.Mods; diff --git a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs index 4b6c984a6..a10fc2a52 100644 --- a/osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs +++ b/osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs @@ -5,7 +5,7 @@ using osu.Framework.Bindables; using osu.Framework.Graphics.Sprites; using osu.Game.Beatmaps; -using osu.Game.Rulesets.Karaoke.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Graphics; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Replays; using osu.Game.Rulesets.Karaoke.UI; From 636819f29c8cd05216801e32917e460eeeb48db7 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 14 Jan 2024 00:48:43 +0800 Subject: [PATCH 2/6] Change the test namespace. --- .../TestSceneKaraokeChangeLogMarkdownContainer.cs | 2 +- .../{ => Changelog}/TestSceneKaraokeChangeLogOverlay.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename osu.Game.Rulesets.Karaoke.Tests/Overlays/{ => Changelog}/TestSceneKaraokeChangeLogMarkdownContainer.cs (97%) rename osu.Game.Rulesets.Karaoke.Tests/Overlays/{ => Changelog}/TestSceneKaraokeChangeLogOverlay.cs (93%) diff --git a/osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogMarkdownContainer.cs b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogMarkdownContainer.cs similarity index 97% rename from osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogMarkdownContainer.cs rename to osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogMarkdownContainer.cs index 518d6b764..59616becf 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogMarkdownContainer.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogMarkdownContainer.cs @@ -11,7 +11,7 @@ using osu.Game.Rulesets.Karaoke.Overlays.Changelog; using osu.Game.Tests.Visual; -namespace osu.Game.Rulesets.Karaoke.Tests.Overlays; +namespace osu.Game.Rulesets.Karaoke.Tests.Overlays.Changelog; [TestFixture] public partial class TestSceneKaraokeChangeLogMarkdownContainer : OsuTestScene diff --git a/osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogOverlay.cs b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogOverlay.cs similarity index 93% rename from osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogOverlay.cs rename to osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogOverlay.cs index c65cb645a..56f3c0d00 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Overlays/TestSceneKaraokeChangeLogOverlay.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/TestSceneKaraokeChangeLogOverlay.cs @@ -5,7 +5,7 @@ using osu.Game.Rulesets.Karaoke.Overlays; using osu.Game.Tests.Visual; -namespace osu.Game.Rulesets.Karaoke.Tests.Overlays; +namespace osu.Game.Rulesets.Karaoke.Tests.Overlays.Changelog; [TestFixture] public partial class TestSceneKaraokeChangeLogOverlay : OsuTestScene From 65e67bd1051a67292ce46b3cb3da0d30e90f9c60 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 13 Jan 2024 17:04:06 +0800 Subject: [PATCH 3/6] Add the comment. --- .../Online/API/Requests/Responses/APIChangelogIndex.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogIndex.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogIndex.cs index bec2284f5..8cc004e0c 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogIndex.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogIndex.cs @@ -8,9 +8,18 @@ namespace osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses; public class APIChangelogIndex { + /// + /// All available builds with no content. + /// public List Builds { get; set; } = new(); + /// + /// All preview builds display in the main page. + /// public List PreviewBuilds { get; set; } = new(); + /// + /// All available years that will be shown in the sidebar. + /// public int[] Years { get; set; } = Array.Empty(); } From 5f019f3124ce8f1e4d39bd028ca94a85522d7f9e Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 13 Jan 2024 23:56:54 +0800 Subject: [PATCH 4/6] Implement the utility for able to query the list/content form the github and get some useful info. --- .../API/Requests/ChangelogRequestUtils.cs | 65 +++++++++++++++++++ .../API/Requests/GetChangelogBuildRequest.cs | 2 +- .../API/Requests/GetChangelogRequest.cs | 36 ++-------- .../API/Requests/GithubChangeLogAPIRequest.cs | 22 +------ 4 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke/Online/API/Requests/ChangelogRequestUtils.cs diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/ChangelogRequestUtils.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/ChangelogRequestUtils.cs new file mode 100644 index 000000000..78eed10f4 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/ChangelogRequestUtils.cs @@ -0,0 +1,65 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Octokit; +using osu.Game.Rulesets.Karaoke.Extensions; + +namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; + +public static class ChangelogRequestUtils +{ + public const string ORGANIZATION_NAME = "karaoke-dev"; + + private const string project_name = $"{ORGANIZATION_NAME}.github.io"; + private const string branch_name = "master"; + private const string changelog_path = "content/changelog"; + + public static Task> GetAllChangelogs(IGitHubClient client) + { + return client + .Repository + .Content + .GetAllContents(ORGANIZATION_NAME, project_name, changelog_path); + } + + public static string GetDocumentUrl(RepositoryContent content) + => $"https://raw.githubusercontent.com/{ORGANIZATION_NAME}/{project_name}/{branch_name}/{content.Path}/"; + + public static string GetRootUrl(RepositoryContent content) + => content.HtmlUrl; + + public static string GetVersion(RepositoryContent content) + => content.Name; + + public static DateTimeOffset GetPublishDateFromName(RepositoryContent content) + { + string? name = content.Name; + var regex = new Regex("(?[-0-9]+).(?[-0-9]{2})(?[-0-9]{2})"); + var result = regex.Match(name); + if (!result.Success) + return DateTimeOffset.MaxValue; + + int year = result.GetGroupValue("year"); + int month = result.GetGroupValue("month"); + int day = result.GetGroupValue("day"); + + return new DateTimeOffset(new DateTime(year, month, day)); + } + + public static async Task GetChangelogContent(IGitHubClient client, string version) + { + string changeLogPath = $"{changelog_path}/{version}/index.md"; + byte[]? content = await client + .Repository + .Content + .GetRawContent(ORGANIZATION_NAME, project_name, changeLogPath) + .ConfigureAwait(false); + + // convert the content to a string + return System.Text.Encoding.UTF8.GetString(content); + } +} diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs index b8d495959..3eec9bad3 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs @@ -18,7 +18,7 @@ public GetChangelogBuildRequest(APIChangelogBuild originBuild) protected override async Task Perform(IGitHubClient client) { - string contentString = await GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); + string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); return CreateBuildWithContent(originBuild, contentString); } } diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs index 5c41fff43..2fd3cad21 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs @@ -1,14 +1,11 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. -using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using System.Threading.Tasks; using Octokit; using osu.Framework.Extensions.IEnumerableExtensions; -using osu.Game.Rulesets.Karaoke.Extensions; using osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses; namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; @@ -31,11 +28,7 @@ protected override async Task Perform(IGitHubClient client) private static async Task> getAllBuilds(IGitHubClient client) { - var reposAscending = await client - .Repository - .Content - .GetAllContents(ORGANIZATION_NAME, PROJECT_NAME, CHANGELOG_PATH) - .ConfigureAwait(false); + var reposAscending = await ChangelogRequestUtils.GetAllChangelogs(client).ConfigureAwait(false); var builds = reposAscending .Reverse() @@ -57,33 +50,16 @@ private static APIChangelogBuild createBuild(RepositoryContent content) { return new APIChangelogBuild { - DocumentUrl = getDocumentUrl(content.Path), - RootUrl = content.HtmlUrl, - Version = content.Name, - PublishedAt = getPublishDateFromName(content.Name), + DocumentUrl = ChangelogRequestUtils.GetDocumentUrl(content), + RootUrl = ChangelogRequestUtils.GetRootUrl(content), + Version = ChangelogRequestUtils.GetVersion(content), + PublishedAt = ChangelogRequestUtils.GetPublishDateFromName(content), }; - - static DateTimeOffset getPublishDateFromName(string name) - { - var regex = new Regex("(?[-0-9]+).(?[-0-9]{2})(?[-0-9]{2})"); - var result = regex.Match(name); - if (!result.Success) - return DateTimeOffset.MaxValue; - - int year = result.GetGroupValue("year"); - int month = result.GetGroupValue("month"); - int day = result.GetGroupValue("day"); - - return new DateTimeOffset(new DateTime(year, month, day)); - } - - string getDocumentUrl(string path) - => $"https://raw.githubusercontent.com/{ORGANIZATION_NAME}/{PROJECT_NAME}/{BRANCH_NAME}/{path}/"; } private static async Task createPreviewBuild(IGitHubClient client, APIChangelogBuild originBuild) { - string contentString = await GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); + string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); return CreateBuildWithContent(originBuild, contentString); } diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs index a56b7f0ba..d0ed6b4f5 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs @@ -1,37 +1,17 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. -using System.Threading.Tasks; -using Octokit; using osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses; namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; public abstract class GithubChangeLogAPIRequest : GithubAPIRequest where T : class { - protected const string ORGANIZATION_NAME = "karaoke-dev"; - protected const string PROJECT_NAME = $"{ORGANIZATION_NAME}.github.io"; - protected const string BRANCH_NAME = "master"; - protected const string CHANGELOG_PATH = "content/changelog"; - protected GithubChangeLogAPIRequest() - : base(ORGANIZATION_NAME) + : base(ChangelogRequestUtils.ORGANIZATION_NAME) { } - protected static async Task GetChangelogContent(IGitHubClient client, string version) - { - string changeLogPath = $"{CHANGELOG_PATH}/{version}/index.md"; - byte[]? content = await client - .Repository - .Content - .GetRawContent(ORGANIZATION_NAME, PROJECT_NAME, changeLogPath) - .ConfigureAwait(false); - - // convert the content to a string - return System.Text.Encoding.UTF8.GetString(content); - } - protected static APIChangelogBuild CreateBuildWithContent(APIChangelogBuild originBuild, string content) { return new APIChangelogBuild From 700cd2a997410f958e769ddb5adaba9588b95e40 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 14 Jan 2024 00:04:42 +0800 Subject: [PATCH 5/6] Follow the `SoloScoreInfo` in the osu.game to move the method into the response class. --- .../API/Requests/GetChangelogBuildRequest.cs | 5 +-- .../API/Requests/GetChangelogRequest.cs | 9 ++++-- .../API/Requests/GithubChangeLogAPIRequest.cs | 31 ------------------- .../Requests/Responses/APIChangelogBuild.cs | 17 ++++++++++ 4 files changed, 27 insertions(+), 35 deletions(-) delete mode 100644 osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs index 3eec9bad3..a80bf5888 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogBuildRequest.cs @@ -7,11 +7,12 @@ namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; -public class GetChangelogBuildRequest : GithubChangeLogAPIRequest +public class GetChangelogBuildRequest : GithubAPIRequest { private readonly APIChangelogBuild originBuild; public GetChangelogBuildRequest(APIChangelogBuild originBuild) + : base(ChangelogRequestUtils.ORGANIZATION_NAME) { this.originBuild = originBuild; } @@ -19,6 +20,6 @@ public GetChangelogBuildRequest(APIChangelogBuild originBuild) protected override async Task Perform(IGitHubClient client) { string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); - return CreateBuildWithContent(originBuild, contentString); + return originBuild.CreateBuildWithContent(contentString); } } diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs index 2fd3cad21..1e210e6c1 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GetChangelogRequest.cs @@ -10,8 +10,13 @@ namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; -public class GetChangelogRequest : GithubChangeLogAPIRequest +public class GetChangelogRequest : GithubAPIRequest { + public GetChangelogRequest() + : base(ChangelogRequestUtils.ORGANIZATION_NAME) + { + } + protected override async Task Perform(IGitHubClient client) { var builds = await getAllBuilds(client).ConfigureAwait(false); @@ -60,7 +65,7 @@ private static APIChangelogBuild createBuild(RepositoryContent content) private static async Task createPreviewBuild(IGitHubClient client, APIChangelogBuild originBuild) { string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false); - return CreateBuildWithContent(originBuild, contentString); + return originBuild.CreateBuildWithContent(contentString); } private static int[] generateYears(IEnumerable builds) diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs deleted file mode 100644 index d0ed6b4f5..000000000 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/GithubChangeLogAPIRequest.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) andy840119 . Licensed under the GPL Licence. -// See the LICENCE file in the repository root for full licence text. - -using osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses; - -namespace osu.Game.Rulesets.Karaoke.Online.API.Requests; - -public abstract class GithubChangeLogAPIRequest : GithubAPIRequest where T : class -{ - protected GithubChangeLogAPIRequest() - : base(ChangelogRequestUtils.ORGANIZATION_NAME) - { - } - - protected static APIChangelogBuild CreateBuildWithContent(APIChangelogBuild originBuild, string content) - { - return new APIChangelogBuild - { - DocumentUrl = originBuild.DocumentUrl, - RootUrl = originBuild.RootUrl, - Version = originBuild.Version, - Content = content, - Versions = - { - Previous = originBuild.Versions.Previous, - Next = originBuild.Versions.Next, - }, - PublishedAt = originBuild.PublishedAt, - }; - } -} diff --git a/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogBuild.cs b/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogBuild.cs index 9773c4a71..ca07baff6 100644 --- a/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogBuild.cs +++ b/osu.Game.Rulesets.Karaoke/Online/API/Requests/Responses/APIChangelogBuild.cs @@ -59,4 +59,21 @@ public class VersionNavigation } public override string ToString() => $"Karaoke! {DisplayVersion}"; + + public APIChangelogBuild CreateBuildWithContent(string content) + { + return new APIChangelogBuild + { + DocumentUrl = DocumentUrl, + RootUrl = RootUrl, + Version = Version, + Content = content, + Versions = + { + Previous = Versions.Previous, + Next = Versions.Next, + }, + PublishedAt = PublishedAt, + }; + } } From b180bd4595bd905dfa47584cf0d940c07c0ebf82 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 14 Jan 2024 11:24:47 +0800 Subject: [PATCH 6/6] Separate the logic for get the pull-request info from the url. --- .../Changelog/ChangelogPullRequestInfoTest.cs | 37 +++++++ .../Changelog/ChangeLogMarkdownContainer.cs | 56 +++-------- .../Changelog/ChangelogPullRequestInfo.cs | 97 +++++++++++++++++++ 3 files changed, 147 insertions(+), 43 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/ChangelogPullRequestInfoTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangelogPullRequestInfo.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/ChangelogPullRequestInfoTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/ChangelogPullRequestInfoTest.cs new file mode 100644 index 000000000..bce06d127 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/Overlays/Changelog/ChangelogPullRequestInfoTest.cs @@ -0,0 +1,37 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Linq; +using NUnit.Framework; +using osu.Game.Rulesets.Karaoke.Overlays.Changelog; + +namespace osu.Game.Rulesets.Karaoke.Tests.Overlays.Changelog; + +public class ChangelogPullRequestInfoTest +{ + [TestCase("#2152@andy840119", new[] { 2152 }, new[] { "andy840119" })] + [TestCase("#2152", new[] { 2152 }, new string[] { })] + [TestCase("@andy840119", new int[] { }, new[] { "andy840119" })] + [TestCase("#2152#2153", new[] { 2152, 2153 }, new string[] { })] + [TestCase("#2152#2152", new[] { 2152 }, new string[] { })] + [TestCase("@andy@andy840119", new int[] { }, new[] { "andy", "andy840119" })] + [TestCase("@andy840119@andy840119", new int[] { }, new[] { "andy840119" })] + [TestCase("https://raw.githubusercontent.com/karaoke-dev/karaoke-dev.github.io/master/content/changelog/2023.1212/#2152@andy840119", new[] { 2152 }, new[] { "andy840119" })] // the actual data that will be get in this method. + public void TestGetPullRequestInfoFromLink(string url, int[] expectedPrs, string[] expectedUserNames) + { + var result = ChangelogPullRequestInfo.GetPullRequestInfoFromLink("karaoke", url)!; + + Assert.AreEqual(expectedPrs, result.PullRequests.Select(x => x.Number)); + Assert.AreEqual(expectedUserNames, result.Users.Select(x => x.UserName)); + } + + [TestCase("unknown_repo", "#2152@andy840119")] // "unknown_repo" does not in the repo list. + [TestCase("karaoke", "")] // there's no pr number or username. + [TestCase("karaoke", "hello")] // invalid pr number or username. + [TestCase("karaoke", "#aaa")] // invalid pr number. + public void TestTestGetPullRequestInfoFromLinkWithNull(string link, string url) + { + var result = ChangelogPullRequestInfo.GetPullRequestInfoFromLink(link, url); + Assert.IsNull(result); + } +} diff --git a/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangeLogMarkdownContainer.cs b/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangeLogMarkdownContainer.cs index 66d094fab..c1ce7c354 100644 --- a/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangeLogMarkdownContainer.cs +++ b/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangeLogMarkdownContainer.cs @@ -2,16 +2,13 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using Markdig.Syntax.Inlines; using osu.Framework.Graphics; using osu.Framework.Graphics.Sprites; using osu.Framework.Layout; using osu.Game.Graphics; using osu.Game.Graphics.Containers.Markdown; -using osu.Game.Rulesets.Karaoke.Extensions; using osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses; using osuTK; @@ -40,78 +37,51 @@ public partial class ChangeLogMarkdownTextFlowContainer : OsuMarkdownTextFlowCon { protected override void AddImage(LinkInline linkInline) => AddDrawable(new ChangeLogMarkdownImage(linkInline)); - private readonly IDictionary githubUrls = new Dictionary - { - { "karaoke", "https://github.com/karaoke-dev/karaoke/" }, - { "edge", "https://github.com/karaoke-dev/karaoke/" }, - { "github.io", "https://github.com/karaoke-dev/karaoke-dev.github.io/" }, - { "launcher", "https://github.com/karaoke-dev/launcher/" }, - { "sample", "https://github.com/karaoke-dev/sample-beatmap/" }, - { "microphone-package", "https://github.com/karaoke-dev/osu-framework-microphone/" }, - { "font-package", "https://github.com/karaoke-dev/osu-framework-font/" }, - }; - protected override void AddLinkText(string text, LinkInline linkInline) { if (linkInline.Url == null) return; - if (!githubUrls.ContainsKey(text)) + var pullRequestInfo = ChangelogPullRequestInfo.GetPullRequestInfoFromLink(text, linkInline.Url); + + if (pullRequestInfo == null) { base.AddLinkText(text, linkInline); return; } - var baseUri = new Uri(githubUrls[text]); - - // Get hash tag with number - const string issue_regex = "#(?[0-9]+)|@(?[0-9A-z]+)"; - var result = Regex.Matches(linkInline.Url, issue_regex, RegexOptions.IgnoreCase); - - if (!result.Any()) - return; + var pullRequests = pullRequestInfo.PullRequests; + var users = pullRequestInfo.Users; - // add issue if has user - string[] issues = result.Select(x => x.GetGroupValue("issue")).Where(x => !string.IsNullOrEmpty(x)).ToArray(); - - if (issues.Any()) + if (pullRequests.Any()) { AddText("("); - foreach (string issue in issues) + foreach (var pullRequest in pullRequests) { - if (string.IsNullOrEmpty(issue)) - continue; - - AddDrawable(new OsuMarkdownLinkText($"{text}#{issue}", new LinkInline + AddDrawable(new OsuMarkdownLinkText($"{text}#{pullRequest.Number}", new LinkInline { - Url = new Uri(baseUri, $"pull/{issue}").AbsoluteUri, + Url = pullRequest.Url, })); - if (issue != issues.LastOrDefault()) + if (pullRequest != pullRequests.LastOrDefault()) AddText(", "); } AddText(")"); } - // add use name if has user - var usernames = result.Select(x => x.GetGroupValue("username")).Where(x => !string.IsNullOrEmpty(x)); - - foreach (string user in usernames) + foreach (var user in users) { - if (string.IsNullOrEmpty(user)) - return; - var textScale = new Vector2(0.7f); AddText(" by ", t => { t.Scale = textScale; t.Padding = new MarginPadding { Top = 6 }; }); - AddDrawable(new UserLinkText(user, new LinkInline + AddDrawable(new UserLinkText(user.UserName, new LinkInline { - Url = $"https://github.com/{user}", + Url = user.UserUrl, }) { Scale = textScale, diff --git a/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangelogPullRequestInfo.cs b/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangelogPullRequestInfo.cs new file mode 100644 index 000000000..cab7994bc --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Overlays/Changelog/ChangelogPullRequestInfo.cs @@ -0,0 +1,97 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using osu.Game.Rulesets.Karaoke.Extensions; + +namespace osu.Game.Rulesets.Karaoke.Overlays.Changelog; + +public class ChangelogPullRequestInfo +{ + private static readonly IDictionary repo_urls = new Dictionary + { + { "karaoke", "https://github.com/karaoke-dev/karaoke/" }, + { "edge", "https://github.com/karaoke-dev/karaoke/" }, + { "github.io", "https://github.com/karaoke-dev/karaoke-dev.github.io/" }, + { "launcher", "https://github.com/karaoke-dev/launcher/" }, + { "sample", "https://github.com/karaoke-dev/sample-beatmap/" }, + { "microphone-package", "https://github.com/karaoke-dev/osu-framework-microphone/" }, + { "font-package", "https://github.com/karaoke-dev/osu-framework-font/" }, + }; + + public PullRequestInfo[] PullRequests { get; set; } = Array.Empty(); + + public UserInfo[] Users { get; set; } = Array.Empty(); + + public class PullRequestInfo + { + public int Number { get; set; } + + public string Url { get; set; } = string.Empty; + } + + public class UserInfo + { + public string UserName { get; set; } = string.Empty; + + public string UserUrl => $"https://github.com/{UserName}"; + } + + /// + /// Trying to parse the pull-request info from the url. + /// + /// + /// #2152@andy840119
+ /// #2152
+ /// #2152#2153
+ /// @andy840119
+ /// @andy@andy840119 + ///
+ /// Link text + /// Link url + /// + public static ChangelogPullRequestInfo? GetPullRequestInfoFromLink(string text, string url) + { + if (!repo_urls.ContainsKey(text)) + return null; + + const string pull_request_key = "pull_request"; + const string username_key = "username"; + const string pull_request_regex = $"#(?<{pull_request_key}>[0-9]+)|@(?<{username_key}>[0-9A-z]+)"; + + // note: should have at least one pr number or one username, + var result = Regex.Matches(url, pull_request_regex, RegexOptions.IgnoreCase); + if (!result.Any()) + return null; + + // get pull-request or username + var prs = result.Select(x => x.GetGroupValue(pull_request_key)) + .Where(x => !string.IsNullOrEmpty(x)) + .Distinct(); + var usernames = result.Select(x => x.GetGroupValue(username_key)) + .Where(x => !string.IsNullOrEmpty(x)) + .Distinct(); + + return new ChangelogPullRequestInfo + { + PullRequests = prs.Select(x => generatePullRequestInfo(repo_urls[text], x)).ToArray(), + Users = usernames.Select(generateUserInfo).ToArray(), + }; + + static PullRequestInfo generatePullRequestInfo(string url, string pr) => + new() + { + Number = int.Parse(pr), + Url = new Uri(new Uri(url), $"pull/{pr}").AbsoluteUri, + }; + + static UserInfo generateUserInfo(string username) => + new() + { + UserName = username, + }; + } +}