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

Commit 1976b95

Browse files
JT ArchieJT Archie
JT Archie
authored and
JT Archie
committed
support paths and ignore_paths for #42
1 parent 9ab1168 commit 1976b95

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ were created via users that forked from your repo.
6565

6666
* `password`: *Optional.* Password for HTTP(S) auth when pulling/pushing.
6767

68+
* `paths`: *Optional.* If specified (as a list of glob patterns), only changes
69+
to the specified files will yield new versions from `check`.
70+
71+
* `ignore_paths`: *Optional.* The inverse of `paths`; changes to the specified
72+
files are ignored.
73+
6874
* `skip_ssl_verification`: *Optional.* Skips git ssl verification by exporting
6975
`GIT_SSL_NO_VERIFY=true`.
7076

assets/lib/filters/path.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative '../input'
2+
3+
module Filters
4+
class Path
5+
def initialize(pull_requests:, input: Input.instance)
6+
@pull_requests = pull_requests
7+
@input = input
8+
end
9+
10+
def pull_requests
11+
paths = @input.source.paths || []
12+
ignore_paths = @input.source.ignore_paths || []
13+
14+
return @pull_requests if paths.empty? && ignore_paths.empty?
15+
16+
@memoized ||= @pull_requests.select do |pr|
17+
files = Octokit.pull_request_files(@input.source.repo, pr.id)
18+
unless paths.empty?
19+
files.select! do |file|
20+
paths.find do |path|
21+
File.fnmatch?(path, file['filename'])
22+
end
23+
end
24+
end
25+
unless ignore_paths.empty?
26+
files.reject! do |file|
27+
ignore_paths.find do |path|
28+
File.fnmatch?(path, file['filename'])
29+
end
30+
end
31+
end
32+
!files.empty?
33+
end
34+
end
35+
end
36+
end

assets/lib/repository.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
require_relative 'filters/all'
22
require_relative 'filters/context'
33
require_relative 'filters/fork'
4+
require_relative 'filters/path'
45

56
class Repository
67
attr_reader :name
78

8-
def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Fork, Filters::Context])
9+
def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Path, Filters::Fork, Filters::Context])
910
@filters = filters
1011
@name = name
1112
@input = input

spec/filters/path_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require_relative '../../assets/lib/filters/path'
2+
require_relative '../../assets/lib/pull_request'
3+
require 'webmock/rspec'
4+
5+
describe Filters::Path do
6+
let(:ignore_pr) do
7+
PullRequest.new(pr: { 'number' => 1 })
8+
end
9+
10+
let(:pr) do
11+
PullRequest.new(pr: { 'number' => 2 })
12+
end
13+
14+
let(:pull_requests) { [ignore_pr, pr] }
15+
16+
def stub_json(uri, body)
17+
stub_request(:get, uri)
18+
.to_return(headers: { 'Content-Type' => 'application/json' }, body: body.to_json)
19+
end
20+
21+
context 'when paths are not specified' do
22+
it 'does not filter' do
23+
payload = { 'source' => { 'repo' => 'user/repo' } }
24+
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))
25+
26+
expect(filter.pull_requests).to eq pull_requests
27+
end
28+
end
29+
30+
context 'when paths are specified' do
31+
before do
32+
stub_json(%r{https://api.github.com/repos/user/repo/pulls/1/files}, [{ 'filename' => 'test.cpp' }])
33+
stub_json(%r{https://api.github.com/repos/user/repo/pulls/2/files}, [{ 'filename' => 'README.md' }])
34+
end
35+
36+
context 'that is an ignore path' do
37+
it 'filters out PRs that match that path' do
38+
payload = { 'source' => { 'repo' => 'user/repo', 'ignore_paths' => ['README.md'] } }
39+
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))
40+
41+
expect(filter.pull_requests).to eq [ignore_pr]
42+
end
43+
end
44+
45+
context 'that is a path' do
46+
it 'only return PRs that match that path' do
47+
payload = { 'source' => { 'repo' => 'user/repo', 'paths' => ['README.md'] } }
48+
filter = described_class.new(pull_requests: pull_requests, input: Input.instance(payload: payload))
49+
50+
expect(filter.pull_requests).to eq [pr]
51+
end
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)