Skip to content

Commit f28a4b7

Browse files
committed
Merge pull request travis-ci#99 from travis-ci/mm-dont-sync-duplicate-org-repos
Don't sync duplicate org repos
2 parents d646628 + e05660b commit f28a4b7

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

lib/travis/github/sync/repositories.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,31 @@ def remove
5252

5353
# we have to filter these ourselves because the github api is broken for this
5454
def data
55-
@data ||= fetch.select { |repo| repo['private'] == self.class.private? }
55+
@data ||= filter_duplicates(filter_based_on_repo_permission)
56+
end
57+
58+
def filter_based_on_repo_permission
59+
fetch.select { |repo| repo['private'] == self.class.private? }
60+
end
61+
62+
def filter_duplicates(repositories)
63+
repositories.each_with_object([]) do |repository, filtered_list|
64+
unless in_filtered_list?(filtered_list, repository)
65+
filtered_list.push(repository)
66+
end
67+
end
68+
end
69+
70+
def in_filtered_list?(filtered_list, other_repository)
71+
filtered_list.any? do |existing_repository|
72+
same_repository_with_admin?(existing_repository, other_repository)
73+
end
74+
end
75+
76+
def same_repository_with_admin?(existing_repository, other_repository)
77+
existing_repository['owner']['login'] == other_repository['owner']['login'] and
78+
existing_repository['name'] == other_repository['name'] and
79+
existing_repository['permissions']['admin'] == true
5680
end
5781

5882
def slugs
@@ -86,6 +110,3 @@ def with_github(&block)
86110
end
87111
end
88112
end
89-
90-
91-

spec/travis/github/sync/repositories_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,45 @@
7373
Travis::Github::Sync::Repository.expects(:unpermit_all).with(user, [removed_repo])
7474
sync.run
7575
end
76+
77+
context "with private forks of organization repositories" do
78+
let(:user_repositories) {[
79+
{ 'name' => 'public', 'owner' => { 'login' => 'sven' }, 'permissions' => { 'admin' => true }, 'private' => false },
80+
{ 'name' => 'private', 'owner' => { 'login' => 'sven' }, 'permissions' => { 'admin' => true }, 'private' => true, 'fork' => true}
81+
]}
82+
let(:duplicate_org_repositories) {[
83+
{ 'name' => 'private', 'owner' => { 'login' => 'sven' }, 'permissions' => { 'admin' => false }, 'private' => true, 'fork' => true}
84+
]}
85+
let(:org_repositories) {[
86+
{ 'name' => 'other', 'owner' => { 'login' => 'sven' }, 'permissions' => { 'admin' => false }, 'private' => true, 'fork' => true}
87+
]}
88+
let(:order) {sequence('github-sync')}
89+
90+
before do
91+
Travis::Github::Sync::Repositories.type = 'private'
92+
Travis::Github::Sync::Repository.unstub(:new)
93+
end
94+
95+
it "should not sync the organization's duplicate" do
96+
Travis::Github::Sync::Repository.expects(:new).once.returns(stub('repository', :run => public_repo))
97+
GH.expects(:[]).with('user/repos').returns(user_repositories).in_sequence(order)
98+
GH.expects(:[]).with('orgs/the-org/repos').returns(duplicate_org_repositories).in_sequence(order)
99+
sync.run
100+
end
101+
102+
it "should sync the organization's repository when it's not a duplicate" do
103+
Travis::Github::Sync::Repository.expects(:new).twice.returns(stub('repository', :run => public_repo))
104+
GH.expects(:[]).with('user/repos').returns(user_repositories).in_sequence(order)
105+
GH.expects(:[]).with('orgs/the-org/repos').returns(org_repositories).in_sequence(order)
106+
sync.run
107+
end
108+
109+
it "should sync the organization's repository when it has admin rights" do
110+
# this is an unlikely scenario, but as the code checks for it, a test is in order
111+
Travis::Github::Sync::Repository.expects(:new).twice.returns(stub('repository', :run => public_repo))
112+
GH.expects(:[]).with('user/repos').returns(duplicate_org_repositories).in_sequence(order)
113+
GH.expects(:[]).with('orgs/the-org/repos').returns(user_repositories).in_sequence(order)
114+
sync.run
115+
end
116+
end
76117
end

0 commit comments

Comments
 (0)