diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-12-19 12:12:18 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-12-19 12:12:18 +0300 |
commit | 7869f5cabe1f146e3b9059517a5514e56eefc7d6 (patch) | |
tree | 9dc6ac69c4cbfba415ba1da4b54ff60a9bfec4a5 | |
parent | 2c49c1af660a8e69446be442df81f9beaf0cf168 (diff) |
Implement recovered pipeline emails in detailed statusrecovered-pipelines
Closes #24644
-rw-r--r-- | app/mailers/emails/pipelines.rb | 27 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 18 | ||||
-rw-r--r-- | app/services/notification_service.rb | 18 | ||||
-rw-r--r-- | app/views/notify/pipeline_success_email.html.haml | 2 | ||||
-rw-r--r-- | app/views/notify/pipeline_success_email.text.erb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/status/core.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/ci/status/failed.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/ci/status/success.rb | 14 |
8 files changed, 79 insertions, 18 deletions
diff --git a/app/mailers/emails/pipelines.rb b/app/mailers/emails/pipelines.rb index 9460a6cd2be..ba3f7eaf216 100644 --- a/app/mailers/emails/pipelines.rb +++ b/app/mailers/emails/pipelines.rb @@ -1,11 +1,29 @@ module Emails module Pipelines - def pipeline_success_email(pipeline, recipients) - pipeline_mail(pipeline, recipients, 'succeeded') + def self.prepare_email(mailer, pipeline) + status = pipeline.detailed_status(nil) + template = status.pipeline_email_template + + return unless template + + recipients = yield + + return unless recipients.any? + + mailer.public_send( + template, + pipeline, + recipients, + status.pipeline_email_status + ).deliver_later + end + + def pipeline_success_email(pipeline, recipients, status) + pipeline_mail(pipeline, recipients, status) end - def pipeline_failed_email(pipeline, recipients) - pipeline_mail(pipeline, recipients, 'failed') + def pipeline_failed_email(pipeline, recipients, status) + pipeline_mail(pipeline, recipients, status) end private @@ -13,6 +31,7 @@ module Emails def pipeline_mail(pipeline, recipients, status) @project = pipeline.project @pipeline = pipeline + @status = status @merge_request = pipeline.merge_requests.first add_headers diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 48354cdbefb..359c517b27b 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -254,6 +254,24 @@ module Ci builds.latest.failed_but_allowed.any? end + def recovered? + success? && previous_pipeline.try(:failed?) + end + + def first_success? + success? && previous_pipeline.nil? + end + + def previous_pipeline + return @previous_pipeline if defined?(@previous_pipeline) + + @previous_pipeline = + project.pipelines. + where(ref: ref). + where("#{self.class.quoted_table_name}.id < ?", id). + order(id: :desc).first + end + def config_processor return nil unless ci_yaml_file return @config_processor if defined?(@config_processor) diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 9a7af5730d2..b99e4c21229 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -312,18 +312,12 @@ class NotificationService end def pipeline_finished(pipeline, recipients = nil) - email_template = "pipeline_#{pipeline.status}_email" - - return unless mailer.respond_to?(email_template) - - recipients ||= build_recipients( - pipeline, - pipeline.project, - nil, # The acting user, who won't be added to recipients - action: pipeline.status).map(&:notification_email) - - if recipients.any? - mailer.public_send(email_template, pipeline, recipients).deliver_later + Emails::Pipelines.prepare_email(mailer, pipeline) do + build_recipients( + pipeline, + pipeline.project, + nil, # The acting user, who won't be added to recipients + action: pipeline.status).map(&:notification_email) end end diff --git a/app/views/notify/pipeline_success_email.html.haml b/app/views/notify/pipeline_success_email.html.haml index 56c1949ab2b..c805c4a0425 100644 --- a/app/views/notify/pipeline_success_email.html.haml +++ b/app/views/notify/pipeline_success_email.html.haml @@ -65,7 +65,7 @@ %td{style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;vertical-align:middle;color:#ffffff;text-align:center;padding-right:5px;"} %img{alt: "✓", height: "13", src: image_url('mailers/ci_pipeline_notif_v1/icon-check-green-inverted.gif'), style: "display:block;", width: "13"}/ %td{style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;vertical-align:middle;color:#ffffff;text-align:center;"} - Your pipeline has passed. + = "Your pipeline has #{@status}." %tr.spacer %td{style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;height:18px;font-size:18px;line-height:18px;"} diff --git a/app/views/notify/pipeline_success_email.text.erb b/app/views/notify/pipeline_success_email.text.erb index 40e5e306426..66a86b0f5d6 100644 --- a/app/views/notify/pipeline_success_email.text.erb +++ b/app/views/notify/pipeline_success_email.text.erb @@ -1,4 +1,4 @@ -Your pipeline has passed. +Your pipeline has <%= @status %>. Project: <%= @project.name %> ( <%= project_url(@project) %> ) Branch: <%= @pipeline.ref %> ( <%= commits_url(@pipeline) %> ) diff --git a/lib/gitlab/ci/status/core.rb b/lib/gitlab/ci/status/core.rb index 46fef8262c1..2f4c995ab4c 100644 --- a/lib/gitlab/ci/status/core.rb +++ b/lib/gitlab/ci/status/core.rb @@ -63,6 +63,14 @@ module Gitlab def action_title raise NotImplementedError end + + def pipeline_email_template + nil + end + + def pipeline_email_status + raise NotImplementedError + end end end end diff --git a/lib/gitlab/ci/status/failed.rb b/lib/gitlab/ci/status/failed.rb index c5b5e3203ad..8cb8ba080e0 100644 --- a/lib/gitlab/ci/status/failed.rb +++ b/lib/gitlab/ci/status/failed.rb @@ -13,6 +13,14 @@ module Gitlab def icon 'icon_status_failed' end + + def pipeline_email_template + :pipeline_failed_email + end + + def pipeline_email_status + 'failed' + end end end end diff --git a/lib/gitlab/ci/status/success.rb b/lib/gitlab/ci/status/success.rb index c09c5f006e3..9fb26b048ea 100644 --- a/lib/gitlab/ci/status/success.rb +++ b/lib/gitlab/ci/status/success.rb @@ -13,6 +13,20 @@ module Gitlab def icon 'icon_status_success' end + + def pipeline_email_template + if subject.recovered? || subject.first_success? + :pipeline_success_email + end + end + + def pipeline_email_status + if subject.recovered? + 'recovered' + else + 'passed' + end + end end end end |