FIX: Don't display webhooks for inactive plugins (#9206)

* FIX: Don't display webhooks for inactive plugins

This commit ensures that we don't show webhooks for plugins that are not
installed or that are disabled.

Bug report:

https://meta.discourse.org/t/webhookeventtype-and-the-solved-and-assign-plugins/144180

* rename to just 'active', it's cleaner
This commit is contained in:
Blake Erickson 2020-03-17 10:39:24 -06:00 committed by GitHub
parent e950471c0f
commit 919e405c48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -16,7 +16,7 @@ class Admin::WebHooksController < Admin::AdminController
json = { json = {
web_hooks: serialize_data(web_hooks, AdminWebHookSerializer), web_hooks: serialize_data(web_hooks, AdminWebHookSerializer),
extras: { extras: {
event_types: WebHookEventType.all, event_types: WebHookEventType.active,
default_event_types: WebHook.default_event_types, default_event_types: WebHook.default_event_types,
content_types: WebHook.content_types.map { |name, id| { id: id, name: name } }, content_types: WebHook.content_types.map { |name, id| { id: id, name: name } },
delivery_statuses: WebHook.last_delivery_statuses.map { |name, id| { id: id, name: name.to_s } }, delivery_statuses: WebHook.last_delivery_statuses.map { |name, id| { id: id, name: name.to_s } },

View File

@ -19,6 +19,15 @@ class WebHookEventType < ActiveRecord::Base
default_scope { order('id ASC') } default_scope { order('id ASC') }
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
def self.active
ids_to_exclude = []
ids_to_exclude << SOLVED unless defined?(SiteSetting.solved_enabled) && SiteSetting.solved_enabled
ids_to_exclude << ASSIGN unless defined?(SiteSetting.assign_enabled) && SiteSetting.assign_enabled
self.where.not(id: ids_to_exclude)
end
end end
# == Schema Information # == Schema Information

View File

@ -44,6 +44,22 @@ describe WebHook do
expect(post_hook.payload_url).to eq("https://example.com") expect(post_hook.payload_url).to eq("https://example.com")
end end
it "excludes disabled plugin web_hooks" do
web_hook_event_types = WebHookEventType.active.find_by(name: 'solved')
expect(web_hook_event_types).to eq(nil)
end
it "includes non-plugin web_hooks" do
web_hook_event_types = WebHookEventType.active.where(name: 'topic')
expect(web_hook_event_types.count).to eq(1)
end
it "includes enabled plugin web_hooks" do
SiteSetting.stubs(:solved_enabled).returns(true)
web_hook_event_types = WebHookEventType.active.where(name: 'solved')
expect(web_hook_event_types.count).to eq(1)
end
describe '#active_web_hooks' do describe '#active_web_hooks' do
it "returns unique hooks" do it "returns unique hooks" do
post_hook.web_hook_event_types << WebHookEventType.find_by(name: 'topic') post_hook.web_hook_event_types << WebHookEventType.find_by(name: 'topic')