All files / src/commands github-token-commands.ts

90.78% Statements 128/141
91.66% Branches 11/12
0% Functions 0/1
90.78% Lines 128/141

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x                 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 4x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x  
import * as vscode from 'vscode';
import { storeGitHubAccessToken, clearGitHubAccessToken } from '../services/github-verification-service';
 
/**
 * Command to set GitHub Personal Access Token for repository verification
 */
export const setGitHubTokenCommand = vscode.commands.registerCommand(
    'collabAgent.setGitHubToken',
    async () => {
        const token = await vscode.window.showInputBox({
            prompt: 'Enter your GitHub Personal Access Token',
            placeHolder: 'ghp_xxxxxxxxxxxxxxxxxxxx',
            password: true,
            ignoreFocusOut: true,
            validateInput: (value) => {
                if (!value || value.trim().length === 0) {
                    return 'Token cannot be empty';
                }
                if (!value.startsWith('ghp_') && !value.startsWith('github_pat_')) {
                    return 'Invalid token format. Should start with "ghp_" or "github_pat_"';
                }
                return null;
            }
        });
 
        if (!token) {
            return;
        }
 
        // Test the token by making a simple API call
        try {
            const response = await fetch('https://api.github.com/user', {
                headers: {
                    'Authorization': `token ${token}`,
                    'Accept': 'application/vnd.github.v3+json',
                    'User-Agent': 'CollabAgent-VSCode'
                }
            });
 
            if (!response.ok) {
                if (response.status === 401) {
                    vscode.window.showErrorMessage('Invalid GitHub token. Please check your token and try again.');
                    return;
                }
                throw new Error(`GitHub API returned ${response.status}`);
            }
 
            const userData = await response.json();
 
            // Store the token
            await storeGitHubAccessToken(token);
 
            vscode.window.showInformationMessage(
                `✓ GitHub token verified and saved! Authenticated as: ${userData.login}`
            );
        } catch (error: any) {
            vscode.window.showErrorMessage(
                `Failed to verify GitHub token: ${error.message}`
            );
        }
    }
);
 
/**
 * Command to clear stored GitHub token
 */
export const clearGitHubTokenCommand = vscode.commands.registerCommand(
    'collabAgent.clearGitHubToken',
    async () => {
        const confirm = await vscode.window.showWarningMessage(
            'Are you sure you want to clear your GitHub token?',
            'Yes',
            'Cancel'
        );
 
        if (confirm === 'Yes') {
            await clearGitHubAccessToken();
            vscode.window.showInformationMessage('GitHub token cleared successfully');
        }
    }
);
 
/**
 * Command to check GitHub token status
 */
export const checkGitHubTokenCommand = vscode.commands.registerCommand(
    'collabAgent.checkGitHubToken',
    async () => {
        const { globalContext } = require('../extension');
        const token = globalContext?.globalState.get('github_access_token') as string | undefined;
 
        if (!token) {
            const action = await vscode.window.showWarningMessage(
                'No GitHub token found. Repository verification is disabled.',
                'Set Token',
                'Learn More'
            );
 
            if (action === 'Set Token') {
                vscode.commands.executeCommand('collabAgent.setGitHubToken');
            } else if (action === 'Learn More') {
                vscode.env.openExternal(vscode.Uri.parse('https://github.com/settings/tokens'));
            }
            return;
        }
 
        // Test the token
        try {
            const response = await fetch('https://api.github.com/user', {
                headers: {
                    'Authorization': `token ${token}`,
                    'Accept': 'application/vnd.github.v3+json',
                    'User-Agent': 'CollabAgent-VSCode'
                }
            });
 
            if (!response.ok) {
                if (response.status === 401) {
                    vscode.window.showErrorMessage(
                        'GitHub token is invalid or expired. Please set a new token.',
                        'Set New Token'
                    ).then(action => {
                        if (action === 'Set New Token') {
                            vscode.commands.executeCommand('collabAgent.setGitHubToken');
                        }
                    });
                    return;
                }
            }
 
            const userData = await response.json();
            vscode.window.showInformationMessage(
                `✓ GitHub token is valid! Authenticated as: ${userData.login}`
            );
        } catch (error: any) {
            vscode.window.showErrorMessage(
                `Failed to verify GitHub token: ${error.message}`
            );
        }
    }
);