Class: Danger::RequestSources::GitLab
Constant Summary
RequestSource::DANGER_REPO_NAME
Instance Attribute Summary collapse
#ci_source, #environment, #ignored_violations
Class Method Summary
collapse
Instance Method Summary
collapse
#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
#parse_comment, #parse_message_from_row, #parse_tables_from_comment, #table_kind_from_title, #violations_from_table
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.
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_json ⇒ Object
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_json ⇒ Object
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_vars ⇒ Object
14
15
16
|
# File 'lib/danger/request_sources/gitlab.rb', line 14
def self.env_vars
["DANGER_GITLAB_API_TOKEN"]
end
|
.optional_env_vars ⇒ Object
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_commit ⇒ Object
62
63
64
65
|
# File 'lib/danger/request_sources/gitlab.rb', line 62
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
|
#client ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# 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
require "gitlab"
params = { private_token: token }
params[:endpoint] = endpoint
@client ||= Gitlab.client(params)
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
|
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/danger/request_sources/gitlab.rb', line 141
def (except: nil, danger_id: "danger")
.each do ||
next unless .generated_by_danger?(danger_id)
next if .id == except
client.(
ci_source.repo_slug,
ci_source.pull_request_id,
.id
)
end
end
|
#endpoint ⇒ Object
54
55
56
|
# File 'lib/danger/request_sources/gitlab.rb', line 54
def endpoint
@endpoint ||= @environment["DANGER_GITLAB_API_BASE_URL"] || "https://gitlab.com/api/v3"
end
|
#fetch_details ⇒ Object
94
95
96
97
98
|
# File 'lib/danger/request_sources/gitlab.rb', line 94
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)
self.ignored_violations = ignored_violations_from_pr
end
|
#host ⇒ Object
58
59
60
|
# File 'lib/danger/request_sources/gitlab.rb', line 58
def host
@host ||= @environment["DANGER_GITLAB_HOST"] || "gitlab.com"
end
|
#ignored_violations_from_pr ⇒ Object
100
101
102
|
# File 'lib/danger/request_sources/gitlab.rb', line 100
def ignored_violations_from_pr
GetIgnoredViolation.new(self.mr_json.description).call
end
|
67
68
69
70
71
72
|
# File 'lib/danger/request_sources/gitlab.rb', line 67
def
||= begin
client.(ci_source.repo_slug, ci_source.pull_request_id)
.map { || .from_gitlab() }
end
end
|
#mr_diff ⇒ Object
74
75
76
77
78
79
|
# File 'lib/danger/request_sources/gitlab.rb', line 74
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
|
#organisation ⇒ String
Returns The organisation name, is nil if it can’t be detected.
154
155
156
|
# File 'lib/danger/request_sources/gitlab.rb', line 154
def organisation
nil
end
|
#scm ⇒ Object
50
51
52
|
# File 'lib/danger/request_sources/gitlab.rb', line 50
def scm
@scm ||= GitRepo.new
end
|
#setup_danger_branches ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/danger/request_sources/gitlab.rb', line 81
def setup_danger_branches
head_commit = self.scm.head_commit
scm.ensure_commitish_exists! base_commit
self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"
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", new_comment: false) ⇒ Object
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
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/danger/request_sources/gitlab.rb', line 104
def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false)
= .select { || .generated_by_danger?(danger_id) }
= || .empty?
if
previous_violations = {}
else
= .first.body
previous_violations = ()
end
if previous_violations.empty? && (warnings + errors + messages + markdowns).empty?
(danger_id: danger_id)
else
body = (warnings: warnings,
errors: errors,
messages: messages,
markdowns: markdowns,
previous_violations: previous_violations,
danger_id: danger_id,
template: "gitlab")
if .empty?
client.(
ci_source.repo_slug, ci_source.pull_request_id, body
)
else
original_id = .first.id
client.(
ci_source.repo_slug, ci_source.pull_request_id, original_id, body
)
end
end
end
|
#validates_as_api_source? ⇒ Boolean
46
47
48
|
# File 'lib/danger/request_sources/gitlab.rb', line 46
def validates_as_api_source?
@token && !@token.empty?
end
|