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", "Acceptance Prod DEBUG"], 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
23
24
25
# 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",
                                                   "Acceptance Prod DEBUG"],

  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
  @changes_detector = GitlabSpecificFilesChangesDetector.new
  @build_types_getter = CiToolkit::BuildTypes.new
end

Instance Method Details

#big?Boolean

Returns:

  • (Boolean)


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

def big?
  lines_of_code_changed.to_i > 500
end

#build_typesObject



90
91
92
# File 'lib/ci_toolkit/gitlab_pr.rb', line 90

def build_types
  @build_types_getter.get_build_types(@build_types, comments, labels)
end

#certificate_pinning_logic_modified?Boolean

Returns:

  • (Boolean)


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

def certificate_pinning_logic_modified?
  @changes_detector.certificate_pinning_logic_modified(files)
end

#comment(text) ⇒ Object



44
45
46
47
# File 'lib/ci_toolkit/gitlab_pr.rb', line 44

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

#commentsObject



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

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

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



74
75
76
77
78
79
80
81
# File 'lib/ci_toolkit/gitlab_pr.rb', line 74

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



54
55
56
# File 'lib/ci_toolkit/gitlab_pr.rb', line 54

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

#delete_comments_including_text(text) ⇒ Object



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

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

#filesObject



70
71
72
# File 'lib/ci_toolkit/gitlab_pr.rb', line 70

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

#find_comments_including_text(text) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ci_toolkit/gitlab_pr.rb', line 58

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



83
84
85
86
87
88
# File 'lib/ci_toolkit/gitlab_pr.rb', line 83

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



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

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

  status.description
end

#infrastructure_work?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ci_toolkit/gitlab_pr.rb', line 94

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

#labelsObject



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

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

#lines_of_code_changedObject



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

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

#numberObject



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

def number
  @pr_number
end

#realm_module_modified?Boolean

Returns:

  • (Boolean)


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

def realm_module_modified?
  @changes_detector.realm_module_modified(files)
end

#titleObject



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

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

#work_in_progress?Boolean

Returns:

  • (Boolean)


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

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