summaryrefslogtreecommitdiff
path: root/scripts/generate-failed-package-and-test-mr-message.rb
blob: a53b393498c7bd3a8fcdf0edbfbadf9107ba7d05 (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
#!/usr/bin/env ruby

# frozen_string_literal: true

require 'optparse'
require 'json'

require_relative 'api/create_merge_request_discussion'
require_relative 'api/commit_merge_requests'
require_relative 'api/get_package_and_test_job'

class GenerateFailedPackageAndTestMrMessage
  DEFAULT_OPTIONS = {
    project: nil
  }.freeze

  def initialize(options)
    @project = options.fetch(:project)
  end

  def execute
    return unless failed_package_and_test_job

    add_discussion_to_mr
  end

  private

  attr_reader :project

  def add_discussion_to_mr
    CreateMergeRequestDiscussion.new(
      API::DEFAULT_OPTIONS.merge(merge_request: merge_request)
    ).execute(content)
  end

  def failed_package_and_test_job
    @failed_package_and_test_job ||= GetPackageAndTestJob.new(API::DEFAULT_OPTIONS).execute
  end

  def merge_request
    @merge_request ||= CommitMergeRequests.new(
      API::DEFAULT_OPTIONS.merge(sha: ENV['CI_MERGE_REQUEST_SOURCE_BRANCH_SHA'])
    ).execute.first
  end

  def content
    <<~MARKDOWN
    :warning: @#{author_username} The `e2e:package-and-test-ee` job has failed.

    - Pipeline: #{pipeline_link}
    - `package-and-test` pipeline: #{failed_package_and_test_job['web_url']}

    Ping your team's associated Software Engineer in Test (SET) to confirm the failures are unrelated to the merge request.
    If there's no SET assigned, ask for assistance on the `#quality` Slack channel.
    MARKDOWN
  end

  def author_username
    merge_request['author']['username'] if merge_request
  end

  def pipeline_link
    "[##{ENV['CI_PIPELINE_ID']}](#{ENV['CI_PIPELINE_URL']})"
  end
end

if $PROGRAM_NAME == __FILE__
  options = GenerateFailedPackageAndTestMrMessage::DEFAULT_OPTIONS.dup

  OptionParser.new do |opts|
    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end.parse!

  GenerateFailedPackageAndTestMrMessage.new(options).execute
end