diff --git a/recce/github.py b/recce/github.py index c572617d..af75c454 100644 --- a/recce/github.py +++ b/recce/github.py @@ -3,7 +3,7 @@ import re import zipfile from datetime import datetime -from typing import List +from typing import List, Tuple, Optional import requests from github import Artifact, Github, Auth, UnknownObjectException, PullRequest @@ -116,7 +116,7 @@ def recce_base_artifact(**kwargs): pass -def get_pull_request(branch, owner, repo_name, github_token=None): +def get_pull_request(branch, owner, repo_name, github_token=None) -> Tuple[Optional[type(PullRequest)], Optional[str]]: g = Github(auth=Auth.Token(github_token)) if github_token else Github() try: @@ -125,36 +125,28 @@ def get_pull_request(branch, owner, repo_name, github_token=None): for pr in pulls: if pr.head.ref == branch: - return pr + return pr, None except UnknownObjectException: - if github_token is None: - print(f"Repository {owner}/{repo_name} not found. Please provide '$GITHUB_TOKEN' environment variable.") - else: - print( - f"Repository {owner}/{repo_name} not found. If it is private repo, please add the 'repo' scope to the token.") - return None + if github_token is not None: + return None, f"Repository {owner}/{repo_name} not found. If it is private repo, please add the 'repo' scope to the token." - return None + return None, None -def recce_pr_information(github_token=None) -> PullRequest: +def recce_pr_information(github_token=None) -> Tuple[Optional[type(PullRequest)], Optional[str]]: branch = current_branch() repo = hosting_repo() if not repo: - print('This is not a git repository.') - return + return None, 'This is not a git repository.' if '/' not in repo: - print('This is not a GitHub repository.') - return + return None, 'This is not a GitHub repository.' owner, repo_name = repo.split('/') github_token = github_token if github_token else os.getenv("GITHUB_TOKEN") - pr = get_pull_request(branch, owner, repo_name, github_token) - - return pr if pr else None + return get_pull_request(branch, owner, repo_name, github_token) def is_github_codespace(): diff --git a/recce/pull_request.py b/recce/pull_request.py index 63b55d12..8da7deeb 100644 --- a/recce/pull_request.py +++ b/recce/pull_request.py @@ -34,7 +34,10 @@ def fetch_pr_metadata(**kwargs): pr_info.repository = metadata.get('github_repository') else: repo = hosting_repo() - pr = recce_pr_information(github_token=kwargs.get('github_token')) + pr, message = recce_pr_information(github_token=kwargs.get('github_token')) + if kwargs.get('cloud') and message: + print(message) + if pr: pr_info.repository = repo pr_info.id = pr.number diff --git a/recce/state.py b/recce/state.py index 464bdb69..d5ba7675 100644 --- a/recce/state.py +++ b/recce/state.py @@ -179,7 +179,7 @@ def __init__(self, if self.cloud_mode: if not self.cloud_options.get('token'): raise Exception('No GitHub token is provided to access the pull request information.') - self.pr_info = fetch_pr_metadata(github_token=self.cloud_options.get('token')) + self.pr_info = fetch_pr_metadata(cloud=self.cloud_mode, github_token=self.cloud_options.get('token')) if self.pr_info.id is None: raise Exception('Cannot get the pull request information from GitHub.') @@ -483,7 +483,7 @@ def __init__(self, cloud_options: Optional[Dict[str, str]] = None): if not self.cloud_options.get('token'): raise Exception('No GitHub token is provided to access the pull request information.') - self.pr_info = fetch_pr_metadata(github_token=self.cloud_options.get('token')) + self.pr_info = fetch_pr_metadata(cloud=True, github_token=self.cloud_options.get('token')) if self.pr_info.id is None: raise Exception('Cannot get the pull request information from GitHub.')