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, #character_from_emoji, #generate_comment, #generate_description, #generate_inline_comment_body, #generate_inline_markdown_body, #markdown_link_to_message, #markdown_parser, #messages_are_equivalent, #parse_comment, #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, #validates_as_ci?

Constructor Details

#initialize(ci_source, environment) ⇒ GitLab

Returns a new instance of GitLab.



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

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.



10
11
12
# File 'lib/danger/request_sources/gitlab.rb', line 10

def commits_json
  @commits_json
end

#mr_jsonObject

Returns the value of attribute mr_json.



10
11
12
# File 'lib/danger/request_sources/gitlab.rb', line 10

def mr_json
  @mr_json
end

Class Method Details

.env_varsObject



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

def self.env_vars
  ["DANGER_GITLAB_API_TOKEN"]
end

.optional_env_varsObject



16
17
18
# File 'lib/danger/request_sources/gitlab.rb', line 16

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

Instance Method Details

#base_commitObject



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

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

#clientObject



27
28
29
30
31
32
33
34
# File 'lib/danger/request_sources/gitlab.rb', line 27

def client
  token = @environment["DANGER_GITLAB_API_TOKEN"]
  raise "No API token given, please provide one using `DANGER_GITLAB_API_TOKEN`" unless token
  params = { private_token: token }
  params[:endpoint] = endpoint

  @client ||= Gitlab.client(params)
end

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/danger/request_sources/gitlab.rb', line 131

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(
      escaped_ci_slug,
      ci_source.pull_request_id,
      comment.id
    )
  end
end

#endpointObject



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

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

#escaped_ci_slugObject



67
68
69
# File 'lib/danger/request_sources/gitlab.rb', line 67

def escaped_ci_slug
  @escaped_ci_slug ||= CGI.escape(ci_source.repo_slug)
end

#fetch_detailsObject



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

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

#hostObject



48
49
50
# File 'lib/danger/request_sources/gitlab.rb', line 48

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

#ignored_violations_from_pr(mr_json) ⇒ Object



90
91
92
93
94
# File 'lib/danger/request_sources/gitlab.rb', line 90

def ignored_violations_from_pr(mr_json)
  pr_body = mr_json.description
  return [] if pr_body.nil?
  pr_body.chomp.scan(/>\s*danger\s*:\s*ignore\s*"(.*)"/i).flatten
end

#mr_commentsObject



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

def mr_comments
  @comments ||= client.merge_request_comments(escaped_ci_slug, ci_source.pull_request_id)
                      .map { |comment| Comment.from_gitlab(comment) }
end

#mr_diffObject



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

def mr_diff
  @mr_diff ||= client.merge_request_changes(escaped_ci_slug, ci_source.pull_request_id)
                     .changes.map { |change| change["diff"] }.join("\n")
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



144
145
146
# File 'lib/danger/request_sources/gitlab.rb', line 144

def organisation
  nil # TODO: Implement this
end

#scmObject



40
41
42
# File 'lib/danger/request_sources/gitlab.rb', line 40

def scm
  @scm ||= GitRepo.new
end

#setup_danger_branchesObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/danger/request_sources/gitlab.rb', line 71

def setup_danger_branches
  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! 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! head_commit
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
end

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/danger/request_sources/gitlab.rb', line 96

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

  if editable_comments.empty?
    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(
        escaped_ci_slug, ci_source.pull_request_id, body
      )
    else
      original_id = editable_comments.first.id
      client.edit_merge_request_comment(
        escaped_ci_slug, ci_source.pull_request_id, original_id, body
      )
    end
  end
end

#validates_as_api_source?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/danger/request_sources/gitlab.rb', line 36

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