Skip to content

Commit

Permalink
add who_can_marge_pr and who_can_close_issue
Browse files Browse the repository at this point in the history
  • Loading branch information
marusinm committed Jul 11, 2019
1 parent 311478a commit 171f1fc
Show file tree
Hide file tree
Showing 9 changed files with 1,120 additions and 255 deletions.
16 changes: 15 additions & 1 deletion ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import datetime
from enum import IntEnum
from typing import Optional, Match, List, Dict
from typing import Optional, Match, List, Dict, Set
from urllib.request import urlopen

from ogr.parsing import parse_git_repo
Expand Down Expand Up @@ -366,6 +366,20 @@ def get_owners(self) -> List[str]:
"""
raise NotImplementedError()

def who_can_close_issue(self) -> Set[str]:
"""
Get all usernames who have permissions to modify an Issue
:return: Set of usernames
"""
raise NotImplementedError()

def who_can_merge_pr(self) -> Set[str]:
"""
Get all usernames who have permissions to modify a PR
:return: Set of usernames
"""
raise NotImplementedError()

def can_close_issue(self, username: str, issue: Issue) -> bool:
"""
Check if user have permissions to modify an Issue
Expand Down
47 changes: 33 additions & 14 deletions ogr/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# SOFTWARE.

import logging
from typing import Optional, Dict, List, Type
from typing import Optional, Dict, List, Type, Set

import github
from github import (
Expand Down Expand Up @@ -233,35 +233,54 @@ def get_owners(self) -> List[str]:
# in case of github, repository has only one owner
return [self.github_repo.owner.login]

def can_close_issue(self, username: str, issue: Issue) -> bool:
def who_can_close_issue(self) -> Set[str]:
try:
collaborators = self._get_collaborators_with_permission()
except github.GithubException:
logger.debug(
f"{username} must have push access to view repository permissions."
f"Current Github token must have push access to view repository permissions."
)
return False
return set()

usernames = []
for login, permission in collaborators.items():
if permission in ["admin", "write"] and username == login:
return True
if permission in ["admin", "write"]:
usernames.append(login)

if username == issue.author:
return True
return set(usernames)

return False

def can_merge_pr(self, username) -> bool:
def who_can_merge_pr(self) -> Set[str]:
try:
collaborators = self._get_collaborators_with_permission()
except github.GithubException:
logger.debug(
f"{username} must have push access to view repository permissions."
f"Current Github token must have push access to view repository permissions."
)
return False
return set()

usernames = []
for login, permission in collaborators.items():
if permission in ["admin", "write"] and username == login:
if permission in ["admin", "write"]:
usernames.append(login)

return set(usernames)

def can_close_issue(self, username: str, issue: Issue) -> bool:
allowed_users = self.who_can_close_issue()

for allowed_user in allowed_users:
if username == allowed_user:
return True
if username == issue.author:
return True

return False

def can_merge_pr(self, username) -> bool:
allowed_users = self.who_can_merge_pr()

for allowed_user in allowed_users:
if username == allowed_user:
return True

return False
Expand Down
25 changes: 16 additions & 9 deletions ogr/services/pagure.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,28 +361,35 @@ def get_owners(self) -> List[str]:
project = self.get_project_info()
return project["access_users"]["owner"]

def can_close_issue(self, username: str, issue: Issue) -> bool:
def who_can_close_issue(self) -> Set[str]:
users: Set[str] = set()
project = self.get_project_info()
users.update(project["access_users"]["admin"])
users.update(project["access_users"]["commit"])
users.update(project["access_users"]["ticket"])
users.update(project["access_users"]["owner"])
if username in users:
return True
return users

def who_can_merge_pr(self) -> Set[str]:
users: Set[str] = set()
project = self.get_project_info()
users.update(project["access_users"]["admin"])
users.update(project["access_users"]["commit"])
users.update(project["access_users"]["owner"])
return users

def can_close_issue(self, username: str, issue: Issue) -> bool:
allowed_users = self.who_can_close_issue()
if username in allowed_users:
return True
if username == issue.author:
return True

return False

def can_merge_pr(self, username) -> bool:
users: Set[str] = set()
project = self.get_project_info()
users.update(project["access_users"]["admin"])
users.update(project["access_users"]["commit"])
users.update(project["access_users"]["owner"])
if username in users:
allowed_users = self.who_can_merge_pr()
if username in allowed_users:
return True

return False
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,49 @@ GET:
ok: true
reason: OK
status_code: 200
- content: !!binary |
ewogICJhY2Nlc3NfZ3JvdXBzIjogewogICAgImFkbWluIjogW10sCiAgICAiY29tbWl0IjogW10s
CiAgICAidGlja2V0IjogW10KICB9LAogICJhY2Nlc3NfdXNlcnMiOiB7CiAgICAiYWRtaW4iOiBb
XSwKICAgICJjb21taXQiOiBbXSwKICAgICJvd25lciI6IFsKICAgICAgIm1hcnVzaW5tIgogICAg
XSwKICAgICJ0aWNrZXQiOiBbXQogIH0sCiAgImNsb3NlX3N0YXR1cyI6IFtdLAogICJjdXN0b21f
a2V5cyI6IFtdLAogICJkYXRlX2NyZWF0ZWQiOiAiMTU2MTI5NzA5MSIsCiAgImRhdGVfbW9kaWZp
ZWQiOiAiMTU2MTI5NzA5MSIsCiAgImRlc2NyaXB0aW9uIjogIk5vIHB1cnBvc2UgcHJvamVjdCBw
dWJsaXNoZWQgb24gUGFndXJlLiIsCiAgImZ1bGxuYW1lIjogIm9nci10ZXN0IiwKICAiaWQiOiA2
MzgzLAogICJtaWxlc3RvbmVzIjoge30sCiAgIm5hbWUiOiAib2dyLXRlc3QiLAogICJuYW1lc3Bh
Y2UiOiBudWxsLAogICJwYXJlbnQiOiBudWxsLAogICJwcmlvcml0aWVzIjoge30sCiAgInRhZ3Mi
OiBbXSwKICAidXJsX3BhdGgiOiAib2dyLXRlc3QiLAogICJ1c2VyIjogewogICAgImZ1bGxuYW1l
IjogIk1hcmVrIE1hcnVcdTAxNjFpbiIsCiAgICAibmFtZSI6ICJtYXJ1c2lubSIKICB9Cn0=
json:
access_groups:
admin: []
commit: []
ticket: []
access_users:
admin: []
commit: []
owner:
- marusinm
ticket: []
close_status: []
custom_keys: []
date_created: '1561297091'
date_modified: '1561297091'
description: No purpose project published on Pagure.
fullname: ogr-test
id: 6383
milestones: {}
name: ogr-test
namespace: null
parent: null
priorities: {}
tags: []
url_path: ogr-test
user:
fullname: "Marek Maru\u0161in"
name: marusinm
ok: true
reason: OK
status_code: 200
https://pagure.io/api/0/ogr-test/issue/1:
empty:
empty:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,46 @@ GET:
ok: true
reason: OK
status_code: 200
- content: !!binary |
ewogICJhY2Nlc3NfZ3JvdXBzIjogewogICAgImFkbWluIjogW10sCiAgICAiY29tbWl0IjogW10s
CiAgICAidGlja2V0IjogW10KICB9LAogICJhY2Nlc3NfdXNlcnMiOiB7CiAgICAiYWRtaW4iOiBb
XSwKICAgICJjb21taXQiOiBbXSwKICAgICJvd25lciI6IFsKICAgICAgIm1hcnVzaW5tIgogICAg
XSwKICAgICJ0aWNrZXQiOiBbXQogIH0sCiAgImNsb3NlX3N0YXR1cyI6IFtdLAogICJjdXN0b21f
a2V5cyI6IFtdLAogICJkYXRlX2NyZWF0ZWQiOiAiMTU2MTI5NzA5MSIsCiAgImRhdGVfbW9kaWZp
ZWQiOiAiMTU2MTI5NzA5MSIsCiAgImRlc2NyaXB0aW9uIjogIk5vIHB1cnBvc2UgcHJvamVjdCBw
dWJsaXNoZWQgb24gUGFndXJlLiIsCiAgImZ1bGxuYW1lIjogIm9nci10ZXN0IiwKICAiaWQiOiA2
MzgzLAogICJtaWxlc3RvbmVzIjoge30sCiAgIm5hbWUiOiAib2dyLXRlc3QiLAogICJuYW1lc3Bh
Y2UiOiBudWxsLAogICJwYXJlbnQiOiBudWxsLAogICJwcmlvcml0aWVzIjoge30sCiAgInRhZ3Mi
OiBbXSwKICAidXJsX3BhdGgiOiAib2dyLXRlc3QiLAogICJ1c2VyIjogewogICAgImZ1bGxuYW1l
IjogIk1hcmVrIE1hcnVcdTAxNjFpbiIsCiAgICAibmFtZSI6ICJtYXJ1c2lubSIKICB9Cn0=
json:
access_groups:
admin: []
commit: []
ticket: []
access_users:
admin: []
commit: []
owner:
- marusinm
ticket: []
close_status: []
custom_keys: []
date_created: '1561297091'
date_modified: '1561297091'
description: No purpose project published on Pagure.
fullname: ogr-test
id: 6383
milestones: {}
name: ogr-test
namespace: null
parent: null
priorities: {}
tags: []
url_path: ogr-test
user:
fullname: "Marek Maru\u0161in"
name: marusinm
ok: true
reason: OK
status_code: 200
6 changes: 6 additions & 0 deletions tests/integration/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,17 @@ def test_get_owners(self):
assert ["user-cont"] == owners

def test_issue_permissions(self):
users = self.colin_project.who_can_close_issue()
assert "usercont-release-bot" in users

issue = self.colin_project.get_issue_info(1)
assert self.colin_project.can_close_issue("usercont-release-bot", issue)
assert not self.colin_project.can_close_issue("marusinm", issue)

def test_pr_permissions(self):
users = self.colin_project.who_can_merge_pr()
assert "usercont-release-bot" in users

assert self.colin_project.can_merge_pr("usercont-release-bot")
assert not self.colin_project.can_merge_pr("marusinm")

Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_pagure.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,15 @@ def test_get_owners(self):
assert ["marusinm"] == owners

def test_issue_permissions(self):
owners = self.ogr_test_project.who_can_close_issue()
assert "marusinm" in owners

issue = self.ogr_test_project.get_issue_info(1)
assert self.ogr_test_project.can_close_issue("marusinm", issue)

def test_pr_permissions(self):
owners = self.ogr_test_project.who_can_merge_pr()
assert "marusinm" in owners
assert self.ogr_test_project.can_merge_pr("marusinm")


Expand Down

0 comments on commit 171f1fc

Please sign in to comment.