Commit b2ed5469 authored by Nicolas Dular's avatar Nicolas Dular

Show onboarding progress for learn-gitlab project

Shows a badge on the "Learn GitLab" project menu with the
completion percentage of the onboarding progress.
parent 9d49646c
......@@ -28,23 +28,9 @@ 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
......
# 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
......@@ -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' }
......
......@@ -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")
})
......
# 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
......@@ -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