Skip to content

Commit 8dff43c

Browse files
Fix remote tracking does not support SSH (#256)
Fixes #244 So far, only the hands-ons are affected. Exercises are not affected until Tour 8, where pushing to remote repo is required. List of updated `hands-ons` are listed in the Incident Review below. **Solution**: Implemented method `get_github_git_protocol` to obtain the GitHub preferred protocol (`https` or `ssh`) Implemented method `get_remote_url` to get the correct remote url for a repo. `get_remote_url` calls `get_github_git_protocol` to check for the protocol that user is using, before configuring the url accordingly. for some hands-ons where forking is required, and we push to the forked repo: - use `clone_repo_with_gh` - remove the upstream with `remove_remote("upstream", verbose)` for hands-on where we have to do `git remote add origin` manually: - obtain the correct remote url using `get_remote_url` before adding it manually. Tested using both `https` and `ssh` protocol, no unexpected behavior so far. refer to this [Incident Review](https://docs.google.com/document/d/1Fw-BgXCHorESJnvw0w2GDfrpn8DyUvg7Q3X9WBJk-KE/edit?tab=t.0#heading=h.puki3yyz9ga3) for details --------- Co-authored-by: Vikram Goyal <vikramgoyalsg@gmail.com>
1 parent 5f3c511 commit 8dff43c

File tree

7 files changed

+53
-10
lines changed

7 files changed

+53
-10
lines changed

exercise_utils/github_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def get_github_username(verbose: bool) -> str:
5454
return ""
5555

5656

57+
def get_github_git_protocol(verbose: bool) -> str:
58+
"""returns GitHub CLI's preferred Git transport protocol"""
59+
result = run(["gh", "config", "get", "git_protocol"], verbose)
60+
if result.is_success():
61+
protocol = result.stdout.splitlines()[0].strip()
62+
return protocol
63+
return ""
64+
65+
5766
def has_repo(repo_name: str, is_fork: bool, verbose: bool) -> bool:
5867
"""Returns if the given repository exists under the current user's repositories."""
5968
command = ["gh", "repo", "view", repo_name]
@@ -107,3 +116,15 @@ def get_fork_name(
107116
forkname = result.stdout.splitlines()[0]
108117
return forkname
109118
return ""
119+
120+
121+
def get_remote_url(repository_name: str, verbose: bool) -> str:
122+
"""Returns a remote repo url based on the configured git protocol"""
123+
remote_url = f"https://github.com/{repository_name}.git"
124+
preferred_protocol = get_github_git_protocol(verbose)
125+
126+
if preferred_protocol == "ssh":
127+
remote_url = f"git@github.com:{repository_name}.git"
128+
129+
return remote_url
130+

hands_on/fetch_merge.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
from logging import info
12
from exercise_utils.cli import run_command
23
import os
34

45
from exercise_utils.git import clone_repo_with_git
6+
from exercise_utils.github_cli import get_remote_url
57

68
__requires_git__ = True
7-
__requires_github__ = False
9+
__requires_github__ = True
810

911

1012
def download(verbose: bool):
13+
ahead_repo = "git-mastery/samplerepo-finances-2"
14+
ahead_repo_url = get_remote_url(ahead_repo, verbose)
15+
1116
clone_repo_with_git(
1217
"https://github.com/git-mastery/samplerepo-finances.git", verbose
1318
)
@@ -18,7 +23,7 @@ def download(verbose: bool):
1823
"remote",
1924
"set-url",
2025
"origin",
21-
"https://github.com/git-mastery/samplerepo-finances-2.git",
26+
ahead_repo_url,
2227
],
2328
verbose,
2429
)

hands_on/force_push.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from exercise_utils.github_cli import (
55
clone_repo_with_gh,
66
create_repo,
7+
get_remote_url,
78
get_github_username,
89
)
910

@@ -16,14 +17,18 @@
1617

1718

1819
def download(verbose: bool):
20+
username = get_github_username(verbose)
21+
remote_repo = f"{username}/{REPO_NAME}"
22+
remote_url = get_remote_url(remote_repo, verbose)
23+
1924
create_repo(REPO_NAME, verbose)
2025
clone_repo_with_gh(UPSTREAM_REPO, verbose, WORK_DIR)
2126
os.chdir(WORK_DIR)
2227
remove_remote("origin", verbose)
2328

2429
add_remote(
2530
"origin",
26-
f"https://github.com/{get_github_username(verbose)}/{REPO_NAME}",
31+
remote_url,
2732
verbose,
2833
)
2934
run_command(["git", "push", "-u", "origin", "main"], verbose)

hands_on/populate_remote.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from exercise_utils.github_cli import (
66
create_repo,
77
delete_repo,
8+
get_remote_url,
89
get_github_username,
910
has_repo,
1011
)
@@ -60,7 +61,9 @@ def _create_things_repository(verbose: bool):
6061

6162
def _link_repositories(verbose: bool):
6263
full_repo_name = _get_full_repo_name(verbose)
63-
add_remote("origin", f"https://github.com/{full_repo_name}", verbose)
64+
remote_url = get_remote_url(full_repo_name, verbose)
65+
66+
add_remote("origin", remote_url, verbose)
6467

6568

6669
def _get_full_repo_name(verbose: bool) -> str:

hands_on/remote_branch_pull.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22

33
from exercise_utils.file import append_to_file
4-
from exercise_utils.git import add, checkout, clone_repo_with_git, commit, push
4+
from exercise_utils.git import add, checkout, clone_repo_with_git, commit, push, remove_remote
55
from exercise_utils.github_cli import (
6+
clone_repo_with_gh,
67
get_github_username,
78
fork_repo,
89
has_repo,
@@ -26,10 +27,11 @@ def download(verbose: bool):
2627
delete_repo(full_repo_name, verbose)
2728

2829
fork_repo(TARGET_REPO, FORK_NAME, verbose, False)
29-
clone_repo_with_git(f"https://github.com/{full_repo_name}", verbose, LOCAL_DIR)
30+
clone_repo_with_gh(full_repo_name, verbose, LOCAL_DIR)
3031

3132
os.chdir(LOCAL_DIR)
3233

34+
remove_remote("upstream", verbose)
3335
checkout("hiring", True, verbose)
3436
append_to_file("employees.txt", "Receptionist: Pam\n")
3537
add(["employees.txt"], verbose)

hands_on/remote_branch_push.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from exercise_utils.git import clone_repo_with_git
1+
import os
2+
from exercise_utils.git import clone_repo_with_git, remove_remote
23
from exercise_utils.github_cli import (
4+
clone_repo_with_gh,
35
delete_repo,
46
fork_repo,
57
get_fork_name,
@@ -25,6 +27,8 @@ def download(verbose: bool):
2527
fork_repo(f"{REPO_OWNER}/{REPO_NAME}", FORK_NAME, verbose, False)
2628

2729
existing_name = get_fork_name(REPO_NAME, REPO_OWNER, username, verbose)
28-
clone_repo_with_git(
29-
f"https://github.com/{username}/{existing_name}", verbose, REPO_NAME
30+
clone_repo_with_gh(
31+
f"{username}/{existing_name}", verbose, REPO_NAME
3032
)
33+
os.chdir(REPO_NAME)
34+
remove_remote("upstream", verbose)

hands_on/update_remote.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from exercise_utils.git import add, init, commit, add_remote
66
from exercise_utils.github_cli import (
77
delete_repo,
8+
get_remote_url,
89
has_repo,
910
get_github_username,
1011
create_repo,
@@ -18,6 +19,8 @@
1819

1920
def download(verbose: bool):
2021
username = get_github_username(verbose)
22+
remote_repo = f"{username}/{REPO_NAME}"
23+
remote_url = get_remote_url(remote_repo, verbose)
2124

2225
os.makedirs("things")
2326
os.chdir("things")
@@ -61,6 +64,6 @@ def download(verbose: bool):
6164
delete_repo(REPO_NAME, verbose)
6265

6366
create_repo(REPO_NAME, verbose)
64-
add_remote("origin", f"https://github.com/{username}/{REPO_NAME}", verbose)
67+
add_remote("origin", remote_url, verbose)
6568

6669
run_command(["git", "push", "-u", "origin", "main"], verbose)

0 commit comments

Comments
 (0)