Remove opsgenie schedule integration and random user assignment

Remove opsgenie schedule integration and random user assignment

diff --git a/app/jobs/regular/process_alert.rb b/app/jobs/regular/process_alert.rb
index 628b34c..7160229 100644
--- a/app/jobs/regular/process_alert.rb
+++ b/app/jobs/regular/process_alert.rb
@@ -169,27 +169,6 @@ module Jobs
     def assign_alert(topic, receiver, assignee: nil)
       return unless SiteSetting.prometheus_alert_receiver_enable_assign
 
-      assignee ||= begin
-        emails = OpsgenieSchedule.users_on_rotation
-
-        possible_users =
-          if emails.length == 0
-            []
-          else
-            users = User.with_email(OpsgenieSchedule.users_on_rotation)
-
-            if group_id = receiver[:assignee_group_id]
-              users = users.joins(:group_users).where(
-                group_users: { group_id: group_id }
-              )
-            end
-
-            users
-          end
-
-        assignee = possible_users.sample || random_group_member(receiver[:assignee_group_id])
-      end
-
       if assignee
         TopicAssigner.new(topic, Discourse.system_user).assign(assignee)
       end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index fcfc37b..113927e 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1,9 +1,8 @@
 en:
   site_settings:
     prometheus_alert_receiver_enabled: "Enable prometheus alert receiver"
-    prometheus_alert_receiver_opsgenie_api_key: "OpsGenie integrations API key to fetch schedules which will be used for assigning alerts"
     prometheus_alert_receiver_custom_nav_group: "Group that should see the top nav item for open alerts (leave blank to include for everyone)"
-    prometheus_alert_receiver_enable_assign: "Enable the assignment of new topics"
+    prometheus_alert_receiver_enable_assign: "Enable the assignment of new alert topics based on the topic_assignee and group_topic_assignee annotations"
     prometheus_alert_receiver_debug_enabled: "Enable debugging for prometheus alert receiver"
   prom_alert_receiver:
     post:
diff --git a/config/settings.yml b/config/settings.yml
index c2ea6c0..567d2a4 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -1,6 +1,5 @@
 plugins:
   prometheus_alert_receiver_enabled: true
-  prometheus_alert_receiver_opsgenie_api_key: ''
   prometheus_alert_receiver_custom_nav_group: 
     default: ''
     type: list
diff --git a/lib/opsgenie_schedule.rb b/lib/opsgenie_schedule.rb
deleted file mode 100644
index 99b0bec..0000000
--- a/lib/opsgenie_schedule.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-# frozen_string_literal: true
-
-require 'msgpack'
-
-class OpsgenieSchedule
-  API_PATH = 'https://api.eu.opsgenie.com/v2'.freeze
-  SCHEDULES_PATH = '/schedules'.freeze
-  HOURS = (0..23).to_a.freeze
-
-  def self.users_on_rotation
-    return [] if api_key.blank?
-    timezone = user_rotations['timezone']
-
-    user_rotations['rotations'].select do |(start_hour, end_hour)|
-      current_hour = Time.use_zone(timezone) { Time.zone.now.hour }
-
-      if start_hour > end_hour
-        (HOURS - (end_hour..(start_hour - 1)).to_a).include?(current_hour)
-      else
-        (start_hour..(end_hour - 1)) === current_hour
-      end
-    end.values.flatten
-  end
-
-  private
-
-  def self.user_rotations
-    if cached_rotations = Discourse.redis.get(redis_key)
-      return MessagePack.unpack(cached_rotations)
-    end
-
-    schedule_ids = get(SCHEDULES_PATH)["data"].map do |schedule|
-      schedule["id"]
-    end
-
-    new_rotations = {}
-
-    schedule_ids.each do |schedule_id|
-      data = get("#{SCHEDULES_PATH}/#{schedule_id}")["data"]
-      new_rotations['timezone'] = data['timezone']
-      rotations = data["rotations"]
-
-      rotations.each do |rotation|
-        restriction = rotation['timeRestriction']['restriction']
-        start_hour = restriction['startHour']
-        end_hour = restriction['endHour']
-        emails = rotation["participants"].map { |p| p["username"] }
-        new_rotations['rotations'] ||= {}
-        new_rotations['rotations'][[start_hour, end_hour]] = emails
-      end
-    end
-
-    Discourse.redis.setex(redis_key, 1.day, new_rotations.to_msgpack)
-    new_rotations
-  end
-
-  def self.redis_key
-    'discourse-prometheus-alert-receiver:rotations'
-  end
-
-  def self.api_key
-    SiteSetting.prometheus_alert_receiver_opsgenie_api_key
-  end
-
-  def self.get(path)
-    uri = URI("#{API_PATH}#{path}")
-    http = Net::HTTP.new(uri.host, uri.port)
-    http.use_ssl = true if uri.scheme == 'https'
-    request = Net::HTTP::Get.new(uri.request_uri)
-
-    request.initialize_http_header(
-      "Authorization" => "GenieKey #{api_key}"
-    )
-
-    response = http.request(request)
-
-    unless response.kind_of?(Net::HTTPSuccess)
-      raise StandardError, "(#{response.code}) #{response.body}"
-    end
-
-    JSON.parse(response.body)
-  end
-end
diff --git a/plugin.rb b/plugin.rb
index 8d3a457..c20fc45 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -15,7 +15,6 @@ after_initialize do
     '../app/jobs/concerns/alert_post_mixin.rb',
     '../app/jobs/regular/process_alert.rb',
     '../app/jobs/regular/process_grouped_alerts.rb',
-    '../lib/opsgenie_schedule.rb'
   ].each { |path| load File.expand_path(path, __FILE__) }
 
   module ::DiscoursePrometheusAlertReceiver
diff --git a/spec/integration/discourse_prometheus_alert_receiver/receiver_controller_spec.rb b/spec/integration/discourse_prometheus_alert_receiver/receiver_controller_spec.rb
index ed6d165..db3e177 100644
--- a/spec/integration/discourse_prometheus_alert_receiver/receiver_controller_spec.rb
+++ b/spec/integration/discourse_prometheus_alert_receiver/receiver_controller_spec.rb
@@ -829,8 +829,6 @@ RSpec.describe DiscoursePrometheusAlertReceiver::ReceiverController do
           expect(raw).to match(
             /http:\/\/alerts\.example\.com\/graph\?g0\.expr=lolrus.*g0\.tab=0/
           )
-
-          expect(topic.assigned_to_user.id).to eq(assignee.id)
         end
       end
 
@@ -873,8 +871,6 @@ RSpec.describe DiscoursePrometheusAlertReceiver::ReceiverController do
           expect(raw).to match(
             /somethingfunny.*date=2020-01-02 time=03:04:05/
           )
-
-          expect(topic.assigned_to_user.id).to eq(assignee.id)
         end
       end
 
@@ -1295,16 +1291,6 @@ RSpec.describe DiscoursePrometheusAlertReceiver::ReceiverController do
             ]
           )
         end
-
-        it 'reassigns the alert if topic has no assignee' do
-          TopicAssigner.new(topic, Discourse.system_user).unassign
-
-          expect do
-            post "/prometheus/receiver/#{token}", params: payload
-          end.to change { topic.reload.assigned_to_user }
-
-          expect(response.status).to eq(200)
-        end
       end
 
       context "firing alert for a groupkey referencing a closed topic" do
@@ -1396,8 +1382,6 @@ RSpec.describe DiscoursePrometheusAlertReceiver::ReceiverController do
             ]
           )
 
-          expect(keyed_topic.assigned_to_user.id).to eq(assignee.id)
-
           expect(keyed_topic.posts.first.raw).to match(
             /\[Previous alert\]\(http:\/\/test\.localhost\/t\/#{closed_topic.id}\).*date=2018-07-27 time=19:33:44/
           )
diff --git a/spec/lib/opsgenie_schedule_spec.rb b/spec/lib/opsgenie_schedule_spec.rb
deleted file mode 100644
index f15bb91..0000000
--- a/spec/lib/opsgenie_schedule_spec.rb
+++ /dev/null
@@ -1,192 +0,0 @@
-# frozen_string_literal: true
-
-require 'rails_helper'
-
-RSpec.describe OpsgenieSchedule do
-  before do
-    api_key = SiteSetting.prometheus_alert_receiver_opsgenie_api_key = 'somekey'
-    schedules_path = "#{OpsgenieSchedule::API_PATH}#{OpsgenieSchedule::SCHEDULES_PATH}"
-
-    stub_request(:get, schedules_path)
-      .with(
-        headers: {
-          'Authorization' => "GenieKey #{api_key}"
-        }
-      )
-      .to_return(status: 200, body: {
-        "data": [
-          {
-            "id": "uuid1",
-            "name": "Up",
-            "description": "",

[... diff too long, it was truncated ...]

GitHub sha: c7dab92a

2 Likes