refactor: web hook
This commit is contained in:
6
Handlers/IAlchegosEventHandler.cs
Normal file
6
Handlers/IAlchegosEventHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public interface IAlchegosEventHandler : IWebhookEventHandler
|
||||
{
|
||||
|
||||
}
|
||||
6
Handlers/IGiteaEventHandler.cs
Normal file
6
Handlers/IGiteaEventHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public interface IGiteaEventHandler: IWebhookEventHandler
|
||||
{
|
||||
|
||||
}
|
||||
9
Handlers/IWebhookEventHandler.cs
Normal file
9
Handlers/IWebhookEventHandler.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public interface IWebhookEventHandler
|
||||
{
|
||||
Task HandleAsync(JsonNode payload, IRabbitPublisher publisher);
|
||||
}
|
||||
24
Handlers/IssueCommentEventHandler.cs
Normal file
24
Handlers/IssueCommentEventHandler.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public class IssueCommentEventHandler : IGiteaEventHandler
|
||||
{
|
||||
public async Task HandleAsync(JsonNode payload, IRabbitPublisher publisher)
|
||||
{
|
||||
if (payload["action"]?.ToString() != "created")
|
||||
return;
|
||||
|
||||
var message = new Dictionary<string, string>
|
||||
{
|
||||
{"repo_url", payload["repository"]?["url"]?.ToString()},
|
||||
{"repo_owner", payload["repository"]?["owner"]?["login"]?.ToString()},
|
||||
{"repo_name", payload["repository"]?["name"]?.ToString()},
|
||||
{"issue_id", payload["issue"]?["id"]?.ToString()},
|
||||
{"comment_id", payload["comment"]?["id"]?.ToString()},
|
||||
};
|
||||
await publisher.PublishAsync("alchegos", "issue_commented", JsonSerializer.Serialize(message));
|
||||
}
|
||||
}
|
||||
50
Handlers/IssuesEventHandler.cs
Normal file
50
Handlers/IssuesEventHandler.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public class IssuesEventHandler : IGiteaEventHandler
|
||||
{
|
||||
public async Task HandleAsync(JsonNode payload, IRabbitPublisher publisher)
|
||||
{
|
||||
string action = payload["action"]?.ToString() ?? "";
|
||||
string repo_url = payload["repository"]?["url"]?.ToString();
|
||||
string repo_owner = payload["repository"]?["owner"]?["login"]?.ToString();
|
||||
string repo_name = payload["repository"]?["name"]?.ToString();
|
||||
string issue_url = payload["issue"]?["url"]?.ToString();
|
||||
string issue_id = payload["issue"]?["id"]?.ToString();
|
||||
|
||||
var labels = payload["issue"]?["labels"]?.AsArray() ?? new JsonArray();
|
||||
|
||||
var routingKeys = new Dictionary<string, string>
|
||||
{
|
||||
{"status/ready", "ready_to_develop"},
|
||||
{"status/completed", "task_completed"}
|
||||
};
|
||||
|
||||
if (action is "opened" or "label_updated")
|
||||
{
|
||||
foreach (var label in labels)
|
||||
{
|
||||
string labelName = label["name"]?.ToString().Trim() ?? label.ToString().Trim();
|
||||
if (routingKeys.TryGetValue(labelName, out string routingKey))
|
||||
{
|
||||
var message = new Dictionary<string, string>
|
||||
{
|
||||
{ "repo_url", repo_url },
|
||||
{ "repo_owner", repo_owner },
|
||||
{ "repo_name", repo_name },
|
||||
{ "issue_url", issue_url },
|
||||
{ "issue_id", issue_id },
|
||||
};
|
||||
await publisher.PublishAsync(
|
||||
exchange: "alchegos",
|
||||
routingKey: routingKey,
|
||||
message: JsonSerializer.Serialize(message));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Handlers/ProjectPlanEventHandler.cs
Normal file
32
Handlers/ProjectPlanEventHandler.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public class ProjectPlanEventHandler : IGiteaEventHandler
|
||||
{
|
||||
public async Task HandleAsync(JsonNode payload, IRabbitPublisher publisher)
|
||||
{
|
||||
string projectName = payload["title"]?.ToString();
|
||||
string action = payload["action"]?.ToString();
|
||||
string plan = payload["content"]?.ToString();
|
||||
|
||||
Dictionary<string, string> routingKeys = new Dictionary<string, string>
|
||||
{
|
||||
{"create", "new_project_plan_created"},
|
||||
{"update", "project_plan_updated"},
|
||||
};
|
||||
|
||||
Dictionary<string, string> message = new()
|
||||
{
|
||||
{ "project_name", projectName },
|
||||
{ "project_plan", plan }
|
||||
};
|
||||
await publisher.PublishAsync(
|
||||
exchange: "alchegos",
|
||||
routingKey: routingKeys.GetValueOrDefault(action, ""),
|
||||
message: JsonSerializer.Serialize(message)
|
||||
);
|
||||
}
|
||||
}
|
||||
49
Handlers/PullRequestEventHandler.cs
Normal file
49
Handlers/PullRequestEventHandler.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public class PullRequestEventHandler : IGiteaEventHandler
|
||||
{
|
||||
public async Task HandleAsync(JsonNode payload, IRabbitPublisher publisher)
|
||||
{
|
||||
string action = payload["action"]?.ToString() ?? "";
|
||||
string repo_url = payload["repository"]?["url"]?.ToString();
|
||||
string repo_owner = payload["repository"]?["owner"]?["login"]?.ToString();
|
||||
string repo_name = payload["repository"]?["name"]?.ToString();
|
||||
string branch = payload["pull_request"]?["head"]?["ref"]?.ToString();
|
||||
|
||||
JsonArray labels = payload["pull_request"]?["labels"]?.AsArray() ?? new JsonArray();
|
||||
|
||||
Dictionary<string, string> routingKeys = new Dictionary<string, string>
|
||||
{
|
||||
{"status/pending_review", "pull_request_pending_review"},
|
||||
{"status/pending_test", "pull_request_pending_test"}
|
||||
};
|
||||
|
||||
if (action is "opened" or "label_updated")
|
||||
{
|
||||
foreach (JsonNode label in labels)
|
||||
{
|
||||
string labelName = label["name"]?.ToString().Trim() ?? label.ToString().Trim();
|
||||
if (routingKeys.TryGetValue(labelName, out string routingKey))
|
||||
{
|
||||
Dictionary<string, string> message = new Dictionary<string, string>
|
||||
{
|
||||
{"repo_url", repo_url},
|
||||
{"repo_owner", repo_owner},
|
||||
{"repo_name", repo_name},
|
||||
{"branch", branch}
|
||||
};
|
||||
await publisher.PublishAsync(
|
||||
exchange: "alchegos",
|
||||
routingKey: routingKey,
|
||||
message: JsonSerializer.Serialize(message)
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Handlers/PushEventHandler.cs
Normal file
34
Handlers/PushEventHandler.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Alchegos.Core.Services.RabbitMQ;
|
||||
|
||||
namespace Alchegos.Gitea.Webhook.Handlers;
|
||||
|
||||
public class PushEventHandler : IGiteaEventHandler
|
||||
{
|
||||
public async Task HandleAsync(JsonNode payload, IRabbitPublisher publisher)
|
||||
{
|
||||
string branch = payload["ref"]?.ToString()?.Replace("refs/heads/", "") ?? "";
|
||||
if (branch == "main")
|
||||
return;
|
||||
|
||||
JsonArray commits = payload["commits"]?.AsArray() ?? new JsonArray();
|
||||
foreach (var commit in commits)
|
||||
{
|
||||
if (commit["message"]?.ToString()?.Trim() == "init")
|
||||
{
|
||||
Dictionary<string, string> message = new Dictionary<string, string>
|
||||
{
|
||||
{"repo_url", payload["repository"]?["url"]?.ToString()},
|
||||
{"repo_owner", payload["repository"]?["owner"]?["login"]?.ToString()},
|
||||
{"repo_name", payload["repository"]?["name"]?.ToString()}
|
||||
};
|
||||
await publisher.PublishAsync(
|
||||
exchange: "alchegos",
|
||||
routingKey:"project_initialized",
|
||||
message: JsonSerializer.Serialize(message));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user