FEATURE: add reviewable score updated webhook (#12846)

FEATURE: add reviewable score updated webhook (#12846)

Adds a webhook to notify when a reviewable score is updated.

This is different from created or status changed as additional flags can roll in and update the score without updating status. Useful for applications looking to integrate in with Discourse’s scores

diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index 9b729bd..0d6ecb6 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -213,6 +213,8 @@ class Reviewable < ActiveRecord::Base
     update(score: self.score + rs.score, latest_score: rs.created_at, force_review: force_review)
     topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic
 
+    DiscourseEvent.trigger(:reviewable_score_updated, self)
+
     rs
   end
 
@@ -625,6 +627,10 @@ protected
     )
 
     self.score = result[0].score
+
+    DiscourseEvent.trigger(:reviewable_score_updated, self)
+
+    self.score
   end
 
   def increment_version!(version = nil)
diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb
index e762093..4bf6c2a 100644
--- a/config/initializers/012-web_hook_events.rb
+++ b/config/initializers/012-web_hook_events.rb
@@ -85,8 +85,13 @@ end
   end
 end
 
-DiscourseEvent.on(:reviewable_created) do |reviewable|
-  WebHook.enqueue_object_hooks(:reviewable, reviewable, :reviewable_created, reviewable.serializer)
+%i(
+  reviewable_created
+  reviewable_score_updated
+).each do |event|
+  DiscourseEvent.on(event) do |reviewable|
+    WebHook.enqueue_object_hooks(:reviewable, reviewable, event, reviewable.serializer)
+  end
 end
 
 DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable|
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
index f6b873a..03c665a 100644
--- a/spec/models/web_hook_spec.rb
+++ b/spec/models/web_hook_spec.rb
@@ -494,6 +494,17 @@ describe WebHook do
       payload = JSON.parse(job_args["payload"])
       expect(payload["id"]).to eq(reviewable.id)
 
+      reviewable.add_score(
+        Discourse.system_user,
+        ReviewableScore.types[:off_topic],
+        reason: "test"
+      )
+      job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
+
+      expect(job_args["event_name"]).to eq("reviewable_score_updated")
+      payload = JSON.parse(job_args["payload"])
+      expect(payload["id"]).to eq(reviewable.id)
+
       reviewable.perform(Discourse.system_user, :reject_user_delete)
       job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
 

GitHub sha: fa393b29

This commit appears in #12846 which was approved by lis2. It was merged by featheredtoast.