diff options
Diffstat (limited to 'app/models/onboarding')
-rw-r--r-- | app/models/onboarding/completion.rb | 70 | ||||
-rw-r--r-- | app/models/onboarding/learn_gitlab.rb | 38 | ||||
-rw-r--r-- | app/models/onboarding/progress.rb | 118 |
3 files changed, 226 insertions, 0 deletions
diff --git a/app/models/onboarding/completion.rb b/app/models/onboarding/completion.rb new file mode 100644 index 00000000000..49fdb102209 --- /dev/null +++ b/app/models/onboarding/completion.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +module Onboarding + class Completion + include Gitlab::Utils::StrongMemoize + include Gitlab::Experiment::Dsl + + ACTION_ISSUE_IDS = { + pipeline_created: 7, + trial_started: 2, + required_mr_approvals_enabled: 11, + code_owners_enabled: 10 + }.freeze + + ACTION_PATHS = [ + :issue_created, + :git_write, + :merge_request_created, + :user_added + ].freeze + + def initialize(namespace, current_user = nil) + @namespace = namespace + @current_user = current_user + end + + def percentage + return 0 unless onboarding_progress + + attributes = onboarding_progress.attributes.symbolize_keys + + total_actions = action_columns.count + completed_actions = action_columns.count { |column| attributes[column].present? } + + (completed_actions.to_f / total_actions * 100).round + end + + private + + def onboarding_progress + strong_memoize(:onboarding_progress) do + ::Onboarding::Progress.find_by(namespace: namespace) + end + end + + def action_columns + strong_memoize(:action_columns) do + tracked_actions.map { |action_key| ::Onboarding::Progress.column_name(action_key) } + end + end + + def tracked_actions + ACTION_ISSUE_IDS.keys + ACTION_PATHS + deploy_section_tracked_actions + end + + def deploy_section_tracked_actions + experiment( + :security_actions_continuous_onboarding, + namespace: namespace, + user: current_user, + sticky_to: current_user + ) do |e| + e.control { [:security_scan_enabled] } + e.candidate { [:license_scanning_run, :secure_dependency_scanning_run, :secure_dast_run] } + end.run + end + + attr_reader :namespace, :current_user + end +end diff --git a/app/models/onboarding/learn_gitlab.rb b/app/models/onboarding/learn_gitlab.rb new file mode 100644 index 00000000000..d7a189ed6e2 --- /dev/null +++ b/app/models/onboarding/learn_gitlab.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Onboarding + class LearnGitlab + PROJECT_NAME = 'Learn GitLab' + PROJECT_NAME_ULTIMATE_TRIAL = 'Learn GitLab - Ultimate trial' + BOARD_NAME = 'GitLab onboarding' + LABEL_NAME = 'Novice' + + def initialize(current_user) + @current_user = current_user + end + + def available? + project && board && label + end + + def project + @project ||= current_user.projects.find_by_name([PROJECT_NAME, PROJECT_NAME_ULTIMATE_TRIAL]) + end + + def board + return unless project + + @board ||= project.boards.find_by_name(BOARD_NAME) + end + + def label + return unless project + + @label ||= project.labels.find_by_name(LABEL_NAME) + end + + private + + attr_reader :current_user + end +end diff --git a/app/models/onboarding/progress.rb b/app/models/onboarding/progress.rb new file mode 100644 index 00000000000..ecc78418256 --- /dev/null +++ b/app/models/onboarding/progress.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +module Onboarding + class Progress < ApplicationRecord + self.table_name = 'onboarding_progresses' + + 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, + :issue_created, + :issue_auto_closed, + :repository_imported, + :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, + :secure_cluster_image_scanning_run, + :license_scanning_run + ].freeze + + 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 + + class << self + def onboard(namespace) + return unless root_namespace?(namespace) + + create(namespace: namespace) + end + + def onboarding?(namespace) + where(namespace: namespace).any? + end + + def register(namespace, actions) + actions = Array(actions) + return unless root_namespace?(namespace) && actions.difference(ACTIONS).empty? + + 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) + 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 + + def not_completed?(namespace_id, action) + return unless ACTIONS.include?(action) + + action_column = column_name(action) + exists?(namespace_id: namespace_id, action_column => nil) + end + + def column_name(action) + :"#{action}_at" + end + + private + + def root_namespace?(namespace) + namespace&.root? + end + end + + def number_of_completed_actions + attributes.extract!(*ACTIONS.map { |action| self.class.column_name(action).to_s }).compact!.size + end + + private + + def namespace_is_root_namespace + return unless namespace + + errors.add(:namespace, _('must be a root namespace')) if namespace.has_parent? + end + end +end |