diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-08-25 20:01:10 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-08-26 16:56:55 +0800 |
commit | 34cbc51e238a6b1626ef3d656b2dc80db6463971 (patch) | |
tree | 9ab4f2b6f8862f12d1e5235ec691d46667e73eab /app/models/project_services/pipelines_email_service.rb | |
parent | 7bbb523b23638c52b3c0ba43d8f3dbef8840aad6 (diff) | |
download | gitlab-ce-34cbc51e238a6b1626ef3d656b2dc80db6463971.tar.gz |
Add a new pipeline email service. TODO: email templates and tests
Diffstat (limited to 'app/models/project_services/pipelines_email_service.rb')
-rw-r--r-- | app/models/project_services/pipelines_email_service.rb | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/app/models/project_services/pipelines_email_service.rb b/app/models/project_services/pipelines_email_service.rb new file mode 100644 index 00000000000..1f852445c1c --- /dev/null +++ b/app/models/project_services/pipelines_email_service.rb @@ -0,0 +1,93 @@ +class PipelinesEmailService < Service + prop_accessor :recipients + boolean_accessor :add_pusher + boolean_accessor :notify_only_broken_pipelines + validates :recipients, + presence: true, + if: ->(s) { s.activated? && !s.add_pusher? } + + def initialize_properties + self.properties ||= { notify_only_broken_builds: true } + end + + def title + 'Pipelines emails' + end + + def description + 'Email the pipelines status to a list of recipients.' + end + + def to_param + 'pipelines_email' + end + + def supported_events + %w[pipeline] + end + + def execute(data, force = false) + return unless supported_events.include?(data[:object_kind]) + return unless force || should_build_be_notified?(data) + + all_recipients = retrieve_recipients(data) + + return unless all_recipients.any? + + PipelineEmailWorker.perform_async(data, all_recipients) + end + + def can_test? + project.pipelines.count > 0 + end + + def disabled_title + 'Please setup a pipeline on your repository.' + end + + def test_data(project, user) + data = Gitlab::DataBuilder::Pipeline.build(project.pipelines.last) + data[:user] = user.hook_attrs + data + end + + def fields + [ + { type: 'textarea', + name: 'recipients', + placeholder: 'Emails separated by comma' }, + { type: 'checkbox', + name: 'add_pusher', + label: 'Add pusher to recipients list' }, + { type: 'checkbox', + name: 'notify_only_broken_pipelines' }, + ] + end + + def test(data) + result = execute(data, true) + + { success: true, result: result } + rescue StandardError => error + { success: false, result: error } + end + + def should_build_be_notified?(data) + case data[:object_attributes][:status] + when 'success' + !notify_only_broken_pipelines? + else + false + end + end + + def retrieve_recipients(data) + all_recipients = recipients.to_s.split(',').reject(&:blank?) + + if add_pusher? && data[:user].try(:[], :email) + all_recipients << data[:user][:email] + else + all_recipients + end + end +end |