新增:系统钩子来捕捉pr提交

This commit is contained in:
yystopf 2023-01-11 15:37:25 +08:00
parent 7ebd069424
commit dc6b0413da
4 changed files with 75 additions and 2 deletions

21
models/webhook/webhook.go Normal file
View File

@ -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
}

View File

@ -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)

View File

@ -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 {

View File

@ -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
}