Class: CiToolkit::GitlabPr

Inherits:
DvcsPr
  • Object
show all
Defined in:
lib/ci_toolkit/gitlab_pr.rb

Overview

Can be used to retrieve information about a PR on Github via the Github API

Instance Method Summary collapse

Methods included from AbstractInterface

included

Constructor Details

#initialize(env = CiToolkit::BitriseEnv.new, build_types = ENV["BUILD_TYPES"]&.split(/,/) || ["BluetoothDemo", "Acceptance PreProd", "Acceptance Prod", "Latest Prod", "Latest PreProd", "Mock", "Design System"], bot = CiToolkit::GitlabBot.new) ⇒ GitlabPr

Returns a new instance of GitlabPr.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ci_toolkit/gitlab_pr.rb', line 8

def initialize(
  env = CiToolkit::BitriseEnv.new,
  build_types = ENV["BUILD_TYPES"]&.split(/,/) || ["BluetoothDemo", "Acceptance PreProd", "Acceptance Prod",
                                                   "Latest Prod", "Latest PreProd", "Mock", "Design System"],

  bot = CiToolkit::GitlabBot.new
)
  super()
  @pr_number = env.pull_request_number
  @repo_slug = env.repository_path
  @commit_sha = env.git_commit
  @_client = bot.client
  @build_types = build_types
  @bot = bot
end

Instance Method Details

#big?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ci_toolkit/gitlab_pr.rb', line 106

def big?
  lines_of_code_changed.to_i > 500
end

#build_typesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/ci_toolkit/gitlab_pr.rb', line 87

def build_types
  types = []
  @build_types.each do |type|
    if comments.include?("#{type} build") || labels.include?("#{type} build") ||
       comments.include?(type) || labels.include?(type)
      types.push(type)
    end
  end
  types
end

#comment(text) ⇒ Object



41
42
43
44
# File 'lib/ci_toolkit/gitlab_pr.rb', line 41

def comment(text)
  # github comment character limit is 65536
  client.create_merge_request_note(@repo_slug, @pr_number, text[0...65_500])
end

#commentsObject



37
38
39
# File 'lib/ci_toolkit/gitlab_pr.rb', line 37

def comments
  client.merge_request_notes(@repo_slug, @pr_number).map(&:body)
end

#create_status(state, context, target_url, description) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/ci_toolkit/gitlab_pr.rb', line 71

def create_status(state, context, target_url, description)
  client.update_commit_status(
    @repo_slug,
    @commit_sha,
    state,
    { name: context, target_url:, description: }
  )
end

#delete_comment(comment_id) ⇒ Object



51
52
53
# File 'lib/ci_toolkit/gitlab_pr.rb', line 51

def delete_comment(comment_id)
  client.delete_merge_request_note(@repo_slug, @pr_number, comment_id)
end

#delete_comments_including_text(text) ⇒ Object



46
47
48
49
# File 'lib/ci_toolkit/gitlab_pr.rb', line 46

def delete_comments_including_text(text)
  comments = find_comments_including_text(text)
  comments.each { |comment| delete_comment(comment.id) }
end

#filesObject



67
68
69
# File 'lib/ci_toolkit/gitlab_pr.rb', line 67

def files
  client.merge_request_changes(@repo_slug, @pr_number).changes
end

#find_comments_including_text(text) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/ci_toolkit/gitlab_pr.rb', line 55

def find_comments_including_text(text)
  comments = []
  client.merge_request_notes(@repo_slug, @pr_number).map do |item|
    comments << item if item.body&.include? text
  end
  comments
end

#get_status(context) ⇒ Object



80
81
82
83
84
85
# File 'lib/ci_toolkit/gitlab_pr.rb', line 80

def get_status(context)
  client.commit_status(@repo_slug, @commit_sha).each do |status|
    return status if status.name == context
  end
  nil
end

#get_status_description(context) ⇒ Object



115
116
117
118
119
120
# File 'lib/ci_toolkit/gitlab_pr.rb', line 115

def get_status_description(context)
  status = get_status(context)
  return if status.nil?

  status.description
end

#infrastructure_work?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ci_toolkit/gitlab_pr.rb', line 98

def infrastructure_work?
  !(title =~ /\[infra\]/i).nil? || labels.include?("Infra")
end

#labelsObject



63
64
65
# File 'lib/ci_toolkit/gitlab_pr.rb', line 63

def labels
  client.merge_request(@repo_slug, @pr_number).labels.map { |item| item }
end

#lines_of_code_changedObject



32
33
34
35
# File 'lib/ci_toolkit/gitlab_pr.rb', line 32

def lines_of_code_changed
  pr = client.merge_request(@repo_slug, @pr_number)
  pr.changes_count
end

#numberObject



28
29
30
# File 'lib/ci_toolkit/gitlab_pr.rb', line 28

def number
  @pr_number
end

#realm_module_modified?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/ci_toolkit/gitlab_pr.rb', line 110

def realm_module_modified?
  modified_files = files.select { |file| file&.old_path&.start_with? "cache/" }
  modified_files.length.positive?
end

#titleObject



24
25
26
# File 'lib/ci_toolkit/gitlab_pr.rb', line 24

def title
  client.merge_request(@repo_slug, @pr_number).title || ""
end

#work_in_progress?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/ci_toolkit/gitlab_pr.rb', line 102

def work_in_progress?
  title.include?("[WIP]") || labels.include?("WIP")
end