Skip to content

Commit

Permalink
use new webhook parser for github
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed May 26, 2024
1 parent 5fd3fda commit 048ca6e
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Flurl;
using Humanizer;
using ZiziBot.Types.Vendor.GitHub;

namespace ZiziBot.Application.Handlers.RestApis.Webhook.Partial;

public class GitHubEventRequest : GitHubEvent, IWebhookRequestBase<bool>
{
}

public class GitHubEventHandler : IRequestHandler<GitHubEventRequest, WebhookResponseBase<bool>>
{
public async Task<WebhookResponseBase<bool>> Handle(GitHubEventRequest request, CancellationToken cancellationToken)
{
var response = new WebhookResponseBase<bool>();

var commits = request.Commits.ToList();
var commitCount = commits.Count;
var repository = request.Repository;
var branchName = request.Ref.Split('/').Last();
var treeUrl = repository.HtmlUrl.AppendPathSegment($"tree/{branchName}");
var commitsStr = "commit".ToQuantity(commitCount);

var htmlMessage = HtmlMessage.Empty
.Url(request.Compare, $"🏗 {commitsStr}").Bold($" to ").Url(repository.HtmlUrl, $"{repository.FullName}")
.Text(":").Url(treeUrl, $"{branchName}")
.Br().Br();

commits.ForEach(commit => {
htmlMessage.Url(commit.Url.ToString(), commit.Id[..7])
.Text(": ")
.TextBr($"{commit.Message} by {commit.Author.Name}");
});

await Task.Delay(0, cancellationToken);

response.FormattedHtml = htmlMessage.ToString();

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Humanizer;
using ZiziBot.Types.Vendor.GitLab;

namespace ZiziBot.Application.Handlers.RestApis.Webhook.GitLab;
namespace ZiziBot.Application.Handlers.RestApis.Webhook.Partial;

public class GitLabEventRequest : GitLabEvent, IWebhookRequestBase<bool>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using ZiziBot.Application.Handlers.RestApis.Webhook.GitLab;
using ZiziBot.Application.Handlers.RestApis.Webhook.Partial;
using ZiziBot.DataSource.MongoEf;
using ZiziBot.DataSource.MongoEf.Entities;

Expand Down Expand Up @@ -84,7 +84,8 @@ CancellationToken cancellationToken
break;
}

var webhookRequest = webhookSource switch {
IWebhookRequestBase<bool>? webhookRequest = webhookSource switch {
WebhookSource.GitHub => content.Deserialize<GitHubEventRequest>(),
WebhookSource.GitLab => content.Deserialize<GitLabEventRequest>(),
_ => default
};
Expand All @@ -104,7 +105,8 @@ CancellationToken cancellationToken
text: webhookResponse.FormattedHtml,
messageThreadId: webhookChat.MessageThreadId,
parseMode: ParseMode.Html,
disableWebPagePreview: true
disableWebPagePreview: true,
cancellationToken: cancellationToken
);

mongoEfContext.WebhookHistory.Add(new WebhookHistoryEntity {
Expand All @@ -113,6 +115,7 @@ CancellationToken cancellationToken
ChatId = webhookChat.ChatId,
MessageId = sentMessage.MessageId,
WebhookSource = WebhookSource.GitHub,
Elapsed = stopwatch.Elapsed,
Payload = content,
Status = EventStatus.Complete
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ public class GithubWebhookEventHandler(
ChatSettingRepository chatSettingRepository
) : GithubWebhookEventProcessor
{
protected override async Task ProcessPushWebhookAsync(WebhookHeaders headers, PushEvent pushEvent)
{
logger.LogInformation("Push event received");

var commits = pushEvent.Commits.ToList();
var commitCount = commits.Count;
var repository = pushEvent.Repository;
var branchName = pushEvent.Ref.Split('/').Last();
var treeUrl = repository.HtmlUrl.AppendPathSegment($"tree/{branchName}");
var commitsStr = "commit".ToQuantity(commitCount);

var htmlMessage = HtmlMessage.Empty
.Url(pushEvent.Compare, $"🏗 {commitsStr}").Bold($" to ").Url(repository.HtmlUrl, $"{repository.FullName}")
.Text(":").Url(treeUrl, $"{branchName}")
.Br().Br();

commits.ForEach(commit => {
htmlMessage.Url(commit.Url.ToString(), commit.Id[..7])
.Text(": ")
.TextBr($"{commit.Message} by {commit.Author.Name}");
});

await SendMessage(htmlMessage.ToString());
}
// protected override async Task ProcessPushWebhookAsync(WebhookHeaders headers, PushEvent pushEvent)
// {
// logger.LogInformation("Push event received");
//
// var commits = pushEvent.Commits.ToList();
// var commitCount = commits.Count;
// var repository = pushEvent.Repository;
// var branchName = pushEvent.Ref.Split('/').Last();
// var treeUrl = repository.HtmlUrl.AppendPathSegment($"tree/{branchName}");
// var commitsStr = "commit".ToQuantity(commitCount);
//
// var htmlMessage = HtmlMessage.Empty
// .Url(pushEvent.Compare, $"🏗 {commitsStr}").Bold($" to ").Url(repository.HtmlUrl, $"{repository.FullName}")
// .Text(":").Url(treeUrl, $"{branchName}")
// .Br().Br();
//
// commits.ForEach(commit => {
// htmlMessage.Url(commit.Url.ToString(), commit.Id[..7])
// .Text(": ")
// .TextBr($"{commit.Message} by {commit.Author.Name}");
// });
//
// await SendMessage(htmlMessage.ToString());
// }

protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent,
PullRequestAction action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class WebhookHistoryEntity : EntityBase
public required int MessageId { get; set; }
public int MessageThreadId { get; set; }
public required WebhookSource WebhookSource { get; set; }
public TimeSpan Elapsed { get; set; }
public required string Payload { get; set; }
}
Loading

0 comments on commit 048ca6e

Please sign in to comment.