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

Commit 0f06d79

Browse files
JT ArchieJT Archie
JT Archie
authored and
JT Archie
committed
disable PRs from forks
1 parent 76d1bfc commit 0f06d79

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ resource_types:
5656
through, without relying on a status being on it. This feature should only be used in
5757
concourse version 1.2.x and higher and the [`version: every`](http://concourse.ci/get-step.html#get-version).
5858

59+
* `disbale_forks`: *Optional.* If set to `true`, it will not filter out pull requests that
60+
were created via users that forked from your repo.
61+
5962
* `username`: *Optional.* Username for HTTP(S) auth when pulling/pushing.
6063
This is needed when only HTTP/HTTPS protocol for git is available (which does not support private key auth)
6164
and auth is required.

Diff for: assets/lib/filters/fork.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Filters
2+
class Fork
3+
def initialize(pull_requests:, input: Input.instance)
4+
@pull_requests = pull_requests
5+
@input = input
6+
end
7+
8+
def pull_requests
9+
if @input.source.disable_forks
10+
@memoized ||= @pull_requests.delete_if(&:from_fork?)
11+
else
12+
@pull_requests
13+
end
14+
end
15+
end
16+
end

Diff for: assets/lib/pull_request.rb

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def initialize(pr:)
1010
@pr = pr
1111
end
1212

13+
def from_fork?
14+
base_repo != head_repo
15+
end
16+
1317
def equals?(id:, sha:)
1418
[self.sha, self.id.to_s] == [sha, id.to_s]
1519
end
@@ -33,4 +37,14 @@ def sha
3337
def url
3438
@pr['html_url']
3539
end
40+
41+
private
42+
43+
def base_repo
44+
@pr['base']['repo']['full_name']
45+
end
46+
47+
def head_repo
48+
@pr['head']['repo']['full_name']
49+
end
3650
end

Diff for: assets/lib/repository.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
require_relative 'filters/all'
22
require_relative 'filters/context'
3+
require_relative 'filters/fork'
34

45
class Repository
56
attr_reader :name
67

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

Diff for: spec/commands/check_spec.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def stub_json(uri, body)
160160
context 'when there is more than one open pull request' do
161161
before do
162162
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [
163-
{ number: 2, head: { sha: 'zyxwvu' } },
164-
{ number: 1, head: { sha: 'abcdef' } }
163+
{ number: 2, head: { sha: 'zyxwvu', repo: { full_name: 'someotherowner/repo' } }, base: { repo: { full_name: 'jtarchie/test' } } },
164+
{ number: 1, head: { sha: 'abcdef', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } }
165165
])
166166
end
167167

@@ -186,6 +186,14 @@ def stub_json(uri, body)
186186
expect(check(payload)).to eq []
187187
end
188188
end
189+
190+
context 'and `disallow_forks` is set to true' do
191+
it 'returns the most recently updated internal pull request' do
192+
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [])
193+
194+
expect(check('source' => { 'repo' => 'jtarchie/test', 'disable_forks' => true })).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }]
195+
end
196+
end
189197
end
190198
end
191199
end

0 commit comments

Comments
 (0)