diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-06-02 12:16:11 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-06-02 12:16:11 +0200 |
commit | fe0b2f81c7c9680a11288e0cdffc3e80dc1e8d58 (patch) | |
tree | fd19bea3c1118cb7438f0d2476bad322ccb5c421 /lib | |
parent | aa0d6b07b6a87459e75c69111643b9d6fe8a97c2 (diff) | |
download | gitlab-ce-fe0b2f81c7c9680a11288e0cdffc3e80dc1e8d58.tar.gz |
Refine implementation of pipeline stage seeds
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 15 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seed.rb | 49 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seeds.rb | 62 |
3 files changed, 58 insertions, 68 deletions
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 4b0e87a945b..22af2671b18 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -50,14 +50,17 @@ module Ci end end - def stage_seeds(ref:, tag: false, trigger: nil) - Gitlab::Ci::Stage::Seeds.new.tap do |seeds| - @stages.uniq.each do |stage| - builds = builds_for_stage_and_ref(stage, ref, tag, trigger) + def stage_seeds(pipeline) + trigger_request = pipeline.trigger_requests.first - seeds.append_stage(stage, builds) if builds.any? - end + seeds = @stages.uniq.map do |stage| + builds = builds_for_stage_and_ref( + stage, pipeline.ref, pipeline.tag?, trigger_request) + + Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any? end + + seeds.compact end def build_attributes(name) diff --git a/lib/gitlab/ci/stage/seed.rb b/lib/gitlab/ci/stage/seed.rb new file mode 100644 index 00000000000..f81f9347b4d --- /dev/null +++ b/lib/gitlab/ci/stage/seed.rb @@ -0,0 +1,49 @@ +module Gitlab + module Ci + module Stage + class Seed + attr_reader :pipeline + delegate :project, to: :pipeline + + def initialize(pipeline, stage, jobs) + @pipeline = pipeline + @stage = { name: stage } + @jobs = jobs.to_a.dup + end + + def user=(current_user) + @jobs.map! do |attributes| + attributes.merge(user: current_user) + end + end + + def stage + @stage.merge(project: project) + end + + def builds + trigger = pipeline.trigger_requests.first + + @jobs.map do |attributes| + attributes.merge(project: project, + ref: pipeline.ref, + tag: pipeline.tag, + trigger_request: trigger) + end + end + + def create! + pipeline.stages.create!(stage).tap do |stage| + builds_attributes = builds.map do |attributes| + attributes.merge(stage_id: stage.id) + end + + pipeline.builds.create!(builds_attributes).each do |build| + yield build if block_given? + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/stage/seeds.rb b/lib/gitlab/ci/stage/seeds.rb deleted file mode 100644 index cb5174a166b..00000000000 --- a/lib/gitlab/ci/stage/seeds.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Gitlab - module Ci - module Stage - class Seeds - Seed = Struct.new(:stage, :jobs) - - def initialize - @stages = [] - end - - def has_stages? - @stages.any? - end - - def stages - @stages.map(&:stage) - end - - def jobs - @stages.map(&:jobs).flatten - end - - def append_stage(stage, jobs) - @stages << Seed.new({ name: stage }, jobs) - end - - def pipeline=(pipeline) - trigger_request = pipeline.trigger_requests.first - - stages.each do |attributes| - attributes.merge!( - pipeline: pipeline, - project: pipeline.project, - ) - end - - jobs.each do |attributes| - attributes.merge!( - pipeline: pipeline, - project: pipeline.project, - ref: pipeline.ref, - tag: pipeline.tag, - trigger_request: trigger_request - ) - end - end - - def user=(current_user) - jobs.each do |attributes| - attributes.merge!(user: current_user) - end - end - - def to_attributes - @stages.map do |seed| - seed.stage.merge(builds_attributes: seed.jobs) - end - end - end - end - end -end |