Class: Danger::DangerfileGitHubPlugin

Inherits:
Plugin
  • Object
show all
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.

Examples:

Warn when a PR is classed as work in progress


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

Declare a PR to be simple to avoid specific Danger rules


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

Ensure that labels have been used on the PR


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

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

Ensure there is a summary for a PR


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

Only accept PRs to the develop branch


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

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

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

Highlight when a celebrity makes a pull request


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

Ensure that all PRs have an assignee


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

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

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"

Note an issue with a particular line on a file using the #L 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 Also:

  • danger/danger

PR Metadata collapse

PR Commit Metadata collapse

GitHub Misc collapse

PR Content collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

all_plugins, clear_external_plugins, inherited, #method_missing

Constructor Details

#initialize(dangerfile) ⇒ DangerfileGitHubPlugin

Returns a new instance of DangerfileGitHubPlugin.



82
83
84
85
86
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 82

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_nameString

The instance name used in the Dangerfile

Returns:



91
92
93
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 91

def self.instance_name
  "github"
end

.new(dangerfile) ⇒ Object

So that this init can fail.



77
78
79
80
# 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

#apiOctokit::Client

Provides access to the GitHub API client used inside Danger. Making it easy to use the GitHub API inside a Dangerfile.

Returns:

  • (Octokit::Client)


172
173
174
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 172

def api
  @github.client
end

#base_commitString

The base commit to which the PR is going to be merged as a parent.

Returns:



147
148
149
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 147

def base_commit
  pr_json[:base][:sha]
end

#branch_for_baseString

The branch to which the PR is going to be merged into.

Returns:



131
132
133
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 131

def branch_for_base
  pr_json[:base][:ref]
end

#branch_for_headString

The branch to which the PR is going to be merged from.

Returns:



139
140
141
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 139

def branch_for_head
  pr_json[:head][:ref]
end

#head_commitString

The head commit to which the PR is requesting to be merged from.

Returns:



155
156
157
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 155

def head_commit
  pr_json[:head][:sha]
end

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.

Parameters:

  • paths (String or Array<String>)

    A list of strings to convert to github anchors

  • full_path (Bool) (defaults to: true)

    Shows the full path as the link’s text, defaults to ‘true`.

Returns:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 193

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_authorString

The username of the author of the Pull Request.

Returns:



115
116
117
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 115

def pr_author
  pr_json[:user][:login].to_s
end

#pr_bodyString

The body text of the Pull Request.

Returns:



107
108
109
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 107

def pr_body
  pr_json[:body].to_s
end

#pr_diffString

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

Returns:



180
181
182
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 180

def pr_diff
  @github.pr_diff
end

#pr_jsonHash

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).

Returns:

  • (Hash)


164
165
166
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 164

def pr_json
  @github.pr_json
end

#pr_labelsString

The labels assigned to the Pull Request.

Returns:



123
124
125
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 123

def pr_labels
  @github.issue_json[:labels].map { |l| l[:name] }
end

#pr_titleString

The title of the Pull Request.

Returns:



99
100
101
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 99

def pr_title
  @github.pr_json[:title].to_s
end