Skip to content

Commit c26cc1c

Browse files
fix(github): honor selected repositories on SSH aliases
1 parent 1b5febe commit c26cc1c

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎packages/source-control-github/src/server/gitHubRepositoryResolution.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,46 @@ describe("selectGitHubBaseRepository", () => {
4848
);
4949
});
5050

51+
it("honors a marked SSH alias origin over upstream", () => {
52+
for (const url of [
53+
"[email protected]:me/web.git",
54+
"ssh://[email protected]:22/me/web.git",
55+
"git@github-work:me/web.git",
56+
]) {
57+
const input = {
58+
remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
59+
resolved: "remote.origin.gh-resolved base",
60+
};
61+
assert.deepStrictEqual(select(input), { owner: "me", name: "web" }, url);
62+
assert.deepStrictEqual(
63+
resolveGitHubRepository({ ...input, hostHint: undefined, defaultHost: "github.com" }),
64+
{ host: "github.com", locator: { host: "github.com", owner: "me", name: "web" } },
65+
url,
66+
);
67+
}
68+
});
69+
70+
it("does not honor a github.com mark on a different or unrecognized host", () => {
71+
for (const url of [
72+
"[email protected]:me/web.git",
73+
"[email protected]:me/web.git",
74+
"[email protected]:me/web.git",
75+
"https://github.com-scale/me/web.git",
76+
"git@work:me/web.git",
77+
]) {
78+
const input = {
79+
remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
80+
resolved: "remote.origin.gh-resolved base",
81+
};
82+
assert.strictEqual(select(input), null, url);
83+
assert.deepStrictEqual(
84+
resolveGitHubRepository({ ...input, hostHint: "github.com", defaultHost: "github.com" }),
85+
{ host: "github.com", locator: { host: "github.com", owner: "acme", name: "web" } },
86+
url,
87+
);
88+
}
89+
});
90+
5191
it("leaves gh to choose when it might weigh the remotes differently", () => {
5292
for (const input of [
5393
{ remotes: "" },
@@ -60,7 +100,7 @@ describe("selectGitHubBaseRepository", () => {
60100
["mirror", "[email protected]:me/web.git"],
61101
),
62102
},
63-
{ remotes: remotes(["origin", "git@github-work:me/web.git"]) },
103+
{ remotes: remotes(["origin", "git@work:me/web.git"]) },
64104
{
65105
remotes: remotes(
66106
["origin", "[email protected]:me/web.git"],

‎packages/source-control-github/src/server/gitHubRepositoryResolution.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function parseFetchRemotes(
2626
* remote `gh repo set-default` marked, else the first of upstream, github, origin (in any case), else
2727
* the only remote.
2828
*
29-
* Null whenever gh might weigh the remotes differently: a remote on another host or under an SSH alias,
29+
* Null whenever gh might weigh the remotes differently: a remote on another host or under an unrecognized SSH alias,
3030
* more than one mark, or several remotes with none of those names.
3131
*/
3232
export function selectGitHubBaseRepository(input: {
@@ -37,7 +37,8 @@ export function selectGitHubBaseRepository(input: {
3737
const host = input.host.toLowerCase();
3838
const repositories = new Map<string, { readonly owner: string; readonly name: string }>();
3939
for (const remote of parseFetchRemotes(input.remotes)) {
40-
const [remoteHost, owner, name, ...rest] = normalizeGitRemoteUrl(remote.url).split("/");
40+
const [normalizedHost, owner, name, ...rest] = normalizeGitRemoteUrl(remote.url).split("/");
41+
const remoteHost = gitHubApiHostForRemote(remote.url) ?? normalizedHost;
4142
if (remoteHost !== host || !owner || !name || rest.length > 0) return null;
4243
repositories.set(remote.name, { owner, name });
4344
}

0 commit comments

Comments
 (0)