From 1bd2327c314ca8d5782298e80443c0fbbba0b66a Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <godfat@godfat.org>
Date: Wed, 31 Aug 2016 21:38:02 +0800
Subject: [PATCH] Add mock email templates

---
 app/mailers/emails/pipelines.rb               | 17 ++++---
 app/models/ci/build.rb                        |  7 ++-
 app/models/service.rb                         |  1 +
 .../notify/pipeline_failed_email.html.haml    | 44 +++++++++++++++++++
 .../notify/pipeline_failed_email.text.erb     | 15 +++++++
 .../notify/pipeline_succeeded_email.html.haml | 26 +++++++++++
 .../notify/pipeline_succeeded_email.text.erb  |  7 +++
 7 files changed, 106 insertions(+), 11 deletions(-)
 create mode 100644 app/views/notify/pipeline_failed_email.html.haml
 create mode 100644 app/views/notify/pipeline_failed_email.text.erb
 create mode 100644 app/views/notify/pipeline_succeeded_email.html.haml
 create mode 100644 app/views/notify/pipeline_succeeded_email.text.erb

diff --git a/app/mailers/emails/pipelines.rb b/app/mailers/emails/pipelines.rb
index 48bfc974768..7c181d2e366 100644
--- a/app/mailers/emails/pipelines.rb
+++ b/app/mailers/emails/pipelines.rb
@@ -11,28 +11,27 @@ module Emails
     private
 
     def pipeline_mail(params, to, status)
-      @params = params
+      @project = params.project
+      @pipeline = params.pipeline
       add_headers
 
       mail(to: to, subject: pipeline_subject(status))
     end
 
     def add_headers
-      @project = @params.project # `add_project_headers` needs this
       add_project_headers
-      add_pipeline_headers(@params.pipeline)
+      add_pipeline_headers
     end
 
-    def add_pipeline_headers(pipeline)
-      headers['X-GitLab-Pipeline-Id'] = pipeline.id
-      headers['X-GitLab-Pipeline-Ref'] = pipeline.ref
-      headers['X-GitLab-Pipeline-Status'] = pipeline.status
+    def add_pipeline_headers
+      headers['X-GitLab-Pipeline-Id'] = @pipeline.id
+      headers['X-GitLab-Pipeline-Ref'] = @pipeline.ref
+      headers['X-GitLab-Pipeline-Status'] = @pipeline.status
     end
 
     def pipeline_subject(status)
       subject(
-        "Pipeline #{status} for #{@params.project.name}",
-        @params.pipeline.short_sha)
+        "Pipeline #{status} for #{@project.name}", @pipeline.short_sha)
     end
   end
 end
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 096b3b801af..755edadb7a4 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -133,8 +133,11 @@ module Ci
     end
 
     def trace_with_state(state = nil)
-      trace_with_state = Ci::Ansi2html::convert(trace, state) if trace.present?
-      trace_with_state || {}
+      if trace.present?
+        Ci::Ansi2html.convert(trace, state)
+      else
+        {}
+      end
     end
 
     def timeout
diff --git a/app/models/service.rb b/app/models/service.rb
index 09b4717a523..668c3fb69e3 100644
--- a/app/models/service.rb
+++ b/app/models/service.rb
@@ -198,6 +198,7 @@ class Service < ActiveRecord::Base
       bamboo
       buildkite
       builds_email
+      pipelines_email
       bugzilla
       campfire
       custom_issue_tracker
diff --git a/app/views/notify/pipeline_failed_email.html.haml b/app/views/notify/pipeline_failed_email.html.haml
new file mode 100644
index 00000000000..8d68b9e8483
--- /dev/null
+++ b/app/views/notify/pipeline_failed_email.html.haml
@@ -0,0 +1,44 @@
+.p
+  Project:
+  = @project.path_with_namespace
+.p
+  Branch:
+  = @pipeline.ref
+.p
+  Commit:
+  = @pipeline.short_sha
+  (
+  = @pipeline.sha
+  )
+.p
+  Commit Message:
+  = @pipeline.git_commit_message
+.p
+  Commit Author:
+  = @pipeline.git_author_name
+.p
+  Pusher:
+  = @pipeline.user.try(:name)
+- failed = @pipeline.statuses.latest.failed
+.p
+  Pipeline #
+  = @pipeline.id
+  had
+  = failed.size
+  failed
+  = 'job'.plural(failed.size)
+  .
+
+- failed.each do |job|
+  .p
+    ID:
+    = job.id
+  .p
+    Stage:
+    = job.stage
+  .p
+    Name:
+    = job.name
+  .p
+    Trace:
+    = job.trace_with_state[:html].html_safe
diff --git a/app/views/notify/pipeline_failed_email.text.erb b/app/views/notify/pipeline_failed_email.text.erb
new file mode 100644
index 00000000000..9808213af99
--- /dev/null
+++ b/app/views/notify/pipeline_failed_email.text.erb
@@ -0,0 +1,15 @@
+Project: <%= @project.path_with_namespace %>
+Branch: <%= @pipeline.ref %>
+Commit: <%= @pipeline.short_sha %> (<%= @pipeline.sha %>)
+Commit Message: <%= @pipeline.git_commit_message %>
+Commit Author: <%= @pipeline.git_author_name %>
+Pusher: <%= @pipeline.user.try(:name) %>
+<% failed = @pipeline.statuses.latest.failed %>
+Pipeline #<%= @pipeline.id %> had <%= failed.size %> failed <%= 'job'.plural(failed.size) %>.
+
+<% failed.each do |job| %>
+ID: <%= job.id %>
+Stage: <%= job.stage %>
+Name: <%= job.name %>
+Trace: <%= job.trace_with_state[:html] %>
+<% end %>
diff --git a/app/views/notify/pipeline_succeeded_email.html.haml b/app/views/notify/pipeline_succeeded_email.html.haml
new file mode 100644
index 00000000000..64cf7cfe103
--- /dev/null
+++ b/app/views/notify/pipeline_succeeded_email.html.haml
@@ -0,0 +1,26 @@
+.p
+  Project:
+  = @project.path_with_namespace
+.p
+  Branch:
+  = @pipeline.ref
+.p
+  Commit:
+  = @pipeline.short_sha
+  (
+  = @pipeline.sha
+  )
+.p
+  Commit Message:
+  = @pipeline.git_commit_message
+.p
+  Commit Author:
+  = @pipeline.git_author_name
+.p
+  Pusher:
+  = @pipeline.user.try(:name)
+- failed = @pipeline.statuses.latest.failed
+.p
+  Pipeline #
+  = @pipeline.id
+  had succeeded.
diff --git a/app/views/notify/pipeline_succeeded_email.text.erb b/app/views/notify/pipeline_succeeded_email.text.erb
new file mode 100644
index 00000000000..6821d1f4459
--- /dev/null
+++ b/app/views/notify/pipeline_succeeded_email.text.erb
@@ -0,0 +1,7 @@
+Project: <%= @project.path_with_namespace %>
+Branch: <%= @pipeline.ref %>
+Commit: <%= @pipeline.short_sha %> (<%= @pipeline.sha %>)
+Commit Message: <%= @pipeline.git_commit_message %>
+Commit Author: <%= @pipeline.git_author_name %>
+Pusher: <%= @pipeline.user.try(:name) %>
+Pipeline #<%= @pipeline.id %> had succeeded.
-- 
GitLab