Skip to content

Commit

Permalink
Merge pull request #2164 from andy840119/refactor-the-changelog-again
Browse files Browse the repository at this point in the history
Refactor the changelog again.
  • Loading branch information
andy840119 committed Jan 14, 2024
2 parents 5805806 + b180bd4 commit 620ecc7
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) andy840119 <andy840119@gmail.com>. 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Karaoke/Mods/KaraokeModDisableNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Karaoke/Mods/KaraokeModHiddenNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Karaoke/Mods/KaraokeModPractice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) andy840119 <andy840119@gmail.com>. 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<IReadOnlyList<RepositoryContent>> 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("(?<year>[-0-9]+).(?<month>[-0-9]{2})(?<day>[-0-9]{2})");
var result = regex.Match(name);
if (!result.Success)
return DateTimeOffset.MaxValue;

int year = result.GetGroupValue<int>("year");
int month = result.GetGroupValue<int>("month");
int day = result.GetGroupValue<int>("day");

return new DateTimeOffset(new DateTime(year, month, day));
}

public static async Task<string> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

namespace osu.Game.Rulesets.Karaoke.Online.API.Requests;

public class GetChangelogBuildRequest : GithubChangeLogAPIRequest<APIChangelogBuild>
public class GetChangelogBuildRequest : GithubAPIRequest<APIChangelogBuild>
{
private readonly APIChangelogBuild originBuild;

public GetChangelogBuildRequest(APIChangelogBuild originBuild)
: base(ChangelogRequestUtils.ORGANIZATION_NAME)
{
this.originBuild = originBuild;
}

protected override async Task<APIChangelogBuild> Perform(IGitHubClient client)
{
string contentString = await GetChangelogContent(client, originBuild.Version).ConfigureAwait(false);
return CreateBuildWithContent(originBuild, contentString);
string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false);
return originBuild.CreateBuildWithContent(contentString);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Copyright (c) andy840119 <andy840119@gmail.com>. 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;

public class GetChangelogRequest : GithubChangeLogAPIRequest<APIChangelogIndex>
public class GetChangelogRequest : GithubAPIRequest<APIChangelogIndex>
{
public GetChangelogRequest()
: base(ChangelogRequestUtils.ORGANIZATION_NAME)
{
}

protected override async Task<APIChangelogIndex> Perform(IGitHubClient client)
{
var builds = await getAllBuilds(client).ConfigureAwait(false);
Expand All @@ -31,11 +33,7 @@ protected override async Task<APIChangelogIndex> Perform(IGitHubClient client)

private static async Task<List<APIChangelogBuild>> 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()
Expand All @@ -57,34 +55,17 @@ 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("(?<year>[-0-9]+).(?<month>[-0-9]{2})(?<day>[-0-9]{2})");
var result = regex.Match(name);
if (!result.Success)
return DateTimeOffset.MaxValue;

int year = result.GetGroupValue<int>("year");
int month = result.GetGroupValue<int>("month");
int day = result.GetGroupValue<int>("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<APIChangelogBuild> createPreviewBuild(IGitHubClient client, APIChangelogBuild originBuild)
{
string contentString = await GetChangelogContent(client, originBuild.Version).ConfigureAwait(false);
return CreateBuildWithContent(originBuild, contentString);
string contentString = await ChangelogRequestUtils.GetChangelogContent(client, originBuild.Version).ConfigureAwait(false);
return originBuild.CreateBuildWithContent(contentString);
}

private static int[] generateYears(IEnumerable<APIChangelogBuild> builds)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ namespace osu.Game.Rulesets.Karaoke.Online.API.Requests.Responses;

public class APIChangelogIndex
{
/// <summary>
/// All available builds with no content.
/// </summary>
public List<APIChangelogBuild> Builds { get; set; } = new();

/// <summary>
/// All preview builds display in the main page.
/// </summary>
public List<APIChangelogBuild> PreviewBuilds { get; set; } = new();

/// <summary>
/// All available years that will be shown in the sidebar.
/// </summary>
public int[] Years { get; set; } = Array.Empty<int>();
}
Loading

0 comments on commit 620ecc7

Please sign in to comment.