Commit b17e364e authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'nicolasdular/show-learn-gitlab-progress' into 'master'

Show onboarding progress for learn-gitlab project

See merge request gitlab-org/gitlab!60506
parents b18aa67e b2ed5469
......@@ -38,7 +38,7 @@ module Registrations
end
def learn_gitlab
@learn_gitlab ||= LearnGitlab.new(current_user)
@learn_gitlab ||= LearnGitlab::Project.new(current_user)
end
end
end
......@@ -28,27 +28,13 @@ module LearnGitlabHelper
private
ACTION_ISSUE_IDS = {
issue_created: 4,
git_write: 6,
pipeline_created: 7,
merge_request_created: 9,
user_added: 8,
trial_started: 2,
required_mr_approvals_enabled: 11,
code_owners_enabled: 10
}.freeze
ACTION_DOC_URLS = {
security_scan_enabled: 'https://docs.gitlab.com/ee/user/application_security/security_dashboard/#gitlab-security-dashboard-security-center-and-vulnerability-reports'
}.freeze
def action_urls
ACTION_ISSUE_IDS.transform_values { |id| project_issue_url(learn_gitlab_project, id) }.merge(ACTION_DOC_URLS)
LearnGitlab::Onboarding::ACTION_ISSUE_IDS.transform_values { |id| project_issue_url(learn_gitlab_project, id) }
.merge(LearnGitlab::Onboarding::ACTION_DOC_URLS)
end
def learn_gitlab_project
@learn_gitlab_project ||= LearnGitlab.new(current_user).project
@learn_gitlab_project ||= LearnGitlab::Project.new(current_user).project
end
def onboarding_progress(project)
......@@ -57,6 +43,6 @@ module LearnGitlabHelper
def learn_gitlab_onboarding_available?(project)
OnboardingProgress.onboarding?(project.namespace) &&
LearnGitlab.new(current_user).available?
LearnGitlab::Project.new(current_user).available?
end
end
# frozen_string_literal: true
class LearnGitlab
PROJECT_NAME = 'Learn GitLab'
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)
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
# frozen_string_literal: true
module LearnGitlab
class Onboarding
include Gitlab::Utils::StrongMemoize
ACTION_ISSUE_IDS = {
issue_created: 4,
git_write: 6,
pipeline_created: 7,
merge_request_created: 9,
user_added: 8,
trial_started: 2,
required_mr_approvals_enabled: 11,
code_owners_enabled: 10
}.freeze
ACTION_DOC_URLS = {
security_scan_enabled: 'https://docs.gitlab.com/ee/user/application_security/security_dashboard/#gitlab-security-dashboard-security-center-and-vulnerability-reports'
}.freeze
def initialize(namespace)
@namespace = namespace
end
def completed_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.to_f * 100).round
end
private
def onboarding_progress
strong_memoize(:onboarding_progress) do
OnboardingProgress.find_by(namespace: namespace) # rubocop: disable CodeReuse/ActiveRecord
end
end
def action_columns
strong_memoize(:action_columns) do
tracked_actions.map { |action_key| OnboardingProgress.column_name(action_key) }
end
end
def tracked_actions
ACTION_ISSUE_IDS.keys + ACTION_DOC_URLS.keys
end
attr_reader :namespace
end
end
# frozen_string_literal: true
module LearnGitlab
class Project
PROJECT_NAME = 'Learn GitLab'
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)
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
......@@ -4,6 +4,8 @@ module Sidebars
module Projects
module Menus
class LearnGitlabMenu < ::Sidebars::Menu
include Gitlab::Utils::StrongMemoize
override :link
def link
project_learn_gitlab_path(context.project)
......@@ -19,6 +21,20 @@ module Sidebars
_('Learn GitLab')
end
override :has_pill?
def has_pill?
context.learn_gitlab_experiment_enabled
end
override :pill_count
def pill_count
strong_memoize(:pill_count) do
percentage = LearnGitlab::Onboarding.new(context.project.namespace).completed_percentage
"#{percentage}%"
end
end
override :extra_container_html_options
def nav_link_html_options
{ class: 'home' }
......
......@@ -76,7 +76,7 @@ RSpec.describe Registrations::ExperienceLevelsController do
let(:learn_gitlab_available?) { true }
before do
allow_next_instance_of(LearnGitlab) do |learn_gitlab|
allow_next_instance_of(LearnGitlab::Project) do |learn_gitlab|
allow(learn_gitlab).to receive(:available?).and_return(learn_gitlab_available?)
allow(learn_gitlab).to receive(:project).and_return(project)
allow(learn_gitlab).to receive(:board).and_return(issues_board)
......@@ -136,7 +136,7 @@ RSpec.describe Registrations::ExperienceLevelsController do
let(:params) { super().merge(experience_level: :novice) }
before do
allow_next(LearnGitlab).to receive(:available?).and_return(false)
allow_next(LearnGitlab::Project).to receive(:available?).and_return(false)
end
it 'does not add a BoardLabel' do
......
......@@ -7,14 +7,14 @@ RSpec.describe LearnGitlabHelper do
include Devise::Test::ControllerHelpers
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, name: LearnGitlab::PROJECT_NAME, namespace: user.namespace) }
let_it_be(:project) { create(:project, name: LearnGitlab::Project::PROJECT_NAME, namespace: user.namespace) }
let_it_be(:namespace) { project.namespace }
before do
project.add_developer(user)
allow(helper).to receive(:user).and_return(user)
allow_next_instance_of(LearnGitlab) do |learn_gitlab|
allow_next_instance_of(LearnGitlab::Project) do |learn_gitlab|
allow(learn_gitlab).to receive(:project).and_return(project)
end
......@@ -41,12 +41,12 @@ RSpec.describe LearnGitlabHelper do
it 'sets correct path and completion status' do
expect(onboarding_actions_data[:git_write]).to eq({
url: project_issue_url(project, LearnGitlabHelper::ACTION_ISSUE_IDS[:git_write]),
url: project_issue_url(project, LearnGitlab::Onboarding::ACTION_ISSUE_IDS[:git_write]),
completed: true,
svg: helper.image_path("learn_gitlab/git_write.svg")
})
expect(onboarding_actions_data[:pipeline_created]).to eq({
url: project_issue_url(project, LearnGitlabHelper::ACTION_ISSUE_IDS[:pipeline_created]),
url: project_issue_url(project, LearnGitlab::Onboarding::ACTION_ISSUE_IDS[:pipeline_created]),
completed: false,
svg: helper.image_path("learn_gitlab/pipeline_created.svg")
})
......@@ -75,7 +75,7 @@ RSpec.describe LearnGitlabHelper do
before do
stub_experiment_for_subject(learn_gitlab_a: experiment_a, learn_gitlab_b: experiment_b)
allow(OnboardingProgress).to receive(:onboarding?).with(project.namespace).and_return(onboarding)
allow_next(LearnGitlab, user).to receive(:available?).and_return(learn_gitlab_available)
allow_next(LearnGitlab::Project, user).to receive(:available?).and_return(learn_gitlab_available)
end
context 'when signed in' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe LearnGitlab::Onboarding do
describe '#completed_percentage' do
let(:completed_actions) { {} }
let(:onboarding_progress) { build(:onboarding_progress, namespace: namespace, **completed_actions) }
let(:namespace) { build(:namespace) }
let_it_be(:tracked_action_columns) do
tracked_actions = described_class::ACTION_ISSUE_IDS.keys + described_class::ACTION_DOC_URLS.keys
tracked_actions.map { |key| OnboardingProgress.column_name(key) }
end
before do
expect(OnboardingProgress).to receive(:find_by).with(namespace: namespace).and_return(onboarding_progress)
end
subject { described_class.new(namespace).completed_percentage }
context 'when no onboarding_progress exists' do
let(:onboarding_progress) { nil }
it { is_expected.to eq(0) }
end
context 'when no action has been completed' do
it { is_expected.to eq(0) }
end
context 'when one action has been completed' do
let(:completed_actions) { Hash[tracked_action_columns.first, Time.current] }
it { is_expected.to eq(11) }
end
context 'when all tracked actions have been completed' do
let(:completed_actions) do
tracked_action_columns.to_h { |action| [action, Time.current] }
end
it { is_expected.to eq(100) }
end
end
end
......@@ -2,11 +2,11 @@
require 'spec_helper'
RSpec.describe LearnGitlab do
RSpec.describe LearnGitlab::Project do
let_it_be(:current_user) { create(:user) }
let_it_be(:learn_gitlab_project) { create(:project, name: LearnGitlab::PROJECT_NAME) }
let_it_be(:learn_gitlab_board) { create(:board, project: learn_gitlab_project, name: LearnGitlab::BOARD_NAME) }
let_it_be(:learn_gitlab_label) { create(:label, project: learn_gitlab_project, name: LearnGitlab::LABEL_NAME) }
let_it_be(:learn_gitlab_project) { create(:project, name: LearnGitlab::Project::PROJECT_NAME) }
let_it_be(:learn_gitlab_board) { create(:board, project: learn_gitlab_project, name: LearnGitlab::Project::BOARD_NAME) }
let_it_be(:learn_gitlab_label) { create(:label, project: learn_gitlab_project, name: LearnGitlab::Project::LABEL_NAME) }
before do
learn_gitlab_project.add_developer(current_user)
......
......@@ -28,4 +28,32 @@ RSpec.describe Sidebars::Projects::Menus::LearnGitlabMenu do
end
end
end
describe '#has_pill?' do
context 'when learn gitlab experiment is enabled' do
it 'returns true' do
expect(subject.has_pill?).to eq true
end
end
context 'when learn gitlab experiment is disabled' do
let(:experiment_enabled) { false }
it 'returns false' do
expect(subject.has_pill?).to eq false
end
end
end
describe '#pill_count' do
before do
expect_next_instance_of(LearnGitlab::Onboarding) do |onboarding|
expect(onboarding).to receive(:completed_percentage).and_return(20)
end
end
it 'returns pill count' do
expect(subject.pill_count).to eq '20%'
end
end
end
......@@ -56,6 +56,9 @@ RSpec.describe 'layouts/nav/sidebar/_project' do
describe 'Learn GitLab' do
it 'has a link to the learn GitLab experiment' do
allow(view).to receive(:learn_gitlab_experiment_enabled?).and_return(true)
allow_next_instance_of(LearnGitlab::Onboarding) do |onboarding|
expect(onboarding).to receive(:completed_percentage).and_return(20)
end
render
......
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