新增:获取webhook推送历史接口

This commit is contained in:
yystopf 2022-12-27 16:54:49 +08:00
parent be7e6c1551
commit 6973f230e6
5 changed files with 89 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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