|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import {newSession} from "../auth/auth"; |
| 3 | +import {log, logError} from "../log"; |
| 4 | +import {validateTunnelUrl} from "./tunnelUrl"; |
| 5 | +import {WebSocketDapAdapter} from "./webSocketDapAdapter"; |
| 6 | + |
| 7 | +/** The custom debug type registered in package.json contributes.debuggers. */ |
| 8 | +export const DEBUG_TYPE = "github-actions-job"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Registers the Actions Job Debugger command and debug adapter factory. |
| 12 | + * |
| 13 | + * Contributes: |
| 14 | + * - A command-palette command that prompts for a tunnel URL and starts a debug session. |
| 15 | + * - A DebugAdapterDescriptorFactory that returns an inline DAP-over-WS adapter. |
| 16 | + */ |
| 17 | +export function registerDebugger(context: vscode.ExtensionContext): void { |
| 18 | + // Register the inline adapter factory for our debug type. |
| 19 | + context.subscriptions.push( |
| 20 | + vscode.debug.registerDebugAdapterDescriptorFactory(DEBUG_TYPE, new ActionsDebugAdapterFactory()) |
| 21 | + ); |
| 22 | + |
| 23 | + // Register the connect command. |
| 24 | + context.subscriptions.push( |
| 25 | + vscode.commands.registerCommand("github-actions.debugger.connect", () => connectToDebugger()) |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +// ── Connect command ────────────────────────────────────────────────── |
| 30 | + |
| 31 | +async function connectToDebugger(): Promise<void> { |
| 32 | + // 1. Prompt for the tunnel URL. |
| 33 | + const rawUrl = await vscode.window.showInputBox({ |
| 34 | + title: "Connect to Actions Job Debugger", |
| 35 | + prompt: "Enter the debugger tunnel URL (wss://…)", |
| 36 | + placeHolder: "wss://xxxx-4711.region.devtunnels.ms/", |
| 37 | + ignoreFocusOut: true, |
| 38 | + validateInput: input => { |
| 39 | + if (!input) { |
| 40 | + return "A tunnel URL is required"; |
| 41 | + } |
| 42 | + const result = validateTunnelUrl(input); |
| 43 | + return result.valid ? null : result.reason; |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + if (!rawUrl) { |
| 48 | + return; // user cancelled |
| 49 | + } |
| 50 | + |
| 51 | + const validation = validateTunnelUrl(rawUrl); |
| 52 | + if (!validation.valid) { |
| 53 | + void vscode.window.showErrorMessage(`Invalid tunnel URL: ${validation.reason}`); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // 2. Acquire a GitHub auth session. The token is used as a Bearer token |
| 58 | + // against the Dev Tunnel relay, which accepts VS Code's GitHub app tokens. |
| 59 | + let session: vscode.AuthenticationSession; |
| 60 | + try { |
| 61 | + session = await newSession("Sign in to GitHub to connect to the Actions job debugger."); |
| 62 | + } catch (e) { |
| 63 | + void vscode.window.showErrorMessage(`GitHub authentication required: ${(e as Error).message}`); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // 3. Launch the debug session. The factory will use the tunnel URL and token |
| 68 | + // from the configuration to create the websocket adapter. |
| 69 | + const config: vscode.DebugConfiguration = { |
| 70 | + type: DEBUG_TYPE, |
| 71 | + name: "Actions Job Debugger", |
| 72 | + request: "attach", |
| 73 | + tunnelUrl: validation.url, |
| 74 | + __token: session.accessToken |
| 75 | + }; |
| 76 | + |
| 77 | + log(`Starting debug session for ${validation.url}`); |
| 78 | + |
| 79 | + const started = await vscode.debug.startDebugging(undefined, config); |
| 80 | + if (!started) { |
| 81 | + void vscode.window.showErrorMessage("Failed to start the debug session. Check the GitHub Actions output for details."); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// ── Debug adapter factory ──────────────────────────────────────────── |
| 86 | + |
| 87 | +class ActionsDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory { |
| 88 | + async createDebugAdapterDescriptor( |
| 89 | + session: vscode.DebugSession |
| 90 | + ): Promise<vscode.DebugAdapterDescriptor> { |
| 91 | + const tunnelUrl = session.configuration.tunnelUrl as string | undefined; |
| 92 | + const token = session.configuration.__token as string | undefined; |
| 93 | + |
| 94 | + if (!tunnelUrl || !token) { |
| 95 | + throw new Error( |
| 96 | + "Missing tunnel URL or authentication token. Use the 'Connect to Actions Job Debugger' command to start a session." |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + const adapter = new WebSocketDapAdapter(tunnelUrl, token); |
| 101 | + |
| 102 | + try { |
| 103 | + await adapter.connect(); |
| 104 | + } catch (e) { |
| 105 | + adapter.dispose(); |
| 106 | + const msg = (e as Error).message; |
| 107 | + logError(e as Error, "Failed to connect debugger adapter"); |
| 108 | + throw new Error(`Could not connect to the debugger tunnel: ${msg}`); |
| 109 | + } |
| 110 | + |
| 111 | + return new vscode.DebugAdapterInlineImplementation(adapter); |
| 112 | + } |
| 113 | +} |
0 commit comments