class Danger::DangerfileGitHubPlugin

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.

@example Warn when a PR is classed as work in progress

warn "PR is classed as Work in Progress" if github.pr_title.include? "[WIP]"

@example Declare a PR to be simple to avoid specific Danger rules

declared_trivial = (github.pr_title + github.pr_body).include?("#trivial")

@example Ensure that labels have been used on the PR

failure "Please add labels to this PR" if github.pr_labels.empty?

@example Check if a user is in a specific GitHub org, and message them if so

unless github.api.organization_member?('danger', github.pr_author)
  message "@#{github.pr_author} is not a contributor yet, would you like to join the Danger org?"
end

@example Ensure there is a summary for a PR

failure "Please provide a summary in the Pull Request description" if github.pr_body.length < 5

@example Only accept PRs to the develop branch

failure "Please re-submit this PR to develop, we may have already fixed your issue." if github.branch_for_base != "develop"

@example Note when PRs don’t reference a milestone, which goes away when it does

has_milestone = github.pr_json["milestone"] != nil
warn("This PR does not refer to an existing milestone", sticky: false) unless has_milestone

@example Note when a PR cannot be manually merged, which goes away when you can

can_merge = github.pr_json["mergeable"]
warn("This PR cannot be merged yet.", sticky: false) unless can_merge

@example Highlight when a celebrity makes a pull request

message "Welcome, Danger." if github.pr_author == "dangermcshane"

@example Ensure that all PRs have an assignee

warn "This PR does not have any assignees yet." unless github.pr_json["assignee"]

@example Send a message with links to a collection of specific files

if git.modified_files.include? "config/*.js"
  config_files = git.modified_files.select { |path| path.include? "config/" }
  message "This PR changes #{ github.html_link(config_files) }"
end

@example Highlight with a clickable link if a Package.json is changed

warn "#{github.html_link("Package.json")} was edited." if git.modified_files.include? "Package.json"

@example Note an issue with a particular line on a file using the L[num] syntax, e.g. ‘#L23`

linter_json = `my_linter lint "file"`
results = JSON.parse linter_json
unless results.empty?
  file, line, warning = result.first
  warn "#{github.html_link("#{file}#L#{line}")} has linter issue: #{warning}."
end

@see danger/danger @tags core, github

Public Class Methods

instance_name() click to toggle source

The instance name used in the Dangerfile @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 92
def self.instance_name
  "github"
end
new(dangerfile) click to toggle source

So that this init can fail.

Calls superclass method
# 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
new(dangerfile) click to toggle source
Calls superclass method
# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 83
def initialize(dangerfile)
  super(dangerfile)

  @github = dangerfile.env.request_source
end

Public Instance Methods

api() click to toggle source

@!group GitHub Misc Provides access to the GitHub API client used inside Danger. Making it easy to use the GitHub API inside a Dangerfile. @return [Octokit::Client]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 200
def api
  @github.client
end
base_commit() click to toggle source

@!group PR Commit Metadata The base commit to which the PR is going to be merged as a parent. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 175
def base_commit
  pr_json["base"]["sha"]
end
branch_for_base() click to toggle source

@!group PR Commit Metadata The branch to which the PR is going to be merged into. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 159
def branch_for_base
  pr_json["base"]["ref"]
end
branch_for_head() click to toggle source

@!group PR Commit Metadata The branch to which the PR is going to be merged from. @return [String]

# 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) click to toggle source

@!group GitHub Misc 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. ‘{warning: true, error: false}`) @param [Bool or Hash<Symbol, Bool>] dismiss

Ignore out of range inline messages, defaults to `true`

@return [void]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 244
def dismiss_out_of_range_messages(dismiss = true)
  if dismiss.kind_of?(Hash)
    @github.dismiss_out_of_range_messages = dismiss
  elsif dismiss.kind_of?(TrueClass)
    @github.dismiss_out_of_range_messages = true
  elsif dismiss.kind_of?(FalseClass)
    @github.dismiss_out_of_range_messages = false
  end
end
head_commit() click to toggle source

@!group PR Commit Metadata The head commit to which the PR is requesting to be merged from. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 183
def head_commit
  pr_json["head"]["sha"]
end
pr_author() click to toggle source

@!group PR Metadata The username of the author of the Pull Request. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 135
def pr_author
  pr_json["user"]["login"].to_s
end
pr_body() click to toggle source

@!group PR Metadata The body text of the Pull Request. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 127
def pr_body
  pr_json["body"].to_s
end
pr_diff() click to toggle source

@!group PR Content The unified diff produced by Github for this PR see [Unified diff](en.wikipedia.org/wiki/Diff_utility#Unified_format) @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 208
def pr_diff
  @github.pr_diff
end
pr_draft?() click to toggle source

@!group PR Metadata Whether the PR is a Draft. @return [Boolean]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 151
def pr_draft?
  pr_json["draft"] == true
end
pr_json() click to toggle source

@!group GitHub Misc 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). @return [Hash]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 192
def pr_json
  @github.pr_json
end
pr_labels() click to toggle source

@!group PR Metadata The labels assigned to the Pull Request. @return [String]

# 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() click to toggle source

@!group PR Metadata The title of the Pull Request. @return [String]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 119
def pr_title
  @github.pr_json["title"].to_s
end
review() click to toggle source

@!group PR Review

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

@return [ReviewDSL]

# File lib/danger/danger_core/plugins/dangerfile_github_plugin.rb, line 111
def review
  @github.review
end

Private Instance Methods