新增:webhook编辑支持http_method

This commit is contained in:
yystopf 2023-02-28 14:16:46 +08:00
parent dc9cd9e168
commit 3cbba6e36f
4 changed files with 127 additions and 12 deletions

View File

@ -72,17 +72,5 @@ func GetRepoContributors(repo *gitea_git.Repository, page, pageSize int) (int, [
},
})
// skip := (page - 1) * pageSize
// for _, con := range contributorsArr {
// if skip > 0 {
// contributors = append(contributors, &RepoContributor{
// Commits: con,
// })
// skip--
// }
// }
return total, contributors, nil
}

View File

@ -16,6 +16,13 @@ type CreateHookOption struct {
Active bool `json:"active"`
}
type EditHookOption struct {
Config map[string]string `json:"config"`
Events []string `json:"events"`
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
Active *bool `json:"active"`
}
type HookTask struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`

View File

@ -128,6 +128,7 @@ func Routers(ctx gocontext.Context) *web.Route {
m.Group("/hooks", func() {
m.Combo("").Post(bind(hat_api.CreateHookOption{}), repo.CreateHook)
m.Group("/{id}", func() {
m.Combo("").Patch(bind(hat_api.EditHookOption{}), repo.EditHook)
m.Get("/hooktasks", repo.ListHookTask)
})
}, reqToken(), reqAdmin(), reqWebhooksEnabled())

View File

@ -16,8 +16,127 @@ import (
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"
jsoniter "github.com/json-iterator/go"
)
func EditHook(ctx *context.APIContext) {
form := web.GetForm(ctx).(*hat_api.EditHookOption)
hookID := ctx.ParamsInt64(":id")
editRepoHook(ctx, form, hookID)
}
func editRepoHook(ctx *context.APIContext, form *hat_api.EditHookOption, hookID int64) {
repo := ctx.Repo
hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
if err != nil {
return
}
if !editHook(ctx, form, hook) {
return
}
updated, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
if err != nil {
return
}
ctx.JSON(http.StatusOK, convert.ToHook(repo.RepoLink, updated))
}
func editHook(ctx *context.APIContext, form *hat_api.EditHookOption, w *webhook.Webhook) bool {
if form.Config != nil {
if url, ok := form.Config["url"]; ok {
w.URL = url
}
if secret, ok := form.Config["secret"]; ok {
w.Secret = secret
}
if ct, ok := form.Config["content_type"]; ok {
if !webhook.IsValidHookContentType(ct) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
return false
}
w.ContentType = webhook.ToHookContentType(ct)
}
if hw, ok := form.Config["http_method"]; ok {
if !isValidHookHttpMethod(hw) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid http method")
return false
}
w.HTTPMethod = hw
}
if w.Type == webhook.SLACK {
if channel, ok := form.Config["channel"]; ok {
json := jsoniter.ConfigCompatibleWithStandardLibrary
meta, err := json.Marshal(&webhook_service.SlackMeta{
Channel: channel,
Username: form.Config["username"],
IconURL: form.Config["icon_url"],
Color: form.Config["color"],
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
return false
}
w.Meta = string(meta)
}
}
}
// Update events
if len(form.Events) == 0 {
form.Events = []string{"push"}
}
w.PushOnly = false
w.SendEverything = false
w.ChooseEvents = true
w.Create = util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true)
w.Push = util.IsStringInSlice(string(webhook.HookEventPush), form.Events, true)
w.PullRequest = util.IsStringInSlice(string(webhook.HookEventPullRequest), form.Events, true)
// w.Create = util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true)
w.Delete = util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true)
w.Fork = util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true)
w.Issues = util.IsStringInSlice(string(webhook.HookEventIssues), form.Events, true)
w.Issues = issuesHook(form.Events, "issues_only")
w.IssueAssign = issuesHook(form.Events, string(webhook.HookEventIssueAssign))
w.IssueLabel = issuesHook(form.Events, string(webhook.HookEventIssueLabel))
w.IssueMilestone = issuesHook(form.Events, string(webhook.HookEventIssueMilestone))
w.IssueComment = issuesHook(form.Events, string(webhook.HookEventIssueComment))
// w.IssueComment = util.IsStringInSlice(string(webhook.HookEventIssueComment), form.Events, true)
w.Push = util.IsStringInSlice(string(webhook.HookEventPush), form.Events, true)
// w.PullRequest = util.IsStringInSlice(string(webhook.HookEventPullRequest), form.Events, true)
w.PullRequest = pullHook(form.Events, "pull_request_only")
w.PullRequestAssign = pullHook(form.Events, string(webhook.HookEventPullRequestAssign))
w.PullRequestLabel = pullHook(form.Events, string(webhook.HookEventPullRequestLabel))
w.PullRequestMilestone = pullHook(form.Events, string(webhook.HookEventPullRequestMilestone))
w.PullRequestComment = pullHook(form.Events, string(webhook.HookEventPullRequestComment))
w.PullRequestReview = pullHook(form.Events, "pull_request_review")
w.PullRequestSync = pullHook(form.Events, string(webhook.HookEventPullRequestSync))
w.Repository = util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true)
w.Release = util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true)
w.BranchFilter = form.BranchFilter
w.HookEvent.BranchFilter = form.BranchFilter
if err := w.UpdateEvent(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
return false
}
if form.Active != nil {
w.IsActive = *form.Active
}
if err := webhook.UpdateWebhook(w); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
return false
}
return true
}
func CreateHook(ctx *context.APIContext) {
form := web.GetForm(ctx).(*hat_api.CreateHookOption)
if !checkCreateHookOption(ctx, form) {