diff options
Diffstat (limited to 'spec/lib/gitlab/danger/roulette_spec.rb')
-rw-r--r-- | spec/lib/gitlab/danger/roulette_spec.rb | 265 |
1 files changed, 183 insertions, 82 deletions
diff --git a/spec/lib/gitlab/danger/roulette_spec.rb b/spec/lib/gitlab/danger/roulette_spec.rb index b6148cd1407..676edca2459 100644 --- a/spec/lib/gitlab/danger/roulette_spec.rb +++ b/spec/lib/gitlab/danger/roulette_spec.rb @@ -2,16 +2,23 @@ require 'fast_spec_helper' require 'webmock/rspec' +require 'timecop' require 'gitlab/danger/roulette' -describe Gitlab::Danger::Roulette do +RSpec.describe Gitlab::Danger::Roulette do + around do |example| + Timecop.freeze(Time.utc(2020, 06, 22, 10)) { example.run } + end + let(:backend_maintainer) do { username: 'backend-maintainer', name: 'Backend maintainer', role: 'Backend engineer', - projects: { 'gitlab' => 'maintainer backend' } + projects: { 'gitlab' => 'maintainer backend' }, + available: true, + tz_offset_hours: 2.0 } end let(:frontend_reviewer) do @@ -19,7 +26,9 @@ describe Gitlab::Danger::Roulette do username: 'frontend-reviewer', name: 'Frontend reviewer', role: 'Frontend engineer', - projects: { 'gitlab' => 'reviewer frontend' } + projects: { 'gitlab' => 'reviewer frontend' }, + available: true, + tz_offset_hours: 2.0 } end let(:frontend_maintainer) do @@ -27,7 +36,9 @@ describe Gitlab::Danger::Roulette do username: 'frontend-maintainer', name: 'Frontend maintainer', role: 'Frontend engineer', - projects: { 'gitlab' => "maintainer frontend" } + projects: { 'gitlab' => "maintainer frontend" }, + available: true, + tz_offset_hours: 2.0 } end let(:software_engineer_in_test) do @@ -38,7 +49,9 @@ describe Gitlab::Danger::Roulette do projects: { 'gitlab' => 'reviewer qa', 'gitlab-qa' => 'maintainer' - } + }, + available: true, + tz_offset_hours: 2.0 } end let(:engineering_productivity_reviewer) do @@ -46,7 +59,9 @@ describe Gitlab::Danger::Roulette do username: 'eng-prod-reviewer', name: 'EP engineer', role: 'Engineering Productivity', - projects: { 'gitlab' => 'reviewer backend' } + projects: { 'gitlab' => 'reviewer backend' }, + available: true, + tz_offset_hours: 2.0 } end @@ -73,10 +88,17 @@ describe Gitlab::Danger::Roulette do def matching_spin(category, reviewer: { username: nil }, maintainer: { username: nil }, optional: nil) satisfy do |spin| - spin.category == category && - spin.reviewer&.username == reviewer[:username] && - spin.maintainer&.username == maintainer[:username] && - spin.optional_role == optional + bool = spin.category == category + bool &&= spin.reviewer&.username == reviewer[:username] + + bool &&= + if maintainer + spin.maintainer&.username == maintainer[:username] + else + spin.maintainer.nil? + end + + bool && spin.optional_role == optional end end @@ -85,67 +107,114 @@ describe Gitlab::Danger::Roulette do let!(:branch_name) { 'a-branch' } let!(:mr_labels) { ['backend', 'devops::create'] } let!(:author) { Gitlab::Danger::Teammate.new('username' => 'filipa') } - - before do - [ - backend_maintainer, - frontend_reviewer, - frontend_maintainer, - software_engineer_in_test, - engineering_productivity_reviewer - ].each do |person| - stub_person_status(instance_double(Gitlab::Danger::Teammate, username: person[:username]), message: 'making GitLab magic') - end - + let(:timezone_experiment) { false } + let(:spins) do + # Stub the request at the latest time so that we can modify the raw data, e.g. available fields. WebMock .stub_request(:get, described_class::ROULETTE_DATA_URL) .to_return(body: teammate_json) + + subject.spin(project, categories, branch_name, timezone_experiment: timezone_experiment) + end + + before do allow(subject).to receive_message_chain(:gitlab, :mr_author).and_return(author.username) allow(subject).to receive_message_chain(:gitlab, :mr_labels).and_return(mr_labels) end - context 'when change contains backend category' do - it 'assigns backend reviewer and maintainer' do - categories = [:backend] - spins = subject.spin(project, categories, branch_name) + context 'when timezone_experiment == false' do + context 'when change contains backend category' do + let(:categories) { [:backend] } - expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + it 'assigns backend reviewer and maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + end + + context 'when teammate is not available' do + before do + backend_maintainer[:available] = false + end + + it 'assigns backend reviewer and no maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: nil)) + end + end end - end - context 'when change contains frontend category' do - it 'assigns frontend reviewer and maintainer' do - categories = [:frontend] - spins = subject.spin(project, categories, branch_name) + context 'when change contains frontend category' do + let(:categories) { [:frontend] } - expect(spins).to contain_exactly(matching_spin(:frontend, reviewer: frontend_reviewer, maintainer: frontend_maintainer)) + it 'assigns frontend reviewer and maintainer' do + expect(spins).to contain_exactly(matching_spin(:frontend, reviewer: frontend_reviewer, maintainer: frontend_maintainer)) + end end - end - context 'when change contains QA category' do - it 'assigns QA reviewer and sets optional QA maintainer' do - categories = [:qa] - spins = subject.spin(project, categories, branch_name) + context 'when change contains QA category' do + let(:categories) { [:qa] } - expect(spins).to contain_exactly(matching_spin(:qa, reviewer: software_engineer_in_test, optional: :maintainer)) + it 'assigns QA reviewer' do + expect(spins).to contain_exactly(matching_spin(:qa, reviewer: software_engineer_in_test)) + end end - end - context 'when change contains Engineering Productivity category' do - it 'assigns Engineering Productivity reviewer and fallback to backend maintainer' do - categories = [:engineering_productivity] - spins = subject.spin(project, categories, branch_name) + context 'when change contains Engineering Productivity category' do + let(:categories) { [:engineering_productivity] } - expect(spins).to contain_exactly(matching_spin(:engineering_productivity, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + it 'assigns Engineering Productivity reviewer and fallback to backend maintainer' do + expect(spins).to contain_exactly(matching_spin(:engineering_productivity, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + end + end + + context 'when change contains test category' do + let(:categories) { [:test] } + + it 'assigns corresponding SET' do + expect(spins).to contain_exactly(matching_spin(:test, reviewer: software_engineer_in_test)) + end end end - context 'when change contains test category' do - it 'assigns corresponding SET and sets optional test maintainer' do - categories = [:test] - spins = subject.spin(project, categories, branch_name) + context 'when timezone_experiment == true' do + let(:timezone_experiment) { true } + + context 'when change contains backend category' do + let(:categories) { [:backend] } + + it 'assigns backend reviewer and maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + end + + context 'when teammate is not in a good timezone' do + before do + backend_maintainer[:tz_offset_hours] = 5.0 + end - expect(spins).to contain_exactly(matching_spin(:test, reviewer: software_engineer_in_test, optional: :maintainer)) + it 'assigns backend reviewer and no maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: nil)) + end + end + end + + context 'when change includes a category with timezone disabled' do + let(:categories) { [:backend] } + + before do + stub_const("#{described_class}::INCLUDE_TIMEZONE_FOR_CATEGORY", backend: false) + end + + it 'assigns backend reviewer and maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + end + + context 'when teammate is not in a good timezone' do + before do + backend_maintainer[:tz_offset_hours] = 5.0 + end + + it 'assigns backend reviewer and maintainer' do + expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer)) + end + end end end end @@ -217,51 +286,83 @@ describe Gitlab::Danger::Roulette do end describe '#spin_for_person' do - let(:person1) { Gitlab::Danger::Teammate.new('username' => 'rymai') } - let(:person2) { Gitlab::Danger::Teammate.new('username' => 'godfat') } - let(:author) { Gitlab::Danger::Teammate.new('username' => 'filipa') } - let(:ooo) { Gitlab::Danger::Teammate.new('username' => 'jacopo-beschi') } - let(:no_capacity) { Gitlab::Danger::Teammate.new('username' => 'uncharged') } + let(:person_tz_offset_hours) { 0.0 } + let(:person1) do + Gitlab::Danger::Teammate.new( + 'username' => 'rymai', + 'available' => true, + 'tz_offset_hours' => person_tz_offset_hours + ) + end + let(:person2) do + Gitlab::Danger::Teammate.new( + 'username' => 'godfat', + 'available' => true, + 'tz_offset_hours' => person_tz_offset_hours) + end + let(:author) do + Gitlab::Danger::Teammate.new( + 'username' => 'filipa', + 'available' => true, + 'tz_offset_hours' => 0.0) + end + let(:unavailable) do + Gitlab::Danger::Teammate.new( + 'username' => 'jacopo-beschi', + 'available' => false, + 'tz_offset_hours' => 0.0) + end before do - stub_person_status(person1, message: 'making GitLab magic') - stub_person_status(person2, message: 'making GitLab magic') - stub_person_status(ooo, message: 'OOO till 15th') - stub_person_status(no_capacity, message: 'At capacity for the next few days', emoji: 'red_circle') - # we don't stub Filipa, as she is the author and - # we should not fire request checking for her - allow(subject).to receive_message_chain(:gitlab, :mr_author).and_return(author.username) end - it 'returns a random person' do - persons = [person1, person2] + (-4..4).each do |utc_offset| + context "when local hour for person is #{10 + utc_offset} (offset: #{utc_offset})" do + let(:person_tz_offset_hours) { utc_offset } - selected = subject.spin_for_person(persons, random: Random.new) + [false, true].each do |timezone_experiment| + context "with timezone_experiment == #{timezone_experiment}" do + it 'returns a random person' do + persons = [person1, person2] - expect(selected.username).to be_in(persons.map(&:username)) - end + selected = subject.spin_for_person(persons, random: Random.new, timezone_experiment: timezone_experiment) - it 'excludes OOO persons' do - expect(subject.spin_for_person([ooo], random: Random.new)).to be_nil + expect(selected.username).to be_in(persons.map(&:username)) + end + end + end + end end - it 'excludes mr.author' do - expect(subject.spin_for_person([author], random: Random.new)).to be_nil + ((-12..-5).to_a + (5..12).to_a).each do |utc_offset| + context "when local hour for person is #{10 + utc_offset} (offset: #{utc_offset})" do + let(:person_tz_offset_hours) { utc_offset } + + [false, true].each do |timezone_experiment| + context "with timezone_experiment == #{timezone_experiment}" do + it 'returns a random person or nil' do + persons = [person1, person2] + + selected = subject.spin_for_person(persons, random: Random.new, timezone_experiment: timezone_experiment) + + if timezone_experiment + expect(selected).to be_nil + else + expect(selected.username).to be_in(persons.map(&:username)) + end + end + end + end + end end - it 'excludes person with no capacity' do - expect(subject.spin_for_person([no_capacity], random: Random.new)).to be_nil + it 'excludes unavailable persons' do + expect(subject.spin_for_person([unavailable], random: Random.new)).to be_nil end - end - private - - def stub_person_status(person, message: 'dummy message', emoji: 'unicorn') - body = { message: message, emoji: emoji }.to_json - - WebMock - .stub_request(:get, "https://gitlab.com/api/v4/users/#{person.username}/status") - .to_return(body: body) + it 'excludes mr.author' do + expect(subject.spin_for_person([author], random: Random.new)).to be_nil + end end end |