From 5eb48e2ef5a70244d5dd4e3d329d59e67b75dae4 Mon Sep 17 00:00:00 2001
From: John Hope <jhope@gitlab.com>
Date: Tue, 13 Apr 2021 00:46:48 +0000
Subject: [PATCH] Fix notification when new Service Desk Issue is created

---
 app/models/user.rb                            |  1 +
 changelogs/unreleased/confirm_support_bot.yml |  5 ++
 ...20210407150240_confirm_support_bot_user.rb | 23 +++++
 db/schema_migrations/20210407150240           |  1 +
 .../confirm_support_bot_user_spec.rb          | 86 +++++++++++++++++++
 spec/models/user_spec.rb                      |  6 ++
 6 files changed, 122 insertions(+)
 create mode 100644 changelogs/unreleased/confirm_support_bot.yml
 create mode 100644 db/post_migrate/20210407150240_confirm_support_bot_user.rb
 create mode 100644 db/schema_migrations/20210407150240
 create mode 100644 spec/migrations/confirm_support_bot_user_spec.rb

diff --git a/app/models/user.rb b/app/models/user.rb
index 250ac7d3b0485..e12d9bb0f9e0a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -764,6 +764,7 @@ def support_bot
         u.bio = 'The GitLab support bot used for Service Desk'
         u.name = 'GitLab Support Bot'
         u.avatar = bot_avatar(image: 'support-bot.png')
+        u.confirmed_at = Time.zone.now
       end
     end
 
diff --git a/changelogs/unreleased/confirm_support_bot.yml b/changelogs/unreleased/confirm_support_bot.yml
new file mode 100644
index 0000000000000..a37b245c8ab49
--- /dev/null
+++ b/changelogs/unreleased/confirm_support_bot.yml
@@ -0,0 +1,5 @@
+---
+title: Fix notification when new Service Desk Issue is created
+merge_request: 58803
+author:
+type: fixed
diff --git a/db/post_migrate/20210407150240_confirm_support_bot_user.rb b/db/post_migrate/20210407150240_confirm_support_bot_user.rb
new file mode 100644
index 0000000000000..c26ae15312899
--- /dev/null
+++ b/db/post_migrate/20210407150240_confirm_support_bot_user.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class ConfirmSupportBotUser < ActiveRecord::Migration[6.0]
+  SUPPORT_BOT_TYPE = 1
+
+  def up
+    users = Arel::Table.new(:users)
+    um = Arel::UpdateManager.new
+    um.table(users)
+      .where(users[:user_type].eq(SUPPORT_BOT_TYPE))
+      .where(users[:confirmed_at].eq(nil))
+      .set([[users[:confirmed_at], Arel::Nodes::NamedFunction.new('COALESCE', [users[:created_at], Arel::Nodes::SqlLiteral.new('NOW()')])]])
+    connection.execute(um.to_sql)
+  end
+
+  def down
+    # no op
+
+    # The up migration allows for the possibility that the support user might
+    # have already been manually confirmed. It's not reversible as this data is
+    # subsequently lost.
+  end
+end
diff --git a/db/schema_migrations/20210407150240 b/db/schema_migrations/20210407150240
new file mode 100644
index 0000000000000..cfc187d5dd78c
--- /dev/null
+++ b/db/schema_migrations/20210407150240
@@ -0,0 +1 @@
+b5f83e3870dc7c70fbde6071725aa2acb3e99f7c2ed050633c34ed35e696ba1e
\ No newline at end of file
diff --git a/spec/migrations/confirm_support_bot_user_spec.rb b/spec/migrations/confirm_support_bot_user_spec.rb
new file mode 100644
index 0000000000000..f6bcab4aa7d92
--- /dev/null
+++ b/spec/migrations/confirm_support_bot_user_spec.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require_migration!
+
+RSpec.describe ConfirmSupportBotUser, :migration do
+  let(:users) { table(:users) }
+
+  context 'when support bot user is currently unconfirmed' do
+    let!(:support_bot) do
+      create_user!(
+        created_at: 2.days.ago,
+        user_type: User::USER_TYPES['support_bot']
+      )
+    end
+
+    it 'updates the `confirmed_at` attribute' do
+      expect { migrate! }.to change { support_bot.reload.confirmed_at }
+    end
+
+    it 'sets `confirmed_at` to be the same as their `created_at` attribute' do
+      migrate!
+
+      expect(support_bot.reload.confirmed_at).to eq(support_bot.created_at)
+    end
+  end
+
+  context 'when support bot user is already confirmed' do
+    let!(:confirmed_support_bot) do
+      create_user!(
+        user_type: User::USER_TYPES['support_bot'],
+        confirmed_at: 1.day.ago
+      )
+    end
+
+    it 'does not change their `confirmed_at` attribute' do
+      expect { migrate! }.not_to change { confirmed_support_bot.reload.confirmed_at }
+    end
+  end
+
+  context 'when support bot user created_at is null' do
+    let!(:support_bot) do
+      create_user!(
+        user_type: User::USER_TYPES['support_bot'],
+        confirmed_at: nil,
+        record_timestamps: false
+      )
+    end
+
+    it 'updates the `confirmed_at` attribute' do
+      expect { migrate! }.to change { support_bot.reload.confirmed_at }.from(nil)
+    end
+
+    it 'does not change the `created_at` attribute' do
+      expect { migrate!}.not_to change { support_bot.reload.created_at }.from(nil)
+    end
+  end
+
+  context 'with human users that are currently unconfirmed' do
+    let!(:unconfirmed_human) do
+      create_user!(
+        name: 'human',
+        email: 'human@example.com',
+        user_type: nil
+      )
+    end
+
+    it 'does not update their `confirmed_at` attribute' do
+      expect { migrate! }.not_to change { unconfirmed_human.reload.confirmed_at }
+    end
+  end
+
+  private
+
+  def create_user!(name: 'GitLab Support Bot', email: 'support@example.com', user_type:, created_at: Time.now, confirmed_at: nil, record_timestamps: true)
+    users.create!(
+      name: name,
+      email: email,
+      username: name,
+      projects_limit: 0,
+      user_type: user_type,
+      confirmed_at: confirmed_at,
+      record_timestamps: record_timestamps
+    )
+  end
+end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 520f616bfa049..c5819dad42ca9 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -5508,6 +5508,12 @@ def access_levels(groups)
     it_behaves_like 'bot user avatars', :alert_bot, 'alert-bot.png'
     it_behaves_like 'bot user avatars', :support_bot, 'support-bot.png'
     it_behaves_like 'bot user avatars', :security_bot, 'security-bot.png'
+
+    context 'when bot is the support_bot' do
+      subject { described_class.support_bot }
+
+      it { is_expected.to be_confirmed }
+    end
   end
 
   describe '#confirmation_required_on_sign_in?' do
-- 
GitLab