Commit aadc1706 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'mwps-notify' into 'master'

Send an email when MR is set to MWPS

See merge request gitlab-org/gitlab!33460
parents e014ebc5 a59d719a
...@@ -92,6 +92,13 @@ module Emails ...@@ -92,6 +92,13 @@ module Emails
mail_answer_thread(@merge_request, merge_request_thread_options(resolved_by_user_id, recipient_id, reason)) mail_answer_thread(@merge_request, merge_request_thread_options(resolved_by_user_id, recipient_id, reason))
end end
def merge_when_pipeline_succeeds_email(recipient_id, merge_request_id, mwps_set_by_user_id, reason = nil)
setup_merge_request_mail(merge_request_id, recipient_id)
@mwps_set_by = ::User.find(mwps_set_by_user_id)
mail_answer_thread(@merge_request, merge_request_thread_options(mwps_set_by_user_id, recipient_id, reason))
end
private private
def setup_merge_request_mail(merge_request_id, recipient_id, present: false) def setup_merge_request_mail(merge_request_id, recipient_id, present: false)
......
...@@ -177,6 +177,10 @@ class NotifyPreview < ActionMailer::Preview ...@@ -177,6 +177,10 @@ class NotifyPreview < ActionMailer::Preview
Notify.service_desk_thank_you_email(issue.id).message Notify.service_desk_thank_you_email(issue.id).message
end end
def merge_when_pipeline_succeeds_email
Notify.merge_when_pipeline_succeeds_email(user.id, merge_request.id, user.id).message
end
private private
def project def project
......
...@@ -11,7 +11,7 @@ module AutoMerge ...@@ -11,7 +11,7 @@ module AutoMerge
yield if block_given? yield if block_given?
end end
# Notify the event that auto merge is enabled or merge param is updated notify(merge_request)
AutoMergeProcessWorker.perform_async(merge_request.id) AutoMergeProcessWorker.perform_async(merge_request.id)
strategy.to_sym strategy.to_sym
...@@ -62,6 +62,10 @@ module AutoMerge ...@@ -62,6 +62,10 @@ module AutoMerge
private private
# Overridden in child classes
def notify(merge_request)
end
def strategy def strategy
strong_memoize(:strategy) do strong_memoize(:strategy) do
self.class.name.demodulize.remove('Service').underscore self.class.name.demodulize.remove('Service').underscore
......
...@@ -34,5 +34,13 @@ module AutoMerge ...@@ -34,5 +34,13 @@ module AutoMerge
merge_request.actual_head_pipeline&.active? merge_request.actual_head_pipeline&.active?
end end
end end
private
def notify(merge_request)
return unless Feature.enabled?(:mwps_notification, project)
notification_service.async.merge_when_pipeline_succeeds(merge_request, current_user) if merge_request.saved_change_to_auto_merge_enabled?
end
end end
end end
...@@ -582,6 +582,14 @@ class NotificationService ...@@ -582,6 +582,14 @@ class NotificationService
end end
end end
def merge_when_pipeline_succeeds(merge_request, current_user)
recipients = ::NotificationRecipients::BuildService.build_recipients(merge_request, current_user, action: 'merge_when_pipeline_succeeds')
recipients.each do |recipient|
mailer.merge_when_pipeline_succeeds_email(recipient.user.id, merge_request.id, current_user.id).deliver_later
end
end
protected protected
def new_resource_email(target, method) def new_resource_email(target, method)
......
This diff is collapsed.
Merge Request #{@merge_request.to_reference} was scheduled to merge after pipeline succeeds by #{sanitize_name(@mwps_set_by.name)}
Merge Request url: #{project_merge_request_url(@merge_request.target_project, @merge_request)}
= merge_path_description(@merge_request, 'to')
Author: #{sanitize_name(@merge_request.author_name)}
= assignees_label(@merge_request)
...@@ -14528,6 +14528,9 @@ msgstr "" ...@@ -14528,6 +14528,9 @@ msgstr ""
msgid "Merge request dependencies" msgid "Merge request dependencies"
msgstr "" msgstr ""
msgid "Merge request was scheduled to merge after pipeline succeeds"
msgstr ""
msgid "Merge requests" msgid "Merge requests"
msgstr "" msgstr ""
...@@ -28884,6 +28887,9 @@ msgstr "" ...@@ -28884,6 +28887,9 @@ msgstr ""
msgid "vulnerability|dismissed" msgid "vulnerability|dismissed"
msgstr "" msgstr ""
msgid "was scheduled to merge after pipeline succeeds by"
msgstr ""
msgid "wiki page" msgid "wiki page"
msgstr "" msgstr ""
......
...@@ -17,4 +17,20 @@ RSpec.describe Emails::MergeRequests do ...@@ -17,4 +17,20 @@ RSpec.describe Emails::MergeRequests do
expect(subject).to have_body_text current_user.name expect(subject).to have_body_text current_user.name
end end
end end
describe "#merge_when_pipeline_succeeds_email" do
let(:user) { create(:user) }
let(:merge_request) { create(:merge_request) }
let(:current_user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:title) { "Merge request #{merge_request.to_reference} was scheduled to merge after pipeline succeeds by #{current_user.name}" }
subject { Notify.merge_when_pipeline_succeeds_email(user.id, merge_request.id, current_user.id) }
it "has required details" do
expect(subject).to have_content title
expect(subject).to have_content merge_request.to_reference
expect(subject).to have_content current_user.name
end
end
end end
...@@ -69,6 +69,7 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do ...@@ -69,6 +69,7 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do
before do before do
allow(merge_request) allow(merge_request)
.to receive_messages(head_pipeline: pipeline, actual_head_pipeline: pipeline) .to receive_messages(head_pipeline: pipeline, actual_head_pipeline: pipeline)
expect(MailScheduler::NotificationServiceWorker).to receive(:perform_async).with('merge_when_pipeline_succeeds', merge_request, user).once
service.execute(merge_request) service.execute(merge_request)
end end
...@@ -90,6 +91,18 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do ...@@ -90,6 +91,18 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do
end end
end end
context 'without feature enabled' do
it 'does not send notification' do
stub_feature_flags(mwps_notification: false)
allow(merge_request)
.to receive_messages(head_pipeline: pipeline, actual_head_pipeline: pipeline)
expect(MailScheduler::NotificationServiceWorker).not_to receive(:perform_async)
service.execute(merge_request)
end
end
context 'already approved' do context 'already approved' do
let(:service) { described_class.new(project, user, should_remove_source_branch: true) } let(:service) { described_class.new(project, user, should_remove_source_branch: true) }
let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch) } let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch) }
...@@ -106,6 +119,7 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do ...@@ -106,6 +119,7 @@ RSpec.describe AutoMerge::MergeWhenPipelineSucceedsService do
it 'updates the merge params' do it 'updates the merge params' do
expect(SystemNoteService).not_to receive(:merge_when_pipeline_succeeds) expect(SystemNoteService).not_to receive(:merge_when_pipeline_succeeds)
expect(MailScheduler::NotificationServiceWorker).not_to receive(:perform_async).with('merge_when_pipeline_succeeds', any_args)
service.execute(mr_merge_if_green_enabled) service.execute(mr_merge_if_green_enabled)
expect(mr_merge_if_green_enabled.merge_params).to have_key('should_remove_source_branch') expect(mr_merge_if_green_enabled.merge_params).to have_key('should_remove_source_branch')
......
...@@ -2023,6 +2023,26 @@ RSpec.describe NotificationService, :mailer do ...@@ -2023,6 +2023,26 @@ RSpec.describe NotificationService, :mailer do
let(:notification_trigger) { notification.resolve_all_discussions(merge_request, @u_disabled) } let(:notification_trigger) { notification.resolve_all_discussions(merge_request, @u_disabled) }
end end
end end
describe '#merge_when_pipeline_succeeds' do
it 'send notification that merge will happen when pipeline succeeds' do
notification.merge_when_pipeline_succeeds(merge_request, assignee)
should_email(merge_request.author)
should_email(@u_watcher)
should_email(@subscriber)
end
it_behaves_like 'participating notifications' do
let(:participant) { create(:user, username: 'user-participant') }
let(:issuable) { merge_request }
let(:notification_trigger) { notification.merge_when_pipeline_succeeds(merge_request, @u_disabled) }
end
it_behaves_like 'project emails are disabled' do
let(:notification_target) { merge_request }
let(:notification_trigger) { notification.merge_when_pipeline_succeeds(merge_request, @u_disabled) }
end
end
end end
describe 'Projects', :deliver_mails_inline do describe 'Projects', :deliver_mails_inline do
......
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