Commit 8cca6c83 authored by Grzegorz Bizon's avatar Grzegorz Bizon

DRY creating groups of common builds in a stage

parent f89f232d
...@@ -31,6 +31,14 @@ module Ci ...@@ -31,6 +31,14 @@ module Ci
end end
end end
def self.fabricate(stage)
stage.statuses.ordered.latest
.sort_by(&:sortable_name).group_by(&:group_name)
.map do |group_name, grouped_statuses|
self.new(stage, name: group_name, jobs: grouped_statuses)
end
end
private private
def commit_statuses def commit_statuses
......
...@@ -16,11 +16,7 @@ module Ci ...@@ -16,11 +16,7 @@ module Ci
end end
def groups def groups
@groups ||= statuses.ordered.latest @groups ||= Ci::Group.fabricate(self)
.sort_by(&:sortable_name).group_by(&:group_name)
.map do |group_name, grouped_statuses|
Ci::Group.new(self, name: group_name, jobs: grouped_statuses)
end
end end
def to_param def to_param
......
...@@ -81,11 +81,7 @@ module Ci ...@@ -81,11 +81,7 @@ module Ci
end end
def groups def groups
@groups ||= statuses.ordered.latest @groups ||= Ci::Group.fabricate(self)
.sort_by(&:sortable_name).group_by(&:group_name)
.map do |group_name, grouped_statuses|
Ci::Group.new(self, name: group_name, jobs: grouped_statuses)
end
end end
def has_warnings? def has_warnings?
......
...@@ -41,4 +41,55 @@ describe Ci::Group do ...@@ -41,4 +41,55 @@ describe Ci::Group do
end end
end end
end end
describe '.fabricate' do
let(:pipeline) { create(:ci_empty_pipeline) }
let(:stage) { create(:ci_stage_entity, pipeline: pipeline) }
before do
create_build(:ci_build, name: 'rspec 0 2')
create_build(:ci_build, name: 'rspec 0 1')
create_build(:ci_build, name: 'spinach 0 1')
create_build(:commit_status, name: 'aaaaa')
end
it 'returns an array of three groups' do
expect(stage.groups).to be_a Array
expect(stage.groups).to all(be_a Ci::Group)
expect(stage.groups.size).to eq 3
end
it 'returns groups with correctly ordered statuses' do
expect(stage.groups.first.jobs.map(&:name))
.to eq ['aaaaa']
expect(stage.groups.second.jobs.map(&:name))
.to eq ['rspec 0 1', 'rspec 0 2']
expect(stage.groups.third.jobs.map(&:name))
.to eq ['spinach 0 1']
end
it 'returns groups with correct names' do
expect(stage.groups.map(&:name))
.to eq %w[aaaaa rspec spinach]
end
context 'when a name is nil on legacy pipelines' do
before do
pipeline.builds.first.update_attribute(:name, nil)
end
it 'returns an array of three groups' do
expect(stage.groups.map(&:name))
.to eq ['', 'aaaaa', 'rspec', 'spinach']
end
end
def create_build(type, status: 'success', **opts)
create(type, pipeline: pipeline,
stage: stage.name,
status: status,
stage_id: stage.id,
**opts)
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