Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 901eadb

Browse files
paveldedikaexvir
authored andcommitted
feat(repos): Add env variable to filter out forks
Refs #92
1 parent 6367468 commit 901eadb

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

test/repos/tasks/test_sync_repos.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ def __init__(self, pid):
1414

1515

1616
class FakeGitlabProject(FakeGitProject):
17-
def __init__(self, pid, owner, name, url):
17+
def __init__(self, pid, owner, name, url, is_fork=False):
1818
super().__init__(pid)
1919
self.namespace = {"full_path": self.fake.word() if owner is None else owner}
2020
self.path = self.fake.word() if name is None else name
2121
self.web_url = self.fake.url() if url is None else url
2222

23+
if is_fork:
24+
self.forked_from_project = {"id": self.fake.pyint()}
25+
2326

2427
class FakeGithubProject(FakeGitProject):
25-
def __init__(self, pid, owner, name, url):
28+
def __init__(self, pid, owner, name, url, is_fork=False):
2629
super().__init__(pid)
2730

2831
class FakeOwner:
@@ -33,12 +36,13 @@ def __init__(self, login):
3336
self.owner = FakeOwner(login)
3437
self.name = self.fake.word() if name is None else name
3538
self.svn_url = self.fake.url() if url is None else url
39+
self.fork = is_fork
3640

3741

3842
def generate_project_list(pid=None, owner=None, name=None, url=None, **kwargs):
3943
return {
40-
"gitlab": [FakeGitlabProject(pid, owner, name, url)],
41-
"github": [FakeGithubProject(pid + 1, owner, name, url)],
44+
"gitlab": [FakeGitlabProject(pid, owner, name, url, **kwargs)],
45+
"github": [FakeGithubProject(pid + 1, owner, name, url, **kwargs)],
4246
}
4347

4448

@@ -92,3 +96,30 @@ def test_sync_moved_repo(repository):
9296
assert github_project.owner.login == repository.owner
9397
assert github_project.name == repository.name
9498
assert github_project.svn_url == repository.url
99+
100+
101+
def test_sync_skip_forks():
102+
project_list = generate_project_list(pid=1, is_fork=True)
103+
104+
with patch(
105+
"gitlab.v4.objects.ProjectManager.list", return_value=project_list["gitlab"]
106+
), patch(
107+
"github.AuthenticatedUser.AuthenticatedUser.get_repos",
108+
return_value=project_list["github"],
109+
), patch(
110+
"zoo.repos.tasks.settings.SYNC_REPOS_SKIP_FORKS", True
111+
):
112+
sync_repos()
113+
114+
gitlab_project = project_list["gitlab"][0]
115+
github_project = project_list["github"][0]
116+
117+
with pytest.raises(Repository.DoesNotExist):
118+
Repository.objects.get(
119+
remote_id=gitlab_project.id, provider=Provider.GITLAB.value
120+
)
121+
122+
with pytest.raises(Repository.DoesNotExist):
123+
Repository.objects.get(
124+
remote_id=github_project.id, provider=Provider.GITHUB.value
125+
)

zoo/base/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
ZOO_SENTRY_URL=(str, None),
3939
ZOO_SENTRY_ORGANIZATION=(str, None),
4040
ZOO_SENTRY_API_KEY=(str, None),
41+
ZOO_SYNC_REPOS_SKIP_FORKS=(bool, False),
4142
ZOO_AUDITING_CHECKS=(list, []),
4243
ZOO_AUDITING_DROP_ISSUES=(int, 7),
4344
ZOO_SONARQUBE_URL=(str, None),
@@ -251,4 +252,6 @@
251252

252253
GCP_SERVICE_KEY = env("GCP_SERVICE_KEY")
253254

255+
SYNC_REPOS_SKIP_FORKS = env("ZOO_SYNC_REPOS_SKIP_FORKS")
256+
254257
logs.configure_structlog(DEBUG)

zoo/repos/github.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def get_repositories():
2424
"owner": repo.owner.login,
2525
"name": repo.name,
2626
"url": repo.svn_url,
27+
"is_fork": repo.fork,
2728
}
2829
except GithubException:
2930
log.exception("github.get_repositories.error")

zoo/repos/gitlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def get_repositories():
3939
"owner": project.namespace["full_path"],
4040
"name": project.path,
4141
"url": project.web_url,
42+
"is_fork": hasattr(project, "forked_from_project"),
4243
}
4344
except (MissingSchema, GitlabGetError):
4445
log.exception("gitlab.get_repositories.error")

zoo/repos/tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44

55
from celery import shared_task
6+
from django.conf import settings
67
import structlog
78

89
from ..analytics.tasks import repo_analyzers
@@ -22,6 +23,8 @@ def sync_repos():
2223
for project in itertools.chain(
2324
get_github_repositories(), get_gitlab_repositories()
2425
):
26+
if settings.SYNC_REPOS_SKIP_FORKS and project["is_fork"]:
27+
continue
2528
try:
2629
repo = Repository.objects.get(
2730
remote_id=project["id"], provider=project["provider"]

0 commit comments

Comments
 (0)