Commit 21724407 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move MWBS trigger from build to pipeline event

parent 0bbeff3d
...@@ -56,6 +56,10 @@ module Ci ...@@ -56,6 +56,10 @@ module Ci
pipeline.finished_at = Time.now pipeline.finished_at = Time.now
end end
before_transition do |pipeline|
pipeline.update_duration
end
after_transition [:created, :pending] => :running do |pipeline| after_transition [:created, :pending] => :running do |pipeline|
MergeRequest::Metrics.where(merge_request_id: pipeline.merge_requests.map(&:id)). MergeRequest::Metrics.where(merge_request_id: pipeline.merge_requests.map(&:id)).
update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: nil) update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: nil)
...@@ -66,8 +70,8 @@ module Ci ...@@ -66,8 +70,8 @@ module Ci
update_all(latest_build_finished_at: pipeline.finished_at) update_all(latest_build_finished_at: pipeline.finished_at)
end end
before_transition do |pipeline| after_transition [:created, :pending, :running] => :success do |pipeline|
pipeline.update_duration MergeRequests::MergeWhenBuildSucceedsService.new(pipeline.project, nil).trigger(pipeline)
end end
after_transition do |pipeline, transition| after_transition do |pipeline, transition|
...@@ -292,11 +296,9 @@ module Ci ...@@ -292,11 +296,9 @@ module Ci
# Merge requests for which the current pipeline is running against # Merge requests for which the current pipeline is running against
# the merge request's latest commit. # the merge request's latest commit.
def merge_requests def merge_requests
@merge_requests ||= @merge_requests ||= project.merge_requests
begin .where(source_branch: self.ref)
project.merge_requests.where(source_branch: self.ref). .select { |merge_request| merge_request.pipeline.try(:id) == self.id }
select { |merge_request| merge_request.pipeline.try(:id) == self.id }
end
end end
private private
......
...@@ -98,10 +98,6 @@ class CommitStatus < ActiveRecord::Base ...@@ -98,10 +98,6 @@ class CommitStatus < ActiveRecord::Base
true true
end end
after_transition [:created, :pending, :running] => :success do |commit_status|
MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
end
after_transition any => :failed do |commit_status| after_transition any => :failed do |commit_status|
MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status) MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status)
end end
......
...@@ -2,14 +2,14 @@ module MergeRequests ...@@ -2,14 +2,14 @@ module MergeRequests
class AddTodoWhenBuildFailsService < MergeRequests::BaseService class AddTodoWhenBuildFailsService < MergeRequests::BaseService
# Adds a todo to the parent merge_request when a CI build fails # Adds a todo to the parent merge_request when a CI build fails
def execute(commit_status) def execute(commit_status)
each_merge_request(commit_status) do |merge_request| commit_status_merge_requests(commit_status) do |merge_request|
todo_service.merge_request_build_failed(merge_request) todo_service.merge_request_build_failed(merge_request)
end end
end end
# Closes any pending build failed todos for the parent MRs when a build is retried # Closes any pending build failed todos for the parent MRs when a build is retried
def close(commit_status) def close(commit_status)
each_merge_request(commit_status) do |merge_request| commit_status_merge_requests(commit_status) do |merge_request|
todo_service.merge_request_build_retried(merge_request) todo_service.merge_request_build_retried(merge_request)
end end
end end
......
...@@ -42,11 +42,9 @@ module MergeRequests ...@@ -42,11 +42,9 @@ module MergeRequests
super(:merge_request) super(:merge_request)
end end
def merge_request_from(commit_status) def merge_requests_for(branches, sha)
branches = commit_status.ref
# This is for ref-less builds # This is for ref-less builds
branches ||= @project.repository.branch_names_contains(commit_status.sha) branches ||= @project.repository.branch_names_contains(sha)
return [] if branches.blank? return [] if branches.blank?
...@@ -56,14 +54,15 @@ module MergeRequests ...@@ -56,14 +54,15 @@ module MergeRequests
merge_requests.uniq.select(&:source_project) merge_requests.uniq.select(&:source_project)
end end
def each_merge_request(commit_status) def pipeline_merge_requests(pipeline)
merge_request_from(commit_status).each do |merge_request| merge_requests_for(pipeline.ref, pipeline.sha).each do |merge_request|
pipeline = merge_request.pipeline yield merge_request
end
next unless pipeline end
next unless pipeline.sha == commit_status.sha
yield merge_request, pipeline def commit_status_merge_requests(commit_status)
merge_requests_for(commit_status.ref, commit_status.sha).each do |merge_request|
yield merge_request
end end
end end
end end
......
...@@ -18,12 +18,13 @@ module MergeRequests ...@@ -18,12 +18,13 @@ module MergeRequests
merge_request.save merge_request.save
end end
# Triggers the automatic merge of merge_request once the build succeeds # Triggers the automatic merge of merge_request once the pipeline succeeds
def trigger(commit_status) def trigger(pipeline)
each_merge_request(commit_status) do |merge_request, pipeline| return unless pipeline.success?
pipeline_merge_requests(pipeline) do |merge_request|
next unless merge_request.merge_when_build_succeeds? next unless merge_request.merge_when_build_succeeds?
next unless merge_request.mergeable? next unless merge_request.mergeable?
next unless pipeline.success?
MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params) MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params)
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