Skip to content

Commit 2323cba

Browse files
committed
Enhance GitHubClient authentication and improve PR input form cancel behavior
1 parent 60e737e commit 2323cba

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/common/api/ghClient.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,28 @@ export class GitHubClient {
2121
return this._repo;
2222
}
2323

24-
private async getAuthToken(): Promise<string | undefined> {
24+
private async getAuthToken(reAuthenticate = false): Promise<string | undefined> {
2525
try {
26-
const session = await authentication.getSession('github', ['repo'], { createIfNone: true });
26+
const scope = ['repo'];
27+
const session = reAuthenticate
28+
? await authentication.getSession('github', scope, {
29+
forceNewSession: true,
30+
clearSessionPreference: true,
31+
})
32+
: await authentication.getSession('github', scope, { createIfNone: true });
2733
return session.accessToken;
2834
} catch (error) {
2935
throw new Error(`GitHub authentication failed: ${error}`);
3036
}
3137
}
3238

33-
private async makeRequest<T>(endpoint: string, method: string = 'GET', body?: any): Promise<T> {
34-
const token = await this.getAuthToken();
39+
private async makeRequest<T>(
40+
endpoint: string,
41+
method: string = 'GET',
42+
body?: any,
43+
reAuthenticate = false
44+
): Promise<T> {
45+
const token = await this.getAuthToken(reAuthenticate);
3546
const url = `${GitHubClient.BASE_URL}${endpoint}`;
3647

3748
const headers: { [key: string]: string } = {
@@ -62,6 +73,10 @@ export class GitHubClient {
6273
});
6374

6475
res.on('end', () => {
76+
if (res.statusCode && res.statusCode === 403) {
77+
// todo: add reauthenticate method and retry
78+
}
79+
6580
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
6681
try {
6782
resolve(JSON.parse(data));

src/extension.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { getGitExecutor } from './utils/getGitExecutor';
1717
import { GitHubClient } from './common/api/ghClient';
1818

1919
export function activate(context: vscode.ExtensionContext) {
20-
console.log(`Extension "${EXTENSION_NAME}" is now active!`);
21-
2220
const commandManager = new CommandManager();
2321

2422
const configManager = new ConfigurationManager();
@@ -37,7 +35,7 @@ export function activate(context: vscode.ExtensionContext) {
3735
prCloneService
3836
);
3937

40-
logService.info('Start...');
38+
logService.info(`Extension "${EXTENSION_NAME}" is now active!`);
4139

4240
// Set initial context to hide PR Clone view and commits view
4341
setContextShowPRClone(false);

src/webview/Apps/PR/pages/PrInputForm/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ export const PrInputForm: React.FC<PrInputFormProps> = ({
3535

3636
useEffect(() => {
3737
const handleKeyDown = (event: KeyboardEvent) => {
38-
if (event.key === 'Escape' && !loadPullRequestData.isLoading) {
39-
onCancel();
38+
if (event.key === 'Escape') {
39+
handleCancel();
4040
}
4141
};
4242

4343
document.addEventListener('keydown', handleKeyDown);
4444
return () => document.removeEventListener('keydown', handleKeyDown);
45-
}, [onCancel, loadPullRequestData.isLoading]);
45+
}, [loadPullRequestData.isLoading]);
46+
47+
const handleCancel = () => {
48+
if (loadPullRequestData.isLoading) {
49+
// Cancel the fetch process but don't close panel
50+
loadPullRequestData.finish();
51+
logger.info('PR fetch cancelled by user');
52+
// todo: remove onCancel and send message to provider to cancer running request
53+
onCancel();
54+
} else {
55+
// Close the panel when not fetching
56+
onCancel();
57+
}
58+
};
4659

4760
const handleSubmit = (e: React.FormEvent) => {
4861
logger.info('Fetching PR data ...')
@@ -74,8 +87,7 @@ export const PrInputForm: React.FC<PrInputFormProps> = ({
7487
<Button
7588
type="button"
7689
variant="secondary"
77-
onClick={onCancel}
78-
disabled={loadPullRequestData.isLoading}
90+
onClick={handleCancel}
7991
>
8092
Cancel
8193
</Button>

0 commit comments

Comments
 (0)