diff --git a/server/subscribe.go b/server/subscribe.go index f19d39af7..def6f0fe8 100644 --- a/server/subscribe.go +++ b/server/subscribe.go @@ -124,31 +124,46 @@ func (p *Plugin) getUserID() string { return p.getConfig().botUserID } -func (p *Plugin) matchesSubsciptionFilters(wh *webhook, filters SubscriptionFilters) bool { +func (p *Plugin) matchesSubsciptionFilters(wh *webhook, sub ChannelSubscription) bool { + filters := sub.Filters + + subCheckID := model.NewId() + log := func(msg interface{}) { + p.client.Log.Debug("matchesSubsciptionFilters DEBUG LOG", "issue_id", wh.Issue.ID, "sub_check_id", subCheckID, "msg", msg, "sub_name", sub.Name) + } + + log(1) + webhookEvents := wh.Events() foundEvent := false eventTypes := filters.Events if eventTypes.Intersection(webhookEvents).Len() > 0 { + log(2) foundEvent = true } else if eventTypes.ContainsAny(eventUpdatedAny) { + log(3) for _, eventType := range webhookEvents.Elems() { if strings.HasPrefix(eventType, "event_updated") || strings.HasSuffix(eventType, "comment") { + log("4 " + eventType) foundEvent = true } } } if !foundEvent { + log(5) return false } issue := &wh.JiraWebhook.Issue if filters.IssueTypes.Len() != 0 && !filters.IssueTypes.ContainsAny(issue.Fields.Type.ID) { + log(6) return false } if filters.Projects.Len() != 0 && !filters.Projects.ContainsAny(issue.Fields.Project.Key) { + log(7) return false } @@ -159,29 +174,39 @@ func (p *Plugin) matchesSubsciptionFilters(wh *webhook, filters SubscriptionFilt // Broken filter, values must be provided if inclusion == "" || (field.Values.Len() == 0 && inclusion != FilterEmpty) { + log(8) return false } if field.Key == securityLevelField { containsSecurityLevelFilter = true + log(9) + if inclusion == FilterExcludeAny && useEmptySecurityLevel { + log(10) inclusion = FilterEmpty } } value := getIssueFieldValue(issue, field.Key) if !isValidFieldInclusion(field, value, inclusion) { + log(11) + log(fmt.Sprintf("field %s value %s inclusion %s", field.Key, value.Elems(), inclusion)) return false } } if !containsSecurityLevelFilter && useEmptySecurityLevel { securityLevel := getIssueFieldValue(issue, securityLevelField) + log(12) if securityLevel.Len() > 0 { + log(13) return false } } + log(14) + return true } @@ -200,19 +225,27 @@ func isValidFieldInclusion(field FieldFilter, value StringSet, inclusion string) } func (p *Plugin) getChannelsSubscribed(wh *webhook, instanceID types.ID) ([]ChannelSubscription, error) { + p.API.LogDebug("getChannelsSubscribed 1") subs, err := p.getSubscriptions(instanceID) if err != nil { + p.API.LogDebug("getChannelsSubscribed 2") return nil, err } + p.API.LogDebug("getChannelsSubscribed 3", "lensubs", len(subs.Channel.ByID)) + var channelSubscriptions []ChannelSubscription subIds := subs.Channel.ByID for _, sub := range subIds { - if p.matchesSubsciptionFilters(wh, sub.Filters) { + if p.matchesSubsciptionFilters(wh, sub) { channelSubscriptions = append(channelSubscriptions, sub) + p.client.Log.Debug("matches for sub " + sub.Name) + } else { + p.client.Log.Debug("does not match for sub " + sub.Name) } } + p.API.LogDebug("getChannelsSubscribed 4") return channelSubscriptions, nil } @@ -747,16 +780,22 @@ func (p *Plugin) hasPermissionToManageSubscription(instanceID types.ID, userID, func (p *Plugin) httpSubscribeWebhook(w http.ResponseWriter, r *http.Request, instanceID types.ID) (status int, err error) { conf := p.getConfig() + p.client.Log.Debug("httpSubscribeWebhook 1") + if conf.Secret == "" { return respondErr(w, http.StatusForbidden, fmt.Errorf("JIRA plugin not configured correctly; must provide Secret")) } + p.client.Log.Debug("httpSubscribeWebhook 2") + status, err = verifyHTTPSecret(conf.Secret, r.FormValue("secret")) if err != nil { return respondErr(w, status, err) } + p.client.Log.Debug("httpSubscribeWebhook 3") + bb, err := io.ReadAll(r.Body) if err != nil { return respondErr(w, http.StatusInternalServerError, err) @@ -765,6 +804,8 @@ func (p *Plugin) httpSubscribeWebhook(w http.ResponseWriter, r *http.Request, in p.client.Log.Debug("Webhook Event Log", "event", string(bb)) } + p.client.Log.Debug("httpSubscribeWebhook 4") + // If there is space in the queue, immediately return a 200; we will process the webhook event async. // If the queue is full, return a 503; we will not process that webhook event. select { @@ -772,8 +813,10 @@ func (p *Plugin) httpSubscribeWebhook(w http.ResponseWriter, r *http.Request, in InstanceID: instanceID, Data: bb, }: + p.client.Log.Debug("httpSubscribeWebhook webhook event queued") return http.StatusOK, nil default: + p.client.Log.Debug("httpSubscribeWebhook webhook event not queued") return respondErr(w, http.StatusServiceUnavailable, nil) } } diff --git a/server/webhook_worker.go b/server/webhook_worker.go index 859cc6bcc..7e9846d9b 100644 --- a/server/webhook_worker.go +++ b/server/webhook_worker.go @@ -22,6 +22,7 @@ type webhookMessage struct { func (ww webhookWorker) work() { for msg := range ww.workQueue { + ww.p.client.Log.Debug("webhookWorker.work") err := ww.process(msg) if err != nil { if errors.Is(err, errWebhookeventUnsupported) { @@ -41,31 +42,41 @@ func (ww webhookWorker) process(msg *webhookMessage) (err error) { } }() + ww.p.client.Log.Debug("webhookWorker.process 1") + wh, err := ParseWebhook(msg.Data) if err != nil { + ww.p.client.Log.Debug("webhookWorker.process 2") return err } if _, _, err = wh.PostNotifications(ww.p, msg.InstanceID); err != nil { + ww.p.client.Log.Debug("webhookWorker.process 3") ww.p.errorf("WebhookWorker id: %d, error posting notifications, err: %v", ww.id, err) } v := wh.(*webhook) if err = v.JiraWebhook.expandIssue(ww.p, msg.InstanceID); err != nil { + ww.p.client.Log.Debug("webhookWorker.process 4") return err } channelsSubscribed, err := ww.p.getChannelsSubscribed(v, msg.InstanceID) if err != nil { + ww.p.client.Log.Debug("webhookWorker.process 5") return err } + ww.p.client.Log.Debug("webhookWorker.process 6", "len_channels_subscribed", len(channelsSubscribed)) + botUserID := ww.p.getUserID() for _, channelSubscribed := range channelsSubscribed { if _, _, err1 := wh.PostToChannel(ww.p, msg.InstanceID, channelSubscribed.ChannelID, botUserID, channelSubscribed.Name); err1 != nil { + ww.p.client.Log.Debug("webhookWorker.process 7") ww.p.errorf("WebhookWorker id: %d, error posting to channel, err: %v", ww.id, err1) } } + ww.p.client.Log.Debug("webhookWorker.process 8") return nil }