Skip to content

Commit 2369164

Browse files
Copilotalexr00
andcommitted
Fix getOrigin to fall back to configured GitHub remotes when upstream is not GitHub
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent cb99369 commit 2369164

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/github/folderRepositoryManager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,18 +1493,18 @@ export class FolderRepositoryManager extends Disposable {
14931493
return this.createAndAddGitHubRepository(remote, this._credentialStore);
14941494
}
14951495

1496-
Logger.error(`The remote '${upstreamRef.remote}' is not a GitHub repository.`, this.id);
1496+
Logger.warn(`The remote '${upstreamRef.remote}' is not a GitHub repository. Falling back to configured GitHub remotes.`, this.id);
14971497

1498-
// No GitHubRepository? We currently won't try pushing elsewhere,
1499-
// so fail.
1500-
throw new BadUpstreamError(this.repository.state.HEAD!.name!, upstreamRef, 'is not a GitHub repo');
1498+
// No GitHubRepository? Fall back to using configured GitHub remotes
1499+
// instead of failing, to support scenarios where the upstream is set to
1500+
// a non-GitHub remote but GitHub remotes are configured separately.
1501+
} else {
1502+
// Otherwise, we'll push upstream.
1503+
return upstream;
15011504
}
1502-
1503-
// Otherwise, we'll push upstream.
1504-
return upstream;
15051505
}
15061506

1507-
// If no upstream is set, let's go digging.
1507+
// If no upstream is set, or upstream is not a GitHub remote, let's go digging.
15081508
const [first, ...rest] = this._githubRepositories;
15091509
return !rest.length // Is there only one GitHub remote?
15101510
? first // I GUESS THAT'S WHAT WE'RE GOING WITH, THEN.

src/test/github/folderRepositoryManager.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ describe('PullRequestManager', function () {
3030
let manager: FolderRepositoryManager;
3131
let telemetry: MockTelemetry;
3232
let mockThemeWatcher: MockThemeWatcher;
33+
let mockRepository: MockRepository;
3334

3435
beforeEach(function () {
3536
sinon = createSandbox();
3637
MockCommandRegistry.install(sinon);
3738

3839
telemetry = new MockTelemetry();
3940
mockThemeWatcher = new MockThemeWatcher();
40-
const repository = new MockRepository();
41+
mockRepository = new MockRepository();
4142
const context = new MockExtensionContext();
4243
const credentialStore = new CredentialStore(telemetry, context);
4344
const repositoriesManager = new RepositoriesManager(credentialStore, telemetry);
44-
manager = new FolderRepositoryManager(0, context, repository, telemetry, new GitApiImpl(repositoriesManager), credentialStore, new CreatePullRequestHelper(), mockThemeWatcher);
45+
manager = new FolderRepositoryManager(0, context, mockRepository, telemetry, new GitApiImpl(repositoriesManager), credentialStore, new CreatePullRequestHelper(), mockThemeWatcher);
4546
});
4647

4748
afterEach(function () {
@@ -68,6 +69,38 @@ describe('PullRequestManager', function () {
6869
assert.deepStrictEqual(manager.activePullRequest, pr);
6970
});
7071
});
72+
73+
describe('getOrigin', function () {
74+
it('falls back to configured GitHub remote when upstream is not GitHub', async function () {
75+
// Setup: Add a GitHub remote
76+
const githubUrl = 'https://github.com/test/repo.git';
77+
await mockRepository.addRemote('github', githubUrl);
78+
79+
const githubProtocol = new Protocol(githubUrl);
80+
const githubRemote = new GitHubRemote('github', githubUrl, githubProtocol, GitHubServerType.GitHubDotCom);
81+
const rootUri = Uri.file('/test/repo');
82+
const githubRepository = new GitHubRepository(1, githubRemote, rootUri, manager.credentialStore, telemetry);
83+
84+
// Manually set up the GitHub repository in the manager (simulating successful initialization)
85+
(manager as any)._githubRepositories = [githubRepository];
86+
87+
// Add a non-GitHub remote (simulating the scenario from the bug)
88+
await mockRepository.addRemote('origin', 'https://example.com/git/repo.git');
89+
90+
// Create a branch with upstream set to non-GitHub remote
91+
await mockRepository.createBranch('test-branch', true);
92+
await mockRepository.setBranchUpstream('test-branch', 'refs/remotes/origin/main');
93+
94+
// Mock getAllGitHubRemotes to return only the github remote
95+
sinon.stub(manager, 'getAllGitHubRemotes').resolves([githubRemote]);
96+
97+
// Act: getOrigin should fall back to the configured GitHub remote instead of throwing
98+
const result = await manager.getOrigin();
99+
100+
// Assert: Should return the GitHub repository, not throw an error
101+
assert.strictEqual(result, githubRepository);
102+
});
103+
});
71104
});
72105

73106
describe('titleAndBodyFrom', function () {

0 commit comments

Comments
 (0)