Skip to content

Commit

Permalink
Merge pull request #577 from yuiseo/feature/448
Browse files Browse the repository at this point in the history
Feature/448 refactoring type naming, extension fetcher 함수 dependencies 제거
  • Loading branch information
DaYoung-woo authored Aug 4, 2024
2 parents fdf2a01 + 8087514 commit a838853
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 77 deletions.
62 changes: 1 addition & 61 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import { AnalysisEngine } from "@githru-vscode-ext/analysis-engine";
import * as vscode from "vscode";

import { COMMAND_LAUNCH, COMMAND_LOGIN_WITH_GITHUB, COMMAND_RESET_GITHUB_AUTH } from "./commands";
import { Credentials } from "./credentials";
import { GithubTokenUndefinedError, WorkspacePathUndefinedError } from "./errors/ExtensionError";
import { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository";
import { mapClusterNodesFrom } from "./utils/csm.mapper";
import {
findGit,
getBranches,
getCurrentBranchName,
getDefaultBranchName,
getGitConfig,
getGitLog,
getRepo,
} from "./utils/git.util";
import WebviewLoader from "./webview-loader";

let myStatusBarItem: vscode.StatusBarItem;
const projectName = "githru";

function normalizeFsPath(fsPath: string) {
return fsPath.replace(/\\/g, "/");
}

export async function activate(context: vscode.ExtensionContext) {
const { subscriptions, extensionPath, secrets } = context;
const credentials = new Credentials();
Expand All @@ -42,63 +27,18 @@ export async function activate(context: vscode.ExtensionContext) {
myStatusBarItem.text = `$(check) ${projectName}`;
return;
}
const gitPath = (await findGit()).path;

const currentWorkspaceUri = vscode.workspace.workspaceFolders?.[0].uri;
if (!currentWorkspaceUri) {
throw new WorkspacePathUndefinedError("Cannot find current workspace path");
}

const currentWorkspacePath = normalizeFsPath(currentWorkspaceUri.fsPath);

const githubToken: string | undefined = await getGithubToken(secrets);
if (!githubToken) {
throw new GithubTokenUndefinedError("Cannot find your GitHub token. Retrying github authentication...");
}

const fetchBranches = async () => await getBranches(gitPath, currentWorkspacePath);
const fetchCurrentBranch = async () => {
let branchName;
try {
branchName = await getCurrentBranchName(gitPath, currentWorkspacePath);
} catch (error) {
console.error(error);
}

if (!branchName) {
const branchList = (await fetchBranches()).branchList;
branchName = getDefaultBranchName(branchList);
}
return branchName;
};

const initialBaseBranchName = await fetchCurrentBranch();
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => {
const gitLog = await getGitLog(gitPath, currentWorkspacePath);
const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin");
const { owner, repo: initialRepo } = getRepo(gitConfig);
webLoader.setGlobalOwnerAndRepo(owner, initialRepo);
const repo = initialRepo[0];
const engine = new AnalysisEngine({
isDebugMode: true,
gitLog,
owner,
repo,
auth: githubToken,
baseBranchName,
});

const { isPRSuccess, csmDict } = await engine.analyzeGit();
if (isPRSuccess) console.log("crawling PR failed");

return mapClusterNodesFrom(csmDict);
};

const webLoader = new WebviewLoader(extensionPath, context, {
fetchClusterNodes,
fetchBranches,
fetchCurrentBranch,
});
const webLoader = new WebviewLoader(extensionPath, context, githubToken);
currentPanel = webLoader.getPanel();

currentPanel?.onDidDispose(
Expand Down
106 changes: 90 additions & 16 deletions packages/vscode/src/webview-loader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { AnalysisEngine } from "@githru-vscode-ext/analysis-engine";
import * as path from "path";
import * as vscode from "vscode";

import { WorkspacePathUndefinedError } from "./errors/ExtensionError";
import { getPrimaryColor, setPrimaryColor } from "./setting-repository";
import type { ClusterNode } from "./types/Node";
import { mapClusterNodesFrom } from "./utils/csm.mapper";
import {
findGit,
getBranches,
getCurrentBranchName,
getDefaultBranchName,
getGitConfig,
getGitLog,
getRepo,
} from "./utils/git.util";

const ANALYZE_DATA_KEY = "memento_analyzed_data";

function normalizeFsPath(fsPath: string) {
return fsPath.replace(/\\/g, "/");
}

export default class WebviewLoader implements vscode.Disposable {
private readonly _panel: vscode.WebviewPanel | undefined;

githubToken: string | undefined;
gitPath: string;
constructor(
private readonly extensionPath: string,
context: vscode.ExtensionContext,
fetcher: GithruFetcherMap
githubToken: string | undefined
) {
const { fetchClusterNodes, fetchBranches, fetchCurrentBranch } = fetcher;
const viewColumn = vscode.ViewColumn.One;
this.githubToken = githubToken;
this.gitPath = ''


this._panel = vscode.window.createWebviewPanel("WebviewLoader", "githru-view", viewColumn, {
enableScripts: true,
Expand All @@ -28,26 +46,27 @@ export default class WebviewLoader implements vscode.Disposable {

this._panel.webview.onDidReceiveMessage(async (message: { command: string; payload?: string }) => {
const { command, payload } = message;
await this.setGitPath()

if (command === "fetchAnalyzedData" || command === "refresh") {
const baseBranchName = (payload && JSON.parse(payload)) ?? (await fetchCurrentBranch());
const baseBranchName = (payload && JSON.parse(payload)) ?? (await this.fetchCurrentBranch());
// Disable Cache temporarily
// const storedAnalyzedData = context.workspaceState.get<ClusterNode[]>(`${ANALYZE_DATA_KEY}_${baseBranchName}`);
// if (!storedAnalyzedData) {

const analyzedData = await fetchClusterNodes(baseBranchName);
const analyzedData = await this.fetchClusterNodes(baseBranchName);
context.workspaceState.update(`${ANALYZE_DATA_KEY}_${baseBranchName}`, analyzedData);

const resMessage = {
command,
payload: analyzedData,
command,
payload: analyzedData,
};

await this.respondToMessage(resMessage);
}

if (command === "fetchBranchList") {
const branches = await fetchBranches();
const branches = await this.fetchBranches();
await this.respondToMessage({
...message,
payload: branches,
Expand Down Expand Up @@ -81,6 +100,68 @@ export default class WebviewLoader implements vscode.Disposable {
});
}

private getCurrentWorkspacePath() {
const currentWorkspaceUri = vscode.workspace.workspaceFolders?.[0].uri;
if (!currentWorkspaceUri) {
throw new WorkspacePathUndefinedError("Cannot find current workspace path");
}
return normalizeFsPath(currentWorkspaceUri.fsPath);
}

private async setGitPath() {
this.gitPath = (await findGit()).path
}

private async fetchBranches() {
try {
return await getBranches(this.gitPath, this.getCurrentWorkspacePath());
} catch (e) {
console.debug(e);
}
}

private async fetchCurrentBranch() {
let branchName;
try {
branchName = await getCurrentBranchName(this.gitPath, this.getCurrentWorkspacePath());
} catch (error) {
console.error(error);
}

if (!branchName) {
const branchList = (await this.fetchBranches())?.branchList;
branchName = getDefaultBranchName(branchList || []);
}
return branchName;
}

private async fetchClusterNodes(baseBranchName?: string) {
const currentWorkspaceUri = vscode.workspace.workspaceFolders?.[0].uri;
if (!currentWorkspaceUri) {
throw new WorkspacePathUndefinedError("Cannot find current workspace path");
}

if (!baseBranchName) {
baseBranchName = await this.fetchCurrentBranch();
}
const gitLog = await getGitLog(this.gitPath, this.getCurrentWorkspacePath());
const gitConfig = await getGitConfig(this.gitPath, this.getCurrentWorkspacePath(), "origin");
const { owner, repo } = getRepo(gitConfig);
const engine = new AnalysisEngine({
isDebugMode: true,
gitLog,
owner,
repo,
auth: this.githubToken,
baseBranchName,
});

const { isPRSuccess, csmDict } = await engine.analyzeGit();
if (isPRSuccess) console.log("crawling PR failed");

return mapClusterNodesFrom(csmDict);
}

private getWebviewContent(webview: vscode.Webview): string {
const reactAppPathOnDisk = vscode.Uri.file(path.join(this.extensionPath, "dist", "webviewApp.js"));
const reactAppUri = webview.asWebviewUri(reactAppPathOnDisk);
Expand Down Expand Up @@ -120,10 +201,3 @@ export default class WebviewLoader implements vscode.Disposable {
}
}
}

type GithruFetcher<D = unknown, P extends unknown[] = []> = (...params: P) => Promise<D>;
type GithruFetcherMap = {
fetchClusterNodes: GithruFetcher<ClusterNode[], [string]>;
fetchBranches: GithruFetcher<{ branchList: string[]; head: string | null }>;
fetchCurrentBranch: GithruFetcher<string>;
};

0 comments on commit a838853

Please sign in to comment.