Skip to content

Commit

Permalink
Merge pull request packit#583 from mfocko/check-runs
Browse files Browse the repository at this point in the history
Refactoring

Reviewed-by: None <None>
Reviewed-by: Matej Focko <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored May 25, 2021
2 parents 19d3b93 + ecfda3f commit 49053ef
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 126 deletions.
9 changes: 6 additions & 3 deletions ogr/services/github/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from github.Issue import Issue as _GithubIssue

from ogr.abstract import Issue, IssueComment, IssueStatus
from ogr.exceptions import GithubAPIException
from ogr.exceptions import GithubAPIException, OperationNotSupported
from ogr.services import github as ogr_github
from ogr.services.base import BaseIssue
from ogr.services.github.comments import GithubIssueComment
Expand Down Expand Up @@ -102,14 +102,17 @@ def create(
labels: Optional[List[str]] = None,
assignees: Optional[list] = None,
) -> "Issue":
if private:
raise OperationNotSupported("Private issues are not supported by Github")

github_issue = project.github_repo.create_issue(
title=title, body=body, labels=labels or [], assignees=assignees or []
)
return GithubIssue(github_issue, project)

@staticmethod
def get(project: "ogr_github.GithubProject", id: int) -> "Issue":
issue = project.github_repo.get_issue(number=id)
def get(project: "ogr_github.GithubProject", issue_id: int) -> "Issue":
issue = project.github_repo.get_issue(number=issue_id)
return GithubIssue(issue, project)

@staticmethod
Expand Down
47 changes: 17 additions & 30 deletions ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from ogr.services.github.issue import GithubIssue
from ogr.services.github.pull_request import GithubPullRequest
from ogr.services.github.release import GithubRelease
from ogr.utils import filter_paths
from ogr.utils import filter_paths, indirect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -277,20 +277,21 @@ def _get_collaborators_with_permission(self) -> dict:
collaborators[user.login] = permission
return collaborators

@indirect(GithubIssue.get_list)
def get_issue_list(
self,
status: IssueStatus = IssueStatus.open,
author: Optional[str] = None,
assignee: Optional[str] = None,
labels: Optional[List[str]] = None,
) -> List[Issue]:
return GithubIssue.get_list(
project=self, status=status, author=author, assignee=assignee, labels=labels
)
pass

@indirect(GithubIssue.get)
def get_issue(self, issue_id: int) -> Issue:
return GithubIssue.get(project=self, id=issue_id)
pass

@indirect(GithubIssue.create)
def create_issue(
self,
title: str,
Expand All @@ -299,20 +300,18 @@ def create_issue(
labels: Optional[List[str]] = None,
assignees: Optional[List[str]] = None,
) -> Issue:
if private:
raise OperationNotSupported("Private issues are not supported by Github")
return GithubIssue.create(
project=self, title=title, body=body, labels=labels, assignees=assignees
)
pass

def delete(self) -> None:
self.github_repo.delete()

@indirect(GithubPullRequest.get_list)
def get_pr_list(self, status: PRStatus = PRStatus.open) -> List[PullRequest]:
return GithubPullRequest.get_list(project=self, status=status)
pass

@indirect(GithubPullRequest.get)
def get_pr(self, pr_id: int) -> PullRequest:
return GithubPullRequest.get(project=self, id=pr_id)
pass

def get_sha_from_tag(self, tag_name: str) -> str:
# TODO: This is ugly. Can we do it better?
Expand All @@ -330,6 +329,7 @@ def get_tag_from_tag_name(self, tag_name: str) -> Optional[GitTag]:
return None

@if_readonly(return_function=GitProjectReadOnly.create_pr)
@indirect(GithubPullRequest.create)
def create_pr(
self,
title: str,
Expand All @@ -338,14 +338,7 @@ def create_pr(
source_branch: str,
fork_username: str = None,
) -> PullRequest:
return GithubPullRequest.create(
project=self,
title=title,
body=body,
target_branch=target_branch,
source_branch=source_branch,
fork_username=fork_username,
)
pass

@if_readonly(
return_function=GitProjectReadOnly.commit_comment,
Expand Down Expand Up @@ -376,6 +369,7 @@ def commit_comment(
return_function=GitProjectReadOnly.set_commit_status,
log_message="Create a status on a commit",
)
@indirect(GithubCommitFlag.set)
def set_commit_status(
self,
commit: str,
Expand All @@ -397,24 +391,17 @@ def set_commit_status(
github.GithubException
:return:
"""
return GithubCommitFlag.set(
project=self,
commit=commit,
state=state,
target_url=target_url,
description=description,
context=context,
trim=trim,
)
pass

@indirect(GithubCommitFlag.get)
def get_commit_statuses(self, commit: str) -> List[CommitFlag]:
"""
Get status of the commit.
:param commit: str
:return: [CommitFlag]
"""
return GithubCommitFlag.get(project=self, commit=commit)
pass

def get_git_urls(self) -> Dict[str, str]:
return {"git": self.github_repo.clone_url, "ssh": self.github_repo.ssh_url}
Expand Down
4 changes: 2 additions & 2 deletions ogr/services/github/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def __get_fork(fork_username: str, repo: _GithubRepository) -> _GithubRepository
return forks[0]

@staticmethod
def get(project: "ogr_github.GithubProject", id: int) -> "PullRequest":
pr = project.github_repo.get_pull(number=id)
def get(project: "ogr_github.GithubProject", pr_id: int) -> "PullRequest":
pr = project.github_repo.get_pull(number=pr_id)
return GithubPullRequest(pr, project)

@staticmethod
Expand Down
17 changes: 13 additions & 4 deletions ogr/services/gitlab/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,30 @@ def create(
labels: Optional[List[str]] = None,
assignees: Optional[List[str]] = None,
) -> "Issue":
assignee_ids = []
for user in assignees or []:
users_list = project.service.gitlab_instance.users.list(username=user)

if not users_list:
raise GitlabAPIException(f"Unable to find '{user}' username")

assignee_ids.append(str(users_list[0].id))

data = {"title": title, "description": body}
if labels:
data["labels"] = ",".join(labels)
if assignees:
data["assignee_ids"] = ",".join(assignees)
data["assignee_ids"] = ",".join(assignee_ids)

issue = project.gitlab_repo.issues.create(data, confidential=private)
return GitlabIssue(issue, project)

@staticmethod
def get(project: "ogr_gitlab.GitlabProject", id: int) -> "Issue":
def get(project: "ogr_gitlab.GitlabProject", issue_id: int) -> "Issue":
try:
return GitlabIssue(project.gitlab_repo.issues.get(id), project)
return GitlabIssue(project.gitlab_repo.issues.get(issue_id), project)
except gitlab.exceptions.GitlabGetError as ex:
raise GitlabAPIException(f"Issue {id} was not found. ", ex)
raise GitlabAPIException(f"Issue {issue_id} was not found. ", ex)

@staticmethod
def get_list(
Expand Down
60 changes: 17 additions & 43 deletions ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ogr.services.gitlab.issue import GitlabIssue
from ogr.services.gitlab.pull_request import GitlabPullRequest
from ogr.services.gitlab.release import GitlabRelease
from ogr.utils import filter_paths
from ogr.utils import filter_paths, indirect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -244,8 +244,9 @@ def request_access(self) -> None:
except gitlab.exceptions.GitlabCreateError as e:
raise GitlabAPIException("Unable to request access", e)

@indirect(GitlabPullRequest.get_list)
def get_pr_list(self, status: PRStatus = PRStatus.open) -> List["PullRequest"]:
return GitlabPullRequest.get_list(project=self, status=status)
pass

def get_sha_from_tag(self, tag_name: str) -> str:
try:
Expand All @@ -255,6 +256,7 @@ def get_sha_from_tag(self, tag_name: str) -> str:
logger.error(f"Tag {tag_name} was not found.")
raise GitlabAPIException(f"Tag {tag_name} was not found.", ex)

@indirect(GitlabPullRequest.create)
def create_pr(
self,
title: str,
Expand All @@ -263,14 +265,7 @@ def create_pr(
source_branch: str,
fork_username: str = None,
) -> "PullRequest":
return GitlabPullRequest.create(
project=self,
title=title,
body=body,
target_branch=target_branch,
source_branch=source_branch,
fork_username=fork_username,
)
pass

def commit_comment(
self, commit: str, body: str, filename: str = None, row: int = None
Expand Down Expand Up @@ -298,6 +293,7 @@ def commit_comment(
raw_comment = commit_object.comments.create({"note": body})
return self._commit_comment_from_gitlab_object(raw_comment, commit)

@indirect(GitlabCommitFlag.set)
def set_commit_status(
self,
commit: str,
Expand All @@ -318,23 +314,16 @@ def set_commit_status(
:param trim: Whether to trim the description to 140 characters
:return: CommitFlag
"""
return GitlabCommitFlag.set(
project=self,
commit=commit,
state=state,
target_url=target_url,
description=description,
context=context,
trim=trim,
)
pass

@indirect(GitlabCommitFlag.get)
def get_commit_statuses(self, commit: str) -> List[CommitFlag]:
"""
Get the statuses of a commit in a project.
:param commit: The SHA of the commit.
:return: [CommitFlag]
"""
return GitlabCommitFlag.get(project=self, commit=commit)
pass

def get_git_urls(self) -> Dict[str, str]:
return {
Expand Down Expand Up @@ -397,20 +386,21 @@ def get_files(

return paths

@indirect(GitlabIssue.get_list)
def get_issue_list(
self,
status: IssueStatus = IssueStatus.open,
author: Optional[str] = None,
assignee: Optional[str] = None,
labels: Optional[List[str]] = None,
) -> List[Issue]:
return GitlabIssue.get_list(
project=self, status=status, author=author, assignee=assignee, labels=labels
)
pass

@indirect(GitlabIssue.get)
def get_issue(self, issue_id: int) -> Issue:
return GitlabIssue.get(project=self, id=issue_id)
pass

@indirect(GitlabIssue.create)
def create_issue(
self,
title: str,
Expand All @@ -419,27 +409,11 @@ def create_issue(
labels: Optional[List[str]] = None,
assignees: Optional[List[str]] = None,
) -> Issue:
pass

ids = []
for user in assignees or []:
users_list = self.service.gitlab_instance.users.list(username=user)

if not users_list:
raise GitlabAPIException(f"Unable to find '{user}' username")

ids.append(str(users_list[0].id))

return GitlabIssue.create(
project=self,
title=title,
body=body,
private=private,
labels=labels,
assignees=ids,
)

@indirect(GitlabPullRequest.get)
def get_pr(self, pr_id: int) -> PullRequest:
return GitlabPullRequest.get(project=self, id=pr_id)
pass

def get_tags(self) -> List["GitTag"]:
tags = self.gitlab_repo.tags.list()
Expand Down
4 changes: 2 additions & 2 deletions ogr/services/gitlab/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def __get_fork(
return forks[0]

@staticmethod
def get(project: "ogr_gitlab.GitlabProject", id: int) -> "PullRequest":
mr = project.gitlab_repo.mergerequests.get(id)
def get(project: "ogr_gitlab.GitlabProject", pr_id: int) -> "PullRequest":
mr = project.gitlab_repo.mergerequests.get(pr_id)
return GitlabPullRequest(mr, project)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions ogr/services/pagure/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def create(
return PagureIssue(new_issue, project)

@staticmethod
def get(project: "ogr_pagure.PagureProject", id: int) -> "Issue":
raw_issue = project._call_project_api("issue", str(id))
def get(project: "ogr_pagure.PagureProject", issue_id: int) -> "Issue":
raw_issue = project._call_project_api("issue", str(issue_id))
return PagureIssue(raw_issue, project)

@staticmethod
Expand Down
Loading

0 comments on commit 49053ef

Please sign in to comment.