Skip to content

Commit

Permalink
Refactor the code to make it easy to understand.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Sep 10, 2024
1 parent d5474f9 commit b4e7e96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,33 @@ protected override void AddLinkText(string text, LinkInline linkInline)

var pullRequestInfo = ChangelogPullRequestInfo.GetPullRequestInfoFromLink(text, linkInline.Url);

if (pullRequestInfo == null)
if (pullRequestInfo != null)
{
base.AddLinkText(text, linkInline);
addPullRequestInfo(pullRequestInfo);
return;
}

base.AddLinkText(text, linkInline);
}

private void addPullRequestInfo(ChangelogPullRequestInfo pullRequestInfo)
{
var pullRequests = pullRequestInfo.PullRequests;
var users = pullRequestInfo.Users;

if (pullRequests.Any())
{
AddText("(");

foreach (var pullRequest in pullRequests)
for (int index = 0; index < pullRequests.Length; index++)
{
AddDrawable(new OsuMarkdownLinkText($"{text}#{pullRequest.Number}", new LinkInline
var pullRequest = pullRequests[index];
AddDrawable(new OsuMarkdownLinkText($"{pullRequestInfo.RepoName}#{pullRequest.Number}", new LinkInline
{
Url = pullRequest.Url,
}));

if (pullRequest != pullRequests.LastOrDefault())
if (index != pullRequests.Length - 1)
AddText(", ");
}

Expand All @@ -81,7 +87,7 @@ protected override void AddLinkText(string text, LinkInline linkInline)
});
AddDrawable(new UserLinkText(user.UserName, new LinkInline
{
Url = user.UserUrl,
Url = user.Url,
})
{
Scale = textScale,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,35 @@ public class ChangelogPullRequestInfo
{ "font-package", "https://github.com/karaoke-dev/osu-framework-font/" },
};

public PullRequestInfo[] PullRequests { get; set; } = Array.Empty<PullRequestInfo>();
public string RepoName { get; init; } = string.Empty;

public UserInfo[] Users { get; set; } = Array.Empty<UserInfo>();
public PullRequestInfo[] PullRequests { get; init; } = Array.Empty<PullRequestInfo>();

public class PullRequestInfo
public UserInfo[] Users { get; init; } = Array.Empty<UserInfo>();

public readonly struct PullRequestInfo
{
public int Number { get; set; }
public PullRequestInfo(string repoName, int number)
{
Number = number;
Url = new Uri(new Uri(repo_urls[repoName]), $"pull/{number}").AbsoluteUri;
}

public int Number { get; } = 0;

public string Url { get; set; } = string.Empty;
public string Url { get; } = string.Empty;
}

public class UserInfo
public readonly struct UserInfo
{
public string UserName { get; set; } = string.Empty;
public UserInfo(string useName)
{
UserName = useName;
}

public string UserName { get; } = string.Empty;

public string UserUrl => $"https://github.com/{UserName}";
public string Url => $"https://github.com/{UserName}";
}

/// <summary>
Expand All @@ -50,20 +63,20 @@ public class UserInfo
/// @andy840119<br/>
/// @andy@andy840119
/// </example>
/// <param name="text">Link text</param>
/// <param name="url">Link url</param>
/// <param name="repo">Link text</param>
/// <param name="info">Link url</param>
/// <returns></returns>
public static ChangelogPullRequestInfo? GetPullRequestInfoFromLink(string text, string url)
public static ChangelogPullRequestInfo? GetPullRequestInfoFromLink(string repo, string info)
{
if (!repo_urls.ContainsKey(text))
if (!repo_urls.ContainsKey(repo))
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);
var result = Regex.Matches(info, pull_request_regex, RegexOptions.IgnoreCase);
if (!result.Any())
return null;

Expand All @@ -77,21 +90,9 @@ public class UserInfo

return new ChangelogPullRequestInfo
{
PullRequests = prs.Select(x => generatePullRequestInfo(repo_urls[text], x)).ToArray(),
Users = usernames.Select(generateUserInfo).ToArray(),
RepoName = repo,
PullRequests = prs.Select(pr => new PullRequestInfo(repo, int.Parse(pr))).ToArray(),
Users = usernames.Select(userName => new UserInfo(userName)).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,
};
}
}

0 comments on commit b4e7e96

Please sign in to comment.