From 4a2242e4795ae71922adae792f0e072ea27d361a Mon Sep 17 00:00:00 2001 From: Nikita Shulga Date: Wed, 1 Nov 2023 10:32:37 -0700 Subject: [PATCH] [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 --- .github/scripts/github_utils.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/scripts/github_utils.py b/.github/scripts/github_utils.py index dd385f0ffbd7..a1a52c1ecf83 100644 --- a/.github/scripts/github_utils.py +++ b/.github/scripts/github_utils.py @@ -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, )