[BE] Use GITHUB_API_URL (#112613)

To avoid hardcoding the same string constant over and over again
Pull Request resolved: https://github.com/pytorch/pytorch/pull/112613
Approved by: https://github.com/seemethere
This commit is contained in:
Nikita Shulga
2023-11-01 10:32:37 -07:00
committed by PyTorch MergeBot
parent fd209543d5
commit 4a2242e479

View File

@ -11,6 +11,9 @@ from urllib.parse import quote
from urllib.request import Request, urlopen
GITHUB_API_URL = "https://api.github.com"
@dataclass
class GitHubComment:
body_text: str
@ -33,7 +36,7 @@ def gh_fetch_url_and_headers(
if headers is None:
headers = {}
token = os.environ.get("GITHUB_TOKEN")
if token is not None and url.startswith("https://api.github.com/"):
if token is not None and url.startswith(f"{GITHUB_API_URL}/"):
headers["Authorization"] = f"token {token}"
data_ = None
@ -129,7 +132,7 @@ def gh_post_pr_comment(
org: str, repo: str, pr_num: int, comment: str, dry_run: bool = False
) -> List[Dict[str, Any]]:
return _gh_post_comment(
f"https://api.github.com/repos/{org}/{repo}/issues/{pr_num}/comments",
f"{GITHUB_API_URL}/repos/{org}/{repo}/issues/{pr_num}/comments",
comment,
dry_run,
)
@ -139,14 +142,14 @@ def gh_post_commit_comment(
org: str, repo: str, sha: str, comment: str, dry_run: bool = False
) -> List[Dict[str, Any]]:
return _gh_post_comment(
f"https://api.github.com/repos/{org}/{repo}/commits/{sha}/comments",
f"{GITHUB_API_URL}/repos/{org}/{repo}/commits/{sha}/comments",
comment,
dry_run,
)
def gh_delete_comment(org: str, repo: str, comment_id: int) -> None:
url = f"https://api.github.com/repos/{org}/{repo}/issues/comments/{comment_id}"
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/issues/comments/{comment_id}"
gh_fetch_url(url, method="DELETE")
@ -157,7 +160,7 @@ def gh_fetch_merge_base(org: str, repo: str, base: str, head: str) -> str:
# https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits
try:
json_data = gh_fetch_url(
f"https://api.github.com/repos/{org}/{repo}/compare/{base}...{head}",
f"{GITHUB_API_URL}/repos/{org}/{repo}/compare/{base}...{head}",
headers={"Accept": "application/vnd.github.v3+json"},
reader=json.load,
)