Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Commit

Permalink
support label filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Mar 9, 2017
1 parent f41a8a1 commit e48e45f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ resource_types:
* `disable_forks`: *Optional.* If set to `true`, it will filter out pull requests that
were created via users that forked from your repo.

* `label`: *Optional.* If set to a string it will only return pull requests that have been
marked with that specific label. It is case insensitive.

* `username`: *Optional.* Username for HTTP(S) auth when pulling/pushing.
This is needed when only HTTP/HTTPS protocol for git is available (which does not support private key auth)
and auth is required.
Expand Down
20 changes: 20 additions & 0 deletions assets/lib/filters/label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Filters
class Label
def initialize(pull_requests:, input: Input.instance)
@pull_requests = pull_requests
@input = input
end

def pull_requests
if @input.source.label
@memoized ||= @pull_requests.select do |pr|
issue = Octokit.issue(@input.source.repo, pr.id)
labels = issue[:labels] || []
labels.find { |l| l['name'].to_s.casecmp(@input.source.label.to_s.downcase).zero? }
end
else
@pull_requests
end
end
end
end
3 changes: 2 additions & 1 deletion assets/lib/repository.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require_relative 'filters/all'
require_relative 'filters/fork'
require_relative 'filters/label'
require_relative 'filters/path'

class Repository
attr_reader :name

def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Path, Filters::Fork])
def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Path, Filters::Fork, Filters::Label])
@filters = filters
@name = name
@input = input
Expand Down
43 changes: 43 additions & 0 deletions spec/filters/label_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative '../../assets/lib/filters/label'
require_relative '../../assets/lib/pull_request'
require 'webmock/rspec'

describe Filters::Label do
let(:ignore_pr) do
PullRequest.new(pr: { 'number' => 1 })
end

let(:pr) do
PullRequest.new(pr: { 'number' => 2 })
end

let(:pull_requests) { [ignore_pr, pr] }

def stub_json(uri, body)
stub_request(:get, uri)
.to_return(headers: { 'Content-Type' => 'application/json' }, body: body.to_json)
end

context 'when no label is specified' do
it 'does not filter' do
payload = { 'source' => { 'repo' => 'user/repo' } }
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))

expect(filter.pull_requests).to eq pull_requests
end
end

context 'when the label for filtering is provided' do
before do
stub_json(%r{https://api.github.com/repos/user/repo/issues/1}, 'labels' => [{ 'name' => 'feature' }])
stub_json(%r{https://api.github.com/repos/user/repo/issues/2}, 'labels' => [{ 'name' => 'bug' }])
end

it 'only returns PRs with that label' do
payload = { 'source' => { 'repo' => 'user/repo', 'label' => 'bug' } }
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))

expect(filter.pull_requests).to eq [pr]
end
end
end

0 comments on commit e48e45f

Please sign in to comment.