Class: Danger::DangerGithubExt

Inherits:
DangerfileGitHubPlugin
  • Object
show all
Defined in:
lib/github_ext/plugin.rb

Overview

This is Danger Plugin for GitHub extension. When installing this plugin, you can additional methods on github instance

Examples:

Determine if pull request is mergeable and mergeable status is clean


github.mergeable?

List labels for the pull request


github.labels

Add labels to the pull request


github.add_labels 'build ok'

Remove labels from the pull request


github.remove_labels 'build failed'

List current statuses for the head commit


github.statuses

Update the title of the pull request


github.update_pr_tile 'Updated title'

Update the body of the pull request


github.update_pr_body 'Updated body'

Close the pull request


github.close

Open the pull request


github.open

See Also:

  • duck8823/danger-github_ext

Instance Method Summary collapse

Constructor Details

#initialize(dangerfile) ⇒ DangerGithubExt

Returns a new instance of DangerGithubExt.



46
47
48
49
50
# File 'lib/github_ext/plugin.rb', line 46

def initialize(dangerfile)
  super(dangerfile)

  self.api.auto_paginate = true
end

Instance Method Details

#add_label(label, color = 'ffffff') ⇒ void

This method returns an undefined value.

Add label with color to the pull request

Parameters:

  • label (String)
  • color (String) (defaults to: 'ffffff')


86
87
88
89
90
91
92
# File 'lib/github_ext/plugin.rb', line 86

def add_label(label, color = 'ffffff')
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.update_label(@repo, label, {:color => color})
  self.api.add_labels_to_an_issue(@repo, @number, Array(label))

end

#add_labels(labels) ⇒ void

This method returns an undefined value.

Add labels to the pull request

Parameters:

  • labels ([String])


75
76
77
78
79
# File 'lib/github_ext/plugin.rb', line 75

def add_labels(labels)
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.add_labels_to_an_issue(@repo, @number, Array(labels))
end

#labels[String]

Deprecated.

Please use #pr_labels instead

List labels for the pull request

Returns:

  • ([String])


63
64
65
66
67
68
69
# File 'lib/github_ext/plugin.rb', line 63

def labels
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.labels_for_issue(@repo, @number).map { |issue|
    issue.name
  }
end

#mergeable?boolean

Determine if pull request is mergeable and mergeable status is clean

Returns:

  • (boolean)


55
56
57
# File 'lib/github_ext/plugin.rb', line 55

def mergeable?
  self.pr_json.attrs[:mergeable_state] == 'clean' && github.pr_json.attrs[:mergeable]
end

#remove_labels(labels) ⇒ void

This method returns an undefined value.

Remove labels from the pull request

Parameters:

  • labels ([String])


98
99
100
101
102
103
104
# File 'lib/github_ext/plugin.rb', line 98

def remove_labels(labels)
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  Array(labels).each do |label|
    self.api.remove_label(@repo, @number, label)
  end
end

#statuses[Hash]

List current statuses for the head commit

Returns:

  • ([Hash])


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/github_ext/plugin.rb', line 109

def statuses
  @repo ||= self.pr_json.base.repo.full_name
  @sha  ||= self.head_commit
  statuses = {}
  self.api.statuses(@repo, @sha).each do |status|
    statuses[status.context] ||= []
    statuses[status.context].push({
                                      context: status.context,
                                      state: status.state,
                                      date: status.updated_at
                                  })
  end
  statuses.map {|_, val|
    val.sort{|a, b| b[:date] <=> a[:date] }[0]
  }
end

#update_pr_body(body) ⇒ Sawyer::Resource

Update the body of pull request

Returns:

  • (Sawyer::Resource)


138
139
140
141
142
# File 'lib/github_ext/plugin.rb', line 138

def update_pr_body(body)
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.update_pull_request(@repo, @number, {:body => body})
end

#update_pr_state(state) ⇒ Sawyer::Resource

Update the pull request state

Returns:

  • (Sawyer::Resource)


147
148
149
150
151
# File 'lib/github_ext/plugin.rb', line 147

def update_pr_state(state)
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.update_pull_request(@repo, @number, {:state => state})
end

#update_pr_title(title) ⇒ Sawyer::Resource

Update the title of the pull request

Returns:

  • (Sawyer::Resource)


129
130
131
132
133
# File 'lib/github_ext/plugin.rb', line 129

def update_pr_title(title)
  @repo ||= self.pr_json.base.repo.full_name
  @number ||= self.pr_json.number
  self.api.update_pull_request(@repo, @number, {:title => title})
end