Repository: djangopackages/djangopackages

Issue:
Add gitlab as a repo option
See https://pypi.python.org/pypi/pyapi-gitlab/6.2.3



Discussion:

Resolving patch:
diff --git a/package/repos/gitlab.py b/package/repos/gitlab.py
new file mode 100644
--- /dev/null
+++ b/package/repos/gitlab.py
@@ -0,0 +1,64 @@
+import re
+
+from django.conf import settings
+from gitlab import Gitlab
+
+from .base_handler import BaseHandler
+
+
+class GitlabHandler(BaseHandler):
+    title = "Github"
+    url_regex = "(http|https|git)://gitlab.com/"
+    url = "https://gitlab.com"
+    repo_regex = r"(?:http|https|git)://gitlab.com/[^/]*/([^/]*)"
+    slug_regex = repo_regex
+    gitlab: Gitlab
+
+    def __init__(self):
+        if settings.GITLAB_TOKEN:
+            self.gitlab = Gitlab(self.url, private_token=settings.GITLAB_TOKEN)
+        else:
+            self.gitlab = Gitlab(self.url)
+
+    def _get_repo(self, repo_url):
+        path = repo_url.replace(f"{self.url}/", "")
+        return self.gitlab.projects.get(path)
+
+    def fetch_metadata(self, package):
+        repo = self._get_repo(package.repo_url)
+        if repo is None:
+            return package
+
+        package.repo_watchers = repo.star_count
+        package.repo_forks = repo.forks_count
+        package.repo_description = repo.description
+
+        return package
+
+    def fetch_commits(self, package):
+
+        repo = self._get_repo(package.repo_url)
+        if repo is None:
+            return package
+
+        from package.models import Commit  # Added here to avoid circular imports
+
+        for commit in repo.commits.list(as_list=False):
+            try:
+                commit_record, created = Commit.objects.get_or_create(
+                    package=package,
+                    commit_date=commit.committed_date,
+                    commit_hash=commit.id,
+                )
+                if not created:
+                    break
+            except Commit.MultipleObjectsReturned:
+                continue
+            # If the commit record already exists, it means we are at the end of the
+            #   list we want to import
+
+        package.save()
+        return package
+
+
+repo_handler = GitlabHandler()
diff --git a/settings/base.py b/settings/base.py
--- a/settings/base.py
+++ b/settings/base.py
@@ -234,7 +234,7 @@
 if LOCAL_INSTALLED_APPS:
     INSTALLED_APPS.extend(LOCAL_INSTALLED_APPS)
 
-SUPPORTED_REPO.extend(["bitbucket", "github"])
+SUPPORTED_REPO.extend(["bitbucket", "github", "gitlab"])
 
 
 AUTHENTICATION_BACKENDS = (
@@ -408,6 +408,8 @@
 GITHUB_APP_ID = environ.get("GITHUB_APP_ID")
 GITHUB_TOKEN = environ.get("GITHUB_TOKEN")
 
+GITLAB_TOKEN = environ.get('GITLAB_TOKEN')
+
 ########### SEKURITY
 ALLOWED_HOSTS = ["*"]
 


Test patch:
diff --git a/package/tests/test_repos.py b/package/tests/test_repos.py
--- a/package/tests/test_repos.py
+++ b/package/tests/test_repos.py
@@ -233,6 +233,16 @@ def setUp(self):
     #     self.assertEqual(self.package.repo_watchers, 0)
     #     self.package.fetch_commits()
 
+class TestGitlabRepo(TestBaseHandler):
+    def setUp(self):
+        super(TestGitlabRepo, self).setUp()
+        self.package = Package.objects.create(
+            title="Django",
+            slug="django",
+            repo_url="https://gitlab.com/delta10/kees",
+            category=self.category
+        )
+
 
 class TestRepos(BaseBase):
     def test_repo_registry(self):