Commit d91836d9 authored by Pavel Shutsin's avatar Pavel Shutsin Committed by Stan Hu

Add user_type field for service users

It will be used for code review analytics
to detect service users
parent 355289e4
...@@ -60,6 +60,7 @@ class User < ApplicationRecord ...@@ -60,6 +60,7 @@ class User < ApplicationRecord
MINIMUM_INACTIVE_DAYS = 180 MINIMUM_INACTIVE_DAYS = 180
enum bot_type: ::UserBotTypeEnums.bots enum bot_type: ::UserBotTypeEnums.bots
enum user_type: ::UserTypeEnums.types
# Override Devise::Models::Trackable#update_tracked_fields! # Override Devise::Models::Trackable#update_tracked_fields!
# to limit database writes to at most once every hour # to limit database writes to at most once every hour
...@@ -336,7 +337,7 @@ class User < ApplicationRecord ...@@ -336,7 +337,7 @@ class User < ApplicationRecord
scope :with_dashboard, -> (dashboard) { where(dashboard: dashboard) } scope :with_dashboard, -> (dashboard) { where(dashboard: dashboard) }
scope :with_public_profile, -> { where(private_profile: false) } scope :with_public_profile, -> { where(private_profile: false) }
scope :bots, -> { where.not(bot_type: nil) } scope :bots, -> { where.not(bot_type: nil) }
scope :humans, -> { where(bot_type: nil) } scope :humans, -> { where(user_type: nil, bot_type: nil) }
scope :with_expiring_and_not_notified_personal_access_tokens, ->(at) do scope :with_expiring_and_not_notified_personal_access_tokens, ->(at) do
where('EXISTS (?)', where('EXISTS (?)',
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
module UserBotTypeEnums module UserBotTypeEnums
def self.bots def self.bots
# When adding a new key, please ensure you are not conflicting with EE-only keys in app/models/user_bot_type_enums.rb # When adding a new key, please ensure you are not conflicting
# with EE-only keys in app/models/user_type_enums.rb
# or app/models/user_bot_type_enums.rb
{ {
alert_bot: 2 alert_bot: 2
} }
......
# frozen_string_literal: true
module UserTypeEnums
def self.types
# When adding a new key, please ensure you are not conflicting
# with EE-only keys in app/models/user_type_enums.rb
# or app/models/user_bot_type_enums.rb
bots
end
def self.bots
{
AlertBot: 2
}
end
end
UserTypeEnums.prepend_if_ee('EE::UserTypeEnums')
# frozen_string_literal: true
class AddUserType < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
with_lock_retries do
add_column :users, :user_type, :integer, limit: 2
end
end
def down
with_lock_retries do
remove_column :users, :user_type
end
end
end
# frozen_string_literal: true
class AddUserTypeIndex < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :users, :user_type
end
def down
remove_concurrent_index :users, :user_type
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_03_03_074328) do ActiveRecord::Schema.define(version: 2020_03_04_090155) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm" enable_extension "pg_trgm"
...@@ -4325,6 +4325,7 @@ ActiveRecord::Schema.define(version: 2020_03_03_074328) do ...@@ -4325,6 +4325,7 @@ ActiveRecord::Schema.define(version: 2020_03_03_074328) do
t.string "last_name", limit: 255 t.string "last_name", limit: 255
t.string "static_object_token", limit: 255 t.string "static_object_token", limit: 255
t.integer "role", limit: 2 t.integer "role", limit: 2
t.integer "user_type", limit: 2
t.index "lower((name)::text)", name: "index_on_users_name_lower" t.index "lower((name)::text)", name: "index_on_users_name_lower"
t.index ["accepted_term_id"], name: "index_users_on_accepted_term_id" t.index ["accepted_term_id"], name: "index_users_on_accepted_term_id"
t.index ["admin"], name: "index_users_on_admin" t.index ["admin"], name: "index_users_on_admin"
...@@ -4347,6 +4348,7 @@ ActiveRecord::Schema.define(version: 2020_03_03_074328) do ...@@ -4347,6 +4348,7 @@ ActiveRecord::Schema.define(version: 2020_03_03_074328) do
t.index ["state"], name: "index_users_on_state_and_internal_ee", where: "((ghost IS NOT TRUE) AND (bot_type IS NULL))" t.index ["state"], name: "index_users_on_state_and_internal_ee", where: "((ghost IS NOT TRUE) AND (bot_type IS NULL))"
t.index ["static_object_token"], name: "index_users_on_static_object_token", unique: true t.index ["static_object_token"], name: "index_users_on_static_object_token", unique: true
t.index ["unconfirmed_email"], name: "index_users_on_unconfirmed_email", where: "(unconfirmed_email IS NOT NULL)" t.index ["unconfirmed_email"], name: "index_users_on_unconfirmed_email", where: "(unconfirmed_email IS NOT NULL)"
t.index ["user_type"], name: "index_users_on_user_type"
t.index ["username"], name: "index_users_on_username" t.index ["username"], name: "index_users_on_username"
t.index ["username"], name: "index_users_on_username_trigram", opclass: :gin_trgm_ops, using: :gin t.index ["username"], name: "index_users_on_username_trigram", opclass: :gin_trgm_ops, using: :gin
end end
......
...@@ -9,7 +9,9 @@ module EE ...@@ -9,7 +9,9 @@ module EE
override :bots override :bots
def bots def bots
# When adding a new key, please ensure you are not redefining a key that already exists in app/models/user_bot_type_enums.rb # When adding a new key, please ensure you are not conflicting
# with EE-only keys in app/models/user_type_enums.rb
# or app/models/user_bot_type_enums.rb
super.merge( super.merge(
support_bot: 1, support_bot: 1,
visual_review_bot: 3 visual_review_bot: 3
......
# frozen_string_literal: true
module EE
module UserTypeEnums
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :types
def types
# When adding a new key, please ensure you are not conflicting
# with EE-only keys in app/models/user_type_enums.rb
# or app/models/user_bot_type_enums.rb
super.merge(ServiceUser: 4)
end
override :bots
def bots
super.merge(SupportBot: 1, VisualReviewBot: 3)
end
end
end
end
---
title: Allow users to be marked as service users
merge_request: 202680
author:
type: other
...@@ -19,7 +19,9 @@ module Analytics ...@@ -19,7 +19,9 @@ module Analytics
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def first_comment_at def first_comment_at
merge_request.related_notes.by_humans.where.not(author_id: merge_request.author_id).fresh.first&.created_at merge_request.related_notes.by_humans
.where.not(author_id: merge_request.author_id)
.fresh.first&.created_at
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -16,6 +16,10 @@ FactoryBot.modify do ...@@ -16,6 +16,10 @@ FactoryBot.modify do
) )
end end
end end
trait :service_user do
user_type { :ServiceUser }
end
end end
factory :omniauth_user do factory :omniauth_user do
......
...@@ -94,10 +94,11 @@ describe Note do ...@@ -94,10 +94,11 @@ describe Note do
end end
describe '.by_humans' do describe '.by_humans' do
it 'return human notes only' do it 'excludes notes by bots and service users' do
user_note = create(:note) user_note = create(:note)
create(:system_note) create(:system_note)
create(:note, author: create(:user, :bot)) create(:note, author: create(:user, :bot))
create(:note, author: create(:user, :service_user))
expect(described_class.by_humans).to match_array([user_note]) expect(described_class.by_humans).to match_array([user_note])
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment