diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 894357e36a..c19f7a4c8d 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -167,186 +167,23 @@ func (w *Webhook) UpdateEvent() error { return err } -// HasCreateEvent returns true if hook enabled create event. -func (w *Webhook) HasCreateEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Create) -} - -// HasDeleteEvent returns true if hook enabled delete event. -func (w *Webhook) HasDeleteEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Delete) -} - -// HasForkEvent returns true if hook enabled fork event. -func (w *Webhook) HasForkEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Fork) -} - -// HasIssuesEvent returns true if hook enabled issues event. -func (w *Webhook) HasIssuesEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Issues) -} - -// HasIssuesAssignEvent returns true if hook enabled issues assign event. -func (w *Webhook) HasIssuesAssignEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.IssueAssign) -} - -// HasIssuesLabelEvent returns true if hook enabled issues label event. -func (w *Webhook) HasIssuesLabelEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.IssueLabel) -} - -// HasIssuesMilestoneEvent returns true if hook enabled issues milestone event. -func (w *Webhook) HasIssuesMilestoneEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.IssueMilestone) -} - -// HasIssueCommentEvent returns true if hook enabled issue_comment event. -func (w *Webhook) HasIssueCommentEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.IssueComment) -} - -// HasPushEvent returns true if hook enabled push event. -func (w *Webhook) HasPushEvent() bool { - return w.PushOnly || w.SendEverything || - (w.ChooseEvents && w.HookEvents.Push) -} - -// HasPullRequestEvent returns true if hook enabled pull request event. -func (w *Webhook) HasPullRequestEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequest) -} - -// HasPullRequestAssignEvent returns true if hook enabled pull request assign event. -func (w *Webhook) HasPullRequestAssignEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestAssign) -} - -// HasPullRequestLabelEvent returns true if hook enabled pull request label event. -func (w *Webhook) HasPullRequestLabelEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestLabel) -} - -// HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event. -func (w *Webhook) HasPullRequestMilestoneEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestMilestone) -} - -// HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event. -func (w *Webhook) HasPullRequestCommentEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestComment) -} - -// HasPullRequestApprovedEvent returns true if hook enabled pull request review event. -func (w *Webhook) HasPullRequestApprovedEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestReview) -} - -// HasPullRequestRejectedEvent returns true if hook enabled pull request review event. -func (w *Webhook) HasPullRequestRejectedEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestReview) -} - -// HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event. -func (w *Webhook) HasPullRequestReviewCommentEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestReview) -} - -// HasPullRequestSyncEvent returns true if hook enabled pull request sync event. -func (w *Webhook) HasPullRequestSyncEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestSync) -} - -// HasWikiEvent returns true if hook enabled wiki event. -func (w *Webhook) HasWikiEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvent.Wiki) -} - -// HasReleaseEvent returns if hook enabled release event. -func (w *Webhook) HasReleaseEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Release) -} - -// HasRepositoryEvent returns if hook enabled repository event. -func (w *Webhook) HasRepositoryEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Repository) -} - -// HasPackageEvent returns if hook enabled package event. -func (w *Webhook) HasPackageEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.Package) -} - -// HasPullRequestReviewRequestEvent returns true if hook enabled pull request review request event. -func (w *Webhook) HasPullRequestReviewRequestEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.PullRequestReviewRequest) -} - -// EventCheckers returns event checkers -func (w *Webhook) EventCheckers() []struct { - Has func() bool - Type webhook_module.HookEventType -} { - return []struct { - Has func() bool - Type webhook_module.HookEventType - }{ - {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}, - {w.HasPullRequestReviewRequestEvent, webhook_module.HookEventPullRequestReviewRequest}, +func (w *Webhook) HasEvent(evt webhook_module.HookEventType) bool { + if w.SendEverything { + return true } + if w.PushOnly { + return evt == webhook_module.HookEventPush + } + return w.HookEvents[evt] } // EventsArray returns an array of hook events func (w *Webhook) EventsArray() []string { events := make([]string, 0, 7) - for _, c := range w.EventCheckers() { - if c.Has() { - events = append(events, string(c.Type)) + for event, enabled := range w.HookEvents { + if enabled { + events = append(events, string(event)) } } return events diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index c6c3f40d46..b3282e6371 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -54,9 +54,9 @@ func TestWebhook_UpdateEvent(t *testing.T) { SendEverything: false, ChooseEvents: false, HookEvents: webhook_module.HookEvents{ - Create: false, - Push: true, - PullRequest: false, + webhook_module.HookEventCreate: false, + webhook_module.HookEventPush: true, + webhook_module.HookEventPullRequest: false, }, } webhook.HookEvent = hookEvent diff --git a/modules/structs/hook.go b/modules/structs/hook.go index ce5742e5c7..1c8e5e2bdf 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -480,6 +480,7 @@ type PackagePayload struct { Action HookPackageAction `json:"action"` Repository *Repository `json:"repository"` Package *Package `json:"package"` + Status *CommitStatus `json:"status"` Organization *User `json:"organization"` Sender *User `json:"sender"` } diff --git a/modules/webhook/events.go b/modules/webhook/events.go new file mode 100644 index 0000000000..888641e15a --- /dev/null +++ b/modules/webhook/events.go @@ -0,0 +1,20 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package webhook + +type HookEvents map[HookEventType]bool + +func (he HookEvents) Get(evt HookEventType) bool { + return he[evt] +} + +// 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"` +} diff --git a/modules/webhook/structs.go b/modules/webhook/structs.go deleted file mode 100644 index 927a91a74c..0000000000 --- a/modules/webhook/structs.go +++ /dev/null @@ -1,39 +0,0 @@ -// 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"` - PullRequestReviewRequest bool `json:"pull_request_review_request"` - 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"` -} diff --git a/modules/webhook/type.go b/modules/webhook/type.go index aa4de45eb4..19df8fa3ab 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -22,6 +22,7 @@ const ( HookEventPullRequestLabel HookEventType = "pull_request_label" HookEventPullRequestMilestone HookEventType = "pull_request_milestone" HookEventPullRequestComment HookEventType = "pull_request_comment" + HookEventPullRequestReview HookEventType = "pull_request_review" HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved" HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected" HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment" @@ -31,21 +32,13 @@ const ( HookEventRepository HookEventType = "repository" HookEventRelease HookEventType = "release" HookEventPackage HookEventType = "package" - HookEventSchedule HookEventType = "schedule" + HookEventSchedule HookEventType = "schedule" // this is not for webhook at the moment, only for actions HookEventStatus HookEventType = "status" ) // 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, @@ -59,14 +52,9 @@ func (h HookEventType) Event() string { return "pull_request_rejected" case HookEventPullRequestReviewComment: return "pull_request_comment" - case HookEventWiki: - return "wiki" - case HookEventRepository: - return "repository" - case HookEventRelease: - return "release" + default: + return string(h) } - return "" } func (h HookEventType) IsPullRequest() bool { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 41f6c5055d..064d8b9c57 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2327,6 +2327,8 @@ settings.event_fork = Fork settings.event_fork_desc = Repository forked. settings.event_wiki = Wiki settings.event_wiki_desc = Wiki page created, renamed, edited or deleted. +settings.event_statuses = Statuses +settings.event_statuses_desc = Commit Status updated from the API. settings.event_release = Release settings.event_release_desc = Release published, updated or deleted in a repository. settings.event_push = Push diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 4328878e19..335e313195 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -185,26 +185,27 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI HookEvent: &webhook_module.HookEvent{ ChooseEvents: true, HookEvents: webhook_module.HookEvents{ - Create: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true), - Delete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true), - Fork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true), - Issues: issuesHook(form.Events, "issues_only"), - IssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)), - IssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)), - IssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)), - IssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)), - Push: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true), - PullRequest: pullHook(form.Events, "pull_request_only"), - PullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)), - PullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)), - PullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)), - PullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)), - PullRequestReview: pullHook(form.Events, "pull_request_review"), - PullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)), - PullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)), - Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true), - Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true), - Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true), + webhook_module.HookEventCreate: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true), + webhook_module.HookEventDelete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true), + webhook_module.HookEventFork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true), + webhook_module.HookEventIssues: issuesHook(form.Events, "issues_only"), + webhook_module.HookEventIssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)), + webhook_module.HookEventIssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)), + webhook_module.HookEventIssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)), + webhook_module.HookEventIssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)), + webhook_module.HookEventPush: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true), + webhook_module.HookEventPullRequest: pullHook(form.Events, "pull_request_only"), + webhook_module.HookEventPullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)), + webhook_module.HookEventPullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)), + webhook_module.HookEventPullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)), + webhook_module.HookEventPullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)), + webhook_module.HookEventPullRequestReview: pullHook(form.Events, "pull_request_review"), + webhook_module.HookEventPullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)), + webhook_module.HookEventPullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)), + webhook_module.HookEventWiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true), + webhook_module.HookEventRepository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true), + webhook_module.HookEventRelease: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true), + webhook_module.HookEventStatus: util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true), }, BranchFilter: form.BranchFilter, }, @@ -356,14 +357,13 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh w.PushOnly = false w.SendEverything = false w.ChooseEvents = true - w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) - w.Push = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true) - w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) - w.Delete = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true) - w.Fork = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true) - w.Repository = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true) - w.Wiki = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true) - w.Release = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true) + w.HookEvents[webhook_module.HookEventCreate] = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) + w.HookEvents[webhook_module.HookEventPush] = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true) + w.HookEvents[webhook_module.HookEventDelete] = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true) + w.HookEvents[webhook_module.HookEventFork] = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true) + w.HookEvents[webhook_module.HookEventRepository] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true) + w.HookEvents[webhook_module.HookEventWiki] = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true) + w.HookEvents[webhook_module.HookEventRelease] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true) w.BranchFilter = form.BranchFilter err := w.SetHeaderAuthorization(form.AuthorizationHeader) @@ -373,21 +373,21 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh } // Issues - w.Issues = issuesHook(form.Events, "issues_only") - w.IssueAssign = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)) - w.IssueLabel = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)) - w.IssueMilestone = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)) - w.IssueComment = issuesHook(form.Events, string(webhook_module.HookEventIssueComment)) + w.HookEvents[webhook_module.HookEventIssues] = issuesHook(form.Events, "issues_only") + w.HookEvents[webhook_module.HookEventIssueAssign] = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)) + w.HookEvents[webhook_module.HookEventIssueLabel] = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)) + w.HookEvents[webhook_module.HookEventIssueMilestone] = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)) + w.HookEvents[webhook_module.HookEventIssueComment] = issuesHook(form.Events, string(webhook_module.HookEventIssueComment)) // Pull requests - w.PullRequest = pullHook(form.Events, "pull_request_only") - w.PullRequestAssign = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)) - w.PullRequestLabel = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)) - w.PullRequestMilestone = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)) - w.PullRequestComment = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)) - w.PullRequestReview = pullHook(form.Events, "pull_request_review") - w.PullRequestReviewRequest = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)) - w.PullRequestSync = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)) + w.HookEvents[webhook_module.HookEventPullRequest] = pullHook(form.Events, "pull_request_only") + w.HookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)) + w.HookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)) + w.HookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)) + w.HookEvents[webhook_module.HookEventPullRequestComment] = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)) + w.HookEvents[webhook_module.HookEventPullRequestReview] = pullHook(form.Events, "pull_request_review") + w.HookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)) + w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)) if err := w.UpdateEvent(); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateEvent", err) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index 1b0ba83af4..9f866e5682 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -163,27 +163,28 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent { SendEverything: form.SendEverything(), ChooseEvents: form.ChooseEvents(), HookEvents: webhook_module.HookEvents{ - Create: form.Create, - Delete: form.Delete, - Fork: form.Fork, - Issues: form.Issues, - IssueAssign: form.IssueAssign, - IssueLabel: form.IssueLabel, - IssueMilestone: form.IssueMilestone, - IssueComment: form.IssueComment, - Release: form.Release, - Push: form.Push, - PullRequest: form.PullRequest, - PullRequestAssign: form.PullRequestAssign, - PullRequestLabel: form.PullRequestLabel, - PullRequestMilestone: form.PullRequestMilestone, - PullRequestComment: form.PullRequestComment, - PullRequestReview: form.PullRequestReview, - PullRequestSync: form.PullRequestSync, - PullRequestReviewRequest: form.PullRequestReviewRequest, - Wiki: form.Wiki, - Repository: form.Repository, - Package: form.Package, + webhook_module.HookEventCreate: form.Create, + webhook_module.HookEventDelete: form.Delete, + webhook_module.HookEventFork: form.Fork, + webhook_module.HookEventIssues: form.Issues, + webhook_module.HookEventIssueAssign: form.IssueAssign, + webhook_module.HookEventIssueLabel: form.IssueLabel, + webhook_module.HookEventIssueMilestone: form.IssueMilestone, + webhook_module.HookEventIssueComment: form.IssueComment, + webhook_module.HookEventRelease: form.Release, + webhook_module.HookEventPush: form.Push, + webhook_module.HookEventPullRequest: form.PullRequest, + webhook_module.HookEventPullRequestAssign: form.PullRequestAssign, + webhook_module.HookEventPullRequestLabel: form.PullRequestLabel, + webhook_module.HookEventPullRequestMilestone: form.PullRequestMilestone, + webhook_module.HookEventPullRequestComment: form.PullRequestComment, + webhook_module.HookEventPullRequestReview: form.PullRequestReview, + webhook_module.HookEventPullRequestSync: form.PullRequestSync, + webhook_module.HookEventPullRequestReviewRequest: form.PullRequestReviewRequest, + webhook_module.HookEventWiki: form.Wiki, + webhook_module.HookEventRepository: form.Repository, + webhook_module.HookEventPackage: form.Package, + webhook_module.HookEventStatus: form.Status, }, BranchFilter: form.BranchFilter, } diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 4f9806dc93..6a6ccda35f 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -219,26 +219,18 @@ type ProtectBranchPriorityForm struct { IDs []int64 } -// __ __ ___. .__ __ -// / \ / \ ____\_ |__ | |__ ____ ____ | | __ -// \ \/\/ // __ \| __ \| | \ / _ \ / _ \| |/ / -// \ /\ ___/| \_\ \ Y ( <_> | <_> ) < -// \__/\ / \___ >___ /___| /\____/ \____/|__|_ \ -// \/ \/ \/ \/ \/ - // WebhookForm form for changing web hook type WebhookForm struct { Events string Create bool Delete bool Fork bool + Push bool Issues bool IssueAssign bool IssueLabel bool IssueMilestone bool IssueComment bool - Release bool - Push bool PullRequest bool PullRequestAssign bool PullRequestLabel bool @@ -249,7 +241,9 @@ type WebhookForm struct { PullRequestReviewRequest bool Wiki bool Repository bool + Release bool Package bool + Status bool Active bool BranchFilter string `binding:"GlobPattern"` AuthorizationHeader string diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index e382f5a9df..91746667ff 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -170,6 +170,12 @@ func (dc dingtalkConvertor) Package(p *api.PackagePayload) (DingtalkPayload, err return createDingtalkPayload(text, text, "view package", p.Package.HTMLURL), nil } +func (dc dingtalkConvertor) Status(p *api.CommitStatusPayload) (DingtalkPayload, error) { + text, _ := getStatusPayloadInfo(p, noneLinkFormatter, true) + + return createDingtalkPayload(text, text, "Status Changed", p.TargetURL), nil +} + func createDingtalkPayload(title, text, singleTitle, singleURL string) DingtalkPayload { return DingtalkPayload{ MsgType: "actionCard", diff --git a/services/webhook/discord.go b/services/webhook/discord.go index c562d98168..52829262ae 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -265,6 +265,12 @@ func (d discordConvertor) Package(p *api.PackagePayload) (DiscordPayload, error) return d.createPayload(p.Sender, text, "", p.Package.HTMLURL, color), nil } +func (d discordConvertor) Status(p *api.CommitStatusPayload) (DiscordPayload, error) { + text, color := getStatusPayloadInfo(p, noneLinkFormatter, false) + + return d.createPayload(p.Sender, text, "", p.TargetURL, color), nil +} + func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) { meta := &DiscordMeta{} if err := json.Unmarshal([]byte(w.Meta), meta); err != nil { diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 7ca7d1cf5f..99d2b52c0f 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -166,6 +166,12 @@ func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error) return newFeishuTextPayload(text), nil } +func (fc feishuConvertor) Status(p *api.CommitStatusPayload) (FeishuPayload, error) { + text, _ := getStatusPayloadInfo(p, noneLinkFormatter, true) + + return newFeishuTextPayload(text), nil +} + func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) { var pc payloadConvertor[FeishuPayload] = feishuConvertor{} return newJSONRequest(pc, w, t, true) diff --git a/services/webhook/general.go b/services/webhook/general.go index dde43bb349..91bf68600f 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -307,6 +307,18 @@ func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, w return text, color } +func getStatusPayloadInfo(p *api.CommitStatusPayload, linkFormatter linkFormatter, withSender bool) (text string, color int) { + refLink := linkFormatter(p.TargetURL, p.Context+"["+p.SHA+"]:"+p.Description) + + text = fmt.Sprintf("Commit Status changed: %s", refLink) + color = greenColor + if withSender { + text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)) + } + + return text, 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) { diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 5e9f808d8b..cc51a1d23f 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -240,6 +240,13 @@ func (m matrixConvertor) Package(p *api.PackagePayload) (MatrixPayload, error) { return m.newPayload(text) } +func (m matrixConvertor) Status(p *api.CommitStatusPayload) (MatrixPayload, error) { + refLink := htmlLinkFormatter(p.TargetURL, p.Context+"["+p.SHA+"]:"+p.Description) + text := fmt.Sprintf("Commit Status changed: %s", refLink) + + return m.newPayload(text) +} + var urlRegex = regexp.MustCompile(`]*?href="([^">]*?)">(.*?)`) func getMessageBody(htmlText string) string { diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 7ef96ffa27..a528536b27 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -303,6 +303,20 @@ func (m msteamsConvertor) Package(p *api.PackagePayload) (MSTeamsPayload, error) ), nil } +func (m msteamsConvertor) Status(p *api.CommitStatusPayload) (MSTeamsPayload, error) { + title, color := getStatusPayloadInfo(p, noneLinkFormatter, false) + + return createMSTeamsPayload( + p.Repo, + p.Sender, + title, + "", + p.TargetURL, + color, + &MSTeamsFact{"CommitStatus:", p.Context}, + ), nil +} + func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTarget string, color int, fact *MSTeamsFact) MSTeamsPayload { facts := make([]MSTeamsFact, 0, 2) if r != nil { diff --git a/services/webhook/packagist.go b/services/webhook/packagist.go index 4d809ab3a6..1f0c0c1f63 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -110,6 +110,10 @@ func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, e return PackagistPayload{}, nil } +func (pc packagistConvertor) Status(_ *api.CommitStatusPayload) (PackagistPayload, error) { + return PackagistPayload{}, nil +} + func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) { meta := &PackagistMeta{} if err := json.Unmarshal([]byte(w.Meta), meta); err != nil { diff --git a/services/webhook/payloader.go b/services/webhook/payloader.go index ab280a25b6..c29ad8ac92 100644 --- a/services/webhook/payloader.go +++ b/services/webhook/payloader.go @@ -28,6 +28,7 @@ type payloadConvertor[T any] interface { Release(*api.ReleasePayload) (T, error) Wiki(*api.WikiPayload) (T, error) Package(*api.PackagePayload) (T, error) + Status(*api.CommitStatusPayload) (T, error) } func convertUnmarshalledJSON[T, P any](convert func(P) (T, error), data []byte) (t T, err error) { @@ -77,6 +78,8 @@ func newPayload[T any](rc payloadConvertor[T], data []byte, event webhook_module return convertUnmarshalledJSON(rc.Wiki, data) case webhook_module.HookEventPackage: return convertUnmarshalledJSON(rc.Package, data) + case webhook_module.HookEventStatus: + return convertUnmarshalledJSON(rc.Status, data) } return t, fmt.Errorf("newPayload unsupported event: %s", event) } diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 2a49df2453..aea4ea2615 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -167,6 +167,12 @@ func (s slackConvertor) Package(p *api.PackagePayload) (SlackPayload, error) { return s.createPayload(text, nil), nil } +func (s slackConvertor) Status(p *api.CommitStatusPayload) (SlackPayload, error) { + text, _ := getStatusPayloadInfo(p, SlackLinkFormatter, true) + + return s.createPayload(text, nil), nil +} + // Push implements payloadConvertor Push method func (s slackConvertor) Push(p *api.PushPayload) (SlackPayload, error) { // n new commits diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index e54d6f2947..6ccaaa82a1 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -174,6 +174,12 @@ func (t telegramConvertor) Package(p *api.PackagePayload) (TelegramPayload, erro return createTelegramPayloadHTML(text), nil } +func (t telegramConvertor) Status(p *api.CommitStatusPayload) (TelegramPayload, error) { + text, _ := getStatusPayloadInfo(p, htmlLinkFormatter, true) + + return createTelegramPayloadHTML(text), nil +} + func createTelegramPayloadHTML(msgHTML string) TelegramPayload { // https://core.telegram.org/bots/api#formatting-options return TelegramPayload{ diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index e0e8fa2fc1..b4609e8a51 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -137,14 +137,8 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook return nil } - for _, e := range w.EventCheckers() { - if event == e.Type { - if !e.Has() { - return nil - } - - break - } + if !w.HasEvent(event) { + return nil } // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 1d8c1d7dac..42b23fd995 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -175,6 +175,12 @@ func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, return newWechatworkMarkdownPayload(text), nil } +func (wc wechatworkConvertor) Status(p *api.CommitStatusPayload) (WechatworkPayload, error) { + text, _ := getStatusPayloadInfo(p, noneLinkFormatter, true) + + return newWechatworkMarkdownPayload(text), nil +} + func newWechatworkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) { var pc payloadConvertor[WechatworkPayload] = wechatworkConvertor{} return newJSONRequest(pc, w, t, true) diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index cf3b0eb053..3b28a4c6c0 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -31,7 +31,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_create_desc"}}
@@ -41,7 +41,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_delete_desc"}}
@@ -51,7 +51,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_fork_desc"}}
@@ -61,7 +61,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_push_desc"}}
@@ -71,7 +71,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_repository_desc"}}
@@ -81,7 +81,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_release_desc"}}
@@ -91,7 +91,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_package_desc"}}
@@ -102,13 +102,24 @@
- + {{ctx.Locale.Tr "repo.settings.event_wiki_desc"}}
+ +
+
+
+ + + {{ctx.Locale.Tr "repo.settings.event_statuses_desc"}} +
+
+
+
@@ -117,7 +128,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_issues_desc"}}
@@ -127,7 +138,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_issue_assign_desc"}}
@@ -137,7 +148,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_issue_label_desc"}}
@@ -147,7 +158,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_issue_milestone_desc"}}
@@ -157,7 +168,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_issue_comment_desc"}}
@@ -172,7 +183,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_desc"}}
@@ -182,7 +193,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_assign_desc"}}
@@ -192,7 +203,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_label_desc"}}
@@ -202,7 +213,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_milestone_desc"}}
@@ -212,7 +223,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_comment_desc"}}
@@ -222,7 +233,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_review_desc"}}
@@ -232,7 +243,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_sync_desc"}}
@@ -242,7 +253,7 @@
- + {{ctx.Locale.Tr "repo.settings.event_pull_request_review_request_desc"}}