forked from Gitlink/gitea-1120-rc1
webhook的http_method和branch_filter字段返回
This commit is contained in:
commit
1014b45ec1
|
@ -55,6 +55,13 @@ func IsValidHookContentType(name string) bool {
|
|||
return ok
|
||||
}
|
||||
|
||||
func IsValidHookHttpMethod(name string) bool {
|
||||
if name == "POST" || name == "GET" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HookEvents is a set of web hook events
|
||||
type HookEvents struct {
|
||||
Create bool `json:"create"`
|
||||
|
@ -115,6 +122,7 @@ type Webhook struct {
|
|||
HookTaskType HookTaskType
|
||||
Meta string `xorm:"TEXT"` // store hook-specific attributes
|
||||
LastStatus HookStatus // Last delivery status
|
||||
BranchFilter string `xorm:"TEXT"`
|
||||
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
|
|
|
@ -255,6 +255,7 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
|
|||
config := map[string]string{
|
||||
"url": w.URL,
|
||||
"content_type": w.ContentType.Name(),
|
||||
"http_method": w.HTTPMethod,
|
||||
}
|
||||
if w.HookTaskType == models.SLACK {
|
||||
s := webhook.GetSlackHook(w)
|
||||
|
@ -265,14 +266,15 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
|
|||
}
|
||||
|
||||
return &api.Hook{
|
||||
ID: w.ID,
|
||||
Type: w.HookTaskType.Name(),
|
||||
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
|
||||
Active: w.IsActive,
|
||||
Config: config,
|
||||
Events: w.EventsArray(),
|
||||
Updated: w.UpdatedUnix.AsTime(),
|
||||
Created: w.CreatedUnix.AsTime(),
|
||||
ID: w.ID,
|
||||
Type: w.HookTaskType.Name(),
|
||||
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
|
||||
BranchFilter: w.HookEvent.BranchFilter,
|
||||
Active: w.IsActive,
|
||||
Config: config,
|
||||
Events: w.EventsArray(),
|
||||
Updated: w.UpdatedUnix.AsTime(),
|
||||
Created: w.CreatedUnix.AsTime(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,12 +19,13 @@ var (
|
|||
|
||||
// Hook a hook is a web hook when one repository changed
|
||||
type Hook struct {
|
||||
ID int64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
URL string `json:"-"`
|
||||
Config map[string]string `json:"config"`
|
||||
Events []string `json:"events"`
|
||||
Active bool `json:"active"`
|
||||
ID int64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
URL string `json:"-"`
|
||||
Config map[string]string `json:"config"`
|
||||
Events []string `json:"events"`
|
||||
Active bool `json:"active"`
|
||||
BranchFilter string `json:"branch_filter"`
|
||||
// swagger:strfmt date-time
|
||||
Updated time.Time `json:"updated_at"`
|
||||
// swagger:strfmt date-time
|
||||
|
|
|
@ -66,6 +66,11 @@ func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption)
|
|||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
||||
return false
|
||||
}
|
||||
|
||||
if !models.IsValidHookHttpMethod(form.Config["http_method"]) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid http method")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -101,13 +106,14 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
|
|||
if len(form.Events) == 0 {
|
||||
form.Events = []string{"push"}
|
||||
}
|
||||
|
||||
w := &models.Webhook{
|
||||
OrgID: orgID,
|
||||
RepoID: repoID,
|
||||
URL: form.Config["url"],
|
||||
ContentType: models.ToHookContentType(form.Config["content_type"]),
|
||||
Secret: form.Config["secret"],
|
||||
HTTPMethod: "POST",
|
||||
HTTPMethod: form.Config["http_method"],
|
||||
HookEvent: &models.HookEvent{
|
||||
ChooseEvents: true,
|
||||
HookEvents: models.HookEvents{
|
||||
|
@ -219,6 +225,14 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
|
|||
w.ContentType = models.ToHookContentType(ct)
|
||||
}
|
||||
|
||||
if hm, ok := form.Config["http_method"]; ok {
|
||||
if !models.IsValidHookHttpMethod(hm) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid http method")
|
||||
return false
|
||||
}
|
||||
w.HTTPMethod = hm
|
||||
}
|
||||
|
||||
if w.HookTaskType == models.SLACK {
|
||||
if channel, ok := form.Config["channel"]; ok {
|
||||
meta, err := json.Marshal(&webhook.SlackMeta{
|
||||
|
|
|
@ -13348,6 +13348,10 @@
|
|||
"type": "boolean",
|
||||
"x-go-name": "Active"
|
||||
},
|
||||
"branch_filter": {
|
||||
"type": "string",
|
||||
"x-go-name": "BranchFilter"
|
||||
},
|
||||
"config": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
|
@ -13367,6 +13371,10 @@
|
|||
},
|
||||
"x-go-name": "Events"
|
||||
},
|
||||
"http_method": {
|
||||
"type": "string",
|
||||
"x-go-name": "HTTPMethod"
|
||||
},
|
||||
"id": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
|
|
Loading…
Reference in New Issue