mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-22 19:39:31 +08:00
GitBot 功能: - 用户级 CRUD API (6 条路由) - Bot 共享功能 - 支持 GitHub/GitLab/Gitee/Gitea 四个平台 Webhook 功能: - 4 个平台 Webhook handler - HMAC-SHA256 签名验证 - Redis 防重入检查 GitTask 功能: - 定义 GitTaskUsecase 接口和 CreateGitTaskReq 类型 - Webhook handler 通过 GitTaskUsecase 创建任务 集成: - 整合到 biz/git/ 模块 - 通过 GitIdentity 动态获取 access_token - config.Task 添加 ImageID/Core/Memory 字段
139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/samber/do"
|
|
|
|
"github.com/chaitin/MonkeyCode/backend/config"
|
|
"github.com/chaitin/MonkeyCode/backend/db"
|
|
"github.com/chaitin/MonkeyCode/backend/domain"
|
|
"github.com/chaitin/MonkeyCode/backend/errcode"
|
|
"github.com/chaitin/MonkeyCode/backend/pkg/cvt"
|
|
)
|
|
|
|
// GitBotUsecase GitBot 业务逻辑
|
|
type GitBotUsecase struct {
|
|
cfg *config.Config
|
|
repo domain.GitBotRepo
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewGitBotUsecase 创建 GitBot 业务逻辑
|
|
func NewGitBotUsecase(i *do.Injector) (domain.GitBotUsecase, error) {
|
|
return &GitBotUsecase{
|
|
cfg: do.MustInvoke[*config.Config](i),
|
|
repo: do.MustInvoke[domain.GitBotRepo](i),
|
|
logger: do.MustInvoke[*slog.Logger](i).With("module", "usecase.GitBotUsecase"),
|
|
}, nil
|
|
}
|
|
|
|
// GetByID 通过 ID 获取 GitBot
|
|
func (u *GitBotUsecase) GetByID(ctx context.Context, id uuid.UUID) (*domain.GitBot, error) {
|
|
bot, err := u.repo.GetByID(ctx, id)
|
|
if err != nil {
|
|
if db.IsNotFound(err) {
|
|
return nil, errcode.ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return (&domain.GitBot{
|
|
WebhookURL: u.webhookURL(bot),
|
|
}).From(bot), nil
|
|
}
|
|
|
|
// GetInstallationID 获取 installation_id
|
|
func (u *GitBotUsecase) GetInstallationID(ctx context.Context, botID uuid.UUID) (int64, error) {
|
|
return u.repo.GetInstallationID(ctx, botID)
|
|
}
|
|
|
|
// GetAccessToken 获取 access_token
|
|
func (u *GitBotUsecase) GetAccessToken(ctx context.Context, botID uuid.UUID) (string, error) {
|
|
identityID, err := u.repo.GetGitIdentityID(ctx, botID)
|
|
if err != nil {
|
|
return "", fmt.Errorf("get git identity id: %w", err)
|
|
}
|
|
if identityID == uuid.Nil {
|
|
bot, err := u.repo.GetByID(ctx, botID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if bot.Token == "" {
|
|
return "", fmt.Errorf("no token found")
|
|
}
|
|
return bot.Token, nil
|
|
}
|
|
// TODO: 从 GitIdentity 获取动态 token
|
|
return "", fmt.Errorf("not implemented")
|
|
}
|
|
|
|
// List 获取用户的 GitBot 列表
|
|
func (u *GitBotUsecase) List(ctx context.Context, uid uuid.UUID) (*domain.ListGitBotResp, error) {
|
|
bots, err := u.repo.List(ctx, uid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.ListGitBotResp{
|
|
Bots: cvt.Iter(bots, func(_ int, bot *db.GitBot) *domain.GitBot {
|
|
return (&domain.GitBot{
|
|
WebhookURL: u.webhookURL(bot),
|
|
}).From(bot)
|
|
}),
|
|
}, nil
|
|
}
|
|
|
|
// Create 创建 GitBot
|
|
func (u *GitBotUsecase) Create(ctx context.Context, uid uuid.UUID, req domain.CreateGitBotReq) (*domain.GitBot, error) {
|
|
bot, err := u.repo.Create(ctx, uid, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return (&domain.GitBot{
|
|
WebhookURL: u.webhookURL(bot),
|
|
}).From(bot), nil
|
|
}
|
|
|
|
// Update 更新 GitBot
|
|
func (u *GitBotUsecase) Update(ctx context.Context, uid uuid.UUID, req domain.UpdateGitBotReq) (*domain.GitBot, error) {
|
|
bot, err := u.repo.Update(ctx, uid, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return (&domain.GitBot{
|
|
WebhookURL: u.webhookURL(bot),
|
|
}).From(bot), nil
|
|
}
|
|
|
|
// Delete 删除 GitBot
|
|
func (u *GitBotUsecase) Delete(ctx context.Context, uid, id uuid.UUID) error {
|
|
return u.repo.Delete(ctx, uid, id)
|
|
}
|
|
|
|
// ListTask 获取 GitBot 任务列表
|
|
func (u *GitBotUsecase) ListTask(ctx context.Context, uid uuid.UUID, req domain.ListGitBotTaskReq) (*domain.ListGitBotTaskResp, error) {
|
|
tasks, pageInfo, err := u.repo.ListTask(ctx, uid, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.ListGitBotTaskResp{
|
|
Tasks: cvt.Iter(tasks, func(_ int, t *db.GitBotTask) *domain.GitBotTask {
|
|
return (&domain.GitBotTask{}).From(t)
|
|
}),
|
|
Page: pageInfo.TotalCount,
|
|
Size: int64(req.Size),
|
|
Total: pageInfo.TotalCount,
|
|
}, nil
|
|
}
|
|
|
|
// ShareBot 共享 GitBot
|
|
func (u *GitBotUsecase) ShareBot(ctx context.Context, uid uuid.UUID, req domain.ShareGitBotReq) error {
|
|
return u.repo.ShareBot(ctx, uid, req)
|
|
}
|
|
|
|
func (u *GitBotUsecase) webhookURL(bot *db.GitBot) string {
|
|
return fmt.Sprintf("%s/api/v1/%s/webhook/%s", u.cfg.Server.BaseURL, bot.Platform, bot.ID.String())
|
|
}
|