Commit aa6b8c8c authored by Thong Kuah's avatar Thong Kuah

Merge branch 'introduce-auto-merge-process-worker' into 'master'

Introduce sidekiq worker for auto merge process

See merge request gitlab-org/gitlab-ce!28780
parents cfc70fbe 197a3d05
...@@ -166,6 +166,16 @@ module Ci ...@@ -166,6 +166,16 @@ module Ci
end end
end end
after_transition any => ::Ci::Pipeline.completed_statuses do |pipeline|
pipeline.run_after_commit do
pipeline.all_merge_requests.each do |merge_request|
next unless merge_request.auto_merge_enabled?
AutoMergeProcessWorker.perform_async(merge_request.id)
end
end
end
after_transition any => [:success, :failed] do |pipeline| after_transition any => [:success, :failed] do |pipeline|
pipeline.run_after_commit do pipeline.run_after_commit do
PipelineNotificationWorker.perform_async(pipeline.id) PipelineNotificationWorker.perform_async(pipeline.id)
......
--- ---
- auto_devops:auto_devops_disable - auto_devops:auto_devops_disable
- auto_merge:auto_merge_process
- cronjob:admin_email - cronjob:admin_email
- cronjob:expire_build_artifacts - cronjob:expire_build_artifacts
- cronjob:gitlab_usage_ping - cronjob:gitlab_usage_ping
......
# frozen_string_literal: true
class AutoMergeProcessWorker
include ApplicationWorker
queue_namespace :auto_merge
def perform(merge_request_id)
MergeRequest.find_by_id(merge_request_id).try do |merge_request|
AutoMergeService.new(merge_request.project, merge_request.merge_user)
.process(merge_request)
end
end
end
...@@ -6,14 +6,7 @@ class PipelineSuccessWorker ...@@ -6,14 +6,7 @@ class PipelineSuccessWorker
queue_namespace :pipeline_processing queue_namespace :pipeline_processing
# rubocop: disable CodeReuse/ActiveRecord
def perform(pipeline_id) def perform(pipeline_id)
Ci::Pipeline.find_by(id: pipeline_id).try do |pipeline| # no-op
pipeline.all_merge_requests.preload(:merge_user).each do |merge_request|
AutoMergeService.new(pipeline.project, merge_request.merge_user)
.process(merge_request)
end end
end
end
# rubocop: enable CodeReuse/ActiveRecord
end end
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
- [pipeline_default, 3] - [pipeline_default, 3]
- [pipeline_cache, 3] - [pipeline_cache, 3]
- [deployment, 3] - [deployment, 3]
- [auto_merge, 3]
- [pipeline_hooks, 2] - [pipeline_hooks, 2]
- [gitlab_shell, 2] - [gitlab_shell, 2]
- [email_receiver, 2] - [email_receiver, 2]
......
...@@ -1381,6 +1381,40 @@ describe Ci::Pipeline, :mailer do ...@@ -1381,6 +1381,40 @@ describe Ci::Pipeline, :mailer do
end end
end end
describe 'auto merge' do
let(:merge_request) { create(:merge_request, :merge_when_pipeline_succeeds) }
let(:pipeline) do
create(:ci_pipeline, :running, project: merge_request.source_project,
ref: merge_request.source_branch,
sha: merge_request.diff_head_sha)
end
before do
merge_request.update_head_pipeline
end
%w[succeed! drop! cancel! skip!].each do |action|
context "when the pipeline recieved #{action} event" do
it 'performs AutoMergeProcessWorker' do
expect(AutoMergeProcessWorker).to receive(:perform_async).with(merge_request.id)
pipeline.public_send(action)
end
end
end
context 'when auto merge is not enabled in the merge request' do
let(:merge_request) { create(:merge_request) }
it 'performs AutoMergeProcessWorker' do
expect(AutoMergeProcessWorker).not_to receive(:perform_async)
pipeline.succeed!
end
end
end
def create_build(name, *traits, queued_at: current, started_from: 0, **opts) def create_build(name, *traits, queued_at: current, started_from: 0, **opts)
create(:ci_build, *traits, create(:ci_build, *traits,
name: name, name: name,
......
...@@ -2,25 +2,29 @@ ...@@ -2,25 +2,29 @@
require 'spec_helper' require 'spec_helper'
describe PipelineSuccessWorker do describe AutoMergeProcessWorker do
describe '#perform' do describe '#perform' do
context 'when pipeline exists' do subject { described_class.new.perform(merge_request&.id) }
let(:pipeline) { create(:ci_pipeline, status: 'success', ref: merge_request.source_branch, project: merge_request.source_project) }
context 'when merge request is found' do
let(:merge_request) { create(:merge_request) } let(:merge_request) { create(:merge_request) }
it 'performs "merge when pipeline succeeds"' do it 'executes AutoMergeService' do
expect_next_instance_of(AutoMergeService) do |auto_merge| expect_next_instance_of(AutoMergeService) do |auto_merge|
expect(auto_merge).to receive(:process) expect(auto_merge).to receive(:process)
end end
described_class.new.perform(pipeline.id) subject
end end
end end
context 'when pipeline does not exist' do context 'when merge request is not found' do
it 'does not raise exception' do let(:merge_request) { nil }
expect { described_class.new.perform(123) }
.not_to raise_error it 'does not execute AutoMergeService' do
expect(AutoMergeService).not_to receive(:new)
subject
end end
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