Skip to content

Commit

Permalink
Merge pull request #742 from nikromen/write-access-to-project
Browse files Browse the repository at this point in the history
Write access to project

Related to packit-service/#1654
RELEASE NOTES BEGIN
Using the method users_with_write_access you can generate the set of users that have write access to the project and the method has_write_access(user) you can find out if the user has write access to the project.
RELEASE NOTES END

Reviewed-by: None <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 15, 2022
2 parents 4cedd8e + a55ba11 commit 985a05f
Show file tree
Hide file tree
Showing 10 changed files with 6,245 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,22 @@ def get_contributors(self) -> Set[str]:
"""
raise NotImplementedError()

def users_with_write_access(self) -> Set[str]:
"""
Returns:
List of users who have write access to the project
"""
raise NotImplementedError("Use subclass instead.")

def has_write_access(self, user: str) -> bool:
"""
Decides whether a given user has write access to the project.
Args:
user: The user we are going to check to see if he/she has access
"""
return user in self.users_with_write_access()


class GitUser(OgrAbstractClass):
"""
Expand Down
3 changes: 3 additions & 0 deletions ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,6 @@ def get_contributors(self) -> Set[str]:
Logins of contributors to the project.
"""
return set(map(lambda c: c.login, self.github_repo.get_contributors()))

def users_with_write_access(self) -> Set[str]:
return self.__get_collaborators()
11 changes: 11 additions & 0 deletions ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,14 @@ def format_contributor(contributor: Dict[str, Any]) -> str:
return set(
map(format_contributor, self.gitlab_repo.repository_contributors(all=True))
)

def users_with_write_access(self) -> Set[str]:
return set(
self._get_collaborators_with_given_access(
access_levels=[
gitlab.const.DEVELOPER_ACCESS,
gitlab.const.MAINTAINER_ACCESS,
gitlab.const.OWNER_ACCESS,
]
)
)
8 changes: 8 additions & 0 deletions ogr/services/pagure/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,11 @@ def get_sha_from_branch(self, branch: str) -> Optional[str]:

def get_contributors(self) -> Set[str]:
raise OperationNotSupported("Pagure doesn't provide list of contributors")

def users_with_write_access(self) -> Set[str]:
users_with_access = self.get_project_info()["access_users"]
result = set()
for access_level in ["commit", "admin", "owner"]:
result.update(users_with_access[access_level])

return result

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/integration/github/test_generic_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,11 @@ def test_get_contributors(self):
owners = self.ogr_project.get_owners()
contributors = self.ogr_project.get_contributors()
assert len(owners) < len(contributors)

def test_write_access_to_repo(self):
users = self.ogr_project.users_with_write_access()
assert "TomasTomecek" in users
assert "naruto" not in users

assert self.ogr_project.has_write_access(user="csomh")
assert not self.ogr_project.has_write_access(user="miko")
Loading

0 comments on commit 985a05f

Please sign in to comment.