diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go new file mode 100644 index 0000000..ee85bc5 --- /dev/null +++ b/models/webhook/hooktask.go @@ -0,0 +1,15 @@ +package webhook + +import ( + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/webhook" +) + +func GetHookTasksByHookID(hookID int64, page, limit int) ([]*webhook.HookTask, error) { + tasks := make([]*webhook.HookTask, 0, limit) + return tasks, db.GetEngine(db.DefaultContext). + Limit(limit, (page-1)*limit). + Where("hook_id=?", hookID). + Desc("id"). + Find(&tasks) +} diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 1379a47..5aea7d5 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -1,10 +1,12 @@ package convert import ( + "encoding/json" "strings" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/webhook" gitea_convert "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" @@ -60,3 +62,32 @@ func ToOrganization(org *organization.Organization, team *organization.Team) (*a OwnerTeam: apiTeam, }, nil } + +func ToHookTask(t *webhook.HookTask) *api.HookTask { + config := map[string]string{ + "url": t.RequestInfo.URL, + "content_type": t.RequestInfo.Headers["Content-Type"], + "http_method": t.RequestInfo.HTTPMethod, + } + + payloadContent := make(map[string]interface{}) + requestContent := make(map[string]interface{}) + responseContent := make(map[string]interface{}) + _ = json.Unmarshal([]byte(t.PayloadContent), &payloadContent) + _ = json.Unmarshal([]byte(t.RequestContent), &requestContent) + _ = json.Unmarshal([]byte(t.ResponseContent), &responseContent) + + return &api.HookTask{ + ID: t.ID, + UUID: t.UUID, + Type: string(t.EventType), + Config: config, + PayloadContent: payloadContent, + EventType: string(t.EventType), + IsDelivered: t.IsDelivered, + Delivered: t.Delivered, + IsSucceed: t.IsSucceed, + RequestContent: requestContent, + ResponseContent: responseContent, + } +} diff --git a/modules/structs/hook.go b/modules/structs/hook.go index 31b654f..4311606 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -15,3 +15,17 @@ type CreateHookOption struct { // default: false Active bool `json:"active"` } + +type HookTask struct { + ID int64 `json:"id"` + UUID string `json:"uuid"` + Type string `json:"type"` + Config map[string]string `json:"config"` + PayloadContent map[string]interface{} `json:"payload_content"` + EventType string `json:"event_type"` + IsDelivered bool `json:"is_delivered"` + Delivered int64 `json:"delivered"` + IsSucceed bool `json:"is_succeed"` + RequestContent map[string]interface{} `json:"request_content"` + ResponseContent map[string]interface{} `json:"response_content"` +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 090b324..57958e1 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -119,7 +119,9 @@ func Routers(ctx gocontext.Context) *web.Route { }) m.Group("/hooks", func() { m.Combo("").Post(bind(hat_api.CreateHookOption{}), repo.CreateHook) - // m.Get("/hooktasks", repo.ListHookTask) + m.Group("/{id}", func() { + m.Get("/hooktasks", repo.ListHookTask) + }) }, reqToken(), reqAdmin(), reqWebhooksEnabled()) m.Group("/contents", func() { m.Get("", repo.GetContentsList) diff --git a/routers/hat/repo/hook.go b/routers/hat/repo/hook.go index eb067a3..52264ec 100644 --- a/routers/hat/repo/hook.go +++ b/routers/hat/repo/hook.go @@ -11,7 +11,10 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/routers/api/v1/utils" webhook_service "code.gitea.io/gitea/services/webhook" + hat_webhook "code.gitlink.org.cn/Gitlink/gitea_hat.git/models/webhook" + hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" ) @@ -154,3 +157,26 @@ func issuesHook(events []string, event string) bool { func pullHook(events []string, event string) bool { return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook.HookEventPullRequest), events, true) } + +func ListHookTask(ctx *context.APIContext) { + listOpts := utils.GetListOptions(ctx) + + hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + if err != nil { + ctx.NotFound() + return + } + + hookTasks, err := hat_webhook.GetHookTasksByHookID(hook.ID, listOpts.Page, listOpts.PageSize) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetHookTasksByHookID", err) + return + } + + apiHookTasks := make([]*hat_api.HookTask, len(hookTasks)) + for i := range hookTasks { + apiHookTasks[i] = hat_convert.ToHookTask(hookTasks[i]) + } + + ctx.JSON(http.StatusOK, &apiHookTasks) +}