1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move webhook into models/webhook/ (#17579)

This commit is contained in:
Lunny Xiao
2021-11-10 13:13:16 +08:00
committed by GitHub
parent edbaa5d3f0
commit 33fca2b537
47 changed files with 770 additions and 717 deletions

View File

@@ -7,7 +7,7 @@ package org
import (
"net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
@@ -40,18 +40,18 @@ func ListHooks(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/HookList"
opts := &models.ListWebhookOptions{
opts := &webhook.ListWebhookOptions{
ListOptions: utils.GetListOptions(ctx),
OrgID: ctx.Org.Organization.ID,
}
count, err := models.CountWebhooksByOpts(opts)
count, err := webhook.CountWebhooksByOpts(opts)
if err != nil {
ctx.InternalServerError(err)
return
}
orgHooks, err := models.ListWebhooksByOpts(opts)
orgHooks, err := webhook.ListWebhooksByOpts(opts)
if err != nil {
ctx.InternalServerError(err)
return
@@ -191,8 +191,8 @@ func DeleteHook(ctx *context.APIContext) {
org := ctx.Org.Organization
hookID := ctx.ParamsInt64(":id")
if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
if models.IsErrWebhookNotExist(err) {
if err := webhook.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
if webhook.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOrgID", err)

View File

@@ -9,13 +9,14 @@ import (
"net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/webhook"
webhook_service "code.gitea.io/gitea/services/webhook"
)
// ListHooks list all hooks of a repository
@@ -48,18 +49,18 @@ func ListHooks(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/HookList"
opts := &models.ListWebhookOptions{
opts := &webhook.ListWebhookOptions{
ListOptions: utils.GetListOptions(ctx),
RepoID: ctx.Repo.Repository.ID,
}
count, err := models.CountWebhooksByOpts(opts)
count, err := webhook.CountWebhooksByOpts(opts)
if err != nil {
ctx.InternalServerError(err)
return
}
hooks, err := models.ListWebhooksByOpts(opts)
hooks, err := webhook.ListWebhooksByOpts(opts)
if err != nil {
ctx.InternalServerError(err)
return
@@ -155,7 +156,7 @@ func TestHook(ctx *context.APIContext) {
commit := convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit)
if err := webhook.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{
if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
Before: ctx.Repo.Commit.ID.String(),
After: ctx.Repo.Commit.ID.String(),
@@ -272,8 +273,8 @@ func DeleteHook(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
if models.IsErrWebhookNotExist(err) {
if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
if webhook.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err)

View File

@@ -8,8 +8,8 @@ import (
"net/http"
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/test"
@@ -27,7 +27,7 @@ func TestTestHook(t *testing.T) {
TestHook(&context.APIContext{Context: ctx, Org: nil})
assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status())
db.AssertExistsAndLoadBean(t, &models.HookTask{
db.AssertExistsAndLoadBean(t, &webhook.HookTask{
RepoID: 1,
HookID: 1,
}, db.Cond("is_delivered=?", false))

View File

@@ -9,22 +9,23 @@ import (
"net/http"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/utils"
"code.gitea.io/gitea/services/webhook"
webhook_service "code.gitea.io/gitea/services/webhook"
)
// GetOrgHook get an organization's webhook. If there is an error, write to
// `ctx` accordingly and return the error
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*models.Webhook, error) {
w, err := models.GetWebhookByOrgID(orgID, hookID)
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) {
w, err := webhook.GetWebhookByOrgID(orgID, hookID)
if err != nil {
if models.IsErrWebhookNotExist(err) {
if webhook.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err)
@@ -36,10 +37,10 @@ func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*models.Webhook,
// GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
// accordingly and return the error
func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook, error) {
w, err := models.GetWebhookByRepoID(repoID, hookID)
func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) {
w, err := webhook.GetWebhookByRepoID(repoID, hookID)
if err != nil {
if models.IsErrWebhookNotExist(err) {
if webhook.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
@@ -52,7 +53,7 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook
// 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 {
if !webhook.IsValidHookTaskType(form.Type) {
if !webhook_service.IsValidHookTaskType(form.Type) {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
return false
}
@@ -62,7 +63,7 @@ func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption)
return false
}
}
if !models.IsValidHookContentType(form.Config["content_type"]) {
if !webhook.IsValidHookContentType(form.Config["content_type"]) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
return false
}
@@ -88,54 +89,54 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
}
func issuesHook(events []string, event string) bool {
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(models.HookEventIssues), events, true)
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook.HookEventIssues), events, true)
}
func pullHook(events []string, event string) bool {
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(models.HookEventPullRequest), events, true)
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook.HookEventPullRequest), events, true)
}
// addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
// an error, write to `ctx` accordingly. Return (webhook, ok)
func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*models.Webhook, bool) {
func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*webhook.Webhook, bool) {
if len(form.Events) == 0 {
form.Events = []string{"push"}
}
w := &models.Webhook{
w := &webhook.Webhook{
OrgID: orgID,
RepoID: repoID,
URL: form.Config["url"],
ContentType: models.ToHookContentType(form.Config["content_type"]),
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
Secret: form.Config["secret"],
HTTPMethod: "POST",
HookEvent: &models.HookEvent{
HookEvent: &webhook.HookEvent{
ChooseEvents: true,
HookEvents: models.HookEvents{
Create: util.IsStringInSlice(string(models.HookEventCreate), form.Events, true),
Delete: util.IsStringInSlice(string(models.HookEventDelete), form.Events, true),
Fork: util.IsStringInSlice(string(models.HookEventFork), form.Events, true),
HookEvents: webhook.HookEvents{
Create: util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true),
Delete: util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true),
Fork: util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true),
Issues: issuesHook(form.Events, "issues_only"),
IssueAssign: issuesHook(form.Events, string(models.HookEventIssueAssign)),
IssueLabel: issuesHook(form.Events, string(models.HookEventIssueLabel)),
IssueMilestone: issuesHook(form.Events, string(models.HookEventIssueMilestone)),
IssueComment: issuesHook(form.Events, string(models.HookEventIssueComment)),
Push: util.IsStringInSlice(string(models.HookEventPush), form.Events, true),
IssueAssign: issuesHook(form.Events, string(webhook.HookEventIssueAssign)),
IssueLabel: issuesHook(form.Events, string(webhook.HookEventIssueLabel)),
IssueMilestone: issuesHook(form.Events, string(webhook.HookEventIssueMilestone)),
IssueComment: issuesHook(form.Events, string(webhook.HookEventIssueComment)),
Push: util.IsStringInSlice(string(webhook.HookEventPush), form.Events, true),
PullRequest: pullHook(form.Events, "pull_request_only"),
PullRequestAssign: pullHook(form.Events, string(models.HookEventPullRequestAssign)),
PullRequestLabel: pullHook(form.Events, string(models.HookEventPullRequestLabel)),
PullRequestMilestone: pullHook(form.Events, string(models.HookEventPullRequestMilestone)),
PullRequestComment: pullHook(form.Events, string(models.HookEventPullRequestComment)),
PullRequestAssign: pullHook(form.Events, string(webhook.HookEventPullRequestAssign)),
PullRequestLabel: pullHook(form.Events, string(webhook.HookEventPullRequestLabel)),
PullRequestMilestone: pullHook(form.Events, string(webhook.HookEventPullRequestMilestone)),
PullRequestComment: pullHook(form.Events, string(webhook.HookEventPullRequestComment)),
PullRequestReview: pullHook(form.Events, "pull_request_review"),
PullRequestSync: pullHook(form.Events, string(models.HookEventPullRequestSync)),
Repository: util.IsStringInSlice(string(models.HookEventRepository), form.Events, true),
Release: util.IsStringInSlice(string(models.HookEventRelease), form.Events, true),
PullRequestSync: pullHook(form.Events, string(webhook.HookEventPullRequestSync)),
Repository: util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true),
Release: util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true),
},
BranchFilter: form.BranchFilter,
},
IsActive: form.Active,
Type: models.HookType(form.Type),
Type: webhook.HookType(form.Type),
}
if w.Type == models.SLACK {
if w.Type == webhook.SLACK {
channel, ok := form.Config["channel"]
if !ok {
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
@@ -147,7 +148,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
return nil, false
}
meta, err := json.Marshal(&webhook.SlackMeta{
meta, err := json.Marshal(&webhook_service.SlackMeta{
Channel: strings.TrimSpace(channel),
Username: form.Config["username"],
IconURL: form.Config["icon_url"],
@@ -163,7 +164,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
if err := w.UpdateEvent(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
return nil, false
} else if err := models.CreateWebhook(w); err != nil {
} else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
return nil, false
}
@@ -206,22 +207,22 @@ func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int6
// editHook edit the webhook `w` according to `form`. If an error occurs, write
// to `ctx` accordingly and return the error. Return whether successful
func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webhook) bool {
func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webhook) bool {
if form.Config != nil {
if url, ok := form.Config["url"]; ok {
w.URL = url
}
if ct, ok := form.Config["content_type"]; ok {
if !models.IsValidHookContentType(ct) {
if !webhook.IsValidHookContentType(ct) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
return false
}
w.ContentType = models.ToHookContentType(ct)
w.ContentType = webhook.ToHookContentType(ct)
}
if w.Type == models.SLACK {
if w.Type == webhook.SLACK {
if channel, ok := form.Config["channel"]; ok {
meta, err := json.Marshal(&webhook.SlackMeta{
meta, err := json.Marshal(&webhook_service.SlackMeta{
Channel: channel,
Username: form.Config["username"],
IconURL: form.Config["icon_url"],
@@ -243,18 +244,18 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
w.PushOnly = false
w.SendEverything = false
w.ChooseEvents = true
w.Create = util.IsStringInSlice(string(models.HookEventCreate), form.Events, true)
w.Push = util.IsStringInSlice(string(models.HookEventPush), form.Events, true)
w.PullRequest = util.IsStringInSlice(string(models.HookEventPullRequest), form.Events, true)
w.Create = util.IsStringInSlice(string(models.HookEventCreate), form.Events, true)
w.Delete = util.IsStringInSlice(string(models.HookEventDelete), form.Events, true)
w.Fork = util.IsStringInSlice(string(models.HookEventFork), form.Events, true)
w.Issues = util.IsStringInSlice(string(models.HookEventIssues), form.Events, true)
w.IssueComment = util.IsStringInSlice(string(models.HookEventIssueComment), form.Events, true)
w.Push = util.IsStringInSlice(string(models.HookEventPush), form.Events, true)
w.PullRequest = util.IsStringInSlice(string(models.HookEventPullRequest), form.Events, true)
w.Repository = util.IsStringInSlice(string(models.HookEventRepository), form.Events, true)
w.Release = util.IsStringInSlice(string(models.HookEventRelease), form.Events, 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.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.Repository = util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true)
w.Release = util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true)
w.BranchFilter = form.BranchFilter
if err := w.UpdateEvent(); err != nil {
@@ -266,7 +267,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
w.IsActive = *form.Active
}
if err := models.UpdateWebhook(w); err != nil {
if err := webhook.UpdateWebhook(w); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
return false
}