2019-05-29 10:38:26 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'webmock/rspec'
|
2020-07-07 17:09:13 -04:00
|
|
|
require 'timecop'
|
2019-05-29 10:38:26 -04:00
|
|
|
|
|
|
|
require 'gitlab/danger/roulette'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Danger::Roulette do
|
2020-07-07 17:09:13 -04:00
|
|
|
around do |example|
|
|
|
|
Timecop.freeze(Time.utc(2020, 06, 22, 10)) { example.run }
|
|
|
|
end
|
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
let(:backend_maintainer) do
|
|
|
|
{
|
|
|
|
username: 'backend-maintainer',
|
|
|
|
name: 'Backend maintainer',
|
|
|
|
role: 'Backend engineer',
|
2020-06-17 14:09:08 -04:00
|
|
|
projects: { 'gitlab' => 'maintainer backend' },
|
|
|
|
available: true,
|
2020-07-07 17:09:13 -04:00
|
|
|
tz_offset_hours: 2.0
|
2020-06-15 08:08:44 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:frontend_reviewer) do
|
|
|
|
{
|
|
|
|
username: 'frontend-reviewer',
|
|
|
|
name: 'Frontend reviewer',
|
|
|
|
role: 'Frontend engineer',
|
2020-06-17 14:09:08 -04:00
|
|
|
projects: { 'gitlab' => 'reviewer frontend' },
|
|
|
|
available: true,
|
2020-07-07 17:09:13 -04:00
|
|
|
tz_offset_hours: 2.0
|
2020-06-15 08:08:44 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:frontend_maintainer) do
|
|
|
|
{
|
|
|
|
username: 'frontend-maintainer',
|
|
|
|
name: 'Frontend maintainer',
|
|
|
|
role: 'Frontend engineer',
|
2020-06-17 14:09:08 -04:00
|
|
|
projects: { 'gitlab' => "maintainer frontend" },
|
|
|
|
available: true,
|
2020-07-07 17:09:13 -04:00
|
|
|
tz_offset_hours: 2.0
|
2020-06-15 08:08:44 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:software_engineer_in_test) do
|
|
|
|
{
|
|
|
|
username: 'software-engineer-in-test',
|
|
|
|
name: 'Software Engineer in Test',
|
|
|
|
role: 'Software Engineer in Test, Create:Source Code',
|
|
|
|
projects: {
|
|
|
|
'gitlab' => 'reviewer qa',
|
|
|
|
'gitlab-qa' => 'maintainer'
|
2020-06-17 14:09:08 -04:00
|
|
|
},
|
|
|
|
available: true,
|
2020-07-07 17:09:13 -04:00
|
|
|
tz_offset_hours: 2.0
|
2020-06-15 08:08:44 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:engineering_productivity_reviewer) do
|
|
|
|
{
|
|
|
|
username: 'eng-prod-reviewer',
|
|
|
|
name: 'EP engineer',
|
|
|
|
role: 'Engineering Productivity',
|
2020-06-17 14:09:08 -04:00
|
|
|
projects: { 'gitlab' => 'reviewer backend' },
|
|
|
|
available: true,
|
2020-07-07 17:09:13 -04:00
|
|
|
tz_offset_hours: 2.0
|
2020-06-15 08:08:44 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-29 10:38:26 -04:00
|
|
|
let(:teammate_json) do
|
|
|
|
[
|
2020-06-15 08:08:44 -04:00
|
|
|
backend_maintainer,
|
|
|
|
frontend_maintainer,
|
|
|
|
frontend_reviewer,
|
|
|
|
software_engineer_in_test,
|
|
|
|
engineering_productivity_reviewer
|
|
|
|
].to_json
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
subject(:roulette) { Object.new.extend(described_class) }
|
|
|
|
|
|
|
|
def matching_teammate(person)
|
2019-05-29 10:38:26 -04:00
|
|
|
satisfy do |teammate|
|
2020-06-15 08:08:44 -04:00
|
|
|
teammate.username == person[:username] &&
|
|
|
|
teammate.name == person[:name] &&
|
|
|
|
teammate.role == person[:role] &&
|
|
|
|
teammate.projects == person[:projects]
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
def matching_spin(category, reviewer: { username: nil }, maintainer: { username: nil }, optional: nil)
|
|
|
|
satisfy do |spin|
|
2020-06-17 14:09:08 -04:00
|
|
|
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
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
describe '#spin' do
|
|
|
|
let!(:project) { 'gitlab' }
|
|
|
|
let!(:branch_name) { 'a-branch' }
|
|
|
|
let!(:mr_labels) { ['backend', 'devops::create'] }
|
|
|
|
let!(:author) { Gitlab::Danger::Teammate.new('username' => 'filipa') }
|
2020-07-07 17:09:13 -04:00
|
|
|
let(:timezone_experiment) { false }
|
2020-06-17 14:09:08 -04:00
|
|
|
let(:spins) do
|
2020-07-07 17:09:13 -04:00
|
|
|
# Stub the request at the latest time so that we can modify the raw data, e.g. available fields.
|
2020-06-15 08:08:44 -04:00
|
|
|
WebMock
|
|
|
|
.stub_request(:get, described_class::ROULETTE_DATA_URL)
|
|
|
|
.to_return(body: teammate_json)
|
2020-06-17 14:09:08 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
subject.spin(project, categories, branch_name, timezone_experiment: timezone_experiment)
|
2020-06-17 14:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-06-15 08:08:44 -04:00
|
|
|
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
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when timezone_experiment == false' do
|
|
|
|
context 'when change contains backend category' do
|
|
|
|
let(:categories) { [:backend] }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
it 'assigns backend reviewer and maintainer' do
|
|
|
|
expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: backend_maintainer))
|
2020-06-17 14:09:08 -04:00
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
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
|
2020-06-17 14:09:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when change contains frontend category' do
|
|
|
|
let(:categories) { [:frontend] }
|
2020-06-17 14:09:08 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
it 'assigns frontend reviewer and maintainer' do
|
|
|
|
expect(spins).to contain_exactly(matching_spin(:frontend, reviewer: frontend_reviewer, maintainer: frontend_maintainer))
|
2020-06-17 14:09:08 -04:00
|
|
|
end
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when change contains QA category' do
|
|
|
|
let(:categories) { [:qa] }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
it 'assigns QA reviewer' do
|
|
|
|
expect(spins).to contain_exactly(matching_spin(:qa, reviewer: software_engineer_in_test))
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when change contains Engineering Productivity category' do
|
|
|
|
let(:categories) { [:engineering_productivity] }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
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
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when change contains test category' do
|
|
|
|
let(:categories) { [:test] }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
it 'assigns corresponding SET' do
|
|
|
|
expect(spins).to contain_exactly(matching_spin(:test, reviewer: software_engineer_in_test))
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
context 'when timezone_experiment == true' do
|
|
|
|
let(:timezone_experiment) { true }
|
|
|
|
|
|
|
|
context 'when change contains backend category' do
|
|
|
|
let(:categories) { [:backend] }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
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 no maintainer' do
|
|
|
|
expect(spins).to contain_exactly(matching_spin(:backend, reviewer: engineering_productivity_reviewer, maintainer: nil))
|
|
|
|
end
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
2020-07-14 14:09:55 -04:00
|
|
|
|
|
|
|
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
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
|
|
|
end
|
2019-05-29 10:38:26 -04:00
|
|
|
|
|
|
|
describe '#team' do
|
|
|
|
subject(:team) { roulette.team }
|
|
|
|
|
|
|
|
context 'HTTP failure' do
|
|
|
|
before do
|
|
|
|
WebMock
|
|
|
|
.stub_request(:get, described_class::ROULETTE_DATA_URL)
|
|
|
|
.to_return(status: 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises a pretty error' do
|
|
|
|
expect { team }.to raise_error(/Failed to read/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'JSON failure' do
|
|
|
|
before do
|
|
|
|
WebMock
|
|
|
|
.stub_request(:get, described_class::ROULETTE_DATA_URL)
|
|
|
|
.to_return(body: 'INVALID JSON')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises a pretty error' do
|
|
|
|
expect { team }.to raise_error(/Failed to parse/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
before do
|
|
|
|
WebMock
|
|
|
|
.stub_request(:get, described_class::ROULETTE_DATA_URL)
|
|
|
|
.to_return(body: teammate_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an array of teammates' do
|
2020-06-15 08:08:44 -04:00
|
|
|
expected_teammates = [
|
|
|
|
matching_teammate(backend_maintainer),
|
|
|
|
matching_teammate(frontend_reviewer),
|
|
|
|
matching_teammate(frontend_maintainer),
|
|
|
|
matching_teammate(software_engineer_in_test),
|
|
|
|
matching_teammate(engineering_productivity_reviewer)
|
|
|
|
]
|
|
|
|
|
|
|
|
is_expected.to contain_exactly(*expected_teammates)
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'memoizes the result' do
|
|
|
|
expect(team.object_id).to eq(roulette.team.object_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#project_team' do
|
2020-06-15 08:08:44 -04:00
|
|
|
subject { roulette.project_team('gitlab-qa') }
|
2019-05-29 10:38:26 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
WebMock
|
|
|
|
.stub_request(:get, described_class::ROULETTE_DATA_URL)
|
|
|
|
.to_return(body: teammate_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters team by project_name' do
|
2020-06-15 08:08:44 -04:00
|
|
|
is_expected.to contain_exactly(matching_teammate(software_engineer_in_test))
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|
|
|
|
end
|
2019-05-30 06:50:40 -04:00
|
|
|
|
|
|
|
describe '#spin_for_person' do
|
2020-07-07 17:09:13 -04:00
|
|
|
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
|
2019-05-30 06:50:40 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(subject).to receive_message_chain(:gitlab, :mr_author).and_return(author.username)
|
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
(-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 }
|
|
|
|
|
|
|
|
[false, true].each do |timezone_experiment|
|
|
|
|
context "with timezone_experiment == #{timezone_experiment}" do
|
|
|
|
it 'returns a random person' do
|
|
|
|
persons = [person1, person2]
|
|
|
|
|
|
|
|
selected = subject.spin_for_person(persons, random: Random.new, timezone_experiment: timezone_experiment)
|
|
|
|
|
|
|
|
expect(selected.username).to be_in(persons.map(&:username))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
((-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]
|
2019-05-30 06:50:40 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
selected = subject.spin_for_person(persons, random: Random.new, timezone_experiment: timezone_experiment)
|
2019-05-30 06:50:40 -04:00
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
if timezone_experiment
|
|
|
|
expect(selected).to be_nil
|
|
|
|
else
|
|
|
|
expect(selected.username).to be_in(persons.map(&:username))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-30 06:50:40 -04:00
|
|
|
end
|
|
|
|
|
2020-07-07 17:09:13 -04:00
|
|
|
it 'excludes unavailable persons' do
|
|
|
|
expect(subject.spin_for_person([unavailable], random: Random.new)).to be_nil
|
2019-05-30 06:50:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes mr.author' do
|
|
|
|
expect(subject.spin_for_person([author], random: Random.new)).to be_nil
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|
2019-05-29 10:38:26 -04:00
|
|
|
end
|