From 4f4104aa100738d8f2181051ad34c7f3cc80befa Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 00:49:28 +0100 Subject: [PATCH 01/16] Remove "import services" for webhook module Previously, there was an "import services/webhooks" inside "modules/notification/webhook". This import was removed (after fighting against many import cycles). Additionally, a few constants were extracted from "models/webhooks" to "modules/notification/webhook". --- .gitignore | 3 + models/migrations/v1_19/v233.go | 8 +- models/webhook/hooktask.go | 7 +- models/webhook/webhook.go | 182 +++++------------- modules/convert/convert.go | 34 ---- modules/notification/notification.go | 2 - modules/notification/webhook/error.go | 49 +++++ modules/notification/webhook/structs.go | 41 ++++ modules/notification/webhook/type.go | 85 ++++++++ routers/api/v1/org/hook.go | 9 +- routers/api/v1/repo/hook.go | 9 +- routers/api/v1/utils/hook.go | 16 +- routers/web/repo/webhook.go | 49 ++--- services/webhook/deliver.go | 3 +- services/webhook/dingtalk.go | 6 +- services/webhook/discord.go | 19 +- services/webhook/feishu.go | 6 +- services/webhook/general.go | 34 ++++ services/webhook/matrix.go | 5 +- services/webhook/msteams.go | 12 +- .../webhook/notifier.go | 117 ++++++----- services/webhook/packagist.go | 27 +-- services/webhook/payloader.go | 30 +-- services/webhook/slack.go | 5 +- services/webhook/telegram.go | 5 +- services/webhook/webhook.go | 57 +++--- services/webhook/wechatwork.go | 6 +- 27 files changed, 457 insertions(+), 369 deletions(-) create mode 100644 modules/notification/webhook/error.go create mode 100644 modules/notification/webhook/structs.go create mode 100644 modules/notification/webhook/type.go rename modules/notification/webhook/webhook.go => services/webhook/notifier.go (79%) diff --git a/.gitignore b/.gitignore index 1ce2a87611e81..cf48f15f5c1ad 100644 --- a/.gitignore +++ b/.gitignore @@ -112,3 +112,6 @@ prime/ # Manpage /man + +# Tags file, simply because it's too large to commit (200MB with node modules) +tags diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go index fe568b64eb572..9ce2f64fd599e 100644 --- a/models/migrations/v1_19/v233.go +++ b/models/migrations/v1_19/v233.go @@ -4,9 +4,9 @@ package v1_19 //nolint import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" - "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" @@ -56,9 +56,9 @@ func batchProcess[T any](x *xorm.Engine, buf []T, query func(limit, start int) * func AddHeaderAuthorizationEncryptedColWebhook(x *xorm.Engine) error { // Add the column to the table type Webhook struct { - ID int64 `xorm:"pk autoincr"` - Type webhook.HookType `xorm:"VARCHAR(16) 'type'"` - Meta string `xorm:"TEXT"` // store hook-specific attributes + ID int64 `xorm:"pk autoincr"` + Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"` + Meta string `xorm:"TEXT"` // store hook-specific attributes // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() HeaderAuthorizationEncrypted string `xorm:"TEXT"` diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 2a37ff31d88d3..de08b654e5926 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "time" @@ -107,7 +108,7 @@ type HookTask struct { UUID string `xorm:"unique"` api.Payloader `xorm:"-"` PayloadContent string `xorm:"LONGTEXT"` - EventType HookEventType + EventType webhook_module.HookEventType IsDelivered bool Delivered int64 DeliveredString string `xorm:"-"` @@ -194,7 +195,7 @@ func GetHookTaskByID(ctx context.Context, id int64) (*HookTask, error) { return nil, err } if !has { - return nil, ErrHookTaskNotExist{ + return nil, webhook_module.ErrHookTaskNotExist{ TaskID: id, } } @@ -217,7 +218,7 @@ func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask, if err != nil { return nil, err } else if !has { - return nil, ErrHookTaskNotExist{ + return nil, webhook_module.ErrHookTaskNotExist{ HookID: hookID, UUID: uuid, } diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 5defc67fd7219..0c710b99905e4 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -5,6 +5,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "fmt" "strings" @@ -20,46 +21,6 @@ import ( "xorm.io/builder" ) -// ErrWebhookNotExist represents a "WebhookNotExist" kind of error. -type ErrWebhookNotExist struct { - ID int64 -} - -// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. -func IsErrWebhookNotExist(err error) bool { - _, ok := err.(ErrWebhookNotExist) - return ok -} - -func (err ErrWebhookNotExist) Error() string { - return fmt.Sprintf("webhook does not exist [id: %d]", err.ID) -} - -func (err ErrWebhookNotExist) Unwrap() error { - return util.ErrNotExist -} - -// ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. -type ErrHookTaskNotExist struct { - TaskID int64 - HookID int64 - UUID string -} - -// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. -func IsErrHookTaskNotExist(err error) bool { - _, ok := err.(ErrHookTaskNotExist) - return ok -} - -func (err ErrHookTaskNotExist) Error() string { - return fmt.Sprintf("hook task does not exist [task: %d, hook: %d, uuid: %s]", err.TaskID, err.HookID, err.UUID) -} - -func (err ErrHookTaskNotExist) Unwrap() error { - return util.ErrNotExist -} - // HookContentType is the content type of a web hook type HookContentType int @@ -117,61 +78,6 @@ func IsValidHookContentType(name string) bool { return ok } -// HookEvents is a set of web hook events -type HookEvents struct { - Create bool `json:"create"` - Delete bool `json:"delete"` - Fork bool `json:"fork"` - Issues bool `json:"issues"` - IssueAssign bool `json:"issue_assign"` - IssueLabel bool `json:"issue_label"` - IssueMilestone bool `json:"issue_milestone"` - IssueComment bool `json:"issue_comment"` - Push bool `json:"push"` - PullRequest bool `json:"pull_request"` - PullRequestAssign bool `json:"pull_request_assign"` - PullRequestLabel bool `json:"pull_request_label"` - PullRequestMilestone bool `json:"pull_request_milestone"` - PullRequestComment bool `json:"pull_request_comment"` - PullRequestReview bool `json:"pull_request_review"` - PullRequestSync bool `json:"pull_request_sync"` - Wiki bool `json:"wiki"` - Repository bool `json:"repository"` - Release bool `json:"release"` - Package bool `json:"package"` -} - -// HookEvent represents events that will delivery hook. -type HookEvent struct { - PushOnly bool `json:"push_only"` - SendEverything bool `json:"send_everything"` - ChooseEvents bool `json:"choose_events"` - BranchFilter string `json:"branch_filter"` - - HookEvents `json:"events"` -} - -// HookType is the type of a webhook -type HookType = string - -// Types of webhooks -const ( - GITEA HookType = "gitea" - GOGS HookType = "gogs" - SLACK HookType = "slack" - DISCORD HookType = "discord" - DINGTALK HookType = "dingtalk" - TELEGRAM HookType = "telegram" - MSTEAMS HookType = "msteams" - FEISHU HookType = "feishu" - MATRIX HookType = "matrix" - WECHATWORK HookType = "wechatwork" - PACKAGIST HookType = "packagist" -) - -// HookStatus is the status of a web hook -type HookStatus int - // Possible statuses of a web hook const ( HookStatusNone = iota @@ -181,20 +87,20 @@ const ( // Webhook represents a web hook object. type Webhook struct { - ID int64 `xorm:"pk autoincr"` - RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook - OrgID int64 `xorm:"INDEX"` - IsSystemWebhook bool - URL string `xorm:"url TEXT"` - HTTPMethod string `xorm:"http_method"` - ContentType HookContentType - Secret string `xorm:"TEXT"` - Events string `xorm:"TEXT"` - *HookEvent `xorm:"-"` - IsActive bool `xorm:"INDEX"` - Type HookType `xorm:"VARCHAR(16) 'type'"` - Meta string `xorm:"TEXT"` // store hook-specific attributes - LastStatus HookStatus // Last delivery status + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook + OrgID int64 `xorm:"INDEX"` + IsSystemWebhook bool + URL string `xorm:"url TEXT"` + HTTPMethod string `xorm:"http_method"` + ContentType HookContentType + Secret string `xorm:"TEXT"` + Events string `xorm:"TEXT"` + *webhook_module.HookEvent `xorm:"-"` + IsActive bool `xorm:"INDEX"` + Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"` + Meta string `xorm:"TEXT"` // store hook-specific attributes + LastStatus webhook_module.HookStatus // Last delivery status // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() HeaderAuthorizationEncrypted string `xorm:"TEXT"` @@ -209,7 +115,7 @@ func init() { // AfterLoad updates the webhook object upon setting a column func (w *Webhook) AfterLoad() { - w.HookEvent = &HookEvent{} + w.HookEvent = &webhook_module.HookEvent{} if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error("Unmarshal[%d]: %v", w.ID, err) } @@ -362,34 +268,34 @@ func (w *Webhook) HasPackageEvent() bool { // EventCheckers returns event checkers func (w *Webhook) EventCheckers() []struct { Has func() bool - Type HookEventType + Type webhook_module.HookEventType } { return []struct { Has func() bool - Type HookEventType + Type webhook_module.HookEventType }{ - {w.HasCreateEvent, HookEventCreate}, - {w.HasDeleteEvent, HookEventDelete}, - {w.HasForkEvent, HookEventFork}, - {w.HasPushEvent, HookEventPush}, - {w.HasIssuesEvent, HookEventIssues}, - {w.HasIssuesAssignEvent, HookEventIssueAssign}, - {w.HasIssuesLabelEvent, HookEventIssueLabel}, - {w.HasIssuesMilestoneEvent, HookEventIssueMilestone}, - {w.HasIssueCommentEvent, HookEventIssueComment}, - {w.HasPullRequestEvent, HookEventPullRequest}, - {w.HasPullRequestAssignEvent, HookEventPullRequestAssign}, - {w.HasPullRequestLabelEvent, HookEventPullRequestLabel}, - {w.HasPullRequestMilestoneEvent, HookEventPullRequestMilestone}, - {w.HasPullRequestCommentEvent, HookEventPullRequestComment}, - {w.HasPullRequestApprovedEvent, HookEventPullRequestReviewApproved}, - {w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected}, - {w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment}, - {w.HasPullRequestSyncEvent, HookEventPullRequestSync}, - {w.HasWikiEvent, HookEventWiki}, - {w.HasRepositoryEvent, HookEventRepository}, - {w.HasReleaseEvent, HookEventRelease}, - {w.HasPackageEvent, HookEventPackage}, + {w.HasCreateEvent, webhook_module.HookEventCreate}, + {w.HasDeleteEvent, webhook_module.HookEventDelete}, + {w.HasForkEvent, webhook_module.HookEventFork}, + {w.HasPushEvent, webhook_module.HookEventPush}, + {w.HasIssuesEvent, webhook_module.HookEventIssues}, + {w.HasIssuesAssignEvent, webhook_module.HookEventIssueAssign}, + {w.HasIssuesLabelEvent, webhook_module.HookEventIssueLabel}, + {w.HasIssuesMilestoneEvent, webhook_module.HookEventIssueMilestone}, + {w.HasIssueCommentEvent, webhook_module.HookEventIssueComment}, + {w.HasPullRequestEvent, webhook_module.HookEventPullRequest}, + {w.HasPullRequestAssignEvent, webhook_module.HookEventPullRequestAssign}, + {w.HasPullRequestLabelEvent, webhook_module.HookEventPullRequestLabel}, + {w.HasPullRequestMilestoneEvent, webhook_module.HookEventPullRequestMilestone}, + {w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestComment}, + {w.HasPullRequestApprovedEvent, webhook_module.HookEventPullRequestReviewApproved}, + {w.HasPullRequestRejectedEvent, webhook_module.HookEventPullRequestReviewRejected}, + {w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestReviewComment}, + {w.HasPullRequestSyncEvent, webhook_module.HookEventPullRequestSync}, + {w.HasWikiEvent, webhook_module.HookEventWiki}, + {w.HasRepositoryEvent, webhook_module.HookEventRepository}, + {w.HasReleaseEvent, webhook_module.HookEventRelease}, + {w.HasPackageEvent, webhook_module.HookEventPackage}, } } @@ -453,7 +359,7 @@ func getWebhook(bean *Webhook) (*Webhook, error) { if err != nil { return nil, err } else if !has { - return nil, ErrWebhookNotExist{bean.ID} + return nil, webhook_module.ErrWebhookNotExist{ID: bean.ID} } return bean, nil } @@ -541,7 +447,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) { if err != nil { return nil, err } else if !has { - return nil, ErrWebhookNotExist{id} + return nil, webhook_module.ErrWebhookNotExist{ID: id} } return webhook, nil } @@ -583,7 +489,7 @@ func deleteWebhook(bean *Webhook) (err error) { if count, err := db.DeleteByBean(ctx, bean); err != nil { return err } else if count == 0 { - return ErrWebhookNotExist{ID: bean.ID} + return webhook_module.ErrWebhookNotExist{ID: bean.ID} } else if _, err = db.DeleteByBean(ctx, &HookTask{HookID: bean.ID}); err != nil { return err } @@ -621,7 +527,7 @@ func DeleteDefaultSystemWebhook(id int64) error { if err != nil { return err } else if count == 0 { - return ErrWebhookNotExist{ID: id} + return webhook_module.ErrWebhookNotExist{ID: id} } if _, err := db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil { diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 756a1f95d9054..a8329f528584f 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -22,13 +22,11 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" - webhook_service "code.gitea.io/gitea/services/webhook" ) // ToEmail convert models.EmailAddress to api.Email @@ -242,38 +240,6 @@ func ToGPGKeyEmail(email *user_model.EmailAddress) *api.GPGKeyEmail { } } -// ToHook convert models.Webhook to api.Hook -func ToHook(repoLink string, w *webhook.Webhook) (*api.Hook, error) { - config := map[string]string{ - "url": w.URL, - "content_type": w.ContentType.Name(), - } - if w.Type == webhook.SLACK { - s := webhook_service.GetSlackHook(w) - config["channel"] = s.Channel - config["username"] = s.Username - config["icon_url"] = s.IconURL - config["color"] = s.Color - } - - authorizationHeader, err := w.HeaderAuthorization() - if err != nil { - return nil, err - } - - return &api.Hook{ - ID: w.ID, - Type: w.Type, - URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID), - Active: w.IsActive, - Config: config, - Events: w.EventsArray(), - AuthorizationHeader: authorizationHeader, - Updated: w.UpdatedUnix.AsTime(), - Created: w.CreatedUnix.AsTime(), - }, nil -} - // ToGitHook convert git.Hook to api.GitHook func ToGitHook(h *git.Hook) *api.GitHook { return &api.GitHook{ diff --git a/modules/notification/notification.go b/modules/notification/notification.go index c3e09bb8a9151..10581eb87f7c6 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -16,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/notification/mail" "code.gitea.io/gitea/modules/notification/mirror" "code.gitea.io/gitea/modules/notification/ui" - "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" ) @@ -36,7 +35,6 @@ func NewContext() { RegisterNotifier(mail.NewNotifier()) } RegisterNotifier(indexer.NewNotifier()) - RegisterNotifier(webhook.NewNotifier()) RegisterNotifier(action.NewNotifier()) RegisterNotifier(mirror.NewNotifier()) } diff --git a/modules/notification/webhook/error.go b/modules/notification/webhook/error.go new file mode 100644 index 0000000000000..a9a0267ae8c81 --- /dev/null +++ b/modules/notification/webhook/error.go @@ -0,0 +1,49 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package webhook + +import ( + "code.gitea.io/gitea/modules/util" + "fmt" +) + +// ErrWebhookNotExist represents a "WebhookNotExist" kind of error. +type ErrWebhookNotExist struct { + ID int64 +} + +// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. +func IsErrWebhookNotExist(err error) bool { + _, ok := err.(ErrWebhookNotExist) + return ok +} + +func (err ErrWebhookNotExist) Error() string { + return fmt.Sprintf("webhook does not exist [id: %d]", err.ID) +} + +func (err ErrWebhookNotExist) Unwrap() error { + return util.ErrNotExist +} + +// ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. +type ErrHookTaskNotExist struct { + TaskID int64 + HookID int64 + UUID string +} + +// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. +func IsErrHookTaskNotExist(err error) bool { + _, ok := err.(ErrHookTaskNotExist) + return ok +} + +func (err ErrHookTaskNotExist) Error() string { + return fmt.Sprintf("hook task does not exist [task: %d, hook: %d, uuid: %s]", err.TaskID, err.HookID, err.UUID) +} + +func (err ErrHookTaskNotExist) Unwrap() error { + return util.ErrNotExist +} diff --git a/modules/notification/webhook/structs.go b/modules/notification/webhook/structs.go new file mode 100644 index 0000000000000..d3bda7d18de41 --- /dev/null +++ b/modules/notification/webhook/structs.go @@ -0,0 +1,41 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package webhook + +// HookEvents is a set of web hook events +type HookEvents struct { + Create bool `json:"create"` + Delete bool `json:"delete"` + Fork bool `json:"fork"` + Issues bool `json:"issues"` + IssueAssign bool `json:"issue_assign"` + IssueLabel bool `json:"issue_label"` + IssueMilestone bool `json:"issue_milestone"` + IssueComment bool `json:"issue_comment"` + Push bool `json:"push"` + PullRequest bool `json:"pull_request"` + PullRequestAssign bool `json:"pull_request_assign"` + PullRequestLabel bool `json:"pull_request_label"` + PullRequestMilestone bool `json:"pull_request_milestone"` + PullRequestComment bool `json:"pull_request_comment"` + PullRequestReview bool `json:"pull_request_review"` + PullRequestSync bool `json:"pull_request_sync"` + Wiki bool `json:"wiki"` + Repository bool `json:"repository"` + Release bool `json:"release"` + Package bool `json:"package"` +} + +// HookEvent represents events that will delivery hook. +type HookEvent struct { + PushOnly bool `json:"push_only"` + SendEverything bool `json:"send_everything"` + ChooseEvents bool `json:"choose_events"` + BranchFilter string `json:"branch_filter"` + + HookEvents `json:"events"` +} + +// HookStatus is the status of a web hook +type HookStatus int diff --git a/modules/notification/webhook/type.go b/modules/notification/webhook/type.go new file mode 100644 index 0000000000000..a0eeac3eb396c --- /dev/null +++ b/modules/notification/webhook/type.go @@ -0,0 +1,85 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package webhook + +// HookEventType is the type of a hook event +type HookEventType string + +// Types of hook events +const ( + HookEventCreate HookEventType = "create" + HookEventDelete HookEventType = "delete" + HookEventFork HookEventType = "fork" + HookEventPush HookEventType = "push" + HookEventIssues HookEventType = "issues" + HookEventIssueAssign HookEventType = "issue_assign" + HookEventIssueLabel HookEventType = "issue_label" + HookEventIssueMilestone HookEventType = "issue_milestone" + HookEventIssueComment HookEventType = "issue_comment" + HookEventPullRequest HookEventType = "pull_request" + HookEventPullRequestAssign HookEventType = "pull_request_assign" + HookEventPullRequestLabel HookEventType = "pull_request_label" + HookEventPullRequestMilestone HookEventType = "pull_request_milestone" + HookEventPullRequestComment HookEventType = "pull_request_comment" + HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved" + HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected" + HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment" + HookEventPullRequestSync HookEventType = "pull_request_sync" + HookEventWiki HookEventType = "wiki" + HookEventRepository HookEventType = "repository" + HookEventRelease HookEventType = "release" + HookEventPackage HookEventType = "package" +) + +// Event returns the HookEventType as an event string +func (h HookEventType) Event() string { + switch h { + case HookEventCreate: + return "create" + case HookEventDelete: + return "delete" + case HookEventFork: + return "fork" + case HookEventPush: + return "push" + case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone: + return "issues" + case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone, + HookEventPullRequestSync: + return "pull_request" + case HookEventIssueComment, HookEventPullRequestComment: + return "issue_comment" + case HookEventPullRequestReviewApproved: + return "pull_request_approved" + case HookEventPullRequestReviewRejected: + return "pull_request_rejected" + case HookEventPullRequestReviewComment: + return "pull_request_comment" + case HookEventWiki: + return "wiki" + case HookEventRepository: + return "repository" + case HookEventRelease: + return "release" + } + return "" +} + +// HookType is the type of a webhook +type HookType = string + +// Types of webhooks +const ( + GITEA HookType = "gitea" + GOGS HookType = "gogs" + SLACK HookType = "slack" + DISCORD HookType = "discord" + DINGTALK HookType = "dingtalk" + TELEGRAM HookType = "telegram" + MSTEAMS HookType = "msteams" + FEISHU HookType = "feishu" + MATRIX HookType = "matrix" + WECHATWORK HookType = "wechatwork" + PACKAGIST HookType = "packagist" +) diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index 2e9d7c656a76f..e5a91ab6a571c 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -4,11 +4,12 @@ package org import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" + webhook_service "code.gitea.io/gitea/services/webhook" "net/http" "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" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" @@ -58,7 +59,7 @@ func ListHooks(ctx *context.APIContext) { hooks := make([]*api.Hook, len(orgHooks)) for i, hook := range orgHooks { - hooks[i], err = convert.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook) + hooks[i], err = webhook_service.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook) if err != nil { ctx.InternalServerError(err) return @@ -99,7 +100,7 @@ func GetHook(ctx *context.APIContext) { return } - apiHook, err := convert.ToHook(org.AsUser().HomeLink(), hook) + apiHook, err := webhook_service.ToHook(org.AsUser().HomeLink(), hook) if err != nil { ctx.InternalServerError(err) return @@ -201,7 +202,7 @@ func DeleteHook(ctx *context.APIContext) { org := ctx.Org.Organization hookID := ctx.ParamsInt64(":id") if err := webhook.DeleteWebhookByOrgID(org.ID, hookID); err != nil { - if webhook.IsErrWebhookNotExist(err) { + if webhook_module.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 0c3d59a41938f..7beec0424eba2 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -5,6 +5,7 @@ package repo import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "net/http" "code.gitea.io/gitea/models/perm" @@ -68,7 +69,7 @@ func ListHooks(ctx *context.APIContext) { apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { - apiHooks[i], err = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) + apiHooks[i], err = webhook_service.ToHook(ctx.Repo.RepoLink, hooks[i]) if err != nil { ctx.InternalServerError(err) return @@ -115,7 +116,7 @@ func GetHook(ctx *context.APIContext) { if err != nil { return } - apiHook, err := convert.ToHook(repo.RepoLink, hook) + apiHook, err := webhook_service.ToHook(repo.RepoLink, hook) if err != nil { ctx.InternalServerError(err) return @@ -176,7 +177,7 @@ func TestHook(ctx *context.APIContext) { commit := convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit) commitID := ctx.Repo.Commit.ID.String() - if err := webhook_service.PrepareWebhook(ctx, hook, webhook.HookEventPush, &api.PushPayload{ + if err := webhook_service.PrepareWebhook(ctx, hook, webhook_module.HookEventPush, &api.PushPayload{ Ref: ref, Before: commitID, After: commitID, @@ -296,7 +297,7 @@ func DeleteHook(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { - if webhook.IsErrWebhookNotExist(err) { + if webhook_module.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 7faf609ae8136..ba8a9332eecab 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -4,13 +4,13 @@ package utils import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/http" "strings" "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" @@ -22,7 +22,7 @@ import ( func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) { w, err := webhook.GetWebhookByOrgID(orgID, hookID) if err != nil { - if webhook.IsErrWebhookNotExist(err) { + if webhook_module.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err) @@ -37,7 +37,7 @@ func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) { w, err := webhook.GetWebhookByRepoID(repoID, hookID) if err != nil { - if webhook.IsErrWebhookNotExist(err) { + if webhook_module.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err) @@ -98,7 +98,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) { // toAPIHook converts the hook to its API representation. // If there is an error, write to `ctx` accordingly. Return (hook, ok) func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) { - apiHook, err := convert.ToHook(repoLink, hook) + apiHook, err := webhook_service.ToHook(repoLink, hook) if err != nil { ctx.Error(http.StatusInternalServerError, "ToHook", err) return nil, false @@ -127,9 +127,9 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID ContentType: webhook.ToHookContentType(form.Config["content_type"]), Secret: form.Config["secret"], HTTPMethod: "POST", - HookEvent: &webhook.HookEvent{ + HookEvent: &webhook_module.HookEvent{ ChooseEvents: true, - HookEvents: webhook.HookEvents{ + HookEvents: webhook_module.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), @@ -160,7 +160,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err) return nil, false } - if w.Type == webhook.SLACK { + if w.Type == webhook_module.SLACK { channel, ok := form.Config["channel"] if !ok { ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel") @@ -253,7 +253,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh w.ContentType = webhook.ToHookContentType(ct) } - if w.Type == webhook.SLACK { + if w.Type == webhook_module.SLACK { if channel, ok := form.Config["channel"]; ok { meta, err := json.Marshal(&webhook_service.SlackMeta{ Channel: channel, diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 18d71c6435a50..ca091d10694c8 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -5,6 +5,7 @@ package repo import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "net/http" @@ -119,7 +120,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"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}} + ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}} orCtx, err := getOrgRepoCtx(ctx) if err != nil { @@ -154,12 +155,12 @@ func WebhooksNew(ctx *context.Context) { } // ParseHookEvent convert web form content to webhook.HookEvent -func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent { - return &webhook.HookEvent{ +func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent { + return &webhook_module.HookEvent{ PushOnly: form.PushOnly(), SendEverything: form.SendEverything(), ChooseEvents: form.ChooseEvents(), - HookEvents: webhook.HookEvents{ + HookEvents: webhook_module.HookEvents{ Create: form.Create, Delete: form.Delete, Fork: form.Fork, @@ -201,7 +202,7 @@ func createWebhook(ctx *context.Context, params webhookParams) { ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true - ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}} + ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}} ctx.Data["HookType"] = params.Type orCtx, err := getOrgRepoCtx(ctx) @@ -326,7 +327,7 @@ func giteaHookParams(ctx *context.Context) webhookParams { } return webhookParams{ - Type: webhook.GITEA, + Type: webhook_module.GITEA, URL: form.PayloadURL, ContentType: contentType, Secret: form.Secret, @@ -354,7 +355,7 @@ func gogsHookParams(ctx *context.Context) webhookParams { } return webhookParams{ - Type: webhook.GOGS, + Type: webhook_module.GOGS, URL: form.PayloadURL, ContentType: contentType, Secret: form.Secret, @@ -376,7 +377,7 @@ func discordHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewDiscordHookForm) return webhookParams{ - Type: webhook.DISCORD, + Type: webhook_module.DISCORD, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -401,7 +402,7 @@ func dingtalkHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewDingtalkHookForm) return webhookParams{ - Type: webhook.DINGTALK, + Type: webhook_module.DINGTALK, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -422,7 +423,7 @@ func telegramHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewTelegramHookForm) return webhookParams{ - Type: webhook.TELEGRAM, + Type: webhook_module.TELEGRAM, URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)), ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -447,7 +448,7 @@ func matrixHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewMatrixHookForm) return webhookParams{ - Type: webhook.MATRIX, + Type: webhook_module.MATRIX, URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)), ContentType: webhook.ContentTypeJSON, HTTPMethod: http.MethodPut, @@ -474,7 +475,7 @@ func mSTeamsHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm) return webhookParams{ - Type: webhook.MSTEAMS, + Type: webhook_module.MSTEAMS, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -495,7 +496,7 @@ func slackHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewSlackHookForm) return webhookParams{ - Type: webhook.SLACK, + Type: webhook_module.SLACK, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -522,7 +523,7 @@ func feishuHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewFeishuHookForm) return webhookParams{ - Type: webhook.FEISHU, + Type: webhook_module.FEISHU, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -543,7 +544,7 @@ func wechatworkHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm) return webhookParams{ - Type: webhook.WECHATWORK, + Type: webhook_module.WECHATWORK, URL: form.PayloadURL, ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -564,7 +565,7 @@ func packagistHookParams(ctx *context.Context) webhookParams { form := web.GetForm(ctx).(*forms.NewPackagistHookForm) return webhookParams{ - Type: webhook.PACKAGIST, + Type: webhook_module.PACKAGIST, URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)), ContentType: webhook.ContentTypeJSON, WebhookForm: form.WebhookForm, @@ -593,7 +594,7 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) { w, err = webhook.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id")) } if err != nil || w == nil { - if webhook.IsErrWebhookNotExist(err) { + if webhook_module.IsErrWebhookNotExist(err) { ctx.NotFound("GetWebhookByID", nil) } else { ctx.ServerError("GetWebhookByID", err) @@ -603,15 +604,15 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) { ctx.Data["HookType"] = w.Type switch w.Type { - case webhook.SLACK: + case webhook_module.SLACK: ctx.Data["SlackHook"] = webhook_service.GetSlackHook(w) - case webhook.DISCORD: + case webhook_module.DISCORD: ctx.Data["DiscordHook"] = webhook_service.GetDiscordHook(w) - case webhook.TELEGRAM: + case webhook_module.TELEGRAM: ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w) - case webhook.MATRIX: + case webhook_module.MATRIX: ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w) - case webhook.PACKAGIST: + case webhook_module.PACKAGIST: ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w) } @@ -688,7 +689,7 @@ func TestWebhook(ctx *context.Context) { Pusher: apiUser, Sender: apiUser, } - if err := webhook_service.PrepareWebhook(ctx, w, webhook.HookEventPush, p); err != nil { + if err := webhook_service.PrepareWebhook(ctx, w, webhook_module.HookEventPush, p); err != nil { ctx.Flash.Error("PrepareWebhook: " + err.Error()) ctx.Status(http.StatusInternalServerError) } else { @@ -707,7 +708,7 @@ func ReplayWebhook(ctx *context.Context) { } if err := webhook_service.ReplayHookTask(ctx, w, hookTaskUUID); err != nil { - if webhook.IsErrHookTaskNotExist(err) { + if webhook_module.IsErrHookTaskNotExist(err) { ctx.NotFound("ReplayHookTask", nil) } else { ctx.ServerError("ReplayHookTask", err) diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index eed711c580060..27329e039a1ba 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "crypto/hmac" "crypto/sha1" @@ -89,7 +90,7 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { } case http.MethodPut: switch w.Type { - case webhook_model.MATRIX: + case webhook_module.MATRIX: txnID, err := getMatrixTxnID([]byte(t.PayloadContent)) if err != nil { return err diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 7cb1fb75fc30e..bc0a6f2db7f26 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -4,11 +4,11 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/url" "strings" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -129,7 +129,7 @@ func (d *DingtalkPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { var text, title string switch p.Action { case api.HookIssueReviewed: @@ -190,6 +190,6 @@ func createDingtalkPayload(title, text, singleTitle, singleURL string) *Dingtalk } // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload -func GetDingtalkPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetDingtalkPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) { return convertPayloader(new(DingtalkPayload), p, event) } diff --git a/services/webhook/discord.go b/services/webhook/discord.go index c9fdc95320429..7a06fe66bfa56 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "net/url" @@ -190,7 +191,7 @@ func (d *DiscordPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (d *DiscordPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (d *DiscordPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { var text, title string var color int switch p.Action { @@ -204,11 +205,11 @@ func (d *DiscordPayload) Review(p *api.PullRequestPayload, event webhook_model.H text = p.Review.Content switch event { - case webhook_model.HookEventPullRequestReviewApproved: + case webhook_module.HookEventPullRequestReviewApproved: color = greenColor - case webhook_model.HookEventPullRequestReviewRejected: + case webhook_module.HookEventPullRequestReviewRejected: color = redColor - case webhook_model.HookEventPullRequestComment: + case webhook_module.HookEventPullRequestComment: color = greyColor default: color = yellowColor @@ -256,7 +257,7 @@ func (d *DiscordPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetDiscordPayload converts a discord webhook into a DiscordPayload -func GetDiscordPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetDiscordPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) { s := new(DiscordPayload) discord := &DiscordMeta{} @@ -269,14 +270,14 @@ func GetDiscordPayload(p api.Payloader, event webhook_model.HookEventType, meta return convertPayloader(s, p, event) } -func parseHookPullRequestEventType(event webhook_model.HookEventType) (string, error) { +func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) { switch event { - case webhook_model.HookEventPullRequestReviewApproved: + case webhook_module.HookEventPullRequestReviewApproved: return "approved", nil - case webhook_model.HookEventPullRequestReviewRejected: + case webhook_module.HookEventPullRequestReviewRejected: return "rejected", nil - case webhook_model.HookEventPullRequestComment: + case webhook_module.HookEventPullRequestComment: return "comment", nil default: diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 58b6fff3318ef..21b838a8e9c1f 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -4,10 +4,10 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -117,7 +117,7 @@ func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, e } // Review implements PayloadConvertor Review method -func (f *FeishuPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (f *FeishuPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { action, err := parseHookPullRequestEventType(event) if err != nil { return nil, err @@ -159,6 +159,6 @@ func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetFeishuPayload converts a ding talk webhook into a FeishuPayload -func GetFeishuPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetFeishuPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) { return convertPayloader(new(FeishuPayload), p, event) } diff --git a/services/webhook/general.go b/services/webhook/general.go index bec752cffe581..47a53f6b1f992 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -4,6 +4,8 @@ package webhook import ( + webhook_model "code.gitea.io/gitea/models/webhook" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "html" "net/url" @@ -223,3 +225,35 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo return text, issueTitle, color } + +// ToHook convert models.Webhook to api.Hook +func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) { + config := map[string]string{ + "url": w.URL, + "content_type": w.ContentType.Name(), + } + if w.Type == webhook_module.SLACK { + s := GetSlackHook(w) + config["channel"] = s.Channel + config["username"] = s.Username + config["icon_url"] = s.IconURL + config["color"] = s.Color + } + + authorizationHeader, err := w.HeaderAuthorization() + if err != nil { + return nil, err + } + + return &api.Hook{ + ID: w.ID, + Type: w.Type, + URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID), + Active: w.IsActive, + Config: config, + Events: w.EventsArray(), + AuthorizationHeader: authorizationHeader, + Updated: w.UpdatedUnix.AsTime(), + Created: w.CreatedUnix.AsTime(), + }, nil +} diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index bd3efd46b273a..291cb12ed97bc 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "crypto/sha1" "encoding/hex" "errors" @@ -173,7 +174,7 @@ func (m *MatrixPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, e } // Review implements PayloadConvertor Review method -func (m *MatrixPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (m *MatrixPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { senderLink := MatrixLinkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName) title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := MatrixLinkFormatter(p.PullRequest.URL, title) @@ -210,7 +211,7 @@ func (m *MatrixPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err } // GetMatrixPayload converts a Matrix webhook into a MatrixPayload -func GetMatrixPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetMatrixPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) { s := new(MatrixPayload) matrix := &MatrixMeta{} diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index ef8366f8d5c77..fcec38ed6ee04 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -4,11 +4,11 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/url" "strings" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -205,7 +205,7 @@ func (m *MSTeamsPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { var text, title string var color int switch p.Action { @@ -219,11 +219,11 @@ func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event webhook_model.H text = p.Review.Content switch event { - case webhook_model.HookEventPullRequestReviewApproved: + case webhook_module.HookEventPullRequestReviewApproved: color = greenColor - case webhook_model.HookEventPullRequestReviewRejected: + case webhook_module.HookEventPullRequestReviewRejected: color = redColor - case webhook_model.HookEventPullRequestComment: + case webhook_module.HookEventPullRequestComment: color = greyColor default: color = yellowColor @@ -297,7 +297,7 @@ func (m *MSTeamsPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetMSTeamsPayload converts a MSTeams webhook into a MSTeamsPayload -func GetMSTeamsPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetMSTeamsPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) { return convertPayloader(new(MSTeamsPayload), p, event) } diff --git a/modules/notification/webhook/webhook.go b/services/webhook/notifier.go similarity index 79% rename from modules/notification/webhook/webhook.go rename to services/webhook/notifier.go index cf056f54c1382..fe0833aa09b95 100644 --- a/modules/notification/webhook/webhook.go +++ b/services/webhook/notifier.go @@ -1,9 +1,11 @@ -// Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2022 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package webhook import ( + "code.gitea.io/gitea/modules/notification" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" issues_model "code.gitea.io/gitea/models/issues" @@ -13,7 +15,6 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -21,20 +22,18 @@ import ( "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - webhook_services "code.gitea.io/gitea/services/webhook" ) +func init() { + notification.RegisterNotifier(&webhookNotifier{}) +} + type webhookNotifier struct { base.NullNotifier } var _ base.Notifier = &webhookNotifier{} -// NewNotifier create a new webhookNotifier notifier -func NewNotifier() base.Notifier { - return &webhookNotifier{} -} - func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) { if err := issue.LoadPoster(ctx); err != nil { log.Error("LoadPoster: %v", err) @@ -54,7 +53,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user return } - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventType(webhook_module.HookEventPullRequestLabel), &api.PullRequestPayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -62,7 +61,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user Sender: convert.ToUser(doer, nil), }) } else { - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueLabel, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventType(webhook_module.HookEventIssueLabel), &api.IssuePayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -80,7 +79,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m mode, _ := access_model.AccessLevel(ctx, doer, repo) // forked webhook - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: oldRepo}, webhook.HookEventFork, &api.ForkPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{ Forkee: convert.ToRepo(ctx, oldRepo, oldMode), Repo: convert.ToRepo(ctx, repo, mode), Sender: convert.ToUser(doer, nil), @@ -92,7 +91,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m // Add to hook queue for created repo after session commit. if u.IsOrganization() { - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -105,7 +104,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { // Add to hook queue for created repo after session commit. - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -116,7 +115,7 @@ func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u } func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) { - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(repo.MustOwner(ctx), nil), @@ -128,7 +127,7 @@ func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { // Add to hook queue for created repo after session commit. - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -159,7 +158,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u apiPullRequest.Action = api.HookIssueAssigned } // Assignee comment triggers a webhook - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestAssign, apiPullRequest); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestAssign, apiPullRequest); err != nil { log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err) return } @@ -177,7 +176,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u apiIssue.Action = api.HookIssueAssigned } // Assignee comment triggers a webhook - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueAssign, apiIssue); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueAssign, apiIssue); err != nil { log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err) return } @@ -193,7 +192,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user return } issue.PullRequest.Issue = issue - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -206,7 +205,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user Sender: convert.ToUser(doer, nil), }) } else { - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -245,7 +244,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use } else { apiPullRequest.Action = api.HookIssueReOpened } - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, apiPullRequest) + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest) } else { apiIssue := &api.IssuePayload{ Index: issue.Index, @@ -258,7 +257,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use } else { apiIssue.Action = api.HookIssueReOpened } - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, apiIssue) + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, apiIssue) } if err != nil { log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err) @@ -276,7 +275,7 @@ func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode } mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueOpened, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -302,7 +301,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues } mode, _ := access_model.AccessLevel(ctx, pull.Issue.Poster, pull.Issue.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pull.Issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), @@ -323,7 +322,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us var err error if issue.IsPull { issue.PullRequest.Issue = issue - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -336,7 +335,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us Sender: convert.ToUser(doer, nil), }) } else { - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -369,15 +368,15 @@ func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_mo return } - var eventType webhook.HookEventType + var eventType webhook_module.HookEventType if c.Issue.IsPull { - eventType = webhook.HookEventPullRequestComment + eventType = webhook_module.HookEventPullRequestComment } else { - eventType = webhook.HookEventIssueComment + eventType = webhook_module.HookEventIssueComment } mode, _ := access_model.AccessLevel(ctx, doer, c.Issue.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: convert.ToAPIIssue(ctx, c.Issue), Comment: convert.ToComment(c), @@ -397,15 +396,15 @@ func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_mo func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, ) { - var eventType webhook.HookEventType + var eventType webhook_module.HookEventType if issue.IsPull { - eventType = webhook.HookEventPullRequestComment + eventType = webhook_module.HookEventPullRequestComment } else { - eventType = webhook.HookEventIssueComment + eventType = webhook_module.HookEventIssueComment } mode, _ := access_model.AccessLevel(ctx, doer, repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), Comment: convert.ToComment(comment), @@ -434,15 +433,15 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo return } - var eventType webhook.HookEventType + var eventType webhook_module.HookEventType if comment.Issue.IsPull { - eventType = webhook.HookEventPullRequestComment + eventType = webhook_module.HookEventPullRequestComment } else { - eventType = webhook.HookEventIssueComment + eventType = webhook_module.HookEventIssueComment } mode, _ := access_model.AccessLevel(ctx, doer, comment.Issue.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: convert.ToAPIIssue(ctx, comment.Issue), Comment: convert.ToComment(comment), @@ -456,7 +455,7 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { // Add to hook queue for created wiki page. - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -469,7 +468,7 @@ func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_mode func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { // Add to hook queue for edit wiki page. - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiEdited, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -482,7 +481,7 @@ func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_mod func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) { // Add to hook queue for edit wiki page. - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -517,7 +516,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use log.Error("LoadIssue: %v", err) return } - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ Action: api.HookIssueLabelUpdated, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -525,7 +524,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Sender: convert.ToUser(doer, nil), }) } else { - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueLabel, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ Action: api.HookIssueLabelUpdated, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -559,7 +558,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * log.Error("LoadIssue: %v", err) return } - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestMilestone, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestMilestone, &api.PullRequestPayload{ Action: hookAction, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -567,7 +566,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * Sender: convert.ToUser(doer, nil), }) } else { - err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueMilestone, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{ Action: hookAction, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -588,7 +587,7 @@ func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo return } - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventPush, &api.PushPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName, Before: opts.OldCommitID, After: opts.NewCommitID, @@ -641,7 +640,7 @@ func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m Action: api.HookIssueClosed, } - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pr.Issue.Repo}, webhook.HookEventPullRequest, apiPullRequest); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest); err != nil { log.Error("PrepareWebhooks: %v", err) } } @@ -655,7 +654,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex issue := pr.Issue mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -672,15 +671,15 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex } func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) { - var reviewHookType webhook.HookEventType + var reviewHookType webhook_module.HookEventType switch review.Type { case issues_model.ReviewTypeApprove: - reviewHookType = webhook.HookEventPullRequestReviewApproved + reviewHookType = webhook_module.HookEventPullRequestReviewApproved case issues_model.ReviewTypeComment: - reviewHookType = webhook.HookEventPullRequestComment + reviewHookType = webhook_module.HookEventPullRequestComment case issues_model.ReviewTypeReject: - reviewHookType = webhook.HookEventPullRequestReviewRejected + reviewHookType = webhook_module.HookEventPullRequestReviewRejected default: // unsupported review webhook type here log.Error("Unsupported review webhook type") @@ -697,7 +696,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue log.Error("models.AccessLevel: %v", err) return } - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{ Action: api.HookIssueReviewed, Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), @@ -717,7 +716,7 @@ func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventCreate, &api.CreatePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{ Ref: refName, Sha: refID, RefType: refType, @@ -738,7 +737,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe return } - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pr.Issue.Repo}, webhook.HookEventPullRequestSync, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequestSync, &api.PullRequestPayload{ Action: api.HookIssueSynchronized, Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), @@ -754,7 +753,7 @@ func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventDelete, &api.DeletePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{ Ref: refName, RefType: refType, PusherType: api.PusherTypeUser, @@ -772,7 +771,7 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model } mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo) - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: rel.Repo}, webhook.HookEventRelease, &api.ReleasePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{ Action: action, Release: convert.ToRelease(rel), Repository: convert.ToRepo(ctx, rel.Repo, mode), @@ -802,7 +801,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use return } - if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventPush, &api.PushPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName, Before: opts.OldCommitID, After: opts.NewCommitID, @@ -835,7 +834,7 @@ func (m *webhookNotifier) NotifyPackageDelete(ctx context.Context, doer *user_mo } func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) { - source := webhook_services.EventSource{ + source := EventSource{ Repository: pd.Repository, Owner: pd.Owner, } @@ -846,7 +845,7 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo return } - if err := webhook_services.PrepareWebhooks(ctx, source, webhook.HookEventPackage, &api.PackagePayload{ + if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{ Action: action, Package: apiPackage, Sender: convert.ToUser(sender, nil), diff --git a/services/webhook/packagist.go b/services/webhook/packagist.go index 815e1a93e9f1f..b03cc1224ff38 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" webhook_model "code.gitea.io/gitea/models/webhook" @@ -20,7 +21,7 @@ type ( } `json:"repository"` } - // PackagistMeta contains the meta data for the webhook + // PackagistMeta contains the metadata for the webhook PackagistMeta struct { Username string `json:"username"` APIToken string `json:"api_token"` @@ -49,62 +50,62 @@ func (f *PackagistPayload) JSONPayload() ([]byte, error) { var _ PayloadConvertor = &PackagistPayload{} // Create implements PayloadConvertor Create method -func (f *PackagistPayload) Create(p *api.CreatePayload) (api.Payloader, error) { +func (f *PackagistPayload) Create(_ *api.CreatePayload) (api.Payloader, error) { return nil, nil } // Delete implements PayloadConvertor Delete method -func (f *PackagistPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { +func (f *PackagistPayload) Delete(_ *api.DeletePayload) (api.Payloader, error) { return nil, nil } // Fork implements PayloadConvertor Fork method -func (f *PackagistPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { +func (f *PackagistPayload) Fork(_ *api.ForkPayload) (api.Payloader, error) { return nil, nil } // Push implements PayloadConvertor Push method -func (f *PackagistPayload) Push(p *api.PushPayload) (api.Payloader, error) { +func (f *PackagistPayload) Push(_ *api.PushPayload) (api.Payloader, error) { return f, nil } // Issue implements PayloadConvertor Issue method -func (f *PackagistPayload) Issue(p *api.IssuePayload) (api.Payloader, error) { +func (f *PackagistPayload) Issue(_ *api.IssuePayload) (api.Payloader, error) { return nil, nil } // IssueComment implements PayloadConvertor IssueComment method -func (f *PackagistPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { +func (f *PackagistPayload) IssueComment(_ *api.IssueCommentPayload) (api.Payloader, error) { return nil, nil } // PullRequest implements PayloadConvertor PullRequest method -func (f *PackagistPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) { +func (f *PackagistPayload) PullRequest(_ *api.PullRequestPayload) (api.Payloader, error) { return nil, nil } // Review implements PayloadConvertor Review method -func (f *PackagistPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (f *PackagistPayload) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (api.Payloader, error) { return nil, nil } // Repository implements PayloadConvertor Repository method -func (f *PackagistPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) { +func (f *PackagistPayload) Repository(_ *api.RepositoryPayload) (api.Payloader, error) { return nil, nil } // Wiki implements PayloadConvertor Wiki method -func (f *PackagistPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { +func (f *PackagistPayload) Wiki(_ *api.WikiPayload) (api.Payloader, error) { return nil, nil } // Release implements PayloadConvertor Release method -func (f *PackagistPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { +func (f *PackagistPayload) Release(_ *api.ReleasePayload) (api.Payloader, error) { return nil, nil } // GetPackagistPayload converts a packagist webhook into a PackagistPayload -func GetPackagistPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetPackagistPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) { s := new(PackagistPayload) packagist := &PackagistMeta{} diff --git a/services/webhook/payloader.go b/services/webhook/payloader.go index 7b04f1dd36a94..8a967ad41b96a 100644 --- a/services/webhook/payloader.go +++ b/services/webhook/payloader.go @@ -4,7 +4,7 @@ package webhook import ( - webhook_model "code.gitea.io/gitea/models/webhook" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" ) @@ -18,40 +18,40 @@ type PayloadConvertor interface { IssueComment(*api.IssueCommentPayload) (api.Payloader, error) Push(*api.PushPayload) (api.Payloader, error) PullRequest(*api.PullRequestPayload) (api.Payloader, error) - Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error) + Review(*api.PullRequestPayload, webhook_module.HookEventType) (api.Payloader, error) Repository(*api.RepositoryPayload) (api.Payloader, error) Release(*api.ReleasePayload) (api.Payloader, error) Wiki(*api.WikiPayload) (api.Payloader, error) } -func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) { +func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_module.HookEventType) (api.Payloader, error) { switch event { - case webhook_model.HookEventCreate: + case webhook_module.HookEventCreate: return s.Create(p.(*api.CreatePayload)) - case webhook_model.HookEventDelete: + case webhook_module.HookEventDelete: return s.Delete(p.(*api.DeletePayload)) - case webhook_model.HookEventFork: + case webhook_module.HookEventFork: return s.Fork(p.(*api.ForkPayload)) - case webhook_model.HookEventIssues, webhook_model.HookEventIssueAssign, webhook_model.HookEventIssueLabel, webhook_model.HookEventIssueMilestone: + case webhook_module.HookEventIssues, webhook_module.HookEventIssueAssign, webhook_module.HookEventIssueLabel, webhook_module.HookEventIssueMilestone: return s.Issue(p.(*api.IssuePayload)) - case webhook_model.HookEventIssueComment, webhook_model.HookEventPullRequestComment: + case webhook_module.HookEventIssueComment, webhook_module.HookEventPullRequestComment: pl, ok := p.(*api.IssueCommentPayload) if ok { return s.IssueComment(pl) } return s.PullRequest(p.(*api.PullRequestPayload)) - case webhook_model.HookEventPush: + case webhook_module.HookEventPush: return s.Push(p.(*api.PushPayload)) - case webhook_model.HookEventPullRequest, webhook_model.HookEventPullRequestAssign, webhook_model.HookEventPullRequestLabel, - webhook_model.HookEventPullRequestMilestone, webhook_model.HookEventPullRequestSync: + case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestLabel, + webhook_module.HookEventPullRequestMilestone, webhook_module.HookEventPullRequestSync: return s.PullRequest(p.(*api.PullRequestPayload)) - case webhook_model.HookEventPullRequestReviewApproved, webhook_model.HookEventPullRequestReviewRejected, webhook_model.HookEventPullRequestReviewComment: + case webhook_module.HookEventPullRequestReviewApproved, webhook_module.HookEventPullRequestReviewRejected, webhook_module.HookEventPullRequestReviewComment: return s.Review(p.(*api.PullRequestPayload), event) - case webhook_model.HookEventRepository: + case webhook_module.HookEventRepository: return s.Repository(p.(*api.RepositoryPayload)) - case webhook_model.HookEventRelease: + case webhook_module.HookEventRelease: return s.Release(p.(*api.ReleasePayload)) - case webhook_model.HookEventWiki: + case webhook_module.HookEventWiki: return s.Wiki(p.(*api.WikiPayload)) } return s, nil diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 1814361a1cd3c..9771fe281e2e0 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "regexp" @@ -231,7 +232,7 @@ func (s *SlackPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, er } // Review implements PayloadConvertor Review method -func (s *SlackPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (s *SlackPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index) @@ -278,7 +279,7 @@ func (s *SlackPayload) createPayload(text string, attachments []SlackAttachment) } // GetSlackPayload converts a slack webhook into a SlackPayload -func GetSlackPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetSlackPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) { s := new(SlackPayload) slack := &SlackMeta{} diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 8bc68490e5040..a1dc471f2a22b 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" @@ -140,7 +141,7 @@ func (t *TelegramPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (t *TelegramPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (t *TelegramPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { var text, attachmentText string switch p.Action { case api.HookIssueReviewed: @@ -185,7 +186,7 @@ func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) } // GetTelegramPayload converts a telegram webhook into a TelegramPayload -func GetTelegramPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetTelegramPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) { return convertPayloader(new(TelegramPayload), p, event) } diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 21173e7cd8255..55c843406e02b 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -11,9 +11,11 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" webhook_model "code.gitea.io/gitea/models/webhook" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -23,57 +25,52 @@ import ( ) type webhook struct { - name webhook_model.HookType - payloadCreator func(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) + name webhook_module.HookType + payloadCreator func(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) } -var webhooks = map[webhook_model.HookType]*webhook{ - webhook_model.SLACK: { - name: webhook_model.SLACK, +var webhooks = map[webhook_module.HookType]*webhook{ + webhook_module.SLACK: { + name: webhook_module.SLACK, payloadCreator: GetSlackPayload, }, - webhook_model.DISCORD: { - name: webhook_model.DISCORD, + webhook_module.DISCORD: { + name: webhook_module.DISCORD, payloadCreator: GetDiscordPayload, }, - webhook_model.DINGTALK: { - name: webhook_model.DINGTALK, + webhook_module.DINGTALK: { + name: webhook_module.DINGTALK, payloadCreator: GetDingtalkPayload, }, - webhook_model.TELEGRAM: { - name: webhook_model.TELEGRAM, + webhook_module.TELEGRAM: { + name: webhook_module.TELEGRAM, payloadCreator: GetTelegramPayload, }, - webhook_model.MSTEAMS: { - name: webhook_model.MSTEAMS, + webhook_module.MSTEAMS: { + name: webhook_module.MSTEAMS, payloadCreator: GetMSTeamsPayload, }, - webhook_model.FEISHU: { - name: webhook_model.FEISHU, + webhook_module.FEISHU: { + name: webhook_module.FEISHU, payloadCreator: GetFeishuPayload, }, - webhook_model.MATRIX: { - name: webhook_model.MATRIX, + webhook_module.MATRIX: { + name: webhook_module.MATRIX, payloadCreator: GetMatrixPayload, }, - webhook_model.WECHATWORK: { - name: webhook_model.WECHATWORK, + webhook_module.WECHATWORK: { + name: webhook_module.WECHATWORK, payloadCreator: GetWechatworkPayload, }, - webhook_model.PACKAGIST: { - name: webhook_model.PACKAGIST, + webhook_module.PACKAGIST: { + name: webhook_module.PACKAGIST, payloadCreator: GetPackagistPayload, }, } -// RegisterWebhook registers a webhook -func RegisterWebhook(name string, webhook *webhook) { - webhooks[name] = webhook -} - // IsValidHookTaskType returns true if a webhook registered func IsValidHookTaskType(name string) bool { - if name == webhook_model.GITEA || name == webhook_model.GOGS { + if name == webhook_module.GITEA || name == webhook_module.GOGS { return true } _, ok := webhooks[name] @@ -157,7 +154,7 @@ func checkBranch(w *webhook_model.Webhook, branch string) bool { } // PrepareWebhook creates a hook task and enqueues it for processing -func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook_model.HookEventType, p api.Payloader) error { +func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook_module.HookEventType, p api.Payloader) error { // Skip sending if webhooks are disabled. if setting.DisableWebhooks { return nil @@ -176,7 +173,7 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). // Integration webhooks (e.g. drone) still receive the required data. if pushEvent, ok := p.(*api.PushPayload); ok && - w.Type != webhook_model.GITEA && w.Type != webhook_model.GOGS && + w.Type != webhook_module.GITEA && w.Type != webhook_module.GOGS && len(pushEvent.Commits) == 0 { return nil } @@ -215,7 +212,7 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook } // PrepareWebhooks adds new webhooks to task queue for given payload. -func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_model.HookEventType, p api.Payloader) error { +func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_module.HookEventType, p api.Payloader) error { owner := source.Owner var ws []*webhook_model.Webhook diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index a77d871dd7f6b..71dbbff27daa6 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -4,10 +4,10 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -135,7 +135,7 @@ func (f *WechatworkPayload) PullRequest(p *api.PullRequestPayload) (api.Payloade } // Review implements PayloadConvertor Review method -func (f *WechatworkPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { +func (f *WechatworkPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) { var text, title string switch p.Action { case api.HookIssueReviewed: @@ -180,6 +180,6 @@ func (f *WechatworkPayload) Release(p *api.ReleasePayload) (api.Payloader, error } // GetWechatworkPayload GetWechatworkPayload converts a ding talk webhook into a WechatworkPayload -func GetWechatworkPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { +func GetWechatworkPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) { return convertPayloader(new(WechatworkPayload), p, event) } From 39b8b271ebdb240a39d32df21c4d978322b818fb Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 01:02:48 +0100 Subject: [PATCH 02/16] Run formatter --- models/migrations/v1_19/v233.go | 2 +- models/webhook/hooktask.go | 2 +- models/webhook/webhook.go | 2 +- modules/notification/webhook/error.go | 3 ++- routers/api/v1/org/hook.go | 4 ++-- routers/api/v1/repo/hook.go | 2 +- routers/api/v1/utils/hook.go | 2 +- routers/web/repo/webhook.go | 2 +- services/webhook/deliver.go | 2 +- services/webhook/dingtalk.go | 2 +- services/webhook/discord.go | 2 +- services/webhook/feishu.go | 2 +- services/webhook/general.go | 4 ++-- services/webhook/matrix.go | 2 +- services/webhook/msteams.go | 2 +- services/webhook/notifier.go | 4 ++-- services/webhook/packagist.go | 2 +- services/webhook/slack.go | 2 +- services/webhook/telegram.go | 2 +- services/webhook/webhook.go | 1 - services/webhook/wechatwork.go | 2 +- 21 files changed, 24 insertions(+), 24 deletions(-) diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go index 9ce2f64fd599e..6de182bfbb260 100644 --- a/models/migrations/v1_19/v233.go +++ b/models/migrations/v1_19/v233.go @@ -4,10 +4,10 @@ package v1_19 //nolint import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index de08b654e5926..7fd267f51eff9 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -4,13 +4,13 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 0c710b99905e4..b872b333bd38b 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -5,7 +5,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "fmt" "strings" @@ -13,6 +12,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" diff --git a/modules/notification/webhook/error.go b/modules/notification/webhook/error.go index a9a0267ae8c81..d95a468b0f1c9 100644 --- a/modules/notification/webhook/error.go +++ b/modules/notification/webhook/error.go @@ -4,8 +4,9 @@ package webhook import ( - "code.gitea.io/gitea/modules/util" "fmt" + + "code.gitea.io/gitea/modules/util" ) // ErrWebhookNotExist represents a "WebhookNotExist" kind of error. diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index e5a91ab6a571c..efeb27609283c 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -4,15 +4,15 @@ package org import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" - webhook_service "code.gitea.io/gitea/services/webhook" "net/http" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" + webhook_service "code.gitea.io/gitea/services/webhook" ) // ListHooks list an organziation's webhooks diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 7beec0424eba2..4590e5635bdbd 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -5,7 +5,6 @@ package repo import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "net/http" "code.gitea.io/gitea/models/perm" @@ -13,6 +12,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index ba8a9332eecab..39faa70c2a5aa 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -4,7 +4,6 @@ package utils import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/http" "strings" @@ -12,6 +11,7 @@ import ( "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" webhook_service "code.gitea.io/gitea/services/webhook" diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index ca091d10694c8..06798be49001e 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -5,7 +5,6 @@ package repo import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "net/http" @@ -21,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 27329e039a1ba..e6d34e5d8b5b4 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "crypto/hmac" "crypto/sha1" @@ -23,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/hostmatcher" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/queue" diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index bc0a6f2db7f26..31030979b941d 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -4,13 +4,13 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/url" "strings" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 7a06fe66bfa56..aafe527fa51c2 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "net/url" @@ -15,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 21b838a8e9c1f..2cf3e4e8b3ddc 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -4,12 +4,12 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" ) diff --git a/services/webhook/general.go b/services/webhook/general.go index 47a53f6b1f992..e3a3120558d2d 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -4,13 +4,13 @@ package webhook import ( - webhook_model "code.gitea.io/gitea/models/webhook" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "html" "net/url" "strings" + webhook_model "code.gitea.io/gitea/models/webhook" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 291cb12ed97bc..489818f928c8f 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "crypto/sha1" "encoding/hex" "errors" @@ -18,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index fcec38ed6ee04..cf09ec2da315d 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -4,13 +4,13 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "net/url" "strings" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" ) diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index fe0833aa09b95..ed19cc6c749ff 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -4,8 +4,6 @@ package webhook import ( - "code.gitea.io/gitea/modules/notification" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" issues_model "code.gitea.io/gitea/models/issues" @@ -18,7 +16,9 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification/base" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/packagist.go b/services/webhook/packagist.go index b03cc1224ff38..a6f7a1d6f25ce 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -4,12 +4,12 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" ) diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 9771fe281e2e0..1faa021d7cac3 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "errors" "fmt" "regexp" @@ -14,6 +13,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" ) diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index a1dc471f2a22b..0cba81484fe6f 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" @@ -13,6 +12,7 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" ) diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 55c843406e02b..5f182b420f155 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -11,7 +11,6 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" webhook_model "code.gitea.io/gitea/models/webhook" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 71dbbff27daa6..644c64c8aef8a 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -4,12 +4,12 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "fmt" "strings" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" ) From 3a2d64de467643217a1e15f3ee5fdb5ed714645b Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 01:18:17 +0100 Subject: [PATCH 03/16] Fix oversights --- services/webhook/notifier.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index ed19cc6c749ff..d28a72746c7b1 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -53,7 +53,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user return } - err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventType(webhook_module.HookEventPullRequestLabel), &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -61,7 +61,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user Sender: convert.ToUser(doer, nil), }) } else { - err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventType(webhook_module.HookEventIssueLabel), &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), From a669ba4444afbd32bf0060e298d3e502a7444aa1 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 01:28:14 +0100 Subject: [PATCH 04/16] Fix tests --- models/webhook/webhook_test.go | 19 ++++++++++--------- services/webhook/deliver_test.go | 5 +++-- services/webhook/dingtalk_test.go | 4 ++-- services/webhook/discord_test.go | 4 ++-- services/webhook/feishu_test.go | 4 ++-- services/webhook/matrix_test.go | 4 ++-- services/webhook/msteams_test.go | 4 ++-- services/webhook/packagist_test.go | 4 ++-- services/webhook/slack_test.go | 4 ++-- services/webhook/telegram_test.go | 4 ++-- services/webhook/webhook_test.go | 13 +++++++------ 11 files changed, 36 insertions(+), 33 deletions(-) diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 2bdafb61b698e..62de67a95de1b 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "testing" "time" @@ -46,11 +47,11 @@ func TestWebhook_History(t *testing.T) { func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}) - hookEvent := &HookEvent{ + hookEvent := &webhook_module.HookEvent{ PushOnly: true, SendEverything: false, ChooseEvents: false, - HookEvents: HookEvents{ + HookEvents: webhook_module.HookEvents{ Create: false, Push: true, PullRequest: false, @@ -59,7 +60,7 @@ func TestWebhook_UpdateEvent(t *testing.T) { webhook.HookEvent = hookEvent assert.NoError(t, webhook.UpdateEvent()) assert.NotEmpty(t, webhook.Events) - actualHookEvent := &HookEvent{} + actualHookEvent := &webhook_module.HookEvent{} assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent)) assert.Equal(t, *hookEvent, *actualHookEvent) } @@ -74,13 +75,13 @@ func TestWebhook_EventsArray(t *testing.T) { "package", }, (&Webhook{ - HookEvent: &HookEvent{SendEverything: true}, + HookEvent: &webhook_module.HookEvent{SendEverything: true}, }).EventsArray(), ) assert.Equal(t, []string{"push"}, (&Webhook{ - HookEvent: &HookEvent{PushOnly: true}, + HookEvent: &webhook_module.HookEvent{PushOnly: true}, }).EventsArray(), ) } @@ -105,7 +106,7 @@ func TestGetWebhookByRepoID(t *testing.T) { _, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, IsErrWebhookNotExist(err)) + assert.True(t, webhook_module.IsErrWebhookNotExist(err)) } func TestGetWebhookByOrgID(t *testing.T) { @@ -116,7 +117,7 @@ func TestGetWebhookByOrgID(t *testing.T) { _, err = GetWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, IsErrWebhookNotExist(err)) + assert.True(t, webhook_module.IsErrWebhookNotExist(err)) } func TestGetActiveWebhooksByRepoID(t *testing.T) { @@ -177,7 +178,7 @@ func TestDeleteWebhookByRepoID(t *testing.T) { err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, IsErrWebhookNotExist(err)) + assert.True(t, webhook_module.IsErrWebhookNotExist(err)) } func TestDeleteWebhookByOrgID(t *testing.T) { @@ -188,7 +189,7 @@ func TestDeleteWebhookByOrgID(t *testing.T) { err := DeleteWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, IsErrWebhookNotExist(err)) + assert.True(t, webhook_module.IsErrWebhookNotExist(err)) } func TestHookTasks(t *testing.T) { diff --git a/services/webhook/deliver_test.go b/services/webhook/deliver_test.go index e7a042f4d2de2..38146e59d913d 100644 --- a/services/webhook/deliver_test.go +++ b/services/webhook/deliver_test.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "net/http" "net/http/httptest" @@ -62,14 +63,14 @@ func TestWebhookDeliverAuthorizationHeader(t *testing.T) { URL: s.URL + "/webhook", ContentType: webhook_model.ContentTypeJSON, IsActive: true, - Type: webhook_model.GITEA, + Type: webhook_module.GITEA, } err := hook.SetHeaderAuthorization("Bearer s3cr3t-t0ken") assert.NoError(t, err) assert.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, hook)) db.GetEngine(db.DefaultContext).NoAutoTime().DB().Logger.ShowSQL(true) - hookTask := &webhook_model.HookTask{HookID: hook.ID, EventType: webhook_model.HookEventPush, Payloader: &api.PushPayload{}} + hookTask := &webhook_model.HookTask{HookID: hook.ID, EventType: webhook_module.HookEventPush, Payloader: &api.PushPayload{}} hookTask, err = webhook_model.CreateHookTask(db.DefaultContext, hookTask) assert.NoError(t, err) diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index 89b5f9d219dcd..3afe0ebc630a0 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -4,10 +4,10 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "net/url" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -162,7 +162,7 @@ func TestDingTalkPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(DingtalkPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &DingtalkPayload{}, pl) diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index df3cdc15bddaa..b8f917f93c285 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -179,7 +179,7 @@ func TestDiscordPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(DiscordPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &DiscordPayload{}, pl) diff --git a/services/webhook/feishu_test.go b/services/webhook/feishu_test.go index df44fd1f724ab..abb4512978035 100644 --- a/services/webhook/feishu_test.go +++ b/services/webhook/feishu_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -124,7 +124,7 @@ func TestFeishuPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(FeishuPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &FeishuPayload{}, pl) diff --git a/services/webhook/matrix_test.go b/services/webhook/matrix_test.go index 754234eccf96f..574d4d3254e4e 100644 --- a/services/webhook/matrix_test.go +++ b/services/webhook/matrix_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -133,7 +133,7 @@ func TestMatrixPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(MatrixPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &MatrixPayload{}, pl) diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index 8daf99f86700b..2956fa3e5edf2 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -277,7 +277,7 @@ func TestMSTeamsPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(MSTeamsPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &MSTeamsPayload{}, pl) diff --git a/services/webhook/packagist_test.go b/services/webhook/packagist_test.go index 4941ae3f01ca9..8695e5fb2bf20 100644 --- a/services/webhook/packagist_test.go +++ b/services/webhook/packagist_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -101,7 +101,7 @@ func TestPackagistPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(PackagistPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.Nil(t, pl) }) diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index db97b351c5758..fec0a95abb988 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -124,7 +124,7 @@ func TestSlackPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(SlackPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &SlackPayload{}, pl) diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index b092f7e732069..03fd8e0929cc0 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -4,9 +4,9 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" - webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -124,7 +124,7 @@ func TestTelegramPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(TelegramPayload) - pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &TelegramPayload{}, pl) diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index a1f197d80935c..b144c3e367f6f 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -4,6 +4,7 @@ package webhook import ( + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" "code.gitea.io/gitea/models/db" @@ -32,12 +33,12 @@ func TestPrepareWebhooks(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) hookTasks := []*webhook_model.HookTask{ - {HookID: 1, EventType: webhook_model.HookEventPush}, + {HookID: 1, EventType: webhook_module.HookEventPush}, } for _, hookTask := range hookTasks { unittest.AssertNotExistsBean(t, hookTask) } - assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_model.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) + assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { unittest.AssertExistsAndLoadBean(t, hookTask) } @@ -48,13 +49,13 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) hookTasks := []*webhook_model.HookTask{ - {HookID: 4, EventType: webhook_model.HookEventPush}, + {HookID: 4, EventType: webhook_module.HookEventPush}, } for _, hookTask := range hookTasks { unittest.AssertNotExistsBean(t, hookTask) } // this test also ensures that * doesn't handle / in any special way (like shell would) - assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) + assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { unittest.AssertExistsAndLoadBean(t, hookTask) } @@ -65,12 +66,12 @@ func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) hookTasks := []*webhook_model.HookTask{ - {HookID: 4, EventType: webhook_model.HookEventPush}, + {HookID: 4, EventType: webhook_module.HookEventPush}, } for _, hookTask := range hookTasks { unittest.AssertNotExistsBean(t, hookTask) } - assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) + assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) for _, hookTask := range hookTasks { unittest.AssertNotExistsBean(t, hookTask) From 16cc5a5ad4e7214f53e3b010d19d858db0527db5 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 01:41:49 +0100 Subject: [PATCH 05/16] =?UTF-8?q?=E2=80=A6=20and=20the=20remaining=20test?= =?UTF-8?q?=20as=20well=20as=20the=20formatter=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/migrations/v1_19/v233_test.go | 8 ++++---- models/webhook/webhook_test.go | 2 +- services/webhook/deliver_test.go | 2 +- services/webhook/dingtalk_test.go | 2 +- services/webhook/discord_test.go | 2 +- services/webhook/feishu_test.go | 2 +- services/webhook/matrix_test.go | 2 +- services/webhook/msteams_test.go | 2 +- services/webhook/packagist_test.go | 2 +- services/webhook/slack_test.go | 2 +- services/webhook/telegram_test.go | 2 +- services/webhook/webhook_test.go | 2 +- 12 files changed, 15 insertions(+), 15 deletions(-) diff --git a/models/migrations/v1_19/v233_test.go b/models/migrations/v1_19/v233_test.go index 9902b7e4ae371..dc6df17862413 100644 --- a/models/migrations/v1_19/v233_test.go +++ b/models/migrations/v1_19/v233_test.go @@ -7,8 +7,8 @@ import ( "testing" "code.gitea.io/gitea/models/migrations/base" - "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" @@ -18,9 +18,9 @@ import ( func Test_AddHeaderAuthorizationEncryptedColWebhook(t *testing.T) { // Create Webhook table type Webhook struct { - ID int64 `xorm:"pk autoincr"` - Type webhook.HookType `xorm:"VARCHAR(16) 'type'"` - Meta string `xorm:"TEXT"` // store hook-specific attributes + ID int64 `xorm:"pk autoincr"` + Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"` + Meta string `xorm:"TEXT"` // store hook-specific attributes // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() HeaderAuthorizationEncrypted string `xorm:"TEXT"` diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 62de67a95de1b..4f477f3e9ceb0 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "testing" "time" @@ -12,6 +11,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" diff --git a/services/webhook/deliver_test.go b/services/webhook/deliver_test.go index 38146e59d913d..97f40b64ce2ba 100644 --- a/services/webhook/deliver_test.go +++ b/services/webhook/deliver_test.go @@ -4,7 +4,6 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "context" "net/http" "net/http/httptest" @@ -15,6 +14,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" webhook_model "code.gitea.io/gitea/models/webhook" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index 3afe0ebc630a0..f96e59121c7cd 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -4,10 +4,10 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "net/url" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index b8f917f93c285..c7b5119a80581 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/services/webhook/feishu_test.go b/services/webhook/feishu_test.go index abb4512978035..6c4bc18a4f049 100644 --- a/services/webhook/feishu_test.go +++ b/services/webhook/feishu_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/matrix_test.go b/services/webhook/matrix_test.go index 574d4d3254e4e..14d12bbbe883c 100644 --- a/services/webhook/matrix_test.go +++ b/services/webhook/matrix_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index 2956fa3e5edf2..cf9319aff82e9 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/packagist_test.go b/services/webhook/packagist_test.go index 8695e5fb2bf20..e9a0e9bab16f9 100644 --- a/services/webhook/packagist_test.go +++ b/services/webhook/packagist_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index fec0a95abb988..de110ba3afb7f 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index 03fd8e0929cc0..8f863c5698033 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -4,9 +4,9 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index b144c3e367f6f..42642ec3f84e7 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -4,13 +4,13 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "testing" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" webhook_model "code.gitea.io/gitea/models/webhook" + webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" From 44e1ffeab7b27a69582b7324bef2feac30add4fb Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 10:40:40 +0100 Subject: [PATCH 06/16] mv modules/notification/webhook -> modules/webhook --- models/migrations/v1_19/v233.go | 2 +- models/migrations/v1_19/v233_test.go | 2 +- models/webhook/hooktask.go | 2 +- models/webhook/webhook.go | 2 +- models/webhook/webhook_test.go | 2 +- modules/{notification => }/webhook/error.go | 0 modules/{notification => }/webhook/structs.go | 0 modules/{notification => }/webhook/type.go | 0 routers/api/v1/org/hook.go | 2 +- routers/api/v1/repo/hook.go | 2 +- routers/api/v1/utils/hook.go | 2 +- routers/web/repo/webhook.go | 2 +- services/webhook/deliver.go | 2 +- services/webhook/deliver_test.go | 2 +- services/webhook/dingtalk.go | 2 +- services/webhook/discord.go | 2 +- services/webhook/discord_test.go | 2 +- services/webhook/feishu.go | 2 +- services/webhook/feishu_test.go | 2 +- services/webhook/general.go | 2 +- services/webhook/matrix.go | 2 +- services/webhook/matrix_test.go | 2 +- services/webhook/msteams.go | 2 +- services/webhook/notifier.go | 2 +- services/webhook/packagist.go | 2 +- services/webhook/packagist_test.go | 2 +- services/webhook/payloader.go | 2 +- services/webhook/slack.go | 2 +- services/webhook/slack_test.go | 2 +- services/webhook/telegram.go | 2 +- services/webhook/telegram_test.go | 2 +- services/webhook/webhook.go | 2 +- services/webhook/webhook_test.go | 2 +- services/webhook/wechatwork.go | 2 +- 34 files changed, 31 insertions(+), 31 deletions(-) rename modules/{notification => }/webhook/error.go (100%) rename modules/{notification => }/webhook/structs.go (100%) rename modules/{notification => }/webhook/type.go (100%) diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go index 6de182bfbb260..f2e73da8e70f0 100644 --- a/models/migrations/v1_19/v233.go +++ b/models/migrations/v1_19/v233.go @@ -7,7 +7,7 @@ import ( "fmt" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" + webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/models/migrations/v1_19/v233_test.go b/models/migrations/v1_19/v233_test.go index dc6df17862413..83558da334e8d 100644 --- a/models/migrations/v1_19/v233_test.go +++ b/models/migrations/v1_19/v233_test.go @@ -8,9 +8,9 @@ import ( "code.gitea.io/gitea/models/migrations/base" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" ) diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 7fd267f51eff9..06d2442ce607b 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -10,9 +10,9 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" gouuid "github.com/google/uuid" ) diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index b872b333bd38b..f9064c4dd190e 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -12,11 +12,11 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" "xorm.io/builder" ) diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 4f477f3e9ceb0..a56848b9e0337 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -11,9 +11,9 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" ) diff --git a/modules/notification/webhook/error.go b/modules/webhook/error.go similarity index 100% rename from modules/notification/webhook/error.go rename to modules/webhook/error.go diff --git a/modules/notification/webhook/structs.go b/modules/webhook/structs.go similarity index 100% rename from modules/notification/webhook/structs.go rename to modules/webhook/structs.go diff --git a/modules/notification/webhook/type.go b/modules/webhook/type.go similarity index 100% rename from modules/notification/webhook/type.go rename to modules/webhook/type.go diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index efeb27609283c..846bf7f1a8317 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -8,9 +8,9 @@ import ( "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" + webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/routers/api/v1/utils" webhook_service "code.gitea.io/gitea/services/webhook" ) diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 4590e5635bdbd..53dc2bf14edb7 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -12,10 +12,10 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" + webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/routers/api/v1/utils" webhook_service "code.gitea.io/gitea/services/webhook" ) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 39faa70c2a5aa..3e9aa30bfdd70 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -11,9 +11,9 @@ import ( "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" webhook_service "code.gitea.io/gitea/services/webhook" ) diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 06798be49001e..8a1dda4d5e834 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -20,11 +20,11 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" + webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/forms" webhook_service "code.gitea.io/gitea/services/webhook" ) diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index e6d34e5d8b5b4..d4be5b26b62ea 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -22,11 +22,11 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/hostmatcher" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/gobwas/glob" ) diff --git a/services/webhook/deliver_test.go b/services/webhook/deliver_test.go index 97f40b64ce2ba..ee63975ad37d1 100644 --- a/services/webhook/deliver_test.go +++ b/services/webhook/deliver_test.go @@ -14,9 +14,9 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" webhook_model "code.gitea.io/gitea/models/webhook" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" ) diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 31030979b941d..99ee6e4d192b8 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -10,9 +10,9 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" dingtalk "gitea.com/lunny/dingtalk_webhook" ) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index aafe527fa51c2..ed44fef404801 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -14,10 +14,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index c7b5119a80581..624d53446a981 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -6,9 +6,9 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 2cf3e4e8b3ddc..4fbf8f76a90ae 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -9,8 +9,8 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( diff --git a/services/webhook/feishu_test.go b/services/webhook/feishu_test.go index 6c4bc18a4f049..84549c1fa5764 100644 --- a/services/webhook/feishu_test.go +++ b/services/webhook/feishu_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/general.go b/services/webhook/general.go index e3a3120558d2d..fafdbb4b6c5ad 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -10,10 +10,10 @@ import ( "strings" webhook_model "code.gitea.io/gitea/models/webhook" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type linkFormatter = func(string, string) string diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 489818f928c8f..cf2b503cdc2df 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -17,10 +17,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" ) const matrixPayloadSizeLimit = 1024 * 64 diff --git a/services/webhook/matrix_test.go b/services/webhook/matrix_test.go index 14d12bbbe883c..8c710942280fa 100644 --- a/services/webhook/matrix_test.go +++ b/services/webhook/matrix_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index cf09ec2da315d..03d92821b9eb3 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -10,9 +10,9 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index d28a72746c7b1..5343b4a857c9e 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -18,10 +18,10 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification/base" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) func init() { diff --git a/services/webhook/packagist.go b/services/webhook/packagist.go index a6f7a1d6f25ce..e47e7d32850f4 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -9,8 +9,8 @@ import ( webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( diff --git a/services/webhook/packagist_test.go b/services/webhook/packagist_test.go index e9a0e9bab16f9..932b56fe9b9a1 100644 --- a/services/webhook/packagist_test.go +++ b/services/webhook/packagist_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/payloader.go b/services/webhook/payloader.go index 8a967ad41b96a..9eff25628bce8 100644 --- a/services/webhook/payloader.go +++ b/services/webhook/payloader.go @@ -4,8 +4,8 @@ package webhook import ( - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) // PayloadConvertor defines the interface to convert system webhook payload to external payload diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 1faa021d7cac3..c2d4a7731e0a4 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -13,9 +13,9 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) // SlackMeta contains the slack metadata diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index de110ba3afb7f..d9828f374f0dd 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 0cba81484fe6f..e5c731fc9291c 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -12,8 +12,8 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index 8f863c5698033..b42b0ccda8847 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 5f182b420f155..afd8e3c105cb1 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -14,11 +14,11 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/gobwas/glob" ) diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 42642ec3f84e7..338b94360bbff 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -10,8 +10,8 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" webhook_model "code.gitea.io/gitea/models/webhook" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" ) diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 644c64c8aef8a..dc8810c4a6ae2 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -9,8 +9,8 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type ( From bfa2c353d8a160aba3a6159a807d22658f7d58ed Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 10:49:38 +0100 Subject: [PATCH 07/16] Change import also in missed tests --- services/webhook/dingtalk_test.go | 2 +- services/webhook/msteams_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index f96e59121c7cd..a4c736ac8bb64 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -7,7 +7,7 @@ import ( "net/url" "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" + webhook_module "code.gitea.io/gitea/modules/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index cf9319aff82e9..ea0416f54e934 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -6,7 +6,7 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/notification/webhook" + webhook_module "code.gitea.io/gitea/modules/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" From e43911d3e0e6ee1f5510ed820aab27489d8ee887 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 10:53:05 +0100 Subject: [PATCH 08/16] Format again --- models/migrations/v1_19/v233.go | 2 +- services/webhook/dingtalk_test.go | 2 +- services/webhook/msteams_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go index f2e73da8e70f0..22a1cc0342530 100644 --- a/models/migrations/v1_19/v233.go +++ b/models/migrations/v1_19/v233.go @@ -7,10 +7,10 @@ import ( "fmt" "code.gitea.io/gitea/modules/json" - webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "xorm.io/builder" "xorm.io/xorm" diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index a4c736ac8bb64..e3122d2f36fca 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -7,8 +7,8 @@ import ( "net/url" "testing" - webhook_module "code.gitea.io/gitea/modules/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index ea0416f54e934..4f378713cc7c6 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -6,8 +6,8 @@ package webhook import ( "testing" - webhook_module "code.gitea.io/gitea/modules/webhook" api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" From a3401d59d256610c1efe8747c4f8c369c613ec99 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 28 Dec 2022 23:40:05 +0100 Subject: [PATCH 09/16] Remove tags file from gitignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index cf48f15f5c1ad..1ce2a87611e81 100644 --- a/.gitignore +++ b/.gitignore @@ -112,6 +112,3 @@ prime/ # Manpage /man - -# Tags file, simply because it's too large to commit (200MB with node modules) -tags From 529b5325b02e12135aed5a4078e5faade6187157 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 29 Dec 2022 12:14:48 +0100 Subject: [PATCH 10/16] Move webhook errors back to models --- models/webhook/hooktask.go | 4 +-- models/webhook/webhook.go | 48 +++++++++++++++++++++++++++++--- models/webhook/webhook_test.go | 8 +++--- modules/webhook/error.go | 50 ---------------------------------- routers/api/v1/org/hook.go | 3 +- routers/api/v1/repo/hook.go | 2 +- routers/api/v1/utils/hook.go | 4 +-- routers/web/repo/webhook.go | 4 +-- 8 files changed, 56 insertions(+), 67 deletions(-) delete mode 100644 modules/webhook/error.go diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 06d2442ce607b..ccce1447bcf8f 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -195,7 +195,7 @@ func GetHookTaskByID(ctx context.Context, id int64) (*HookTask, error) { return nil, err } if !has { - return nil, webhook_module.ErrHookTaskNotExist{ + return nil, ErrHookTaskNotExist{ TaskID: id, } } @@ -218,7 +218,7 @@ func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask, if err != nil { return nil, err } else if !has { - return nil, webhook_module.ErrHookTaskNotExist{ + return nil, ErrHookTaskNotExist{ HookID: hookID, UUID: uuid, } diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index f9064c4dd190e..c27c757604fca 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -21,6 +21,46 @@ import ( "xorm.io/builder" ) +// ErrWebhookNotExist represents a "WebhookNotExist" kind of error. +type ErrWebhookNotExist struct { + ID int64 +} + +// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. +func IsErrWebhookNotExist(err error) bool { + _, ok := err.(ErrWebhookNotExist) + return ok +} + +func (err ErrWebhookNotExist) Error() string { + return fmt.Sprintf("webhook does not exist [id: %d]", err.ID) +} + +func (err ErrWebhookNotExist) Unwrap() error { + return util.ErrNotExist +} + +// ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. +type ErrHookTaskNotExist struct { + TaskID int64 + HookID int64 + UUID string +} + +// IsErrHookTaskNotExist checks if an error is a ErrHookTaskNotExist. +func IsErrHookTaskNotExist(err error) bool { + _, ok := err.(ErrHookTaskNotExist) + return ok +} + +func (err ErrHookTaskNotExist) Error() string { + return fmt.Sprintf("hook task does not exist [task: %d, hook: %d, uuid: %s]", err.TaskID, err.HookID, err.UUID) +} + +func (err ErrHookTaskNotExist) Unwrap() error { + return util.ErrNotExist +} + // HookContentType is the content type of a web hook type HookContentType int @@ -359,7 +399,7 @@ func getWebhook(bean *Webhook) (*Webhook, error) { if err != nil { return nil, err } else if !has { - return nil, webhook_module.ErrWebhookNotExist{ID: bean.ID} + return nil, ErrWebhookNotExist{ID: bean.ID} } return bean, nil } @@ -447,7 +487,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) { if err != nil { return nil, err } else if !has { - return nil, webhook_module.ErrWebhookNotExist{ID: id} + return nil, ErrWebhookNotExist{ID: id} } return webhook, nil } @@ -489,7 +529,7 @@ func deleteWebhook(bean *Webhook) (err error) { if count, err := db.DeleteByBean(ctx, bean); err != nil { return err } else if count == 0 { - return webhook_module.ErrWebhookNotExist{ID: bean.ID} + return ErrWebhookNotExist{ID: bean.ID} } else if _, err = db.DeleteByBean(ctx, &HookTask{HookID: bean.ID}); err != nil { return err } @@ -527,7 +567,7 @@ func DeleteDefaultSystemWebhook(id int64) error { if err != nil { return err } else if count == 0 { - return webhook_module.ErrWebhookNotExist{ID: id} + return ErrWebhookNotExist{ID: id} } if _, err := db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil { diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index a56848b9e0337..c368fc620e2f2 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -106,7 +106,7 @@ func TestGetWebhookByRepoID(t *testing.T) { _, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, webhook_module.IsErrWebhookNotExist(err)) + assert.True(t, IsErrWebhookNotExist(err)) } func TestGetWebhookByOrgID(t *testing.T) { @@ -117,7 +117,7 @@ func TestGetWebhookByOrgID(t *testing.T) { _, err = GetWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, webhook_module.IsErrWebhookNotExist(err)) + assert.True(t, IsErrWebhookNotExist(err)) } func TestGetActiveWebhooksByRepoID(t *testing.T) { @@ -178,7 +178,7 @@ func TestDeleteWebhookByRepoID(t *testing.T) { err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, webhook_module.IsErrWebhookNotExist(err)) + assert.True(t, IsErrWebhookNotExist(err)) } func TestDeleteWebhookByOrgID(t *testing.T) { @@ -189,7 +189,7 @@ func TestDeleteWebhookByOrgID(t *testing.T) { err := DeleteWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) - assert.True(t, webhook_module.IsErrWebhookNotExist(err)) + assert.True(t, IsErrWebhookNotExist(err)) } func TestHookTasks(t *testing.T) { diff --git a/modules/webhook/error.go b/modules/webhook/error.go deleted file mode 100644 index d95a468b0f1c9..0000000000000 --- a/modules/webhook/error.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2022 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package webhook - -import ( - "fmt" - - "code.gitea.io/gitea/modules/util" -) - -// ErrWebhookNotExist represents a "WebhookNotExist" kind of error. -type ErrWebhookNotExist struct { - ID int64 -} - -// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. -func IsErrWebhookNotExist(err error) bool { - _, ok := err.(ErrWebhookNotExist) - return ok -} - -func (err ErrWebhookNotExist) Error() string { - return fmt.Sprintf("webhook does not exist [id: %d]", err.ID) -} - -func (err ErrWebhookNotExist) Unwrap() error { - return util.ErrNotExist -} - -// ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. -type ErrHookTaskNotExist struct { - TaskID int64 - HookID int64 - UUID string -} - -// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist. -func IsErrHookTaskNotExist(err error) bool { - _, ok := err.(ErrHookTaskNotExist) - return ok -} - -func (err ErrHookTaskNotExist) Error() string { - return fmt.Sprintf("hook task does not exist [task: %d, hook: %d, uuid: %s]", err.TaskID, err.HookID, err.UUID) -} - -func (err ErrHookTaskNotExist) Unwrap() error { - return util.ErrNotExist -} diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index 846bf7f1a8317..cabe27e0e313c 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -10,7 +10,6 @@ import ( "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" - webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/routers/api/v1/utils" webhook_service "code.gitea.io/gitea/services/webhook" ) @@ -202,7 +201,7 @@ func DeleteHook(ctx *context.APIContext) { org := ctx.Org.Organization hookID := ctx.ParamsInt64(":id") if err := webhook.DeleteWebhookByOrgID(org.ID, hookID); err != nil { - if webhook_module.IsErrWebhookNotExist(err) { + 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 53dc2bf14edb7..535040375f679 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -297,7 +297,7 @@ func DeleteHook(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { - if webhook_module.IsErrWebhookNotExist(err) { + if webhook.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 3e9aa30bfdd70..fc202f51cfea3 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -22,7 +22,7 @@ import ( func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) { w, err := webhook.GetWebhookByOrgID(orgID, hookID) if err != nil { - if webhook_module.IsErrWebhookNotExist(err) { + if webhook.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err) @@ -37,7 +37,7 @@ func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) { w, err := webhook.GetWebhookByRepoID(repoID, hookID) if err != nil { - if webhook_module.IsErrWebhookNotExist(err) { + if webhook.IsErrWebhookNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err) diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 8a1dda4d5e834..c1fefdfe3e57e 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -594,7 +594,7 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) { w, err = webhook.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id")) } if err != nil || w == nil { - if webhook_module.IsErrWebhookNotExist(err) { + if webhook.IsErrWebhookNotExist(err) { ctx.NotFound("GetWebhookByID", nil) } else { ctx.ServerError("GetWebhookByID", err) @@ -708,7 +708,7 @@ func ReplayWebhook(ctx *context.Context) { } if err := webhook_service.ReplayHookTask(ctx, w, hookTaskUUID); err != nil { - if webhook_module.IsErrHookTaskNotExist(err) { + if webhook.IsErrHookTaskNotExist(err) { ctx.NotFound("ReplayHookTask", nil) } else { ctx.ServerError("ReplayHookTask", err) From ae85520ed91313119b4923921ceb438652e5dcbf Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 29 Dec 2022 14:22:16 +0100 Subject: [PATCH 11/16] =?UTF-8?q?Run=20formatter=E2=80=A6=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/api/v1/utils/hook.go | 2 +- services/convert/convert.go | 6 +++--- services/webhooknotifier/notifier.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 52d799a5fd6ad..69ec9f2d3e477 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -13,8 +13,8 @@ import ( "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" - "code.gitea.io/gitea/services/convert" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/convert" webhook_service "code.gitea.io/gitea/services/webhook" ) diff --git a/services/convert/convert.go b/services/convert/convert.go index 5e41ca617d82d..eaa3c204301ff 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -5,9 +5,6 @@ package convert import ( - webhook_model "code.gitea.io/gitea/models/webhook" - webhook_module "code.gitea.io/gitea/modules/webhook" - "code.gitea.io/gitea/services/webhook" "context" "fmt" "strconv" @@ -25,11 +22,14 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/gitdiff" + "code.gitea.io/gitea/services/webhook" ) // ToEmail convert models.EmailAddress to api.Email diff --git a/services/webhooknotifier/notifier.go b/services/webhooknotifier/notifier.go index af1452920f4c0..cc6f7f7282332 100644 --- a/services/webhooknotifier/notifier.go +++ b/services/webhooknotifier/notifier.go @@ -4,7 +4,6 @@ package webhooknotifier import ( - "code.gitea.io/gitea/services/webhook" "context" "fmt" @@ -24,6 +23,7 @@ import ( api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/convert" + "code.gitea.io/gitea/services/webhook" ) func init() { From 40c7cdea5d0ce5ff53bff3b89f191056dc2402ab Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 29 Dec 2022 14:34:49 +0100 Subject: [PATCH 12/16] Move Webhook status constants also to modules --- models/webhook/webhook.go | 7 ------- modules/webhook/structs.go | 3 --- modules/webhook/type.go | 10 ++++++++++ services/webhook/deliver.go | 4 ++-- services/webhooknotifier/notifier.go | 2 -- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index c27c757604fca..c24404c42cf04 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -118,13 +118,6 @@ func IsValidHookContentType(name string) bool { return ok } -// Possible statuses of a web hook -const ( - HookStatusNone = iota - HookStatusSucceed - HookStatusFail -) - // Webhook represents a web hook object. type Webhook struct { ID int64 `xorm:"pk autoincr"` diff --git a/modules/webhook/structs.go b/modules/webhook/structs.go index d3bda7d18de41..96012de352002 100644 --- a/modules/webhook/structs.go +++ b/modules/webhook/structs.go @@ -36,6 +36,3 @@ type HookEvent struct { HookEvents `json:"events"` } - -// HookStatus is the status of a web hook -type HookStatus int diff --git a/modules/webhook/type.go b/modules/webhook/type.go index a0eeac3eb396c..db4ab17931e9c 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -83,3 +83,13 @@ const ( WECHATWORK HookType = "wechatwork" PACKAGIST HookType = "packagist" ) + +// HookStatus is the status of a web hook +type HookStatus int + +// Possible statuses of a web hook +const ( + HookStatusNone HookStatus = iota + HookStatusSucceed + HookStatusFail +) diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index d4be5b26b62ea..effbe45e56910 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -190,9 +190,9 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { // Update webhook last delivery status. if t.IsSucceed { - w.LastStatus = webhook_model.HookStatusSucceed + w.LastStatus = webhook_module.HookStatusSucceed } else { - w.LastStatus = webhook_model.HookStatusFail + w.LastStatus = webhook_module.HookStatusFail } if err = webhook_model.UpdateWebhookLastStatus(w); err != nil { log.Error("UpdateWebhookLastStatus: %v", err) diff --git a/services/webhooknotifier/notifier.go b/services/webhooknotifier/notifier.go index cc6f7f7282332..8baaccdc6ad10 100644 --- a/services/webhooknotifier/notifier.go +++ b/services/webhooknotifier/notifier.go @@ -5,7 +5,6 @@ package webhooknotifier import ( "context" - "fmt" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" @@ -28,7 +27,6 @@ import ( func init() { notification.RegisterNotifier(&webhookNotifier{}) - fmt.Println("Hi from init") } type webhookNotifier struct { From 0fc541b5e3038c17dedb5c718aae47414192ee21 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 29 Dec 2022 23:46:48 +0100 Subject: [PATCH 13/16] Fix tests --- tests/integration/pull_merge_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index e72d00ffb360b..a3ab469cda2d7 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -32,6 +32,9 @@ import ( repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" + // The tests are not started from main.go, and otherwise the webhooks are not initialized correctly + _ "code.gitea.io/gitea/services/webhooknotifier" + "github.com/stretchr/testify/assert" ) From 927b08f212f92e2d0bd0e04cc1af014b89cee6f7 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 29 Dec 2022 23:50:56 +0100 Subject: [PATCH 14/16] Remove comment as the linter complains about it --- tests/integration/pull_merge_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index a3ab469cda2d7..32f497fd273a2 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -32,7 +32,6 @@ import ( repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" - // The tests are not started from main.go, and otherwise the webhooks are not initialized correctly _ "code.gitea.io/gitea/services/webhooknotifier" "github.com/stretchr/testify/assert" From 996430896d4405986b6e0cc7eb76b02fd08c5fba Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 30 Dec 2022 18:21:20 +0100 Subject: [PATCH 15/16] Remove dependence of migration v233 on a custom type --- models/migrations/v1_19/v233.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go index 22a1cc0342530..ba4cd8e20b995 100644 --- a/models/migrations/v1_19/v233.go +++ b/models/migrations/v1_19/v233.go @@ -10,7 +10,6 @@ import ( "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - webhook_module "code.gitea.io/gitea/modules/webhook" "xorm.io/builder" "xorm.io/xorm" @@ -56,9 +55,9 @@ func batchProcess[T any](x *xorm.Engine, buf []T, query func(limit, start int) * func AddHeaderAuthorizationEncryptedColWebhook(x *xorm.Engine) error { // Add the column to the table type Webhook struct { - ID int64 `xorm:"pk autoincr"` - Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"` - Meta string `xorm:"TEXT"` // store hook-specific attributes + ID int64 `xorm:"pk autoincr"` + Type string `xorm:"VARCHAR(16) 'type'"` + Meta string `xorm:"TEXT"` // store hook-specific attributes // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() HeaderAuthorizationEncrypted string `xorm:"TEXT"` From db7db127819a02c83ec234cc6c6a387c9b477903 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 1 Jan 2023 11:22:48 +0100 Subject: [PATCH 16/16] Move ToHook to ../webhook to prevent an import cycle and a new package as per @lunny --- main.go | 3 - routers/api/v1/org/hook.go | 18 ++--- routers/api/v1/repo/hook.go | 4 +- routers/api/v1/utils/hook.go | 3 +- services/convert/convert.go | 35 -------- services/webhook/general.go | 35 ++++++++ .../{webhooknotifier => webhook}/notifier.go | 79 +++++++++---------- tests/integration/pull_merge_test.go | 2 - 8 files changed, 86 insertions(+), 93 deletions(-) rename services/{webhooknotifier => webhook}/notifier.go (83%) diff --git a/main.go b/main.go index 34a47d80483f7..070e9857d08c0 100644 --- a/main.go +++ b/main.go @@ -22,9 +22,6 @@ import ( _ "code.gitea.io/gitea/modules/markup/markdown" _ "code.gitea.io/gitea/modules/markup/orgmode" - // register webhook notifier - _ "code.gitea.io/gitea/services/webhooknotifier" - "github.com/urfave/cli" ) diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index ef08a08be0e40..4e435c9599a16 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -6,12 +6,12 @@ package org import ( "net/http" - "code.gitea.io/gitea/models/webhook" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" 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/convert" + webhook_service "code.gitea.io/gitea/services/webhook" ) // ListHooks list an organziation's webhooks @@ -39,18 +39,18 @@ func ListHooks(ctx *context.APIContext) { // "200": // "$ref": "#/responses/HookList" - opts := &webhook.ListWebhookOptions{ + opts := &webhook_model.ListWebhookOptions{ ListOptions: utils.GetListOptions(ctx), OrgID: ctx.Org.Organization.ID, } - count, err := webhook.CountWebhooksByOpts(opts) + count, err := webhook_model.CountWebhooksByOpts(opts) if err != nil { ctx.InternalServerError(err) return } - orgHooks, err := webhook.ListWebhooksByOpts(ctx, opts) + orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, opts) if err != nil { ctx.InternalServerError(err) return @@ -58,7 +58,7 @@ func ListHooks(ctx *context.APIContext) { hooks := make([]*api.Hook, len(orgHooks)) for i, hook := range orgHooks { - hooks[i], err = convert.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook) + hooks[i], err = webhook_service.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook) if err != nil { ctx.InternalServerError(err) return @@ -99,7 +99,7 @@ func GetHook(ctx *context.APIContext) { return } - apiHook, err := convert.ToHook(org.AsUser().HomeLink(), hook) + apiHook, err := webhook_service.ToHook(org.AsUser().HomeLink(), hook) if err != nil { ctx.InternalServerError(err) return @@ -200,8 +200,8 @@ func DeleteHook(ctx *context.APIContext) { org := ctx.Org.Organization hookID := ctx.ParamsInt64(":id") - if err := webhook.DeleteWebhookByOrgID(org.ID, hookID); err != nil { - if webhook.IsErrWebhookNotExist(err) { + if err := webhook_model.DeleteWebhookByOrgID(org.ID, hookID); err != nil { + if webhook_model.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 6c611d950227d..100a28d7f6621 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -69,7 +69,7 @@ func ListHooks(ctx *context.APIContext) { apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { - apiHooks[i], err = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) + apiHooks[i], err = webhook_service.ToHook(ctx.Repo.RepoLink, hooks[i]) if err != nil { ctx.InternalServerError(err) return @@ -116,7 +116,7 @@ func GetHook(ctx *context.APIContext) { if err != nil { return } - apiHook, err := convert.ToHook(repo.RepoLink, hook) + apiHook, err := webhook_service.ToHook(repo.RepoLink, hook) if err != nil { ctx.InternalServerError(err) return diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 69ec9f2d3e477..fc202f51cfea3 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -14,7 +14,6 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" - "code.gitea.io/gitea/services/convert" webhook_service "code.gitea.io/gitea/services/webhook" ) @@ -99,7 +98,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) { // toAPIHook converts the hook to its API representation. // If there is an error, write to `ctx` accordingly. Return (hook, ok) func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) { - apiHook, err := convert.ToHook(repoLink, hook) + apiHook, err := webhook_service.ToHook(repoLink, hook) if err != nil { ctx.Error(http.StatusInternalServerError, "ToHook", err) return nil, false diff --git a/services/convert/convert.go b/services/convert/convert.go index eaa3c204301ff..a8329f528584f 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -22,14 +22,11 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" - webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/gitdiff" - "code.gitea.io/gitea/services/webhook" ) // ToEmail convert models.EmailAddress to api.Email @@ -243,38 +240,6 @@ func ToGPGKeyEmail(email *user_model.EmailAddress) *api.GPGKeyEmail { } } -// ToHook convert models.Webhook to api.Hook -func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) { - config := map[string]string{ - "url": w.URL, - "content_type": w.ContentType.Name(), - } - if w.Type == webhook_module.SLACK { - s := webhook.GetSlackHook(w) - config["channel"] = s.Channel - config["username"] = s.Username - config["icon_url"] = s.IconURL - config["color"] = s.Color - } - - authorizationHeader, err := w.HeaderAuthorization() - if err != nil { - return nil, err - } - - return &api.Hook{ - ID: w.ID, - Type: w.Type, - URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID), - Active: w.IsActive, - Config: config, - Events: w.EventsArray(), - AuthorizationHeader: authorizationHeader, - Updated: w.UpdatedUnix.AsTime(), - Created: w.CreatedUnix.AsTime(), - }, nil -} - // ToGitHook convert git.Hook to api.GitHook func ToGitHook(h *git.Hook) *api.GitHook { return &api.GitHook{ diff --git a/services/webhook/general.go b/services/webhook/general.go index bec752cffe581..1f7d204d1f74c 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -9,9 +9,11 @@ import ( "net/url" "strings" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + webhook_module "code.gitea.io/gitea/modules/webhook" ) type linkFormatter = func(string, string) string @@ -223,3 +225,36 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo return text, issueTitle, color } + +// ToHook convert models.Webhook to api.Hook +// This function is not part of the convert package to prevent an import cycle +func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) { + config := map[string]string{ + "url": w.URL, + "content_type": w.ContentType.Name(), + } + if w.Type == webhook_module.SLACK { + s := GetSlackHook(w) + config["channel"] = s.Channel + config["username"] = s.Username + config["icon_url"] = s.IconURL + config["color"] = s.Color + } + + authorizationHeader, err := w.HeaderAuthorization() + if err != nil { + return nil, err + } + + return &api.Hook{ + ID: w.ID, + Type: w.Type, + URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID), + Active: w.IsActive, + Config: config, + Events: w.EventsArray(), + AuthorizationHeader: authorizationHeader, + Updated: w.UpdatedUnix.AsTime(), + Created: w.CreatedUnix.AsTime(), + }, nil +} diff --git a/services/webhooknotifier/notifier.go b/services/webhook/notifier.go similarity index 83% rename from services/webhooknotifier/notifier.go rename to services/webhook/notifier.go index 8baaccdc6ad10..ee80766032ff1 100644 --- a/services/webhooknotifier/notifier.go +++ b/services/webhook/notifier.go @@ -1,7 +1,7 @@ // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package webhooknotifier +package webhook import ( "context" @@ -22,7 +22,6 @@ import ( api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/convert" - "code.gitea.io/gitea/services/webhook" ) func init() { @@ -59,7 +58,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user return } - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -67,7 +66,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user Sender: convert.ToUser(doer, nil), }) } else { - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ Action: api.HookIssueLabelCleared, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -85,7 +84,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m mode, _ := access_model.AccessLevel(ctx, doer, repo) // forked webhook - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{ Forkee: convert.ToRepo(ctx, oldRepo, oldMode), Repo: convert.ToRepo(ctx, repo, mode), Sender: convert.ToUser(doer, nil), @@ -97,7 +96,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m // Add to hook queue for created repo after session commit. if u.IsOrganization() { - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -110,7 +109,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { // Add to hook queue for created repo after session commit. - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -121,7 +120,7 @@ func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u } func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) { - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(repo.MustOwner(ctx), nil), @@ -133,7 +132,7 @@ func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { // Add to hook queue for created repo after session commit. - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Organization: convert.ToUser(u, nil), @@ -164,7 +163,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u apiPullRequest.Action = api.HookIssueAssigned } // Assignee comment triggers a webhook - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestAssign, apiPullRequest); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestAssign, apiPullRequest); err != nil { log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err) return } @@ -182,7 +181,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u apiIssue.Action = api.HookIssueAssigned } // Assignee comment triggers a webhook - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueAssign, apiIssue); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueAssign, apiIssue); err != nil { log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err) return } @@ -198,7 +197,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user return } issue.PullRequest.Issue = issue - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -211,7 +210,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user Sender: convert.ToUser(doer, nil), }) } else { - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -250,7 +249,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use } else { apiPullRequest.Action = api.HookIssueReOpened } - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest) + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest) } else { apiIssue := &api.IssuePayload{ Index: issue.Index, @@ -263,7 +262,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use } else { apiIssue.Action = api.HookIssueReOpened } - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, apiIssue) + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, apiIssue) } if err != nil { log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err) @@ -281,7 +280,7 @@ func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode } mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueOpened, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -307,7 +306,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues } mode, _ := access_model.AccessLevel(ctx, pull.Issue.Poster, pull.Issue.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), @@ -328,7 +327,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us var err error if issue.IsPull { issue.PullRequest.Issue = issue - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -341,7 +340,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us Sender: convert.ToUser(doer, nil), }) } else { - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -382,7 +381,7 @@ func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_mo } mode, _ := access_model.AccessLevel(ctx, doer, c.Issue.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: convert.ToAPIIssue(ctx, c.Issue), Comment: convert.ToComment(c), @@ -410,7 +409,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us } mode, _ := access_model.AccessLevel(ctx, doer, repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), Comment: convert.ToComment(comment), @@ -447,7 +446,7 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo } mode, _ := access_model.AccessLevel(ctx, doer, comment.Issue.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: convert.ToAPIIssue(ctx, comment.Issue), Comment: convert.ToComment(comment), @@ -461,7 +460,7 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { // Add to hook queue for created wiki page. - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -474,7 +473,7 @@ func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_mode func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { // Add to hook queue for edit wiki page. - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiEdited, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -487,7 +486,7 @@ func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_mod func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) { // Add to hook queue for edit wiki page. - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), Sender: convert.ToUser(doer, nil), @@ -522,7 +521,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use log.Error("LoadIssue: %v", err) return } - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{ Action: api.HookIssueLabelUpdated, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -530,7 +529,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Sender: convert.ToUser(doer, nil), }) } else { - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ Action: api.HookIssueLabelUpdated, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -564,7 +563,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * log.Error("LoadIssue: %v", err) return } - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestMilestone, &api.PullRequestPayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestMilestone, &api.PullRequestPayload{ Action: hookAction, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), @@ -572,7 +571,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * Sender: convert.ToUser(doer, nil), }) } else { - err = webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{ + err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{ Action: hookAction, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), @@ -593,7 +592,7 @@ func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo return } - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName, Before: opts.OldCommitID, After: opts.NewCommitID, @@ -646,7 +645,7 @@ func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m Action: api.HookIssueClosed, } - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest); err != nil { + if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest); err != nil { log.Error("PrepareWebhooks: %v", err) } } @@ -660,7 +659,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex issue := pr.Issue mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ @@ -702,7 +701,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue log.Error("models.AccessLevel: %v", err) return } - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{ Action: api.HookIssueReviewed, Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), @@ -722,7 +721,7 @@ func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{ Ref: refName, Sha: refID, RefType: refType, @@ -743,7 +742,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe return } - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequestSync, &api.PullRequestPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequestSync, &api.PullRequestPayload{ Action: api.HookIssueSynchronized, Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), @@ -759,7 +758,7 @@ func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{ Ref: refName, RefType: refType, PusherType: api.PusherTypeUser, @@ -777,7 +776,7 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model } mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo) - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{ Action: action, Release: convert.ToRelease(rel), Repository: convert.ToRepo(ctx, rel.Repo, mode), @@ -807,7 +806,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use return } - if err := webhook.PrepareWebhooks(ctx, webhook.EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName, Before: opts.OldCommitID, After: opts.NewCommitID, @@ -840,7 +839,7 @@ func (m *webhookNotifier) NotifyPackageDelete(ctx context.Context, doer *user_mo } func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) { - source := webhook.EventSource{ + source := EventSource{ Repository: pd.Repository, Owner: pd.Owner, } @@ -851,7 +850,7 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo return } - if err := webhook.PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{ + if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{ Action: action, Package: apiPackage, Sender: convert.ToUser(sender, nil), diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index 32f497fd273a2..e72d00ffb360b 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -32,8 +32,6 @@ import ( repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" - _ "code.gitea.io/gitea/services/webhooknotifier" - "github.com/stretchr/testify/assert" )