Commit 4b6e6d0a authored by Robert Speicher's avatar Robert Speicher

Merge branch 'issue_13343' into 'master'

Allow build email service to be tested

Fixes #13343 

See merge request !5223
parents 9065f9c5 2e9d5358
...@@ -26,6 +26,7 @@ v 8.10.0 (unreleased) ...@@ -26,6 +26,7 @@ v 8.10.0 (unreleased)
- Add a new column `artifacts_size` to table `ci_builds` !4964 - Add a new column `artifacts_size` to table `ci_builds` !4964
- Let Workhorse serve format-patch diffs - Let Workhorse serve format-patch diffs
- Display tooltip for mentioned users and groups !5261 (winniehell) - Display tooltip for mentioned users and groups !5261 (winniehell)
- Allow build email service to be tested
- Added day name to contribution calendar tooltips - Added day name to contribution calendar tooltips
- Make images fit to the size of the viewport !4810 - Make images fit to the size of the viewport !4810
- Fix check for New Branch button on Issue page !4630 (winniehell) - Fix check for New Branch button on Issue page !4630 (winniehell)
......
...@@ -45,8 +45,9 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -45,8 +45,9 @@ class Projects::ServicesController < Projects::ApplicationController
end end
def test def test
data = Gitlab::PushDataBuilder.build_sample(project, current_user) data = @service.test_data(project, current_user)
outcome = @service.test(data) outcome = @service.test(data)
if outcome[:success] if outcome[:success]
message = { notice: 'We sent a request to the provided URL' } message = { notice: 'We sent a request to the provided URL' }
else else
......
...@@ -6,6 +6,7 @@ module Emails ...@@ -6,6 +6,7 @@ module Emails
add_project_headers add_project_headers
add_build_headers('failed') add_build_headers('failed')
mail(to: to, subject: subject("Build failed for #{@project.name}", @build.short_sha)) mail(to: to, subject: subject("Build failed for #{@project.name}", @build.short_sha))
end end
......
...@@ -42,6 +42,19 @@ class BuildsEmailService < Service ...@@ -42,6 +42,19 @@ class BuildsEmailService < Service
end end
end end
def can_test?
project.builds.count > 0
end
def disabled_title
"Please setup a build on your repository."
end
def test_data(project = nil, user = nil)
build = project.builds.last
Gitlab::BuildDataBuilder.build(build)
end
def fields def fields
[ [
{ type: 'textarea', name: 'recipients', placeholder: 'Emails separated by comma' }, { type: 'textarea', name: 'recipients', placeholder: 'Emails separated by comma' },
...@@ -50,6 +63,20 @@ class BuildsEmailService < Service ...@@ -50,6 +63,20 @@ class BuildsEmailService < Service
] ]
end end
def test(data)
begin
# bypass build status verification when testing
data[:build_status] = "failed"
data[:build_allow_failure] = false
result = execute(data)
rescue StandardError => error
return { success: false, result: error }
end
{ success: true, result: result }
end
def should_build_be_notified?(data) def should_build_be_notified?(data)
case data[:build_status] case data[:build_status]
when 'success' when 'success'
......
...@@ -76,6 +76,10 @@ class Service < ActiveRecord::Base ...@@ -76,6 +76,10 @@ class Service < ActiveRecord::Base
[] []
end end
def test_data(project, user)
Gitlab::PushDataBuilder.build_sample(project, user)
end
def supported_events def supported_events
%w(push tag_push issue merge_request wiki_page) %w(push tag_push issue merge_request wiki_page)
end end
...@@ -94,6 +98,11 @@ class Service < ActiveRecord::Base ...@@ -94,6 +98,11 @@ class Service < ActiveRecord::Base
!project.empty_repo? !project.empty_repo?
end end
# reason why service cannot be tested
def disabled_title
"Please setup a project repository."
end
# Provide convenient accessor methods # Provide convenient accessor methods
# for each serialized property. # for each serialized property.
# Also keep track of updated properties in a similar way as ActiveModel::Dirty # Also keep track of updated properties in a similar way as ActiveModel::Dirty
......
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
&nbsp; &nbsp;
- if @service.valid? && @service.activated? - if @service.valid? && @service.activated?
- disabled = @service.can_test? ? '':'disabled' - disabled = @service.can_test? ? '':'disabled'
= link_to 'Test settings', test_namespace_project_service_path(@project.namespace, @project, @service.to_param), class: "btn #{disabled}" = link_to 'Test settings', test_namespace_project_service_path(@project.namespace, @project, @service), class: "btn #{disabled}", title: @service.disabled_title
= link_to "Cancel", namespace_project_services_path(@project.namespace, @project), class: "btn btn-cancel" = link_to "Cancel", namespace_project_services_path(@project.namespace, @project), class: "btn btn-cancel"
...@@ -23,6 +23,44 @@ describe BuildsEmailService do ...@@ -23,6 +23,44 @@ describe BuildsEmailService do
end end
end end
describe '#test_data' do
let(:build) { create(:ci_build) }
let(:project) { build.project }
let(:user) { create(:user) }
before { project.team << [user, :developer] }
it 'builds test data' do
data = subject.test_data(project)
expect(data[:object_kind]).to eq("build")
end
end
describe '#test' do
it 'sends email' do
data = Gitlab::BuildDataBuilder.build(create(:ci_build))
subject.recipients = 'test@gitlab.com'
expect(BuildEmailWorker).to receive(:perform_async)
subject.test(data)
end
context 'notify only failed builds is true' do
it 'sends email' do
data = Gitlab::BuildDataBuilder.build(create(:ci_build))
data[:build_status] = "success"
subject.recipients = 'test@gitlab.com'
expect(subject).not_to receive(:notify_only_broken_builds)
expect(BuildEmailWorker).to receive(:perform_async)
subject.test(data)
end
end
end
describe '#execute' do describe '#execute' do
it 'sends email' do it 'sends email' do
subject.recipients = 'test@gitlab.com' subject.recipients = 'test@gitlab.com'
......
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