Class: Danger::DangerfileGitHubPlugin
- Defined in:
- lib/danger/danger_core/plugins/dangerfile_github_plugin.rb
Overview
Handles interacting with GitHub inside a Dangerfile. Provides a few functions which wrap ‘pr_json` and also through a few standard functions to simplify your code.
PR Review collapse
-
#review ⇒ ReviewDSL
In Beta.
PR Metadata collapse
-
#pr_author ⇒ String
The username of the author of the Pull Request.
-
#pr_body ⇒ String
The body text of the Pull Request.
-
#pr_draft? ⇒ Boolean
Whether the PR is a Draft.
-
#pr_labels ⇒ String
The labels assigned to the Pull Request.
-
#pr_title ⇒ String
The title of the Pull Request.
PR Commit Metadata collapse
-
#base_commit ⇒ String
The base commit to which the PR is going to be merged as a parent.
-
#branch_for_base ⇒ String
The branch to which the PR is going to be merged into.
-
#branch_for_head ⇒ String
The branch to which the PR is going to be merged from.
-
#head_commit ⇒ String
The head commit to which the PR is requesting to be merged from.
GitHub Misc collapse
-
#api ⇒ Octokit::Client
Provides access to the GitHub API client used inside Danger.
-
#dismiss_out_of_range_messages(dismiss = true) ⇒ void
Use to ignore inline messages which lay outside a diff’s range, thereby not posting them in the main comment.
-
#html_link(paths, full_path: true) ⇒ String
Returns a list of HTML anchors for a file, or files in the head repository.
-
#pr_json ⇒ Hash
The hash that represents the PR’s JSON.
PR Content collapse
-
#pr_diff ⇒ String
The unified diff produced by Github for this PR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format).
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) ⇒ DangerfileGitHubPlugin
constructor
A new instance of DangerfileGitHubPlugin.
Methods inherited from Plugin
all_plugins, clear_external_plugins, inherited, #method_missing
Constructor Details
#initialize(dangerfile) ⇒ DangerfileGitHubPlugin
Returns a new instance of DangerfileGitHubPlugin.
83 84 85 86 87 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 83 def initialize(dangerfile) super(dangerfile) @github = 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
92 93 94 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 92 def self.instance_name "github" end |
.new(dangerfile) ⇒ Object
So that this init can fail.
77 78 79 80 81 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 77 def self.new(dangerfile) return nil if dangerfile.env.request_source.class != Danger::RequestSources::GitHub super end |
Instance Method Details
#api ⇒ Octokit::Client
Provides access to the GitHub API client used inside Danger. Making it easy to use the GitHub API inside a Dangerfile.
200 201 202 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 200 def api @github.client end |
#base_commit ⇒ String
The base commit to which the PR is going to be merged as a parent.
175 176 177 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 175 def base_commit pr_json["base"]["sha"] end |
#branch_for_base ⇒ String
The branch to which the PR is going to be merged into.
159 160 161 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 159 def branch_for_base pr_json["base"]["ref"] end |
#branch_for_head ⇒ String
The branch to which the PR is going to be merged from.
167 168 169 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 167 def branch_for_head pr_json["head"]["ref"] 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 them in the main comment. You can set hash to change behavior per each kinds. (ex. ‘true, error: false`)
244 245 246 247 248 249 250 251 252 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 244 def (dismiss = true) if dismiss.kind_of?(Hash) @github. = dismiss elsif dismiss.kind_of?(TrueClass) @github. = true elsif dismiss.kind_of?(FalseClass) @github. = false end end |
#head_commit ⇒ String
The head commit to which the PR is requesting to be merged from.
183 184 185 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 183 def head_commit pr_json["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=’github.com/artsy/eigen/blob/561827e46167077b5e53515b4b7349b8ae04610b/file.txt’>file.txt</a>‘. It returns a string of multiple anchors if passed an array.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 221 def html_link(paths, full_path: true) paths = [paths] unless paths.kind_of?(Array) commit = head_commit repo = pr_json["head"]["repo"]["html_url"] paths = paths.map do |path| url_path = path.start_with?("/") ? path : "/#{path}" text = full_path ? path : File.basename(path) create_link("#{repo}/blob/#{commit}#{url_path}", text) end return paths.first if paths.count < 2 paths.first(paths.count - 1).join(", ") + " & " + paths.last end |
#pr_author ⇒ String
The username of the author of the Pull Request.
135 136 137 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 135 def pr_json["user"]["login"].to_s end |
#pr_body ⇒ String
The body text of the Pull Request.
127 128 129 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 127 def pr_body pr_json["body"].to_s end |
#pr_diff ⇒ String
The unified diff produced by Github for this PR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format)
208 209 210 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 208 def pr_diff @github.pr_diff end |
#pr_draft? ⇒ Boolean
Whether the PR is a Draft.
151 152 153 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 151 def pr_draft? pr_json["draft"] == true end |
#pr_json ⇒ Hash
The hash that represents the PR’s JSON. For an example of what this looks like see the [Danger Fixture’d one](raw.githubusercontent.com/danger/danger/master/spec/fixtures/github_api/pr_response.json).
192 193 194 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 192 def pr_json @github.pr_json end |
#pr_labels ⇒ String
The labels assigned to the Pull Request.
143 144 145 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 143 def pr_labels @github.issue_json["labels"].map { |l| l[:name] } end |
#pr_title ⇒ String
The title of the Pull Request.
119 120 121 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 119 def pr_title @github.pr_json["title"].to_s end |
#review ⇒ ReviewDSL
In Beta. Provides access to creating a GitHub Review instead of a typical GitHub comment.
To use you announce the start of your review, and the end via the ‘start` and `submit` functions, for example:
github.review.start github.review.fail(message) github.review.warn(message) github.review.message(message) github.review.markdown(message) github.review.submit
111 112 113 |
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 111 def review @github.review end |