mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	#2246 fully support of webhooks for pull request
This commit is contained in:
		@@ -58,8 +58,9 @@ func IsValidHookContentType(name string) bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HookEvents struct {
 | 
			
		||||
	Create bool `json:"create"`
 | 
			
		||||
	Push   bool `json:"push"`
 | 
			
		||||
	Create      bool `json:"create"`
 | 
			
		||||
	Push        bool `json:"push"`
 | 
			
		||||
	PullRequest bool `json:"pull_request"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// HookEvent represents events that will delivery hook.
 | 
			
		||||
@@ -157,6 +158,12 @@ func (w *Webhook) HasPushEvent() bool {
 | 
			
		||||
		(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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *Webhook) EventsArray() []string {
 | 
			
		||||
	events := make([]string, 0, 2)
 | 
			
		||||
	if w.HasCreateEvent() {
 | 
			
		||||
@@ -309,8 +316,9 @@ func IsValidHookTaskType(name string) bool {
 | 
			
		||||
type HookEventType string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	HOOK_EVENT_CREATE HookEventType = "create"
 | 
			
		||||
	HOOK_EVENT_PUSH   HookEventType = "push"
 | 
			
		||||
	HOOK_EVENT_CREATE       HookEventType = "create"
 | 
			
		||||
	HOOK_EVENT_PUSH         HookEventType = "push"
 | 
			
		||||
	HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// HookRequest represents hook task request information.
 | 
			
		||||
@@ -422,17 +430,13 @@ func UpdateHookTask(t *HookTask) error {
 | 
			
		||||
 | 
			
		||||
// PrepareWebhooks adds new webhooks to task queue for given payload.
 | 
			
		||||
func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
 | 
			
		||||
	if err := repo.GetOwner(); err != nil {
 | 
			
		||||
		return fmt.Errorf("GetOwner: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ws, err := GetActiveWebhooksByRepoID(repo.ID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// check if repo belongs to org and append additional webhooks
 | 
			
		||||
	if repo.Owner.IsOrganization() {
 | 
			
		||||
	if repo.MustOwner().IsOrganization() {
 | 
			
		||||
		// get hooks for org
 | 
			
		||||
		orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@@ -456,6 +460,10 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
 | 
			
		||||
			if !w.HasPushEvent() {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		case HOOK_EVENT_PULL_REQUEST:
 | 
			
		||||
			if !w.HasPullRequestEvent() {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Use separate objects so modifcations won't be made on payload on non-Gogs type hooks.
 | 
			
		||||
@@ -477,7 +485,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
 | 
			
		||||
			URL:         w.URL,
 | 
			
		||||
			Payloader:   payloader,
 | 
			
		||||
			ContentType: w.ContentType,
 | 
			
		||||
			EventType:   HOOK_EVENT_PUSH,
 | 
			
		||||
			EventType:   event,
 | 
			
		||||
			IsSSL:       w.IsSSL,
 | 
			
		||||
		}); err != nil {
 | 
			
		||||
			return fmt.Errorf("CreateHookTask: %v", err)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user