HookTasks list

This commit is contained in:
wonderful 2021-07-28 18:42:39 +08:00
parent 9c568c2a59
commit ac62431593
7 changed files with 208 additions and 3 deletions

View File

@ -1159,12 +1159,16 @@ type ErrWebhookNotExist struct {
ID int64
}
// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.
func IsErrWebhookNotExist(err error) bool {
_, ok := err.(ErrWebhookNotExist)
return ok
}
func IsErrHookTaskNotExist(err error) bool {
_, ok := err.(ErrWebhookNotExist)
return ok
}
func (err ErrWebhookNotExist) Error() string {
return fmt.Sprintf("webhook does not exist [id: %d]", err.ID)
}

View File

@ -328,6 +328,18 @@ func getWebhook(bean *Webhook) (*Webhook, error) {
return bean, nil
}
func getHookTask(bean *HookTask) (*HookTask, error) {
has, err := x.Get(bean)
if err != nil {
return nil, err
} else if !has {
return nil, ErrWebhookNotExist{bean.ID}
}
return bean, nil
}
// GetWebhookByID returns webhook of repository by given ID.
func GetWebhookByID(id int64) (*Webhook, error) {
return getWebhook(&Webhook{
@ -342,7 +354,12 @@ func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
RepoID: repoID,
})
}
func GetHookTaskByRepoID(repoID, id int64) (*HookTask, error) {
return getHookTask(&HookTask{
ID: id,
RepoID: repoID,
})
}
// GetWebhookByOrgID returns webhook of organization by given ID.
func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
return getWebhook(&Webhook{
@ -374,7 +391,17 @@ func GetWebhooksByRepoID(repoID int64, listOptions ListOptions) ([]*Webhook, err
return webhooks, sess.Find(&webhooks, &Webhook{RepoID: repoID})
}
func GetHookTasksByRepoID(repoID int64, listOptions ListOptions) ([]*HookTask, error) {
if listOptions.Page == 0 {
hooktasks := make([]*HookTask, 0, 5)
return hooktasks,x.Find(&hooktasks, &HookTask{RepoID: repoID})
}
sess := listOptions.getPaginatedSession()
hooktasks := make([]*HookTask, 0, listOptions.PageSize)
return hooktasks, sess.Find(&hooktasks, &HookTask{RepoID: repoID})
}
// GetActiveWebhooksByOrgID returns all active webhooks for an organization.
func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
return getActiveWebhooksByOrgID(x, orgID)

View File

@ -46,6 +46,7 @@ type Context struct {
Repo *Repository
Org *Organization
}
// IsUserSiteAdmin returns true if current user is a site admin

View File

@ -52,7 +52,17 @@ type CreateHookOption struct {
// default: false
Active bool `json:"active"`
}
type CreateHookTaskOption struct {
// required: true
// enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu
Type string `json:"type" binding:"Required"`
//required: true
Config CreateHookOptionConfig `json:"config" binding:"Required"`
Events []string `json:"events"`
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
//default: false
Active bool `json:"active"`
}
// EditHookOption options when modify one hook
type EditHookOption struct {
Config map[string]string `json:"config"`

View File

@ -696,6 +696,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("", report.GetContributors) //获取仓库的所有构建者信息 ****
})
m.Combo("").Get(reqAnyRepoReader(), repo.Get).
Delete(reqToken(), reqOwner(), repo.Delete).
Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), context.RepoRef(), repo.Edit)
@ -712,6 +713,9 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(repo.DeleteHook)
m.Post("/tests", context.RepoRef(), repo.TestHook)
})
m.Group("/hooktasks", func(){
m.Get("",repo.HookTaskList)
})
m.Group("/git", func() {
m.Combo("").Get(repo.ListGitHooks)
m.Group("/:id", func() {

View File

@ -6,6 +6,8 @@
package repo
import (
"code.gitea.io/gitea/modules/setting"
_ "fmt"
"net/http"
"code.gitea.io/gitea/models"
@ -264,3 +266,54 @@ func DeleteHook(ctx *context.APIContext) {
}
ctx.Status(http.StatusNoContent)
}
func HookTaskList(ctx *context.APIContext){
// swagger:operation GET /repos/{owner}/{repo}/hooks/hooktasks repository repoGetHookTasks
// ---
// summary: Get a hooktasks
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Hook"
// "404":
// "$ref": "#/responses/notFound"
//hooks, err := models.GetHookTasksByRepoID(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
repo := ctx.Repo
hookID := ctx.ParamsInt64(":id")
hook, err := utils.GetRepoHookTask(ctx, repo.Repository.ID, hookID)
if err != nil {
return
}
//hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
//if err != nil {
// ctx.Error(http.StatusInternalServerError, "GetWebhooksByRepoID", err)
// return
//}
//apiHooks := make([]*models.HookTask,len(hooks))
//for i := range hooks {
// apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
//}
//hookstasks := make(map[string]string)
//hookTasks := make([][]*models.HookTask, 0, setting.Webhook.PagingNum)
hookTasks := utils.GetHookTasks(hook, setting.Webhook.PagingNum)
ctx.JSON(http.StatusOK, &hookTasks)
}

View File

@ -8,6 +8,7 @@ import (
"encoding/json"
"net/http"
"strings"
"xorm.io/xorm"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
@ -49,6 +50,85 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook
return w, nil
}
func GetRepoHookTask(ctx *context.APIContext, repoID, hookID int64) (*models.HookTask, error) {
w, err := models.GetHookTaskByRepoID(repoID, hookID)
if err != nil {
if models.IsErrHookTaskNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetHookTaskByID", err)
}
return nil, err
}
return w, nil
}
func GetHookTasks(w *models.HookTask, page int )([]*models.HookTask){
hook_id := w.HookID
//tasks := make([]*models.HookTask, 0, setting.Webhook.PagingNum)
hookTasks, _ := models.HookTasks(hook_id, page)
//tasks, _ = hookTasks, err
//for i := range tasks{
// tasks[i].ID = hookTasks[i].ID
// tasks[i].UUID = hookTasks[i].UUID
// tasks[i].URL = hookTasks[i].URL
// tasks[i].ContentType = hookTasks[i].ContentType
// tasks[i].RequestInfo = hookTasks[i].RequestInfo
// tasks[i].RequestContent = hookTasks[i].RequestContent
// tasks[i].HTTPMethod = hookTasks[i].HTTPMethod
// tasks[i].ResponseContent = hookTasks[i].ResponseContent
// tasks[i].ResponseInfo = hookTasks[i].ResponseInfo
//}
//
//
//if err != nil{
// fmt.Println("err ......")
//}
//t := append(tasks, hookTasks)
//tasks := make(map[string]string)
////hooktasks := make([]models.HookTask, len(hookTasks))
//tasks := append(hooktasks, hookTasks)
//HookTasks := make([]*models.HookTask, len(hookTasks))
//H := append(HookTasks, hookTasks)
return hookTasks
//return {
// //ID: w.ID,
// //Type: w.Type,
// //URL: w.URL,
// //Signature: w.Signature,
// //PayloadContent: w.PayloadContent,
// //
// //HTTPMethod: w.HTTPMethod,
// //ResponseContent: w.ResponseContent,
// //ResponseInfo: w.ResponseInfo,
// ID: w.ID,
// UUID: w.UUID,
// Type :w.Type,
// URL :w.URL,
// PayloadContent:w.PayloadContent,
// HTTPMethod : w.HTTPMethod,
// ContentType : w.ContentType,
// EventType : w.EventType,
// //IsSSL : w.IsSSL,
// //IsDelivered :w.IsDelivered,
// Delivered :w.Delivered,
// DeliveredString :w.DeliveredString,
// // History info.
// IsSucceed : w.IsSucceed,
// RequestContent : w.RequestContent,
// RequestInfo : w.RequestInfo,
// ResponseContent : w.ResponseContent,
// ResponseInfo : w.ResponseInfo,
//}
}
// CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
// write the appropriate error to `ctx`. Return whether the form is valid
func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
@ -273,3 +353,29 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
}
return true
}
var (
x *xorm.Engine
tables []interface{}
// HasEngine specifies if we have a xorm.Engine
HasEngine bool
)
func HookTaskList(ctx *context.APIContext,hookId int64, page int)([]*models.HookTask, error){
//tasks := make([]*models.HookTask,0,setting.Webhook.PagingNum)
tasks, err := models.HookTasks(hookId, page)
if err != nil{
if models.IsErrWebhookNotExist(err) {
ctx.NotFound()
}else{
ctx.Error(http.StatusInternalServerError, "HookTaskList", err)
}
return nil, err
}
return tasks, err
}