Skip to content

Commit

Permalink
Merge pull request #11 from adobe/ghes
Browse files Browse the repository at this point in the history
Support github enterprise server for issue management
  • Loading branch information
cziegeler committed Sep 30, 2024
2 parents f6b0f61 + 556c29c commit 2c209a0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
36 changes: 2 additions & 34 deletions src/handlers/issueManagement.handler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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")) {
Expand Down
53 changes: 48 additions & 5 deletions src/utils/github.helper.ts
Original file line number Diff line number Diff line change
@@ -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<Octokit> {
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<Octokit> {
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> {
Expand Down

0 comments on commit 2c209a0

Please sign in to comment.