Skip to content

Commit

Permalink
Merge pull request #426 from DataRecce/feature/drc-570-recce-run-mess…
Browse files Browse the repository at this point in the history
…age-refinement

[Feature] Refine PR information warning message
  • Loading branch information
wcchang1115 authored Sep 24, 2024
2 parents 27137f6 + c699965 commit 7919a3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
28 changes: 10 additions & 18 deletions recce/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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():
Expand Down
5 changes: 4 additions & 1 deletion recce/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions recce/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')

Expand Down Expand Up @@ -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.')

Expand Down

0 comments on commit 7919a3e

Please # to comment.