mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	Fix edithook api can not update package, status and workflow_job events (#34495)
* the origin of this problem is duplicated code
This commit is contained in:
		@@ -159,6 +159,41 @@ func pullHook(events []string, event string) bool {
 | 
				
			|||||||
	return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true)
 | 
						return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func updateHookEvents(events []string) webhook_module.HookEvents {
 | 
				
			||||||
 | 
						if len(events) == 0 {
 | 
				
			||||||
 | 
							events = []string{"push"}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						hookEvents := make(webhook_module.HookEvents)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventCreate] = util.SliceContainsString(events, string(webhook_module.HookEventCreate), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPush] = util.SliceContainsString(events, string(webhook_module.HookEventPush), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventDelete] = util.SliceContainsString(events, string(webhook_module.HookEventDelete), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventFork] = util.SliceContainsString(events, string(webhook_module.HookEventFork), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventRepository] = util.SliceContainsString(events, string(webhook_module.HookEventRepository), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventWiki] = util.SliceContainsString(events, string(webhook_module.HookEventWiki), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventRelease] = util.SliceContainsString(events, string(webhook_module.HookEventRelease), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPackage] = util.SliceContainsString(events, string(webhook_module.HookEventPackage), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventStatus] = util.SliceContainsString(events, string(webhook_module.HookEventStatus), true)
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventWorkflowJob] = util.SliceContainsString(events, string(webhook_module.HookEventWorkflowJob), true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Issues
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventIssues] = issuesHook(events, "issues_only")
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventIssueAssign] = issuesHook(events, string(webhook_module.HookEventIssueAssign))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventIssueLabel] = issuesHook(events, string(webhook_module.HookEventIssueLabel))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventIssueMilestone] = issuesHook(events, string(webhook_module.HookEventIssueMilestone))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventIssueComment] = issuesHook(events, string(webhook_module.HookEventIssueComment))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Pull requests
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequest] = pullHook(events, "pull_request_only")
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(events, string(webhook_module.HookEventPullRequestAssign))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(events, string(webhook_module.HookEventPullRequestLabel))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(events, string(webhook_module.HookEventPullRequestMilestone))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestComment] = pullHook(events, string(webhook_module.HookEventPullRequestComment))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestReview] = pullHook(events, "pull_request_review")
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(events, string(webhook_module.HookEventPullRequestReviewRequest))
 | 
				
			||||||
 | 
						hookEvents[webhook_module.HookEventPullRequestSync] = pullHook(events, string(webhook_module.HookEventPullRequestSync))
 | 
				
			||||||
 | 
						return hookEvents
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is
 | 
					// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is
 | 
				
			||||||
// an error, write to `ctx` accordingly. Return (webhook, ok)
 | 
					// an error, write to `ctx` accordingly. Return (webhook, ok)
 | 
				
			||||||
func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
 | 
					func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
 | 
				
			||||||
@@ -167,9 +202,6 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
 | 
				
			|||||||
		return nil, false
 | 
							return nil, false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(form.Events) == 0 {
 | 
					 | 
				
			||||||
		form.Events = []string{"push"}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if form.Config["is_system_webhook"] != "" {
 | 
						if form.Config["is_system_webhook"] != "" {
 | 
				
			||||||
		sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
 | 
							sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
@@ -188,31 +220,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
 | 
				
			|||||||
		IsSystemWebhook: isSystemWebhook,
 | 
							IsSystemWebhook: isSystemWebhook,
 | 
				
			||||||
		HookEvent: &webhook_module.HookEvent{
 | 
							HookEvent: &webhook_module.HookEvent{
 | 
				
			||||||
			ChooseEvents: true,
 | 
								ChooseEvents: true,
 | 
				
			||||||
			HookEvents: webhook_module.HookEvents{
 | 
								HookEvents:   updateHookEvents(form.Events),
 | 
				
			||||||
				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.HookEventPackage:                  util.SliceContainsString(form.Events, string(webhook_module.HookEventPackage), true),
 | 
					 | 
				
			||||||
				webhook_module.HookEventStatus:                   util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true),
 | 
					 | 
				
			||||||
				webhook_module.HookEventWorkflowJob:              util.SliceContainsString(form.Events, string(webhook_module.HookEventWorkflowJob), true),
 | 
					 | 
				
			||||||
			},
 | 
					 | 
				
			||||||
			BranchFilter: form.BranchFilter,
 | 
								BranchFilter: form.BranchFilter,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		IsActive: form.Active,
 | 
							IsActive: form.Active,
 | 
				
			||||||
@@ -361,19 +369,10 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Update events
 | 
						// Update events
 | 
				
			||||||
	if len(form.Events) == 0 {
 | 
						w.HookEvents = updateHookEvents(form.Events)
 | 
				
			||||||
		form.Events = []string{"push"}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	w.PushOnly = false
 | 
						w.PushOnly = false
 | 
				
			||||||
	w.SendEverything = false
 | 
						w.SendEverything = false
 | 
				
			||||||
	w.ChooseEvents = true
 | 
						w.ChooseEvents = 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
 | 
						w.BranchFilter = form.BranchFilter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := w.SetHeaderAuthorization(form.AuthorizationHeader)
 | 
						err := w.SetHeaderAuthorization(form.AuthorizationHeader)
 | 
				
			||||||
@@ -382,23 +381,6 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
 | 
				
			|||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Issues
 | 
					 | 
				
			||||||
	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.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 {
 | 
						if err := w.UpdateEvent(); err != nil {
 | 
				
			||||||
		ctx.APIErrorInternal(err)
 | 
							ctx.APIErrorInternal(err)
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user