aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-10 13:13:16 +0800
committerGitHub <noreply@github.com>2021-11-10 13:13:16 +0800
commit33fca2b537d36cf998dd27425b2bb8ed5b0965f3 (patch)
tree817f392502e1c176a5cd7e80290520cb940a8416 /routers
parentedbaa5d3f05b5ca397524587ba9db15edd61bc29 (diff)
downloadgitea-33fca2b537d36cf998dd27425b2bb8ed5b0965f3.tar.gz
gitea-33fca2b537d36cf998dd27425b2bb8ed5b0965f3.zip
Move webhook into models/webhook/ (#17579)
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/org/hook.go12
-rw-r--r--routers/api/v1/repo/hook.go15
-rw-r--r--routers/api/v1/repo/hook_test.go4
-rw-r--r--routers/api/v1/utils/hook.go109
-rw-r--r--routers/web/admin/hooks.go8
-rw-r--r--routers/web/org/setting.go5
-rw-r--r--routers/web/repo/webhook.go228
7 files changed, 193 insertions, 188 deletions
diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go
index c5982300eb..6c81616170 100644
--- a/routers/api/v1/org/hook.go
+++ b/routers/api/v1/org/hook.go
@@ -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)
diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go
index 74860fd72f..2969b92502 100644
--- a/routers/api/v1/repo/hook.go
+++ b/routers/api/v1/repo/hook.go
@@ -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)
diff --git a/routers/api/v1/repo/hook_test.go b/routers/api/v1/repo/hook_test.go
index 79b8ec171c..60fa6cead3 100644
--- a/routers/api/v1/repo/hook_test.go
+++ b/routers/api/v1/repo/hook_test.go
@@ -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))
diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go
index a257260cad..aef9b8f4ce 100644
--- a/routers/api/v1/utils/hook.go
+++ b/routers/api/v1/utils/hook.go
@@ -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
}
diff --git a/routers/web/admin/hooks.go b/routers/web/admin/hooks.go
index 13daa9ac9e..8cb99e1d1e 100644
--- a/routers/web/admin/hooks.go
+++ b/routers/web/admin/hooks.go
@@ -7,7 +7,7 @@ package admin
import (
"net/http"
- "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
@@ -34,7 +34,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
sys["Title"] = ctx.Tr("admin.systemhooks")
sys["Description"] = ctx.Tr("admin.systemhooks.desc")
- sys["Webhooks"], err = models.GetSystemWebhooks()
+ sys["Webhooks"], err = webhook.GetSystemWebhooks()
sys["BaseLink"] = setting.AppSubURL + "/admin/hooks"
sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks"
if err != nil {
@@ -44,7 +44,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
def["Title"] = ctx.Tr("admin.defaulthooks")
def["Description"] = ctx.Tr("admin.defaulthooks.desc")
- def["Webhooks"], err = models.GetDefaultWebhooks()
+ def["Webhooks"], err = webhook.GetDefaultWebhooks()
def["BaseLink"] = setting.AppSubURL + "/admin/hooks"
def["BaseLinkNew"] = setting.AppSubURL + "/admin/default-hooks"
if err != nil {
@@ -60,7 +60,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
// DeleteDefaultOrSystemWebhook handler to delete an admin-defined system or default webhook
func DeleteDefaultOrSystemWebhook(ctx *context.Context) {
- if err := models.DeleteDefaultSystemWebhook(ctx.FormInt64("id")); err != nil {
+ if err := webhook.DeleteDefaultSystemWebhook(ctx.FormInt64("id")); err != nil {
ctx.Flash.Error("DeleteDefaultWebhook: " + err.Error())
} else {
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go
index 277ff9d973..d8add77f66 100644
--- a/routers/web/org/setting.go
+++ b/routers/web/org/setting.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
@@ -187,7 +188,7 @@ func Webhooks(ctx *context.Context) {
ctx.Data["BaseLinkNew"] = ctx.Org.OrgLink + "/settings/hooks"
ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
- ws, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{OrgID: ctx.Org.Organization.ID})
+ ws, err := webhook.ListWebhooksByOpts(&webhook.ListWebhookOptions{OrgID: ctx.Org.Organization.ID})
if err != nil {
ctx.ServerError("GetWebhooksByOrgId", err)
return
@@ -199,7 +200,7 @@ func Webhooks(ctx *context.Context) {
// DeleteWebhook response for delete webhook
func DeleteWebhook(ctx *context.Context) {
- if err := models.DeleteWebhookByOrgID(ctx.Org.Organization.ID, ctx.FormInt64("id")); err != nil {
+ if err := webhook.DeleteWebhookByOrgID(ctx.Org.Organization.ID, ctx.FormInt64("id")); err != nil {
ctx.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
} else {
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go
index 0ceb9bc1dc..f47f8d651d 100644
--- a/routers/web/repo/webhook.go
+++ b/routers/web/repo/webhook.go
@@ -13,6 +13,8 @@ import (
"strings"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
@@ -23,7 +25,7 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/forms"
- "code.gitea.io/gitea/services/webhook"
+ webhook_service "code.gitea.io/gitea/services/webhook"
)
const (
@@ -41,7 +43,7 @@ func Webhooks(ctx *context.Context) {
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
- ws, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
+ ws, err := webhook.ListWebhooksByOpts(&webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
if err != nil {
ctx.ServerError("GetWebhooksByRepoID", err)
return
@@ -117,7 +119,7 @@ func checkHookType(ctx *context.Context) string {
// WebhooksNew render creating webhook page
func WebhooksNew(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -152,13 +154,13 @@ func WebhooksNew(ctx *context.Context) {
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
}
-// ParseHookEvent convert web form content to models.HookEvent
-func ParseHookEvent(form forms.WebhookForm) *models.HookEvent {
- return &models.HookEvent{
+// ParseHookEvent convert web form content to webhook.HookEvent
+func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent {
+ return &webhook.HookEvent{
PushOnly: form.PushOnly(),
SendEverything: form.SendEverything(),
ChooseEvents: form.ChooseEvents(),
- HookEvents: models.HookEvents{
+ HookEvents: webhook.HookEvents{
Create: form.Create,
Delete: form.Delete,
Fork: form.Fork,
@@ -188,8 +190,8 @@ func GiteaHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.GITEA
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.GITEA
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -203,12 +205,12 @@ func GiteaHooksNewPost(ctx *context.Context) {
return
}
- contentType := models.ContentTypeJSON
- if models.HookContentType(form.ContentType) == models.ContentTypeForm {
- contentType = models.ContentTypeForm
+ contentType := webhook.ContentTypeJSON
+ if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
+ contentType = webhook.ContentTypeForm
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
HTTPMethod: form.HTTPMethod,
@@ -216,14 +218,14 @@ func GiteaHooksNewPost(ctx *context.Context) {
Secret: form.Secret,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.GITEA,
+ Type: webhook.GITEA,
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
}
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -235,16 +237,16 @@ func GiteaHooksNewPost(ctx *context.Context) {
// GogsHooksNewPost response for creating webhook
func GogsHooksNewPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewGogshookForm)
- newGogsWebhookPost(ctx, *form, models.GOGS)
+ newGogsWebhookPost(ctx, *form, webhook.GOGS)
}
// newGogsWebhookPost response for creating gogs hook
-func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind models.HookType) {
+func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind webhook.HookType) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.GOGS
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.GOGS
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -258,12 +260,12 @@ func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind m
return
}
- contentType := models.ContentTypeJSON
- if models.HookContentType(form.ContentType) == models.ContentTypeForm {
- contentType = models.ContentTypeForm
+ contentType := webhook.ContentTypeJSON
+ if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
+ contentType = webhook.ContentTypeForm
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
ContentType: contentType,
@@ -277,7 +279,7 @@ func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind m
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -292,8 +294,8 @@ func DiscordHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.DISCORD
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.DISCORD
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -306,7 +308,7 @@ func DiscordHooksNewPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.DiscordMeta{
+ meta, err := json.Marshal(&webhook_service.DiscordMeta{
Username: form.Username,
IconURL: form.IconURL,
})
@@ -315,13 +317,13 @@ func DiscordHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.DISCORD,
+ Type: webhook.DISCORD,
Meta: string(meta),
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -329,7 +331,7 @@ func DiscordHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -344,8 +346,8 @@ func DingtalkHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.DINGTALK
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.DINGTALK
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -358,13 +360,13 @@ func DingtalkHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.DINGTALK,
+ Type: webhook.DINGTALK,
Meta: "",
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -372,7 +374,7 @@ func DingtalkHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -387,8 +389,8 @@ func TelegramHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.TELEGRAM
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.TELEGRAM
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -401,7 +403,7 @@ func TelegramHooksNewPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.TelegramMeta{
+ meta, err := json.Marshal(&webhook_service.TelegramMeta{
BotToken: form.BotToken,
ChatID: form.ChatID,
})
@@ -410,13 +412,13 @@ func TelegramHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID),
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.TELEGRAM,
+ Type: webhook.TELEGRAM,
Meta: string(meta),
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -424,7 +426,7 @@ func TelegramHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -439,8 +441,8 @@ func MatrixHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.MATRIX
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.MATRIX
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -453,7 +455,7 @@ func MatrixHooksNewPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.MatrixMeta{
+ meta, err := json.Marshal(&webhook_service.MatrixMeta{
HomeserverURL: form.HomeserverURL,
Room: form.RoomID,
AccessToken: form.AccessToken,
@@ -464,14 +466,14 @@ func MatrixHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, form.RoomID),
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HTTPMethod: "PUT",
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.MATRIX,
+ Type: webhook.MATRIX,
Meta: string(meta),
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -479,7 +481,7 @@ func MatrixHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -494,8 +496,8 @@ func MSTeamsHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.MSTEAMS
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.MSTEAMS
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -508,13 +510,13 @@ func MSTeamsHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.MSTEAMS,
+ Type: webhook.MSTEAMS,
Meta: "",
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -522,7 +524,7 @@ func MSTeamsHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -537,8 +539,8 @@ func SlackHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.SLACK
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.SLACK
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -557,7 +559,7 @@ func SlackHooksNewPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.SlackMeta{
+ meta, err := json.Marshal(&webhook_service.SlackMeta{
Channel: strings.TrimSpace(form.Channel),
Username: form.Username,
IconURL: form.IconURL,
@@ -568,13 +570,13 @@ func SlackHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.SLACK,
+ Type: webhook.SLACK,
Meta: string(meta),
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -582,7 +584,7 @@ func SlackHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -597,8 +599,8 @@ func FeishuHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.FEISHU
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.FEISHU
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -611,13 +613,13 @@ func FeishuHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.FEISHU,
+ Type: webhook.FEISHU,
Meta: "",
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -625,7 +627,7 @@ func FeishuHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -641,8 +643,8 @@ func WechatworkHooksNewPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
- ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
- ctx.Data["HookType"] = models.WECHATWORK
+ ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
+ ctx.Data["HookType"] = webhook.WECHATWORK
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
@@ -655,13 +657,13 @@ func WechatworkHooksNewPost(ctx *context.Context) {
return
}
- w := &models.Webhook{
+ w := &webhook.Webhook{
RepoID: orCtx.RepoID,
URL: form.PayloadURL,
- ContentType: models.ContentTypeJSON,
+ ContentType: webhook.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
- Type: models.WECHATWORK,
+ Type: webhook.WECHATWORK,
Meta: "",
OrgID: orCtx.OrgID,
IsSystemWebhook: orCtx.IsSystemWebhook,
@@ -669,7 +671,7 @@ func WechatworkHooksNewPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.CreateWebhook(w); err != nil {
+ } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
@@ -678,7 +680,7 @@ func WechatworkHooksNewPost(ctx *context.Context) {
ctx.Redirect(orCtx.Link)
}
-func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
+func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
ctx.Data["RequireHighlightJS"] = true
orCtx, err := getOrgRepoCtx(ctx)
@@ -688,16 +690,16 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
}
ctx.Data["BaseLink"] = orCtx.Link
- var w *models.Webhook
+ var w *webhook.Webhook
if orCtx.RepoID > 0 {
- w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
+ w, err = webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
} else if orCtx.OrgID > 0 {
- w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
+ w, err = webhook.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
} else if orCtx.IsAdmin {
- w, err = models.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id"))
+ w, err = webhook.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id"))
}
if err != nil || w == nil {
- if models.IsErrWebhookNotExist(err) {
+ if webhook.IsErrWebhookNotExist(err) {
ctx.NotFound("GetWebhookByID", nil)
} else {
ctx.ServerError("GetWebhookByID", err)
@@ -707,14 +709,14 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
ctx.Data["HookType"] = w.Type
switch w.Type {
- case models.SLACK:
- ctx.Data["SlackHook"] = webhook.GetSlackHook(w)
- case models.DISCORD:
- ctx.Data["DiscordHook"] = webhook.GetDiscordHook(w)
- case models.TELEGRAM:
- ctx.Data["TelegramHook"] = webhook.GetTelegramHook(w)
- case models.MATRIX:
- ctx.Data["MatrixHook"] = webhook.GetMatrixHook(w)
+ case webhook.SLACK:
+ ctx.Data["SlackHook"] = webhook_service.GetSlackHook(w)
+ case webhook.DISCORD:
+ ctx.Data["DiscordHook"] = webhook_service.GetDiscordHook(w)
+ case webhook.TELEGRAM:
+ ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w)
+ case webhook.MATRIX:
+ ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w)
}
ctx.Data["History"], err = w.History(1)
@@ -757,9 +759,9 @@ func WebHooksEditPost(ctx *context.Context) {
return
}
- contentType := models.ContentTypeJSON
- if models.HookContentType(form.ContentType) == models.ContentTypeForm {
- contentType = models.ContentTypeForm
+ contentType := webhook.ContentTypeJSON
+ if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
+ contentType = webhook.ContentTypeForm
}
w.URL = form.PayloadURL
@@ -771,7 +773,7 @@ func WebHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("WebHooksEditPost", err)
return
}
@@ -798,9 +800,9 @@ func GogsHooksEditPost(ctx *context.Context) {
return
}
- contentType := models.ContentTypeJSON
- if models.HookContentType(form.ContentType) == models.ContentTypeForm {
- contentType = models.ContentTypeForm
+ contentType := webhook.ContentTypeJSON
+ if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
+ contentType = webhook.ContentTypeForm
}
w.URL = form.PayloadURL
@@ -811,7 +813,7 @@ func GogsHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("GogsHooksEditPost", err)
return
}
@@ -844,7 +846,7 @@ func SlackHooksEditPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.SlackMeta{
+ meta, err := json.Marshal(&webhook_service.SlackMeta{
Channel: strings.TrimSpace(form.Channel),
Username: form.Username,
IconURL: form.IconURL,
@@ -862,7 +864,7 @@ func SlackHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -889,7 +891,7 @@ func DiscordHooksEditPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.DiscordMeta{
+ meta, err := json.Marshal(&webhook_service.DiscordMeta{
Username: form.Username,
IconURL: form.IconURL,
})
@@ -905,7 +907,7 @@ func DiscordHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -938,7 +940,7 @@ func DingtalkHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -965,7 +967,7 @@ func TelegramHooksEditPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.TelegramMeta{
+ meta, err := json.Marshal(&webhook_service.TelegramMeta{
BotToken: form.BotToken,
ChatID: form.ChatID,
})
@@ -980,7 +982,7 @@ func TelegramHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -1007,7 +1009,7 @@ func MatrixHooksEditPost(ctx *context.Context) {
return
}
- meta, err := json.Marshal(&webhook.MatrixMeta{
+ meta, err := json.Marshal(&webhook_service.MatrixMeta{
HomeserverURL: form.HomeserverURL,
Room: form.RoomID,
AccessToken: form.AccessToken,
@@ -1025,7 +1027,7 @@ func MatrixHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -1058,7 +1060,7 @@ func MSTeamsHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -1091,7 +1093,7 @@ func FeishuHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -1124,7 +1126,7 @@ func WechatworkHooksEditPost(ctx *context.Context) {
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
- } else if err := models.UpdateWebhook(w); err != nil {
+ } else if err := webhook.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
@@ -1136,7 +1138,7 @@ func WechatworkHooksEditPost(ctx *context.Context) {
// TestWebhook test if web hook is work fine
func TestWebhook(ctx *context.Context) {
hookID := ctx.ParamsInt64(":id")
- w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
+ w, err := webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
if err != nil {
ctx.Flash.Error("GetWebhookByID: " + err.Error())
ctx.Status(500)
@@ -1181,7 +1183,7 @@ func TestWebhook(ctx *context.Context) {
Pusher: apiUser,
Sender: apiUser,
}
- if err := webhook.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
+ if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil {
ctx.Flash.Error("PrepareWebhook: " + err.Error())
ctx.Status(500)
} else {
@@ -1192,7 +1194,7 @@ func TestWebhook(ctx *context.Context) {
// DeleteWebhook delete a webhook
func DeleteWebhook(ctx *context.Context) {
- if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
+ if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
} else {
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))