Skip to content

Commit

Permalink
add function for adding assignees to issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KPostOffice committed Jun 4, 2021
1 parent 2742a57 commit 92b74b0
Show file tree
Hide file tree
Showing 11 changed files with 2,022 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ def add_label(self, *labels: str) -> None:
"""
raise NotImplementedError()

def add_assignee(self, *assignees: str) -> None:
"""
Assign users to an issue.
:param assignees: [str]
"""
raise NotImplementedError()


class PRStatus(IntEnum):
open = 1
Expand Down
3 changes: 3 additions & 0 deletions ogr/services/github/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,6 @@ def close(self) -> "Issue":
def add_label(self, *labels: str) -> None:
for label in labels:
self._raw_issue.add_to_labels(label)

def add_assignee(self, *assignees: str) -> None:
self._raw_issue.edit(assignees=list(assignees))
2 changes: 1 addition & 1 deletion ogr/services/github/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def project_create(

new_repo = owner.create_repo(
name=repo,
description=description if description else github.GithubObject.NotSet,
description=description if description else github.GithubObject.NotSet, # type: ignore
)
return GithubProject(
repo=repo,
Expand Down
20 changes: 19 additions & 1 deletion ogr/services/gitlab/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def url(self) -> str:

@property
def assignees(self) -> list:
return self._raw_issue.assignees
try:
return self._raw_issue.assignees
except AttributeError:
return None # if issue has no assignees, the attribute is not present

@property
def description(self) -> str:
Expand Down Expand Up @@ -171,3 +174,18 @@ def add_label(self, *labels: str) -> None:
for label in labels:
self._raw_issue.labels.append(label)
self._raw_issue.save()

def add_assignee(self, *assignees: str) -> None:
assignee_ids = self._raw_issue.__dict__.get("assignee_ids") or []
for assignee in assignees:
users = self.project.service.gitlab_instance.users.list( # type: ignore
username=assignee
)
if not users:
raise GitlabAPIException(f"Unable to find '{assignee}' username")
uid = str(users[0].id)
if uid not in assignee_ids:
assignee_ids.append(str(users[0].id))

self._raw_issue.assignee_ids = assignee_ids
self._raw_issue.save()
13 changes: 12 additions & 1 deletion ogr/services/pagure/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def url(self) -> str:
@property
def assignee(self) -> str:
self.__update()
return self._raw_issue["assignee"]["name"]
try:
return self._raw_issue["assignee"]["name"]
except Exception:
return None

@property
def description(self) -> str:
Expand Down Expand Up @@ -203,3 +206,11 @@ def close(self) -> "PagureIssue":
)
self.__dirty = True
return self

def add_assignee(self, *assignees: str) -> None:
if len(assignees) > 1:
raise PagureAPIException("Pagure does not support multiple assignees")
payload = {"assignee": assignees[0]}
self.project._call_project_api(
"issue", str(self.id), "assign", data=payload, method="POST"
)

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions tests/integration/github/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ def test_issue_labels(self):
assert labels[0].name == "test_lb1"
assert labels[1].name == "test_lb2"

def test_issue_assignees(self):
"""
Remove the assignees from this issue before regenerating the response files:
https://github.com/packit/ogr/issues/4
"""
project = self.service.get_project(repo="ogr", namespace="KPostOffice")
issue = project.get_issue(4)
print(self.service.user.get_username())
assignees = issue.assignees

assert not assignees
issue.add_assignee("KPostOffice")
assignees = project.get_issue(4).assignees
assert len(assignees) == 1
assert assignees[0].login == "KPostOffice"

def test_list_contains_only_issues(self):
issue_list_all = self.ogr_project.get_issue_list(status=IssueStatus.all)
issue_ids = [issue.id for issue in issue_list_all]
Expand Down
Loading

0 comments on commit 92b74b0

Please sign in to comment.