Skip to content

Commit 30680c8

Browse files
Copilotalexr00
andcommitted
Fix: respect useBranchForIssues setting to prevent unwanted branch switching
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 32cdaf1 commit 30680c8

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

src/issues/stateManager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,14 @@ export class StateManager {
418418
if (repoState.currentIssue && issue?.issue.number === repoState.currentIssue.issue.number) {
419419
return;
420420
}
421+
// Check if branch management is disabled
422+
const createBranchConfig = vscode.workspace
423+
.getConfiguration(ISSUES_SETTINGS_NAMESPACE)
424+
.get<string>(USE_BRANCH_FOR_ISSUES);
425+
const shouldCheckoutDefaultBranch = createBranchConfig === 'off' ? false : checkoutDefaultBranch;
426+
421427
if (repoState.currentIssue) {
422-
await repoState.currentIssue.stopWorking(checkoutDefaultBranch);
428+
await repoState.currentIssue.stopWorking(shouldCheckoutDefaultBranch);
423429
}
424430
if (issue) {
425431
this.context.subscriptions.push(issue.onDidChangeCurrentIssueState(() => this.updateStatusBar()));
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { default as assert } from 'assert';
7+
import * as vscode from 'vscode';
8+
import { StateManager } from '../../issues/stateManager';
9+
import { CurrentIssue } from '../../issues/currentIssue';
10+
import { USE_BRANCH_FOR_ISSUES, ISSUES_SETTINGS_NAMESPACE } from '../../common/settingKeys';
11+
12+
// Mock classes for testing
13+
class MockFolderRepositoryManager {
14+
constructor(public repository: { rootUri: vscode.Uri }) {}
15+
}
16+
17+
class MockSingleRepoState {
18+
currentIssue?: MockCurrentIssue;
19+
constructor(public folderManager: MockFolderRepositoryManager) {}
20+
}
21+
22+
class MockCurrentIssue {
23+
stopWorkingCalled = false;
24+
stopWorkingCheckoutFlag = false;
25+
issue = { number: 123 };
26+
27+
async stopWorking(checkoutDefaultBranch: boolean) {
28+
this.stopWorkingCalled = true;
29+
this.stopWorkingCheckoutFlag = checkoutDefaultBranch;
30+
}
31+
}
32+
33+
describe('StateManager branch behavior with useBranchForIssues setting', function () {
34+
let stateManager: StateManager;
35+
let mockContext: vscode.ExtensionContext;
36+
37+
beforeEach(() => {
38+
mockContext = {
39+
workspaceState: {
40+
get: () => undefined,
41+
update: () => Promise.resolve(),
42+
},
43+
subscriptions: [],
44+
} as any;
45+
46+
stateManager = new StateManager(undefined as any, undefined as any, mockContext);
47+
(stateManager as any)._singleRepoStates = new Map();
48+
});
49+
50+
it('should not checkout default branch when useBranchForIssues is off', async function () {
51+
// Mock workspace configuration to return 'off'
52+
const originalGetConfiguration = vscode.workspace.getConfiguration;
53+
vscode.workspace.getConfiguration = (section?: string) => {
54+
if (section === ISSUES_SETTINGS_NAMESPACE) {
55+
return {
56+
get: (key: string) => {
57+
if (key === USE_BRANCH_FOR_ISSUES) {
58+
return 'off';
59+
}
60+
return undefined;
61+
},
62+
} as any;
63+
}
64+
return originalGetConfiguration(section);
65+
};
66+
67+
try {
68+
// Set up test state
69+
const mockUri = vscode.Uri.parse('file:///test');
70+
const mockFolderManager = new MockFolderRepositoryManager({ rootUri: mockUri });
71+
const mockState = new MockSingleRepoState(mockFolderManager);
72+
const mockCurrentIssue = new MockCurrentIssue();
73+
mockState.currentIssue = mockCurrentIssue;
74+
75+
(stateManager as any)._singleRepoStates.set(mockUri.path, mockState);
76+
77+
// Call setCurrentIssue with checkoutDefaultBranch = true
78+
await stateManager.setCurrentIssue(mockState as any, undefined, true, true);
79+
80+
// Verify that stopWorking was called with false (not the original true)
81+
assert.strictEqual(mockCurrentIssue.stopWorkingCalled, true, 'stopWorking should have been called');
82+
assert.strictEqual(mockCurrentIssue.stopWorkingCheckoutFlag, false, 'stopWorking should have been called with checkoutDefaultBranch=false when useBranchForIssues is off');
83+
} finally {
84+
// Restore original configuration
85+
vscode.workspace.getConfiguration = originalGetConfiguration;
86+
}
87+
});
88+
89+
it('should checkout default branch when useBranchForIssues is not off', async function () {
90+
// Mock workspace configuration to return 'on'
91+
const originalGetConfiguration = vscode.workspace.getConfiguration;
92+
vscode.workspace.getConfiguration = (section?: string) => {
93+
if (section === ISSUES_SETTINGS_NAMESPACE) {
94+
return {
95+
get: (key: string) => {
96+
if (key === USE_BRANCH_FOR_ISSUES) {
97+
return 'on';
98+
}
99+
return undefined;
100+
},
101+
} as any;
102+
}
103+
return originalGetConfiguration(section);
104+
};
105+
106+
try {
107+
// Set up test state
108+
const mockUri = vscode.Uri.parse('file:///test');
109+
const mockFolderManager = new MockFolderRepositoryManager({ rootUri: mockUri });
110+
const mockState = new MockSingleRepoState(mockFolderManager);
111+
const mockCurrentIssue = new MockCurrentIssue();
112+
mockState.currentIssue = mockCurrentIssue;
113+
114+
(stateManager as any)._singleRepoStates.set(mockUri.path, mockState);
115+
116+
// Call setCurrentIssue with checkoutDefaultBranch = true
117+
await stateManager.setCurrentIssue(mockState as any, undefined, true, true);
118+
119+
// Verify that stopWorking was called with true (preserving the original value)
120+
assert.strictEqual(mockCurrentIssue.stopWorkingCalled, true, 'stopWorking should have been called');
121+
assert.strictEqual(mockCurrentIssue.stopWorkingCheckoutFlag, true, 'stopWorking should have been called with checkoutDefaultBranch=true when useBranchForIssues is on');
122+
} finally {
123+
// Restore original configuration
124+
vscode.workspace.getConfiguration = originalGetConfiguration;
125+
}
126+
});
127+
});

0 commit comments

Comments
 (0)