Skip to content

Commit

Permalink
UX: show if webhook is disabled (#7217)
Browse files Browse the repository at this point in the history
+ show in staff logs when webhook is created/updated/destroyed
  • Loading branch information
majakomel authored and ZogStriP committed Mar 21, 2019
1 parent bfcbc4d commit 34730a0
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { bufferedRender } from "discourse-common/lib/buffered-render";

export default Ember.Component.extend(
bufferedRender({
classes: ["text-muted", "text-danger", "text-successful"],
icons: ["circle-o", "times-circle", "circle"],
classes: ["text-muted", "text-danger", "text-successful", "text-muted"],
icons: ["circle-o", "times-circle", "circle", "circle"],

@computed("deliveryStatuses", "model.last_delivery_status")
status(deliveryStatuses, lastDeliveryStatus) {
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/admin/web_hooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def create
web_hook = WebHook.new(web_hook_params)

if web_hook.save
StaffActionLogger.new(current_user).log_web_hook(web_hook, UserHistory.actions[:web_hook_create])
render_serialized(web_hook, AdminWebHookSerializer, root: 'web_hook')
else
render_json_error web_hook.errors.full_messages
Expand All @@ -42,6 +43,7 @@ def create

def update
if @web_hook.update_attributes(web_hook_params)
StaffActionLogger.new(current_user).log_web_hook(@web_hook, UserHistory.actions[:web_hook_update], changes: @web_hook.saved_changes)
render_serialized(@web_hook, AdminWebHookSerializer, root: 'web_hook')
else
render_json_error @web_hook.errors.full_messages
Expand All @@ -50,6 +52,7 @@ def update

def destroy
@web_hook.destroy!
StaffActionLogger.new(current_user).log_web_hook(@web_hook, UserHistory.actions[:web_hook_destroy])
render json: success_json
end

Expand Down
10 changes: 8 additions & 2 deletions app/models/user_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def self.actions
entity_export: 66,
change_password: 67,
topic_timestamps_changed: 68,
approve_user: 69
approve_user: 69,
web_hook_create: 70,
web_hook_update: 71,
web_hook_destroy: 72
)
end

Expand Down Expand Up @@ -149,7 +152,10 @@ def self.staff_actions
:entity_export,
:change_name,
:topic_timestamps_changed,
:approve_user
:approve_user,
:web_hook_create,
:web_hook_update,
:web_hook_destroy
]
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/web_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def self.content_types
def self.last_delivery_statuses
@last_delivery_statuses ||= Enum.new(inactive: 1,
failed: 2,
successful: 3)
successful: 3,
disabled: 4)
end

def self.default_event_types
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/admin_web_hook_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ class AdminWebHookSerializer < ApplicationSerializer
def web_hook_event_types
ActiveModel::ArraySerializer.new(object.web_hook_event_types).as_json
end

def last_delivery_status
object.active ? object.last_delivery_status : WebHook.last_delivery_statuses[:disabled]
end
end
24 changes: 24 additions & 0 deletions app/services/staff_action_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,30 @@ def log_post_rejected(rejected_post, opts = {})
))
end

def log_web_hook(web_hook, action, opts = {})
details = [
"webhook_id: #{web_hook.id}",
"payload_url: #{web_hook.payload_url}"
]

if changes = opts[:changes]
changes.reject! { |k, v| k == "updated_at" }
old_values = []
new_values = []
changes.each do |k, v|
old_values << "#{k}: #{v[0]}"
new_values << "#{k}: #{v[1]}"
end
end

UserHistory.create!(params(opts).merge(
action: action,
context: details.join(", "),
previous_value: old_values&.join(", "),
new_value: new_values&.join(", ")
))
end

private

def params(opts = nil)
Expand Down
4 changes: 4 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,7 @@ en:
inactive: "Inactive"
failed: "Failed"
successful: "Successful"
disabled: "Disabled"
events:
none: "There are no related events."
redeliver: "Redeliver"
Expand Down Expand Up @@ -3702,6 +3703,9 @@ en:
change_name: "change name"
topic_timestamps_changed: "topic timestamps changed"
approve_user: "approved user"
web_hook_create: "webhook create"
web_hook_update: "webhook update"
web_hook_destroy: "webhook destroy"
screened_emails:
title: "Screened Emails"
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."
Expand Down
3 changes: 3 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4349,3 +4349,6 @@ en:
unknown: "unknown"
user_merged: "%{username} was merged into this account"
user_delete_self: "Deleted by self from %{url}"
update: "Updated"
create: "Created"
destroy: "Destroyed"
25 changes: 25 additions & 0 deletions spec/requests/admin/web_hooks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

json = ::JSON.parse(response.body)
expect(json["web_hook"]["payload_url"]).to eq("https://meta.discourse.org/")
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:web_hook_create]).count).to eq(1)
end

it 'returns error when field is not filled correctly' do
Expand All @@ -57,6 +58,30 @@
end
end

describe '#update' do
it "logs webhook update" do
put "/admin/api/web_hooks/#{web_hook.id}.json", params: {
web_hook: { active: false, payload_url: "https://test.com" }
}

expect(response.status).to eq(200)
expect(UserHistory.where(acting_user_id: admin.id,
action: UserHistory.actions[:web_hook_update],
new_value: "payload_url: https://test.com, active: false").exists?).to eq(true)
end
end

describe '#destroy' do
it "logs webhook destroy" do
delete "/admin/api/web_hooks/#{web_hook.id}.json", params: {
web_hook: { active: false, payload_url: "https://test.com" }
}

expect(response.status).to eq(200)
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:web_hook_destroy]).exists?).to eq(true)
end
end

describe '#ping' do
it 'enqueues the ping event' do
expect do
Expand Down

1 comment on commit 34730a0

@SamSaffron
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, change looks great!

Please sign in to comment.