Class: Danger::RequestSources::GitLab
Constant Summary
RequestSource::DANGER_REPO_NAME
Instance Attribute Summary collapse
#ci_source, #environment, #ignored_violations
Instance Method Summary
collapse
#generate_comment, #generate_description, #markdown_parser, #parse_comment, #parse_tables_from_comment, #process_markdown, #random_compliment, #table, #table_kind_from_title, #violations_from_table
available_request_sources, #file_url, inherited, #validates_as_ci?
Constructor Details
#initialize(ci_source, environment) ⇒ GitLab
Returns a new instance of GitLab.
11
12
13
14
15
16
|
# File 'lib/danger/request_source/gitlab.rb', line 11
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.
9
10
11
|
# File 'lib/danger/request_source/gitlab.rb', line 9
def commits_json
@commits_json
end
|
#mr_json ⇒ Object
Returns the value of attribute mr_json.
9
10
11
|
# File 'lib/danger/request_source/gitlab.rb', line 9
def mr_json
@mr_json
end
|
Instance Method Details
#base_commit ⇒ Object
43
44
45
46
|
# File 'lib/danger/request_source/gitlab.rb', line 43
def base_commit
first_commit_in_branch = self.commits_json.last.id
@base_comit ||= self.scm.exec "rev-parse #{first_commit_in_branch}^1"
end
|
#client ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/danger/request_source/gitlab.rb', line 18
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
|
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/danger/request_source/gitlab.rb', line 120
def (except: nil, danger_id: "danger")
.each do ||
next unless .generated_by_danger?(danger_id)
next if .id == except
client.(
escaped_ci_slug,
ci_source.pull_request_id,
.id
)
end
end
|
#endpoint ⇒ Object
35
36
37
|
# File 'lib/danger/request_source/gitlab.rb', line 35
def endpoint
@endpoint ||= @environment["DANGER_GITLAB_API_BASE_URL"] || "https://gitlab.com/api/v3"
end
|
#escaped_ci_slug ⇒ Object
58
59
60
|
# File 'lib/danger/request_source/gitlab.rb', line 58
def escaped_ci_slug
@escaped_ci_slug ||= CGI.escape(ci_source.repo_slug)
end
|
#fetch_details ⇒ Object
73
74
75
76
77
|
# File 'lib/danger/request_source/gitlab.rb', line 73
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
|
#host ⇒ Object
39
40
41
|
# File 'lib/danger/request_source/gitlab.rb', line 39
def host
@host ||= @environment["DANGER_GITLAB_HOST"] || "gitlab.com"
end
|
#ignored_violations_from_pr(mr_json) ⇒ Object
79
80
81
82
83
|
# File 'lib/danger/request_source/gitlab.rb', line 79
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
|
48
49
50
51
|
# File 'lib/danger/request_source/gitlab.rb', line 48
def
@comments ||= client.(escaped_ci_slug, ci_source.pull_request_id)
.map { || Comment.from_gitlab() }
end
|
#mr_diff ⇒ Object
53
54
55
56
|
# File 'lib/danger/request_source/gitlab.rb', line 53
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
|
#organisation ⇒ String
Returns The organisation name, is nil if it can’t be detected.
133
134
135
|
# File 'lib/danger/request_source/gitlab.rb', line 133
def organisation
nil end
|
#scm ⇒ Object
31
32
33
|
# File 'lib/danger/request_source/gitlab.rb', line 31
def scm
@scm ||= GitRepo.new
end
|
#setup_danger_branches ⇒ Object
#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger") ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/danger/request_source/gitlab.rb', line 85
def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger")
= .select { || .generated_by_danger?(danger_id) }
if .empty?
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.(
escaped_ci_slug, ci_source.pull_request_id, body
)
else
original_id = .first.id
client.(
escaped_ci_slug, ci_source.pull_request_id, original_id, body
)
end
end
end
|
#validates_as_api_source? ⇒ Boolean
27
28
29
|
# File 'lib/danger/request_source/gitlab.rb', line 27
def validates_as_api_source?
@token && !@token.empty?
end
|