Commit 750f979e authored by Pavel Shutsin's avatar Pavel Shutsin

Merge branch 'add-ios-specific-templates-experiment-class' into 'master'

Add ios specific templates experiment class

See merge request gitlab-org/gitlab!84589
parents 514a9cca 8d9d0f40
# frozen_string_literal: true
class IosSpecificTemplatesExperiment < ApplicationExperiment
before_run(if: :skip_experiment) { throw(:abort) } # rubocop:disable Cop/BanCatchThrow
private
def skip_experiment
actor_not_able_to_create_pipelines? ||
project_targets_non_ios_platforms? ||
project_has_gitlab_ci? ||
project_has_pipelines?
end
def actor_not_able_to_create_pipelines?
!context.actor.is_a?(User) || !context.actor.can?(:create_pipeline, context.project)
end
def project_targets_non_ios_platforms?
context.project.project_setting.target_platforms.exclude?('ios')
end
def project_has_gitlab_ci?
context.project.has_ci? && context.project.builds_enabled?
end
def project_has_pipelines?
context.project.all_pipelines.count > 0
end
end
...@@ -106,6 +106,12 @@ module Ci ...@@ -106,6 +106,12 @@ module Ci
e.candidate { data[:any_runners_available] = project.active_runners.exists?.to_s } e.candidate { data[:any_runners_available] = project.active_runners.exists?.to_s }
end end
experiment(:ios_specific_templates, actor: current_user, project: project, sticky_to: project) do |e|
e.candidate do
data[:registration_token] = project.runners_token if can?(current_user, :register_project_runners, project)
end
end
data data
end end
......
---
name: ios_specific_templates
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84589
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/356398
milestone: "14.10"
type: experiment
group: group::activation
default_enabled: false
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IosSpecificTemplatesExperiment do
subject do
described_class.new(actor: user, project: project) do |e|
e.candidate { true }
end.run
end
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :auto_devops_disabled) }
let!(:project_setting) { create(:project_setting, project: project, target_platforms: target_platforms) }
let(:target_platforms) { %w(ios) }
before do
stub_experiments(ios_specific_templates: :candidate)
project.add_developer(user) if user
end
it { is_expected.to be true }
describe 'skipping the experiment' do
context 'no actor' do
let_it_be(:user) { nil }
it { is_expected.to be_falsey }
end
context 'actor cannot create pipelines' do
before do
project.add_guest(user)
end
it { is_expected.to be_falsey }
end
context 'targeting a non iOS platform' do
let(:target_platforms) { [] }
it { is_expected.to be_falsey }
end
context 'project has a ci.yaml file' do
before do
allow(project).to receive(:has_ci?).and_return(true)
end
it { is_expected.to be_falsey }
end
context 'project has pipelines' do
before do
create(:ci_pipeline, project: project)
end
it { is_expected.to be_falsey }
end
end
end
...@@ -151,5 +151,46 @@ RSpec.describe Ci::PipelinesHelper do ...@@ -151,5 +151,46 @@ RSpec.describe Ci::PipelinesHelper do
end end
end end
end end
describe 'the `registration_token` attribute' do
subject { data[:registration_token] }
describe 'when the project is eligible for the `ios_specific_templates` experiment' do
let_it_be(:project) { create(:project, :auto_devops_disabled) }
let_it_be(:user) { create(:user) }
before do
allow(helper).to receive(:current_user).and_return(user)
project.add_developer(user)
create(:project_setting, project: project, target_platforms: %w(ios))
end
context 'when the `ios_specific_templates` experiment variant is control' do
before do
stub_experiments(ios_specific_templates: :control)
end
it { is_expected.to be_nil }
end
context 'when the `ios_specific_templates` experiment variant is candidate' do
before do
stub_experiments(ios_specific_templates: :candidate)
end
context 'when the user cannot register project runners' do
before do
allow(helper).to receive(:can?).with(user, :register_project_runners, project).and_return(false)
end
it { is_expected.to be_nil }
end
context 'when the user can register project runners' do
it { is_expected.to eq(project.runners_token) }
end
end
end
end
end end
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