From 556c29cfbe4bce0c540e0c442f91f616ffd4e372 Mon Sep 17 00:00:00 2001 From: Carsten Ziegeler Date: Mon, 30 Sep 2024 11:29:07 +0200 Subject: [PATCH] Support github enterprise server for issue management --- src/handlers/issueManagement.handler.ts | 36 +---------------- src/utils/github.helper.ts | 53 ++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/src/handlers/issueManagement.handler.ts b/src/handlers/issueManagement.handler.ts index fa6b31a..d795981 100644 --- a/src/handlers/issueManagement.handler.ts +++ b/src/handlers/issueManagement.handler.ts @@ -1,44 +1,12 @@ import * as vscode from "vscode"; import { AEM_COMMANDS as commands } from "../aem.commands"; import { Comment, Issue } from "../interfaces/issueManagement.interfaces"; -import { fetchAllIssues, fetchIssueDetailsByNumber, fetchLatestIssueDetails, getGitHubClient } from "../utils/github.helper"; +import { extractRepoDetailsFromWorkspace, fetchAllIssues, fetchIssueDetailsByNumber, fetchLatestIssueDetails, getGitHubClient } from "../utils/github.helper"; import { FETCH_ISSUE_DETAIL_CMD } from "../constants"; import { IssuesManagePrompt } from "../prompts/issueManagement.prompt"; import { IssuesManagePromptProps } from "../interfaces/prompt.Interfaces"; import { getChatResponse } from "../utils/helpers"; -// Extracts owner and repo name from the workspace's Git configuration -async function extractRepoDetailsFromWorkspace(): Promise<{ owner: string; repoName: string } | null> { - const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports; - if (!gitExtension) { - vscode.window.showErrorMessage('Unable to load Git extension'); - return null; - } - - const api = gitExtension.getAPI(1); - if (api.repositories.length === 0) { - vscode.window.showInformationMessage('No Git repositories found'); - return null; - } - - const repo = api.repositories[0]; - const remotes = repo.state.remotes; - if (remotes.length === 0) { - vscode.window.showInformationMessage('No remotes found'); - return null; - } - - const remoteUrl = remotes[0].fetchUrl; - const match = remoteUrl?.match(/github\.com[:/](.+?)\/(.+?)(?:\.git)?$/); - if (!match) { - vscode.window.showErrorMessage('Unable to parse GitHub repository URL'); - return null; - } - - return { owner: match[1], repoName: match[2] }; -} - - function streamIssueDetails( stream: vscode.ChatResponseStream, issue: any, @@ -90,7 +58,7 @@ export async function handleIssueManagement(request: vscode.ChatRequest, stream: issueDetails: "", }; - const octokit = await getGitHubClient(); + const octokit = await getGitHubClient(workspaceDetails.baseUrl, workspaceDetails.provider); // Determine action based on user query if (userQuery.includes("latest issue")) { diff --git a/src/utils/github.helper.ts b/src/utils/github.helper.ts index 73d0cd6..b5b66dd 100644 --- a/src/utils/github.helper.ts +++ b/src/utils/github.helper.ts @@ -1,14 +1,57 @@ import { Octokit } from "@octokit/rest"; import * as vscode from "vscode"; -import { Comment, Issue } from "../interfaces/issueManagement.interfaces"; +import { Issue } from "../interfaces/issueManagement.interfaces"; -// Utility function to get GitHub Octokit client -export async function getGitHubClient(): Promise { - const session = await vscode.authentication.getSession("github", ["repo"], { createIfNone: true }); - return new Octokit({ auth: session.accessToken }); +// Extracts owner and repo name from the workspace's Git configuration +export async function extractRepoDetailsFromWorkspace(): Promise<{ owner: string; repoName: string, baseUrl: string | undefined, provider: string } | null> { + const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports; + if (!gitExtension) { + vscode.window.showErrorMessage('Unable to load Git extension'); + return null; + } + + const api = gitExtension.getAPI(1); + if (api.repositories.length === 0) { + vscode.window.showInformationMessage('No Git repositories found'); + return null; + } + + const repo = api.repositories[0]; + const remotes = repo.state.remotes; + if (remotes.length === 0) { + vscode.window.showInformationMessage('No remotes found'); + return null; + } + + const remoteUrl = remotes[0].fetchUrl; + let match; + if (remoteUrl.startsWith('https://')) { + match = remoteUrl?.match(/https\:\/\/(.+?)[:/](.+?)\/(.+?)(?:\.git)?$/); + } else { + match = remoteUrl?.match(/@(.+?)[:/](.+?)\/(.+?)(?:\.git)?$/); + } + if (!match) { + vscode.window.showErrorMessage('Unable to parse GitHub repository URL'); + return null; + } + const isGH = match[1] === 'github.com'; + return { + owner: match[2], + repoName: match[3], + baseUrl: isGH ? undefined : 'https://' + match[1] + '/api/v3', + provider: (isGH ? 'github' : 'github-enterprise') + }; } +// Utility function to get GitHub Octokit client +export async function getGitHubClient(baseUrl: string | undefined, provider: string): Promise { + const session = await vscode.authentication.getSession(provider, ["repo"], { createIfNone: true }); + return new Octokit({ + auth: session.accessToken, + baseUrl + }); +} // Fetches the latest issue details including comments export async function fetchLatestIssueDetails(owner: string, repoName: string, octokit: Octokit): Promise< Issue| null> {