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

Commit 1ebf079

Browse files
nat-hendersonjtarchie
authored andcommitted
Split into several filters.
1 parent 1c9e054 commit 1ebf079

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

assets/lib/filters/approval.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Filters
2+
class Approval
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.require_review_approval
10+
@pull_requests.delete_if {|x| !x.review_approved? }
11+
end
12+
if @input.source.require_manual_approval
13+
@pull_requests.delete_if {|x| !x.approved_by_collaborator? }
14+
end
15+
if @input.source.authorship_restriction
16+
@pull_requests.delete_if {|x| !x.author_associated? }
17+
end
18+
19+
@pull_requests
20+
end
21+
end
22+
end

assets/lib/pull_request.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,23 @@ def from_fork?
1717
end
1818

1919
def mergeable?
20-
(@pr['mergeable'] &&
21-
@pr['base']['repo']['permissions']['push'] &&
22-
Octokit.pull_request_reviews(base_repo, id).any? {|r| r['state'] == 'APPROVED'})
20+
@pr['mergeable']
21+
end
22+
23+
def review_approved?
24+
Octokit.pull_request_reviews(base_repo, id).any? {|r| r['state'] == 'APPROVED'}
25+
end
26+
27+
def author_associated?
28+
# Checks whether the author is associated with the repo that the PR is against:
29+
# either the owner of that repo, someone invited to collaborate, or a member
30+
# of the organization who owns that repository.
31+
%w(OWNER COLLABORATOR MEMBER).include? @pr['author_association']
32+
end
33+
34+
def approved_by_collaborator?
35+
Octokit.pull_comments(base_repo, id).any? {|c| (%w(OWNER COLLABORATOR MEMBER).include?(c['author_association']) &&
36+
c['body'].downcase.include?('ci ok')) }
2337
end
2438

2539
def equals?(id:, sha:)

assets/lib/repository.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
require_relative 'filters/path'
77
require_relative 'filters/ci_skip'
88
require_relative 'filters/mergeable'
9+
require_relative 'filters/approval'
910

1011
class Repository
1112
attr_reader :name
1213

13-
def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Path, Filters::Fork, Filters::Label, Filters::CISkip, Filters::Mergeable])
14+
def initialize(name:, input: Input.instance, filters: [Filters::All, Filters::Path, Filters::Fork, Filters::Label, Filters::CISkip, Filters::Mergeable, Filters::Approval])
1415
@filters = filters
1516
@name = name
1617
@input = input

0 commit comments

Comments
 (0)