All files / src/commands jira-commands.ts

67.94% Statements 106/156
89.47% Branches 17/19
33.33% Functions 1/3
67.94% Lines 106/156

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 1x 1x 1x 7x 7x 7x 7x 7x 8x 1x 1x 1x 6x 6x 8x 8x 8x 1x 1x 1x 5x 5x 5x 8x 1x 1x 1x 4x 4x 8x 1x 1x 1x 3x 3x 3x 3x 3x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 8x           8x 1x 1x 1x 1x                                       1x 1x 1x 1x                                                      
import * as vscode from 'vscode';
import { JiraService } from '../services/jira-service';
 
/**
 * Command to initiate Jira integration setup for a team.
 * Accessible via Command Palette and status bar.
 */
export async function connectToJiraCommand(context?: vscode.ExtensionContext): Promise<void> {
    try {
        // Check if user is authenticated
        const { getAuthContext } = require('../services/auth-service');
        const authResult = await getAuthContext();
 
        if (!authResult?.context?.isAuthenticated) {
            vscode.window.showErrorMessage('Please sign in first to connect to Jira.');
            return;
        }
 
        // Check if user has teams
        const { getUserTeams } = require('../services/team-service');
        const teamsResult = await getUserTeams();
 
        if (teamsResult.error || !teamsResult.teams || teamsResult.teams.length === 0) {
            vscode.window.showErrorMessage('Please create or join a team first in the Agent Bot tab.');
            return;
        }
 
        // Get current team from global state (same as AgentPanel uses)
        const currentTeamId = context?.globalState.get<string>('collabAgent.currentTeam') ||
                             vscode.workspace.getConfiguration('collabAgent').get<string>('currentTeam');
        if (!currentTeamId) {
            vscode.window.showErrorMessage('Please select a team in the Agent Bot tab first.');
            return;
        }
 
        // Find the current team
        const currentTeam = teamsResult.teams.find((t: any) => t.id === currentTeamId);
        if (!currentTeam) {
            vscode.window.showErrorMessage('Current team not found. Please select a team in the Agent Bot tab.');
            return;
        }
 
        // Check if user is admin of current team (Admin Workflow requirement)
        if (currentTeam.role !== 'admin') {
            vscode.window.showErrorMessage('Only team admins can configure Jira integration. Please ask your team admin to set up Jira.');
            return;
        }
 
        // Check if Jira is already configured
        const jiraService = JiraService.getInstance();
        const existingConfig = await jiraService.getJiraConfig(currentTeamId);
 
        if (existingConfig) {
            const choice = await vscode.window.showQuickPick([
                { label: 'View Current Configuration', description: `Project: ${existingConfig.jira_project_key}` },
                { label: 'Reconfigure Jira', description: 'Set up a different Jira instance/project' },
                { label: 'Remove Jira Integration', description: 'Disconnect Jira from this team' }
            ], {
                placeHolder: 'Jira is already configured for this team'
            });
 
            if (!choice) return;
 
            if (choice.label === 'View Current Configuration') {
                vscode.window.showInformationMessage(
                    `Jira Configuration:\n• URL: ${existingConfig.jira_url}\n• Project: ${existingConfig.jira_project_key}\n• Configured by: ${existingConfig.admin_user_id}`
                );
                return;
            }
 
            if (choice.label === 'Remove Jira Integration') {
                const confirm = await vscode.window.showWarningMessage(
                    'Remove Jira integration from this team? All team members will lose access to Jira tasks.',
                    { modal: true },
                    'Remove'
                );
 
                if (confirm === 'Remove') {
                    await jiraService.removeJiraConfig(currentTeamId);
                    vscode.window.showInformationMessage('Jira integration removed successfully.');
 
                    // Refresh the tasks panel
                    vscode.commands.executeCommand('workbench.view.extension.collabAgent');
                }
                return;
            }
 
            // Fall through to reconfiguration
        }
 
        // Initiate Jira setup
        await jiraService.initiateJiraAuth(currentTeamId, authResult.context.id);
 
        // Refresh the tasks panel to show the new configuration
        vscode.commands.executeCommand('workbench.view.extension.collabAgent');
 
    } catch (error) {
        console.error('Failed to connect to Jira:', error);
        vscode.window.showErrorMessage(
            `Failed to connect to Jira: ${error instanceof Error ? error.message : 'Unknown error'}`
        );
    }
}
 
/**
 * Creates a status bar item for Jira integration.
 */
export function createJiraStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem {
    const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
    statusBarItem.command = 'collabAgent.connectToJira';
    updateJiraStatusBarItem(statusBarItem, context);
    statusBarItem.show();

    // Update status bar periodically to catch team changes
    const updateInterval = setInterval(() => {
        updateJiraStatusBarItem(statusBarItem, context);
    }, 2000); // Check every 2 seconds

    // Clean up interval when extension deactivates
    context.subscriptions.push({
        dispose: () => clearInterval(updateInterval)
    });

    context.subscriptions.push(statusBarItem);
    return statusBarItem;
}
 
/**
 * Updates the Jira status bar item based on current team and Jira configuration.
 */
async function updateJiraStatusBarItem(statusBarItem: vscode.StatusBarItem, context: vscode.ExtensionContext): Promise<void> {
    try {
        // Read from global state (same as AgentPanel)
        const currentTeamId = context.globalState.get<string>('collabAgent.currentTeam');

        if (!currentTeamId) {
            statusBarItem.text = '$(issue-opened) Jira';
            statusBarItem.tooltip = 'No team selected - Click to connect Jira';
            return;
        }

        const jiraService = JiraService.getInstance();
        const config = await jiraService.getJiraConfig(currentTeamId);

        if (config) {
            statusBarItem.text = '$(issue-closed) Jira';
            statusBarItem.tooltip = `Jira connected - Project: ${config.jira_project_key}`;
        } else {
            statusBarItem.text = '$(issue-opened) Jira';
            statusBarItem.tooltip = 'Click to connect Jira for this team';
        }
    } catch (error) {
        statusBarItem.text = '$(issue-opened) Jira';
        statusBarItem.tooltip = 'Click to connect Jira';
    }
}