Skip to content

Commit

Permalink
webhook history
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed May 26, 2024
1 parent 11598d2 commit 52bd95e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public async Task<ApiResponseBase<PostWebhookPayloadResponseDto>> Handle(PostWeb
return response.BadRequest("Webhook payload is empty");
}

var botSetting = await appSettingRepository.GetBotMain();

var webhookChat = await chatSettingRepository.GetWebhookRouteById(request.targetId);

if (webhookChat == null)
Expand All @@ -64,9 +62,8 @@ public async Task<ApiResponseBase<PostWebhookPayloadResponseDto>> Handle(PostWeb
{
case WebhookSource.GitHub:
githubWebhookEventProcessor.RouteId = webhookChat.RouteId;
githubWebhookEventProcessor.ChatId = webhookChat.ChatId;
githubWebhookEventProcessor.ThreadId = webhookChat.MessageThreadId;
githubWebhookEventProcessor.Token = botSetting.Token;
githubWebhookEventProcessor.Payload = request.Content.ToString();
githubWebhookEventProcessor.TransactionId = $"{request.TransactionId}";

await githubWebhookEventProcessor.ProcessWebhookAsync(request.Headers, request.Content.ToString());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
using Octokit.Webhooks.Events.PullRequest;
using Octokit.Webhooks.Events.Star;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using ZiziBot.DataSource.MongoEf;
using ZiziBot.DataSource.MongoEf.Entities;

namespace ZiziBot.Application.Handlers.Telegram.WebHook.GitHub;

public class GithubWebhookEventHandler : GithubWebhookEventProcessor
public class GithubWebhookEventHandler(
ILogger<GithubWebhookEventHandler> logger,
MongoEfContext mongoEfContext,
AppSettingRepository appSettingRepository,
ChatSettingRepository chatSettingRepository
) : GithubWebhookEventProcessor
{
private readonly ILogger<GithubWebhookEventHandler> _logger;

public GithubWebhookEventHandler(ILogger<GithubWebhookEventHandler> logger)
{
_logger = logger;
}

protected override async Task ProcessPushWebhookAsync(WebhookHeaders headers, PushEvent pushEvent)
{
_logger.LogInformation("Push event received");
logger.LogInformation("Push event received");

var commits = pushEvent.Commits.ToList();
var commitCount = commits.Count;
Expand All @@ -33,7 +34,8 @@ protected override async Task ProcessPushWebhookAsync(WebhookHeaders headers, Pu
var commitsStr = "commit".ToQuantity(commitCount);

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

commits.ForEach(commit => {
Expand All @@ -45,7 +47,8 @@ protected override async Task ProcessPushWebhookAsync(WebhookHeaders headers, Pu
await SendMessage(htmlMessage.ToString());
}

protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent, PullRequestAction action)
protected override Task ProcessPullRequestWebhookAsync(WebhookHeaders headers, PullRequestEvent pullRequestEvent,
PullRequestAction action)
{
var htmlMessage = HtmlMessage.Empty;
var repository = pullRequestEvent.Repository;
Expand Down Expand Up @@ -89,7 +92,8 @@ protected override Task ProcessStatusWebhookAsync(WebhookHeaders headers, Status
return SendMessage(htmlMessage.ToString());
}

protected override Task ProcessDeploymentStatusWebhookAsync(WebhookHeaders headers, DeploymentStatusEvent deploymentStatusEvent, DeploymentStatusAction action)
protected override Task ProcessDeploymentStatusWebhookAsync(WebhookHeaders headers,
DeploymentStatusEvent deploymentStatusEvent, DeploymentStatusAction action)
{
var htmlMessage = HtmlMessage.Empty;
var repository = deploymentStatusEvent.Repository;
Expand All @@ -104,7 +108,8 @@ protected override Task ProcessDeploymentStatusWebhookAsync(WebhookHeaders heade
return SendMessage(htmlMessage.ToString());
}

protected override Task ProcessDeploymentWebhookAsync(WebhookHeaders headers, DeploymentEvent deploymentEvent, DeploymentAction action)
protected override Task ProcessDeploymentWebhookAsync(WebhookHeaders headers, DeploymentEvent deploymentEvent,
DeploymentAction action)
{
var htmlMessage = HtmlMessage.Empty;
var repository = deploymentEvent.Repository;
Expand All @@ -121,30 +126,48 @@ protected override Task ProcessDeploymentWebhookAsync(WebhookHeaders headers, De

private async Task SendMessage(string message)
{
var botClient = new TelegramBotClient(Token);
Message sentMessage = new();
var botSetting = await appSettingRepository.GetBotMain();
var webhookChat = await chatSettingRepository.GetWebhookRouteById(RouteId);
var botClient = new TelegramBotClient(botSetting.Token);

if (webhookChat == null)
return;

try
{
await botClient.SendTextMessageAsync(
chatId: ChatId,
sentMessage = await botClient.SendTextMessageAsync(
chatId: webhookChat.ChatId,
text: message.MdToHtml(),
messageThreadId: ThreadId,
messageThreadId: webhookChat.MessageThreadId,
parseMode: ParseMode.Html,
disableWebPagePreview: true
);
}
catch (Exception exception)
{
_logger.LogWarning("Trying send GitHub Webhook without thread to ChatId: {ChatId}", ChatId);
if (exception.Message.Contains("thread not found"))
{
await botClient.SendTextMessageAsync(
chatId: ChatId,
logger.LogWarning("Trying send GitHub Webhook without thread to ChatId: {ChatId}", webhookChat.ChatId);
sentMessage = await botClient.SendTextMessageAsync(
chatId: webhookChat.ChatId,
text: message.MdToHtml(),
parseMode: ParseMode.Html,
disableWebPagePreview: true
);
}
}

mongoEfContext.WebhookHistory.Add(new WebhookHistoryEntity {
RouteId = RouteId,
TransactionId = TransactionId,
ChatId = webhookChat.ChatId,
MessageId = sentMessage.MessageId,
WebhookSource = WebhookSource.GitHub,
Payload = Payload,
Status = EventStatus.Complete
});

await mongoEfContext.SaveChangesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace ZiziBot.Application.Handlers.Telegram.WebHook.GitHub;
public abstract class GithubWebhookEventProcessor : WebhookEventProcessor
{
public string RouteId { get; set; }
public long ChatId { get; set; }
public int ThreadId { get; set; }
public string Token { get; set; }
public string TransactionId { get; set; }
public string Payload { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace ZiziBot.DataSource.MongoEf.Entities;
public class WebhookHistoryEntity : EntityBase
{
public required string RouteId { get; set; }
public long ChatId { get; set; }
public int MessageId { get; set; }
public required long ChatId { get; set; }
public required int MessageId { get; set; }
public int MessageThreadId { get; set; }
public WebhookSource WebhookSource { get; set; }
public string Payload { get; set; }
public required WebhookSource WebhookSource { get; set; }
public required string Payload { get; set; }
}

0 comments on commit 52bd95e

Please sign in to comment.