Class: Danger::DangerfileGitLabPlugin
- Defined in:
- lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb
Overview
Handles interacting with GitLab inside a Dangerfile. Provides a few functions which wrap ‘mr_json` and also through a few standard functions to simplify your code.
MR Metadata collapse
-
#mr_author ⇒ String
The username of the author of the Merge Request.
-
#mr_body ⇒ String
The body text of the Merge Request.
-
#mr_labels ⇒ String
The labels assigned to the Merge Request.
-
#mr_title ⇒ String
The title of the Merge Request.
MR Content collapse
-
#mr_diff ⇒ String
The unified diff produced by GitLab for this MR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format).
MR Commit Metadata collapse
-
#base_commit ⇒ String
The base commit to which the MR is going to be merged as a parent.
-
#branch_for_base ⇒ String
The branch to which the MR is going to be merged into.
-
#branch_for_head ⇒ String
The branch to which the MR is going to be merged from.
-
#branch_for_merge ⇒ String
deprecated
Deprecated.
Please use #branch_for_base instead
-
#head_commit ⇒ String
The head commit to which the MR is requesting to be merged from.
GitLab Misc collapse
-
#api ⇒ GitLab::Client
Provides access to the GitLab API client used inside Danger.
-
#html_link(paths, full_path: true) ⇒ String
Returns a list of HTML anchors for a file, or files in the head repository.
-
#mr_json ⇒ Hash
The hash that represents the MR’s JSON.
-
#repository_web_url ⇒ String
Returns the web_url of the source project.
Gitlab Misc collapse
-
#dismiss_out_of_range_messages(dismiss = true) ⇒ void
Use to ignore inline messages which lay outside a diff’s range, thereby not posting the comment.
Class Method Summary collapse
-
.instance_name ⇒ String
The instance name used in the Dangerfile.
-
.new(dangerfile) ⇒ Object
So that this init can fail.
Instance Method Summary collapse
-
#initialize(dangerfile) ⇒ DangerfileGitLabPlugin
constructor
A new instance of DangerfileGitLabPlugin.
Methods inherited from Plugin
all_plugins, clear_external_plugins, inherited, #method_missing
Constructor Details
#initialize(dangerfile) ⇒ DangerfileGitLabPlugin
Returns a new instance of DangerfileGitLabPlugin.
87 88 89 90 91 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 87 def initialize(dangerfile) super(dangerfile) @gitlab = dangerfile.env.request_source end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Danger::Plugin
Class Method Details
.instance_name ⇒ String
The instance name used in the Dangerfile
83 84 85 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 83 def self.instance_name "gitlab" end |
.new(dangerfile) ⇒ Object
So that this init can fail.
75 76 77 78 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 75 def self.new(dangerfile) return nil if dangerfile.env.request_source.class != Danger::RequestSources::GitLab super end |
Instance Method Details
#api ⇒ GitLab::Client
Provides access to the GitLab API client used inside Danger. Making it easy to use the GitLab API inside a Dangerfile. See the gitlab gem’s [documentation](www.rubydoc.info/gems/gitlab/Gitlab/Client) for accessible methods.
191 192 193 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 191 def api @gitlab.client end |
#base_commit ⇒ String
The base commit to which the MR is going to be merged as a parent
163 164 165 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 163 def base_commit @gitlab.mr_json.diff_refs.base_sha end |
#branch_for_base ⇒ String
The branch to which the MR is going to be merged into.
147 148 149 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 147 def branch_for_base @gitlab.mr_json.target_branch end |
#branch_for_head ⇒ String
The branch to which the MR is going to be merged from.
155 156 157 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 155 def branch_for_head @gitlab.mr_json.source_branch end |
#branch_for_merge ⇒ String
Please use #branch_for_base instead
The branch to which the MR is going to be merged into
139 140 141 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 139 def branch_for_merge branch_for_base end |
#dismiss_out_of_range_messages(dismiss = true) ⇒ void
This method returns an undefined value.
Use to ignore inline messages which lay outside a diff’s range, thereby not posting the comment. You can set hash to change behavior per each kinds. (ex. ‘true, error: false`)
237 238 239 240 241 242 243 244 245 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 237 def (dismiss = true) if dismiss.kind_of?(Hash) @gitlab. = dismiss elsif dismiss.kind_of?(TrueClass) @gitlab. = true elsif dismiss.kind_of?(FalseClass) @gitlab. = false end end |
#head_commit ⇒ String
The head commit to which the MR is requesting to be merged from
171 172 173 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 171 def head_commit @gitlab.mr_json.diff_refs.head_sha end |
#html_link(paths, full_path: true) ⇒ String
Returns a list of HTML anchors for a file, or files in the head repository. An example would be: ‘<a href=’gitlab.com/artsy/eigen/blob/561827e46167077b5e53515b4b7349b8ae04610b/file.txt’>file.txt</a>‘. It returns a string of multiple anchors if passed an array.
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 216 def html_link(paths, full_path: true) paths = [paths] unless paths.kind_of?(Array) commit = head_commit paths = paths.map do |path| url_path = path.start_with?("/") ? path : "/#{path}" text = full_path ? path : File.basename(path) create_link("#{repository_web_url}/blob/#{commit}#{url_path}", text) end return paths.first if paths.count < 2 paths.first(paths.count - 1).join(", ") + " & " + paths.last end |
#mr_author ⇒ String
The username of the author of the Merge Request
113 114 115 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 113 def @gitlab.mr_json..username.to_s end |
#mr_body ⇒ String
The body text of the Merge Request
105 106 107 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 105 def mr_body @gitlab.mr_json.description.to_s end |
#mr_diff ⇒ String
The unified diff produced by GitLab for this MR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format)
130 131 132 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 130 def mr_diff @gitlab.mr_diff end |
#mr_json ⇒ Hash
The hash that represents the MR’s JSON. See documentation for the structure [here](docs.gitlab.com/ce/api/merge_requests.html#get-single-mr)
180 181 182 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 180 def mr_json @gitlab.mr_json.to_hash end |
#mr_labels ⇒ String
The labels assigned to the Merge Request
121 122 123 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 121 def mr_labels @gitlab.mr_json.labels end |
#mr_title ⇒ String
The title of the Merge Request
97 98 99 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 97 def mr_title @gitlab.mr_json.title.to_s end |
#repository_web_url ⇒ String
Returns the web_url of the source project.
199 200 201 202 203 204 |
# File 'lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb', line 199 def repository_web_url @repository_web_url ||= begin project = api.project(mr_json["source_project_id"]) project.web_url end end |