summaryrefslogtreecommitdiff
path: root/scripts/generate-failed-pipeline-slack-message.rb
blob: 699e32872e6203ae810b931513b87b15cc144f49 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby

# frozen_string_literal: true

require_relative 'api/pipeline_failed_jobs'

finder_options = API::DEFAULT_OPTIONS.dup.merge(exclude_allowed_to_fail_jobs: true)
failed_jobs = PipelineFailedJobs.new(finder_options).execute

class SlackReporter
  DEFAULT_FAILED_PIPELINE_REPORT_FILE = 'failed_pipeline_report.json'

  def initialize(failed_jobs)
    @failed_jobs = failed_jobs
    @failed_pipeline_report_file = ENV.fetch('FAILED_PIPELINE_REPORT_FILE', DEFAULT_FAILED_PIPELINE_REPORT_FILE)
  end

  def report
    payload = {
      channel: ENV['SLACK_CHANNEL'],
      username: "Failed pipeline reporter",
      icon_emoji: ":boom:",
      text: "*#{title}*",
      blocks: [
        {
          type: "section",
          text: {
            type: "mrkdwn",
            text: "*#{title}*"
          }
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text: "*Commit*\n#{commit_link}"
            },
            {
              type: "mrkdwn",
              text: "*Triggered by*\n#{triggered_by_link}"
            }
          ]
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text: "*Source*\n#{source} from #{project_link}"
            },
            {
              type: "mrkdwn",
              text: "*Duration*\n#{pipeline_duration} minutes"
            }
          ]
        },
        {
          type: "section",
          text: {
            type: "mrkdwn",
            text: "*Failed jobs (#{failed_jobs.size}):* #{failed_jobs_list}"
          }
        }
      ]
    }

    File.write(failed_pipeline_report_file, JSON.pretty_generate(payload))
  end

  private

  attr_reader :failed_jobs, :failed_pipeline_report_file

  def title
    "Pipeline #{pipeline_link} for #{branch_link} failed"
  end

  def pipeline_link
    "<#{ENV['CI_PIPELINE_URL']}|##{ENV['CI_PIPELINE_ID']}>"
  end

  def branch_link
    "<#{ENV['CI_PROJECT_URL']}/-/commits/#{ENV['CI_COMMIT_REF_NAME']}|`#{ENV['CI_COMMIT_REF_NAME']}`>"
  end

  def pipeline_duration
    ((Time.now - Time.parse(ENV['CI_PIPELINE_CREATED_AT'])) / 60.to_f).round(2)
  end

  def commit_link
    "<#{ENV['CI_PROJECT_URL']}/-/commit/#{ENV['CI_COMMIT_SHA']}|#{ENV['CI_COMMIT_TITLE']}>"
  end

  def source
    "`#{ENV['CI_PIPELINE_SOURCE']}`"
  end

  def project_link
    "<#{ENV['CI_PROJECT_URL']}|#{ENV['CI_PROJECT_NAME']}>"
  end

  def triggered_by_link
    "<#{ENV['CI_SERVER_URL']}/#{ENV['GITLAB_USER_LOGIN']}|#{ENV['GITLAB_USER_NAME']}>"
  end

  def failed_jobs_list
    failed_jobs.map { |job| "<#{job.web_url}|#{job.name}>" }.join(', ')
  end
end

SlackReporter.new(failed_jobs).report