Skip to content

Commit

Permalink
add !pr link command
Browse files Browse the repository at this point in the history
fixes #860
  • Loading branch information
13xforever committed Dec 6, 2022
1 parent 3fd57ab commit 5a802a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
18 changes: 11 additions & 7 deletions CompatBot/Commands/Pr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ internal sealed class Pr: BaseCommandModuleCustom
result.Append('`').Append($"{("#" + pr.Number).PadLeft(maxNum)} by {pr.User?.Login?.PadRightVisible(maxAuthor)}: {pr.Title?.Trim(maxTitleLength).PadRightVisible(maxTitle)}".FixSpaces()).AppendLine($"` <{pr.HtmlUrl}>");
await responseChannel.SendAutosplitMessageAsync(result, blockStart: null, blockEnd: null).ConfigureAwait(false);
}


[Command("link"), Aliases("old")]
[Description("Links to the official binary builds produced after specified PR was merged")]
public Task Link(CommandContext ctx, [Description("PR number")] int pr) => LinkPrBuild(ctx.Client, ctx.Message, pr, true);

#if DEBUG
[Command("stats"), RequiresBotModRole]
public async Task Stats(CommandContext ctx)
Expand All @@ -108,7 +112,7 @@ await ctx.Channel.SendMessageAsync(
}
#endif

public static async Task LinkPrBuild(DiscordClient client, DiscordMessage message, int pr)
public static async Task LinkPrBuild(DiscordClient client, DiscordMessage message, int pr, bool linkOld = false)
{
var prInfo = await GithubClient.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);
if (prInfo is null or {Number: 0})
Expand All @@ -120,7 +124,7 @@ public static async Task LinkPrBuild(DiscordClient client, DiscordMessage messag
var (state, _) = prInfo.GetState();
var embed = prInfo.AsEmbed();
var azureClient = Config.GetAzureDevOpsClient();
if (state == "Open" || state == "Closed")
if (state is "Open" or "Closed")
{
var windowsDownloadHeader = "Windows PR Build";
var linuxDownloadHeader = "Linux PR Build";
Expand Down Expand Up @@ -265,15 +269,15 @@ public static async Task LinkPrBuild(DiscordClient client, DiscordMessage messag
if (!string.IsNullOrEmpty(buildTime))
embed.WithFooter(buildTime);
}
else if (state == "Merged" && azureClient is not null)
else if (state is "Merged" && azureClient is not null)
{
var mergeTime = prInfo.MergedAt.GetValueOrDefault();
var now = DateTime.UtcNow;
var updateInfo = await CompatApiClient.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
if (updateInfo != null)
var updateInfo = await CompatApiClient.GetUpdateAsync(Config.Cts.Token, linkOld ? prInfo.MergeCommitSha : null).ConfigureAwait(false);
if (updateInfo is not null)
{
if (DateTime.TryParse(updateInfo.LatestBuild?.Datetime, out var masterBuildTime) && masterBuildTime.Ticks >= mergeTime.Ticks)
embed = await updateInfo.AsEmbedAsync(client, false, embed, prInfo).ConfigureAwait(false);
embed = await updateInfo.AsEmbedAsync(client, false, embed, prInfo, linkOld).ConfigureAwait(false);
else
{
var waitTime = TimeSpan.FromMinutes(5);
Expand Down
26 changes: 17 additions & 9 deletions CompatBot/Utils/ResultFormatters/UpdateInfoFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ internal static class UpdateInfoFormatter
{
private static readonly GithubClient.Client GithubClient = new(Config.GithubToken);

public static async Task<DiscordEmbedBuilder> AsEmbedAsync(this UpdateInfo? info, DiscordClient client, bool includePrBody = false, DiscordEmbedBuilder? builder = null, Octokit.PullRequest? currentPrInfo = null)
public static async Task<DiscordEmbedBuilder> AsEmbedAsync(this UpdateInfo? info, DiscordClient client, bool includePrBody = false, DiscordEmbedBuilder? builder = null, Octokit.PullRequest? currentPrInfo = null, bool useCurrent = false)
{
if ((info?.LatestBuild?.Windows?.Download ?? info?.LatestBuild?.Linux?.Download ?? info?.LatestBuild?.Mac?.Download) is null)
return builder ?? new DiscordEmbedBuilder {Title = "Error", Description = "Error communicating with the update API. Try again later.", Color = Config.Colors.Maintenance};

var justAppend = builder != null;
var latestBuild = info!.LatestBuild;
var currentBuild = info.CurrentBuild;
var latestPr = latestBuild?.Pr;
var currentPr = info.CurrentBuild?.Pr;
var currentPr = currentBuild?.Pr;
string? url = null;
Octokit.PullRequest? latestPrInfo = null;

Expand Down Expand Up @@ -135,10 +136,12 @@ public static async Task<DiscordEmbedBuilder> AsEmbedAsync(this UpdateInfo? info
}
}

if (!string.IsNullOrEmpty(latestBuild?.Datetime))
var linkedBuild = useCurrent ? currentBuild : latestBuild;
if (!string.IsNullOrEmpty(linkedBuild?.Datetime))
{
var timestampInfo = latestBuildTimestamp?.ToString("u") ?? latestBuild.Datetime;
if (currentPr > 0
var timestampInfo = (useCurrent ? currentBuildTimestamp : latestBuildTimestamp)?.ToString("u") ?? linkedBuild.Datetime;
if (!useCurrent
&& currentPr > 0
&& currentPr != latestPr
&& GetUpdateDelta(latestBuildTimestamp, currentBuildTimestamp) is TimeSpan timeDelta)
timestampInfo += $" ({timeDelta.AsTimeDeltaDescription()} newer)";
Expand All @@ -148,13 +151,18 @@ public static async Task<DiscordEmbedBuilder> AsEmbedAsync(this UpdateInfo? info
timestampInfo += $" ({(DateTime.UtcNow - latestBuildTimestamp.Value).AsTimeDeltaDescription()} ago)";

if (justAppend)
builder.AddField("Latest master build", "This pull request has been merged, and is a part of `master` now");
{
if (useCurrent)
builder.AddField("Archived official build", "This is an **old** archived build for the specified pull request");
else
builder.AddField("Latest official release", "This pull request has been merged, and is a part of the latest official build");
}
builder.WithFooter($"{buildTimestampKind} on {timestampInfo}");
}
return builder
.AddField("Windows download", GetLinkMessage(latestBuild?.Windows, true), true)
.AddField("Linux download", GetLinkMessage(latestBuild?.Linux, true), true)
.AddField("Mac download", GetLinkMessage(latestBuild?.Mac, true), true);
.AddField("Windows download", GetLinkMessage(linkedBuild?.Windows, true), true)
.AddField("Linux download", GetLinkMessage(linkedBuild?.Linux, true), true)
.AddField("Mac download", GetLinkMessage(linkedBuild?.Mac, true), true);
}

private static string GetLinkMessage(BuildLink? link, bool simpleName)
Expand Down

0 comments on commit 5a802a5

Please sign in to comment.