summaryrefslogtreecommitdiff
path: root/app/mailers/emails/pipelines.rb
blob: 0b830f4ee5e925d83507459d657b80410f4ddef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

module Emails
  module Pipelines
    def pipeline_success_email(pipeline, recipients)
      pipeline_mail(pipeline, recipients, 'Succesful')
    end

    def pipeline_failed_email(pipeline, recipients)
      pipeline_mail(pipeline, recipients, 'Failed')
    end

    def pipeline_fixed_email(pipeline, recipients)
      pipeline_mail(pipeline, recipients, 'Fixed')
    end

    private

    def pipeline_mail(pipeline, recipients, status)
      @project = pipeline.project
      @pipeline = pipeline

      @merge_request = if pipeline.merge_request?
                         pipeline.merge_request
                       else
                         pipeline.merge_requests_as_head_pipeline.first
                       end

      add_headers

      # We use bcc here because we don't want to generate these emails for a
      # thousand times. This could be potentially expensive in a loop, and
      # recipients would contain all project watchers so it could be a lot.
      mail(bcc: recipients,
           subject: pipeline_subject(status)) do |format|
        format.html { render layout: 'mailer' }
        format.text { render layout: 'mailer' }
      end
    end

    def add_headers
      add_project_headers
      add_pipeline_headers
    end

    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 = []

      subject << "#{status} pipeline for #{@pipeline.source_ref}"
      subject << @project.name if @project
      subject << @pipeline.short_sha

      subject.join(' | ')
    end
  end
end