def project_create( self, repo: str, namespace: Optional[str] = None, description: Optional[str] = None, ) -> "GithubProject": if namespace: try: owner = self.github.get_organization(namespace) except UnknownObjectException as ex: raise GithubAPIException( f"Group {namespace} not found.") from ex else: owner = self.github.get_user() try: new_repo = owner.create_repo( name=repo, description=description if description else github.GithubObject.NotSet, ) except github.GithubException as ex: raise GithubAPIException("Project creation failed") from ex return GithubProject( repo=repo, namespace=namespace or owner.login, service=self, github_repo=new_repo, )
def __init__(self, raw_issue: _GithubIssue, project: "ogr_github.GithubProject") -> None: if raw_issue.pull_request: raise GithubAPIException( f"Requested issue #{raw_issue.number} is a pull request") super().__init__(raw_issue=raw_issue, project=project)
def get(project: "ogr_github.GithubProject", pr_id: int) -> "PullRequest": try: pr = project.github_repo.get_pull(number=pr_id) except github.UnknownObjectException as ex: raise GithubAPIException( f"No pull request with id {pr_id} found") from ex return GithubPullRequest(pr, project)
def get(project: "ogr_github.GithubProject", issue_id: int) -> "Issue": try: issue = project.github_repo.get_issue(number=issue_id) except github.UnknownObjectException as ex: raise GithubAPIException( f"No issue with id {issue_id} found") from ex return GithubIssue(issue, project)
def add_user(self, user: str, access_level: AccessLevel) -> None: access_dict = { AccessLevel.pull: "Pull", AccessLevel.triage: "Triage", AccessLevel.push: "Push", AccessLevel.admin: "Admin", AccessLevel.maintain: "Maintain", } try: invitation = self.github_repo.add_to_collaborators( user, permission=access_dict[access_level]) except Exception as ex: raise GithubAPIException(f"User {user} not found") from ex if invitation is None: raise GithubAPIException("User already added")
def get_sha_from_tag(self, tag_name: str) -> str: # TODO: This is ugly. Can we do it better? all_tags = self.github_repo.get_tags() for tag in all_tags: if tag.name == tag_name: return tag.commit.sha raise GithubAPIException(f"Tag {tag_name} was not found.")
def __get_fork(fork_username: str, repo: _GithubRepository) -> _GithubRepository: forks = list( filter(lambda fork: fork.owner.login == fork_username, repo.get_forks()) ) if not forks: raise GithubAPIException("Requested fork doesn't exist") return forks[0]
def update_info( self, title: Optional[str] = None, description: Optional[str] = None ) -> "PullRequest": try: self._raw_pr.edit(title=title, body=description) logger.info(f"PR updated: {self._raw_pr.url}") return self except Exception as ex: raise GithubAPIException("there was an error while updating the PR", ex)
def update_pr_info(self, pr_id: int, title: str, description: str): """ Update pull-request information. :param pr_id: int The ID of the pull request :param title: str The title of the pull request :param description str The description of the pull request :return: PullRequest """ pr = self.github_repo.get_pull(number=pr_id) if not pr: raise GithubAPIException("PR was not found.") try: pr.edit(title=title, body=description) logger.info(f"PR updated: {pr.url}") return self._pr_from_github_object(pr) except Exception as ex: raise GithubAPIException("there was an error while updating the PR", ex)
def get_file_content(self, path: str, ref=None) -> str: ref = ref or self.default_branch try: return self.github_repo.get_contents( path=path, ref=ref).decoded_content.decode() except (UnknownObjectException, GithubException) as ex: if ex.status == 404: raise FileNotFoundError( f"File '{path}' on {ref} not found") from ex raise GithubAPIException() from ex
def wrapper(*args, **kwargs): try: return function(*args, **kwargs) except github.BadCredentialsException as ex: raise GithubAPIException("Invalid Github credentials") from ex except gitlab.GitlabAuthenticationError as ex: raise GitlabAPIException("Invalid Gitlab credentials") from ex except requests.exceptions.ConnectionError as ex: raise OgrNetworkError( "Could not perform the request due to a network error") from ex
def get_release(self, identifier=None, name=None, tag_name=None) -> GithubRelease: if tag_name: identifier = self._release_id_from_tag(tag_name) elif name: identifier = self._release_id_from_name(name) if identifier is None: raise GithubAPIException("Release was not found.") release = self.github_repo.get_release(id=identifier) return self._release_from_github_object( raw_release=release, git_tag=self.get_tag_from_tag_name(release.tag_name) )
def _issue_from_github_object(github_issue: GithubIssue) -> Issue: if github_issue.pull_request: raise GithubAPIException( f"Requested issue #{github_issue.number} is a pull request") return Issue( title=github_issue.title, id=github_issue.number, status=IssueStatus[github_issue.state], url=github_issue.html_url, description=github_issue.body, author=github_issue.user.login, created=github_issue.created_at, )
def github_app_private_key(self): if self._github_app_private_key: return self._github_app_private_key if self.github_app_private_key_path: if not Path(self.github_app_private_key_path).is_file(): raise GithubAPIException( f"File with the github-app private key " f"({self.github_app_private_key_path}) " f"does not exist.") return Path(self.github_app_private_key_path).read_text() return None
def project_create(self, repo: str, namespace: str = None) -> "GithubProject": if namespace: try: owner = self.github.get_organization(namespace) except UnknownObjectException: raise GithubAPIException(f"Group {namespace} not found.") else: owner = self.github.get_user() new_repo = owner.create_repo(name=repo) return GithubProject( repo=repo, namespace=namespace or owner.login, service=self, github_repo=new_repo, )
def add_assignee(self, *assignees: str) -> None: try: self._raw_issue.edit(assignees=list(assignees)) except github.GithubException as ex: raise GithubAPIException( "Failed to assign issue, unknown user") from ex
def __get_issue(self, number: int) -> GithubIssue: issue = self.github_repo.get_issue(number=number) if issue.pull_request: raise GithubAPIException( f"Requested issue #{number} is a pull request") return issue