diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go new file mode 100644 index 0000000..0026014 --- /dev/null +++ b/models/webhook/webhook.go @@ -0,0 +1,21 @@ +package webhook + +import ( + "code.gitea.io/gitea/models/db" + gitea_webhook_model "code.gitea.io/gitea/models/webhook" +) + +type Webhook struct { + gitea_webhook_model.Webhook +} + +func GetWebhook(bean *Webhook) (*Webhook, error) { + webhook := bean.Webhook + has, err := db.GetEngine(db.DefaultContext).Get(&webhook) + if err != nil { + return nil, err + } else if !has { + return nil, gitea_webhook_model.ErrWebhookNotExist{webhook.ID} + } + return bean, nil +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 4a63e45..5801c29 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -57,12 +57,13 @@ func Routers(ctx gocontext.Context) *web.Route { m.Use(context.APIAuth(group)) m.Use(context.ToggleAPI(&context.ToggleOptions{ - SignInRequired: setting.Service.RequireSignInView, + SignInRequired: false, })) reqRepoCodeReader := context.RequireRepoReader(unit_model.TypeCode) m.Group("", func() { m.Get("/version", misc.Version) + m.Post("/create_pr_version", bind(gitea_api.PullRequestPayload{}), repo.CreatePrVersion) m.Group("/repos", func() { m.Group("/{username}/{reponame}", func() { m.Post("/transfer", reqOwner(), bind(gitea_api.TransferRepoOption{}), repo.Transfer) diff --git a/routers/hat/repo/pull.go b/routers/hat/repo/pull.go index 0e98b42..e99f64f 100644 --- a/routers/hat/repo/pull.go +++ b/routers/hat/repo/pull.go @@ -15,10 +15,19 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + gitea_api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/web" "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git" + hat_pull_service "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/pull" ) +func CreatePrVersion(ctx *context.APIContext) { + form := web.GetForm(ctx).(*gitea_api.PullRequestPayload) + hat_pull_service.AddToTaskQueue(&issues_model.PullRequest{ID: form.PullRequest.ID}) + ctx.Status(http.StatusNoContent) +} + func GetPullRequest(ctx *context.APIContext) { pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { diff --git a/services/pull/check.go b/services/pull/check.go index 46e65f6..1dae413 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -6,10 +6,13 @@ import ( "strconv" issues_model "code.gitea.io/gitea/models/issues" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" + "code.gitea.io/gitea/modules/setting" hat_issues_model "code.gitlink.org.cn/Gitlink/gitea_hat.git/models/issues" + hat_webhook_model "code.gitlink.org.cn/Gitlink/gitea_hat.git/models/webhook" ) var hatPrPatchCheckerQueue queue.UniqueQueue @@ -45,10 +48,48 @@ func InitializePullRequests(ctx context.Context) { } } +func InitializeCheckHook(ctx context.Context) { + w := webhook_model.Webhook{ + RepoID: 0, + OrgID: 0, + IsSystemWebhook: true, + URL: fmt.Sprintf("%sapi/hat/create_pr_version", setting.AppURL), + HTTPMethod: "POST", + } + _, err := hat_webhook_model.GetWebhook(&hat_webhook_model.Webhook{ + Webhook: w, + }) + if err != nil { + log.Error("Find System Hook: %v", err) + if webhook_model.IsErrWebhookNotExist(err) { + w.HookEvent = &webhook_model.HookEvent{ + ChooseEvents: true, + HookEvents: webhook_model.HookEvents{ + PullRequestSync: true, + }, + BranchFilter: "*", + } + w.ContentType = webhook_model.ContentTypeJSON + w.IsActive = true + w.Type = "gitea" + + err = w.UpdateEvent() + if err != nil { + log.Error("Update System Hook Event: %v", err) + } + err = webhook_model.CreateWebhook(ctx, &w) + if err != nil { + log.Error("Create System Hook: %v", err) + } + } + } + + return +} + func handle(data ...queue.Data) []queue.Data { for _, datum := range data { id, _ := strconv.ParseInt(datum.(string), 10, 64) - fmt.Println(id) } return nil @@ -62,6 +103,7 @@ func Init() error { go graceful.GetManager().RunWithShutdownFns(hatPrPatchCheckerQueue.Run) go graceful.GetManager().RunWithShutdownContext(InitializePullRequests) + go graceful.GetManager().RunWithShutdownContext(InitializeCheckHook) return nil }