Use constants for GitLab permissions (RemoteRepository.admin field)
@@ -41,6 +41,10 @@ class GitLabService(Service):
re.escape(urlparse(adapter.provider_base_url).netloc),
)
+ PERMISSION_NO_ACCESS = 0
+ PERMISSION_MAINTAINER = 40
+ PERMISSION_OWNER = 50
+
def _get_repo_id(self, project):
# The ID or URL-encoded path of the project
# https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
@@ -133,8 +137,17 @@ class GitLabService(Service):
return remote_organizations, remote_repositories
def create_repository(self, fields, privacy=None, organization=None):
- """
- Update or create a repository from GitLab API response.
+ """Update or create a repository from GitLab API response.
+
+ ``admin`` field is computed using the ``permissions`` fields from the
+ repository response. The permission from GitLab is given by an integer:
+ * 0: No access
+ * (... others ...)
+ * 40: Maintainer
+ * 50: Owner
+
+ https://docs.gitlab.com/ee/api/access_requests.html
+ https://gitlab.com/help/user/permissions
:param fields: dictionary of response data from API
:param privacy: privacy level to support
@@ -176,16 +189,17 @@ class GitLabService(Service):
else:
repo.clone_url = fields['http_url_to_repo']
- # 0: No access
- # 40: Maintainer
- # 50: Owner
- # https://docs.gitlab.com/ee/api/access_requests.html
- # https://gitlab.com/help/user/permissions
+ project_access_level = group_access_level = self.PERMISSION_NO_ACCESS
project_access = fields.get('permissions', {}).get('project_access', {})
- project_access_level = project_access.get('access_level', 0) if project_access else 0
+ if project_access:
+ project_access_level = project_access.get('access_level', self.PERMISSION_NO_ACCESS)
group_access = fields.get('permissions', {}).get('group_access', {})
- group_access_level = group_access.get('access_level', 0) if group_access else 0
- repo.admin = project_access_level in (40, 50) or group_access_level in (40, 50)
+ if group_access:
+ group_access_level = group_access.get('access_level', self.PERMISSION_NO_ACCESS)
+ repo.admin = any([
+ project_access_level in (self.PERMISSION_MAINTAINER, self.PERMISSION_OWNER),
+ group_access_level in (self.PERMISSION_MAINTAINER, self.PERMISSION_OWNER),
+ ])
repo.vcs = 'git'
repo.account = self.account
