Class: Danger::RequestSources::GitLab

Inherits:
RequestSource show all
Includes:
Helpers::CommentsHelper
Defined in:
lib/danger/request_sources/gitlab.rb

Constant Summary

Constants inherited from RequestSource

RequestSource::DANGER_REPO_NAME

Instance Attribute Summary collapse

Attributes inherited from RequestSource

#ci_source, #environment, #ignored_violations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CommentsHelper

#apply_template, #generate_comment, #generate_description, #generate_inline_comment_body, #generate_inline_markdown_body, #markdown_link_to_message, #markdown_parser, #messages_are_equivalent, #process_markdown, #random_compliment, #table

Methods included from Helpers::CommentsParsingHelper

#parse_comment, #parse_message_from_row, #parse_tables_from_comment, #table_kind_from_title, #violations_from_table

Methods inherited from RequestSource

available_request_sources, available_source_names_and_envs, #file_url, inherited, source_name

Constructor Details

#initialize(ci_source, environment) ⇒ GitLab

Returns a new instance of GitLab.



22
23
24
25
26
27
# File 'lib/danger/request_sources/gitlab.rb', line 22

def initialize(ci_source, environment)
  self.ci_source = ci_source
  self.environment = environment

  @token = @environment["DANGER_GITLAB_API_TOKEN"]
end

Instance Attribute Details

#commits_jsonObject

Returns the value of attribute commits_json.



12
13
14
# File 'lib/danger/request_sources/gitlab.rb', line 12

def commits_json
  @commits_json
end

#mr_jsonObject

Returns the value of attribute mr_json.



12
13
14
# File 'lib/danger/request_sources/gitlab.rb', line 12

def mr_json
  @mr_json
end

Class Method Details

.env_varsObject



14
15
16
# File 'lib/danger/request_sources/gitlab.rb', line 14

def self.env_vars
  ["DANGER_GITLAB_API_TOKEN"]
end

.optional_env_varsObject



18
19
20
# File 'lib/danger/request_sources/gitlab.rb', line 18

def self.optional_env_vars
  ["DANGER_GITLAB_HOST", "DANGER_GITLAB_API_BASE_URL"]
end

Instance Method Details

#base_commitObject



67
68
69
70
71
72
73
74
# File 'lib/danger/request_sources/gitlab.rb', line 67

def base_commit
  if self.commits_json.last
    first_commit_in_branch = self.commits_json.last.id
    @base_commit ||= self.scm.exec "rev-parse #{first_commit_in_branch}^1"
  else
    @base_commit = ""
  end
end

#clientObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/danger/request_sources/gitlab.rb', line 29

def client
  token = @environment["DANGER_GITLAB_API_TOKEN"]
  raise "No API token given, please provide one using `DANGER_GITLAB_API_TOKEN`" unless token

  # The require happens inline so that it won't cause exceptions when just using the `danger` gem.
  require "gitlab"

  @client ||= Gitlab.client(endpoint: endpoint, private_token: token)
rescue LoadError
  puts "The GitLab gem was not installed, you will need to change your Gem from `danger` to `danger-gitlab`.".red
  puts "\n - See https://github.com/danger/danger/blob/master/CHANGELOG.md#400"
  abort
end

#delete_old_comments!(except: nil, danger_id: "danger") ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/danger/request_sources/gitlab.rb', line 159

def delete_old_comments!(except: nil, danger_id: "danger")
  mr_comments.each do |comment|
    next unless comment.generated_by_danger?(danger_id)
    next if comment.id == except
    client.delete_merge_request_comment(
      ci_source.repo_slug,
      ci_source.pull_request_id,
      comment.id
    )
  end
end

#endpointObject



58
59
60
61
# File 'lib/danger/request_sources/gitlab.rb', line 58

def endpoint
  @endpoint ||= @environment["DANGER_GITLAB_API_BASE_URL"] ||
                "https://gitlab.com/api/v4"
end

#fetch_detailsObject



107
108
109
110
111
112
113
# File 'lib/danger/request_sources/gitlab.rb', line 107

def fetch_details
  self.mr_json = client.merge_request(ci_source.repo_slug, self.ci_source.pull_request_id)
  self.commits_json = client.merge_request_commits(
    ci_source.repo_slug, self.ci_source.pull_request_id
  ).auto_paginate
  self.ignored_violations = ignored_violations_from_pr
end

#hostObject



63
64
65
# File 'lib/danger/request_sources/gitlab.rb', line 63

def host
  @host ||= @environment["DANGER_GITLAB_HOST"] || "gitlab.com"
end

#ignored_violations_from_prObject



115
116
117
# File 'lib/danger/request_sources/gitlab.rb', line 115

def ignored_violations_from_pr
  GetIgnoredViolation.new(self.mr_json.description).call
end

#mr_commentsObject



76
77
78
79
80
81
82
# File 'lib/danger/request_sources/gitlab.rb', line 76

def mr_comments
  @comments ||= begin
    client.merge_request_comments(ci_source.repo_slug, ci_source.pull_request_id, per_page: 100)
      .auto_paginate
      .map { |comment| Comment.from_gitlab(comment) }
  end
end

#mr_diffObject



84
85
86
87
88
89
# File 'lib/danger/request_sources/gitlab.rb', line 84

def mr_diff
  @mr_diff ||= begin
    client.merge_request_changes(ci_source.repo_slug, ci_source.pull_request_id)
      .changes.map { |change| change["diff"] }.join("\n")
  end
end

#organisationString

Returns The organisation name, is nil if it can’t be detected.

Returns:

  • (String)

    The organisation name, is nil if it can’t be detected



172
173
174
# File 'lib/danger/request_sources/gitlab.rb', line 172

def organisation
  nil # TODO: Implement this
end

#scmObject



54
55
56
# File 'lib/danger/request_sources/gitlab.rb', line 54

def scm
  @scm ||= GitRepo.new
end

#setup_danger_branchesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/danger/request_sources/gitlab.rb', line 91

def setup_danger_branches
  raise "Are you running `danger local/pr` against the correct repository? Also this can happen if you run danger on MR without changes" if base_commit.empty?
  base_branch = self.mr_json.source_branch
  head_branch = self.mr_json.target_branch
  head_commit = self.scm.head_commit

  # Next, we want to ensure that we have a version of the current branch at a known location
  scm.ensure_commitish_exists_on_branch! base_branch, base_commit
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"

  # OK, so we want to ensure that we have a known head branch, this will always represent
  # the head of the PR ( e.g. the most recent commit that will be merged. )
  scm.ensure_commitish_exists_on_branch! head_branch, head_commit
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
end

#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/danger/request_sources/gitlab.rb', line 119

def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false)
  editable_comments = mr_comments.select { |comment| comment.generated_by_danger?(danger_id) }

  should_create_new_comment = new_comment || editable_comments.empty?

  if should_create_new_comment
    previous_violations = {}
  else
    comment = editable_comments.first.body
    previous_violations = parse_comment(comment)
  end

  if previous_violations.empty? && (warnings + errors + messages + markdowns).empty?
    # Just remove the comment, if there"s nothing to say.
    delete_old_comments!(danger_id: danger_id)
  else
    body = generate_comment(warnings: warnings,
                              errors: errors,
                            messages: messages,
                           markdowns: markdowns,
                 previous_violations: previous_violations,
                           danger_id: danger_id,
                            template: "gitlab")

    if editable_comments.empty?
      client.create_merge_request_comment(
        ci_source.repo_slug, ci_source.pull_request_id, body
      )
    else
      original_id = editable_comments.first.id
      client.edit_merge_request_comment(
        ci_source.repo_slug,
        ci_source.pull_request_id,
        original_id,
        { body: body }
      )
    end
  end
end

#validates_as_api_source?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/danger/request_sources/gitlab.rb', line 50

def validates_as_api_source?
  @token && !@token.empty?
end

#validates_as_ci?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/danger/request_sources/gitlab.rb', line 43

def validates_as_ci?
  includes_port = self.host.include? ":"
  raise "Port number included in `DANGER_GITLAB_HOST`, this will fail with GitLab CI Runners" if includes_port

  super
end