Skip to content

Commit 6151171

Browse files
committed
Support non-git SSH usernames in remote URL parsing (#360)
GitHub now hands out org-scoped SSH deploy users such as org-21003710@github.com:pytorch/pytorch.git instead of the classic git@github.com:owner/repo.git. The remote-URL regexes hardcoded the "git" username, so the new format failed with: RuntimeError: Couldn't determine repo owner and name from url: ... Match any SSH user ([^@]+@) in both get_github_repo_name_with_owner and _normalize_remote_url, and add tests for both the old and new formats.
1 parent d0f3424 commit 6151171

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/ghstack/github_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ async def get_github_repo_name_with_owner(
3232
# where commits will actually be pushed to
3333
remote_url = await sh.agit("remote", "get-url", "--push", remote_name)
3434
while True:
35-
match = r"^git@{github_url}:/?([^/]+)/(.+?)(?:\.git)?$".format(
35+
# The SSH user can be "git" (e.g. git@github.com:owner/repo.git) or an
36+
# arbitrary identity such as the org-scoped deploy user GitHub now hands
37+
# out (e.g. org-21003710@github.com:owner/repo.git), so match any user.
38+
match = r"^[^@]+@{github_url}:/?([^/]+)/(.+?)(?:\.git)?$".format(
3639
github_url=github_url
3740
)
3841
m = re.match(match, remote_url)
@@ -175,7 +178,9 @@ async def get_github_repo_info(
175178
def _normalize_remote_url(remote_url: str) -> str:
176179
"""Convert SSH remote URL to HTTPS format, strip .git suffix."""
177180
# git@github.com:owner/repo.git -> https://github.com/owner/repo
178-
m = re.match(r"^git@([^:]+):/?(.+?)(?:\.git)?$", remote_url)
181+
# The SSH user may be "git" or an org-scoped identity such as
182+
# org-21003710@github.com:owner/repo.git, so match any user.
183+
m = re.match(r"^[^@]+@([^:]+):/?(.+?)(?:\.git)?$", remote_url)
179184
if m:
180185
return f"https://{m.group(1)}/{m.group(2)}"
181186
return re.sub(r"\.git$", "", remote_url)

test/github_utils/get_repo_name_with_owner.py.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,19 @@ assert_eq(
7070
),
7171
{"owner": "ezyang", "name": "ghstack"},
7272
)
73+
# The classic "git" SSH user.
74+
await git("remote", "add", "ssh-git-user", "git@github.com:pytorch/pytorch.git")
75+
assert_eq(
76+
await ghstack.github_utils.get_github_repo_name_with_owner(
77+
sh=sh, github_url="github.com", remote_name="ssh-git-user"
78+
),
79+
{"owner": "pytorch", "name": "pytorch"},
80+
)
81+
# The newer org-scoped SSH user (see ezyang/ghstack#360).
82+
await git("remote", "add", "ssh-org-user", "org-21003710@github.com:pytorch/pytorch.git")
83+
assert_eq(
84+
await ghstack.github_utils.get_github_repo_name_with_owner(
85+
sh=sh, github_url="github.com", remote_name="ssh-org-user"
86+
),
87+
{"owner": "pytorch", "name": "pytorch"},
88+
)

0 commit comments

Comments
 (0)