-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: wait for release assets uploading
- Loading branch information
1 parent
e0a86f1
commit 4ae9eeb
Showing
7 changed files
with
203 additions
and
82 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/PallasBot.Application.Common/Models/GitHub/GitHubAsset.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PallasBot.Application.Common.Models.GitHub; | ||
|
||
public record GitHubAsset | ||
{ | ||
[JsonPropertyName("id")] | ||
public ulong Id { get; set; } | ||
|
||
[JsonPropertyName("name")] | ||
public string Name { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("size")] | ||
public ulong Size { get; set; } | ||
|
||
[JsonPropertyName("browser_download_url")] | ||
public string BrowserDownloadUrl { get; set; } = string.Empty; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/PallasBot.Application.Common/Models/GitHub/GitHubRelease.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PallasBot.Application.Common.Models.GitHub; | ||
|
||
public record GitHubRelease | ||
{ | ||
[JsonPropertyName("id")] | ||
public ulong Id { get; set; } | ||
|
||
[JsonPropertyName("url")] | ||
public string Url { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("html_url")] | ||
public string HtmlUrl { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("tag_name")] | ||
public string TagName { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("draft")] | ||
public bool Draft { get; set; } | ||
|
||
[JsonPropertyName("prerelease")] | ||
public bool PreRelease { get; set; } | ||
|
||
[JsonPropertyName("assets")] | ||
public List<GitHubAsset> Assets { get; set; } = []; | ||
|
||
[JsonPropertyName("body")] | ||
public string Body { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/PallasBot.Application.Webhook/Consumers/GitHub/MaaReleaseConsumer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Discord; | ||
using Discord.Rest; | ||
using MassTransit; | ||
using Microsoft.Extensions.Logging; | ||
using PallasBot.Application.Common.Services; | ||
using PallasBot.Application.Webhook.Models; | ||
using PallasBot.Domain.Abstract; | ||
using PallasBot.Domain.Constants; | ||
using PallasBot.Domain.Enums; | ||
|
||
namespace PallasBot.Application.Webhook.Consumers.GitHub; | ||
|
||
public class MaaReleaseConsumer : IConsumer<MaaReleaseMqo> | ||
{ | ||
private readonly GitHubApiService _gitHubApiService; | ||
private readonly DiscordRestClient _discordRestClient; | ||
private readonly IDynamicConfigurationService _dynamicConfigurationService; | ||
private readonly ILogger<MaaReleaseConsumer> _logger; | ||
|
||
public MaaReleaseConsumer( | ||
GitHubApiService gitHubApiService, | ||
DiscordRestClient discordRestClient, | ||
IDynamicConfigurationService dynamicConfigurationService, | ||
ILogger<MaaReleaseConsumer> logger) | ||
{ | ||
_gitHubApiService = gitHubApiService; | ||
_discordRestClient = discordRestClient; | ||
_dynamicConfigurationService = dynamicConfigurationService; | ||
_logger = logger; | ||
} | ||
|
||
public async Task Consume(ConsumeContext<MaaReleaseMqo> context) | ||
{ | ||
var m = context.Message; | ||
|
||
var channels = (await _dynamicConfigurationService | ||
.GetAllAsync(DynamicConfigurationKey.MaaReleaseNotificationChannel)) | ||
.Select(x => ulong.TryParse(x.Value, out var v) ? v : (ulong?)null) | ||
.Where(x => x != null) | ||
.Select(x => x!.Value) | ||
.ToList(); | ||
if (channels.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
var releaseAt = m.ReleaseAt; | ||
var shouldCheckAt = releaseAt.AddMinutes(3); | ||
var now = DateTimeOffset.UtcNow; | ||
if (shouldCheckAt > now) | ||
{ | ||
var diff = shouldCheckAt - now; | ||
_logger.LogInformation("Waiting for {Timespan} to check release detail", diff); | ||
await Task.Delay(diff); | ||
} | ||
|
||
var accessToken = await _gitHubApiService.GetGitHubAppAccessTokenAsync(); | ||
var release = await _gitHubApiService.GetReleaseDetailAsync( | ||
MaaConstants.Organization, MaaConstants.MainRepository, m.ReleaseId, accessToken.Token); | ||
|
||
var assetUrls = new Dictionary<string, string>(); | ||
var platforms = GetAssetPlatforms(release.TagName); | ||
foreach (var asset in release.Assets) | ||
{ | ||
if (string.IsNullOrEmpty(asset.Name) is false && platforms.TryGetValue(asset.Name, out var platform)) | ||
{ | ||
var sizeInMegabytes = (double)asset.Size / 1024 / 1024; | ||
assetUrls.Add($"{platform} [{sizeInMegabytes:F1} MB]", asset.BrowserDownloadUrl); | ||
} | ||
} | ||
|
||
var componentBuilder = new ComponentBuilder(); | ||
foreach (var (platform, url) in assetUrls.OrderByDescending(x => x.Value)) | ||
{ | ||
componentBuilder.WithButton( | ||
label: platform, | ||
style: ButtonStyle.Link, | ||
url: url); | ||
} | ||
|
||
var downloadLinkMessage = assetUrls.Count == 0 | ||
? string.Empty | ||
: $"\nOr, download MAA {release.TagName} for your platform by clicking the buttons below."; | ||
|
||
var components = componentBuilder.Build(); | ||
var textMessage = $""" | ||
## 🎉 New MAA Release: ** {release.TagName} ** | ||
Read the full release note [here]({release.HtmlUrl}). | ||
Open or reopen your MAA client to get automatic updates.{downloadLinkMessage} | ||
"""; | ||
|
||
foreach (var channelId in channels) | ||
{ | ||
var channel = (IRestMessageChannel) await _discordRestClient.GetChannelAsync(channelId); | ||
|
||
await channel.SendMessageAsync( | ||
text: textMessage, | ||
components: components); | ||
} | ||
} | ||
|
||
private static Dictionary<string, string> GetAssetPlatforms(string name) | ||
{ | ||
return new Dictionary<string, string> | ||
{ | ||
[$"MAA-{name}-win-x64.zip"] = "Windows (x64)", | ||
[$"MAA-{name}-macos-universal.dmg"] = "macOS (Universal, dmg)", | ||
[$"MAA-{name}-linux-x86_64.tar.gz"] = "Linux (amd64, tar.gz)", | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace PallasBot.Application.Webhook.Models; | ||
|
||
public record MaaReleaseMqo | ||
{ | ||
public required ulong ReleaseId { get; set; } | ||
|
||
public required DateTimeOffset ReleaseAt { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters