2021-01-11 07:10:41 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class OnboardingProgress < ApplicationRecord
|
|
|
|
belongs_to :namespace, optional: false
|
|
|
|
|
|
|
|
validate :namespace_is_root_namespace
|
|
|
|
|
|
|
|
ACTIONS = [
|
|
|
|
:git_pull,
|
|
|
|
:git_write,
|
|
|
|
:merge_request_created,
|
|
|
|
:pipeline_created,
|
|
|
|
:user_added,
|
|
|
|
:trial_started,
|
|
|
|
:subscription_created,
|
|
|
|
:required_mr_approvals_enabled,
|
|
|
|
:code_owners_enabled,
|
|
|
|
:scoped_label_created,
|
|
|
|
:security_scan_enabled,
|
2021-02-25 13:11:05 -05:00
|
|
|
:issue_created,
|
2021-01-11 07:10:41 -05:00
|
|
|
:issue_auto_closed,
|
|
|
|
:repository_imported,
|
2021-12-29 13:13:42 -05:00
|
|
|
:repository_mirrored,
|
|
|
|
:secure_dependency_scanning_run,
|
|
|
|
:secure_container_scanning_run,
|
|
|
|
:secure_dast_run,
|
|
|
|
:secure_secret_detection_run,
|
|
|
|
:secure_coverage_fuzzing_run,
|
|
|
|
:secure_api_fuzzing_run,
|
2022-04-15 08:09:42 -04:00
|
|
|
:secure_cluster_image_scanning_run,
|
|
|
|
:license_scanning_run
|
2021-01-11 07:10:41 -05:00
|
|
|
].freeze
|
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
scope :incomplete_actions, -> (actions) do
|
|
|
|
Array.wrap(actions).inject(self) { |scope, action| scope.where(column_name(action) => nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :completed_actions, -> (actions) do
|
|
|
|
Array.wrap(actions).inject(self) { |scope, action| scope.where.not(column_name(action) => nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :completed_actions_with_latest_in_range, -> (actions, range) do
|
|
|
|
actions = Array(actions)
|
|
|
|
if actions.size == 1
|
|
|
|
where(column_name(actions[0]) => range)
|
|
|
|
else
|
|
|
|
action_columns = actions.map { |action| arel_table[column_name(action)] }
|
|
|
|
completed_actions(actions).where(Arel::Nodes::NamedFunction.new('GREATEST', action_columns).between(range))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-11 07:10:41 -05:00
|
|
|
class << self
|
|
|
|
def onboard(namespace)
|
|
|
|
return unless root_namespace?(namespace)
|
|
|
|
|
2021-08-20 11:10:24 -04:00
|
|
|
create(namespace: namespace)
|
2021-01-11 07:10:41 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 04:09:36 -05:00
|
|
|
def onboarding?(namespace)
|
|
|
|
where(namespace: namespace).any?
|
|
|
|
end
|
|
|
|
|
2021-12-29 13:13:42 -05:00
|
|
|
def register(namespace, actions)
|
|
|
|
actions = Array(actions)
|
|
|
|
return unless root_namespace?(namespace) && actions.difference(ACTIONS).empty?
|
2021-01-11 07:10:41 -05:00
|
|
|
|
2021-12-29 13:13:42 -05:00
|
|
|
onboarding_progress = find_by(namespace: namespace)
|
|
|
|
return unless onboarding_progress
|
|
|
|
|
|
|
|
now = Time.current
|
|
|
|
nil_actions = actions.select { |action| onboarding_progress[column_name(action)].nil? }
|
|
|
|
return if nil_actions.empty?
|
|
|
|
|
|
|
|
updates = nil_actions.inject({}) { |sum, action| sum.merge!({ column_name(action) => now }) }
|
|
|
|
onboarding_progress.update!(updates)
|
2021-01-11 07:10:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def completed?(namespace, action)
|
|
|
|
return unless root_namespace?(namespace) && ACTIONS.include?(action)
|
|
|
|
|
|
|
|
action_column = column_name(action)
|
|
|
|
where(namespace: namespace).where.not(action_column => nil).exists?
|
|
|
|
end
|
|
|
|
|
2021-02-23 10:10:47 -05:00
|
|
|
def not_completed?(namespace_id, action)
|
|
|
|
return unless ACTIONS.include?(action)
|
|
|
|
|
|
|
|
action_column = column_name(action)
|
|
|
|
where(namespace_id: namespace_id).where(action_column => nil).exists?
|
|
|
|
end
|
|
|
|
|
2021-01-11 07:10:41 -05:00
|
|
|
def column_name(action)
|
|
|
|
:"#{action}_at"
|
|
|
|
end
|
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
private
|
|
|
|
|
2021-01-11 07:10:41 -05:00
|
|
|
def root_namespace?(namespace)
|
|
|
|
namespace && namespace.root?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-19 05:10:19 -04:00
|
|
|
def number_of_completed_actions
|
|
|
|
attributes.extract!(*ACTIONS.map { |action| self.class.column_name(action).to_s }).compact!.size
|
|
|
|
end
|
|
|
|
|
2021-01-11 07:10:41 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def namespace_is_root_namespace
|
|
|
|
return unless namespace
|
|
|
|
|
|
|
|
errors.add(:namespace, _('must be a root namespace')) if namespace.has_parent?
|
|
|
|
end
|
|
|
|
end
|