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