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]"

Ensure that labels have been used on the PR


fail "Please add labels to this PR" if github.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 "@#{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

See Also:

  • danger/danger

PR Metadata collapse

PR Commit Metadata collapse

GitHub Misca collapse

GitHub Misc 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.



29
30
31
32
33
34
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 29

def initialize(dangerfile)
  super(dangerfile)
  return nil unless dangerfile.env.request_source.class == Danger::RequestSources::GitHub

  @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_nameObject



36
37
38
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 36

def self.instance_name
  "github"
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)


117
118
119
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 117

def api
  @github.client
end

#base_commitObject

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

Returns:

  • String



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

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

#branch_for_baseString

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

Returns:



76
77
78
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 76

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:



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

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:



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

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

#pr_authorString

The username of the author of the Pull Request.

Returns:



60
61
62
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 60

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

#pr_bodyString

The body text of the Pull Request.

Returns:



52
53
54
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 52

def pr_body
  pr_json[:body].to_s
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/pr_response.json).

Returns:

  • (Hash)


109
110
111
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 109

def pr_json
  @github.pr_json
end

#pr_labelsString

The labels assigned to the Pull Request.

Returns:



68
69
70
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 68

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

#pr_titleString

The title of the Pull Request.

Returns:



44
45
46
# File 'lib/danger/danger_core/plugins/dangerfile_github_plugin.rb', line 44

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