Skip to content

Commit 25b6fc9

Browse files
committed
Fix git remote operations no longer using the token
Also fix up some comments
1 parent d255704 commit 25b6fc9

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

llvm/utils/llvm_push_pr.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _request_and_parse_json(
152152
self, method: str, endpoint: str, json_payload: Optional[dict] = None
153153
) -> dict:
154154
with self._request(method, endpoint, json_payload) as response:
155-
# expect a 200 'OK' or 201 'Created' status on success and JSON body.
155+
# Expect a 200 'OK' or 201 'Created' status on success and JSON body.
156156
self._log_unexpected_status([200, 201], response.status)
157157

158158
response_text = response.read().decode("utf-8")
@@ -164,8 +164,8 @@ def _request_no_content(
164164
self, method: str, endpoint: str, json_payload: Optional[dict] = None
165165
) -> None:
166166
with self._request(method, endpoint, json_payload) as response:
167-
# expected a 204 No Content status on success,
168-
# indicating the operation was successful but there is no body.
167+
# Expected a 204 No Content status on success, indicating the
168+
# operation was successful but there is no body.
169169
self._log_unexpected_status([204], response.status)
170170

171171
def _log_unexpected_status(
@@ -374,10 +374,9 @@ def _rebase_current_branch(self) -> None:
374374
git_env[LLVM_GITHUB_TOKEN_VAR] = self.config.token
375375
git_env["GIT_TERMINAL_PROMPT"] = "0"
376376

377-
self._run_cmd(
378-
["git", "fetch", self.config.upstream_remote, self.config.base_branch],
379-
env=git_env,
380-
)
377+
https_upstream_url = self._get_https_url_for_remote(self.config.upstream_remote)
378+
refspec = f"refs/heads/{self.config.base_branch}:refs/remotes/{self.config.upstream_remote}/{self.config.base_branch}"
379+
self._run_cmd(["git", "fetch", https_upstream_url, refspec], env=git_env)
381380

382381
try:
383382
self._run_cmd(["git", "rebase", target], env=git_env)
@@ -407,13 +406,8 @@ def _rebase_current_branch(self) -> None:
407406
self._run_cmd(["git", "rebase", "--abort"], check=False, env=git_env)
408407
raise LlvmPrError("rebase operation failed.") from e
409408

410-
def _get_authenticated_remote_url(self, remote_name: str) -> str:
411-
"""
412-
Generates an authenticated URL to use for all operations. This includes
413-
for local operations, like rebasing after merging a PR in a stack.
414-
This allows the script to avoid reauthenticating (e.g. via ssh), since
415-
the token can be reused for all operations.
416-
"""
409+
def _get_https_url_for_remote(self, remote_name: str) -> str:
410+
"""Gets the URL for a remote and converts it to HTTPS if necessary."""
417411
remote_url_result = self._run_cmd(
418412
["git", "remote", "get-url", remote_name],
419413
capture_output=True,
@@ -422,12 +416,12 @@ def _get_authenticated_remote_url(self, remote_name: str) -> str:
422416
)
423417
remote_url = remote_url_result.stdout.strip()
424418
if remote_url.startswith("[email protected]:"):
425-
return remote_url.replace(
426-
"[email protected]:", f"https://{self.config.token}@github.com/"
427-
)
419+
return remote_url.replace("[email protected]:", "https://github.com/")
428420
if remote_url.startswith("https://github.com/"):
429-
return remote_url.replace("https://", f"https://{self.config.token}@")
430-
raise LlvmPrError(f"Unsupported remote URL format: {remote_url}")
421+
return remote_url
422+
raise LlvmPrError(
423+
f"Unsupported remote URL format for {remote_name}: {remote_url}"
424+
)
431425

432426
def _get_commit_stack(self) -> List[str]:
433427
target = f"{self.config.upstream_remote}/{self.config.base_branch}"
@@ -450,6 +444,8 @@ def _get_commit_stack(self) -> List[str]:
450444
return result.stdout.strip().splitlines()
451445

452446
def _get_commit_details(self, commit_hash: str) -> tuple[str, str]:
447+
# Get the subject and body from git show. Insert "\n\n" between to make
448+
# parsing simple to do w/ split.
453449
result = self._run_cmd(
454450
["git", "show", "-s", "--format=%s%n%n%b", commit_hash],
455451
capture_output=True,
@@ -493,10 +489,12 @@ def _create_and_push_branch_for_commit(
493489
git_env[LLVM_GITHUB_TOKEN_VAR] = self.config.token
494490
git_env["GIT_TERMINAL_PROMPT"] = "0"
495491

492+
https_remote_url = self._get_https_url_for_remote(self.remote)
493+
496494
push_command = [
497495
"git",
498496
"push",
499-
self.remote,
497+
https_remote_url,
500498
f"{commit_hash}:refs/heads/{branch_name}",
501499
]
502500
self._run_cmd(push_command, env=git_env)
@@ -519,21 +517,21 @@ def _process_commit(
519517
draft=self.config.draft,
520518
)
521519

522-
if not self.config.no_merge:
523-
if self.config.auto_merge:
524-
self.github_api.enable_auto_merge(pr_url)
525-
else:
526-
merged_branch = self.github_api.merge_pr(pr_url)
527-
if merged_branch and not self.repo_settings.get(
528-
"delete_branch_on_merge"
529-
):
530-
# After a merge, the temporary branch should be deleted from
531-
# the user's fork.
532-
self.github_api.delete_branch(merged_branch)
533-
if temp_branch in self.created_branches:
534-
# If the branch was successfully merged, it should not be deleted
535-
# again during cleanup.
536-
self.created_branches.remove(temp_branch)
520+
if self.config.no_merge:
521+
return
522+
523+
if self.config.auto_merge:
524+
self.github_api.enable_auto_merge(pr_url)
525+
else:
526+
merged_branch = self.github_api.merge_pr(pr_url)
527+
if merged_branch and not self.repo_settings.get("delete_branch_on_merge"):
528+
# After a merge, the branch should be deleted.
529+
self.github_api.delete_branch(merged_branch)
530+
531+
if temp_branch in self.created_branches:
532+
# If the branch was successfully merged, it should not be deleted
533+
# again during cleanup.
534+
self.created_branches.remove(temp_branch)
537535

538536
def run(self) -> None:
539537
self.repo_settings = self.github_api.get_repo_settings()
@@ -562,8 +560,8 @@ def run(self) -> None:
562560
if i > 0:
563561
self._rebase_current_branch()
564562

565-
# After a rebase, the commit hashes change, so we need to get
566-
# the latest commit stack.
563+
# After a rebase, the commit hashes can change, so we need to
564+
# get the latest commit stack.
567565
commits = self._get_commit_stack()
568566
if not commits:
569567
self.runner.print("Success! All commits have been landed.")
@@ -668,9 +666,6 @@ def main() -> None:
668666

669667
github_api = GitHubAPI(command_runner, token)
670668
if not args.login:
671-
# Create a temporary API client to get the user login.
672-
# We need the user login for the branch prefix and for creating PRs
673-
# from a fork.
674669
try:
675670
args.login = github_api.get_user_login()
676671
except urllib.error.HTTPError as e:

0 commit comments

Comments
 (0)