Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vscode): Add private method 'ensureOctokitInstance' to resolve Octokit's common logic. #562

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/vscode/src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Credentials {
this.octokit = undefined;
}

registerListeners(context: vscode.ExtensionContext): void {
private registerListeners(context: vscode.ExtensionContext): void {
bbanderson marked this conversation as resolved.
Show resolved Hide resolved
context.subscriptions.push(
vscode.authentication.onDidChangeSessions(async (e) => {
if (e.provider.id === GITHUB_AUTH_PROVIDER_ID) {
Expand All @@ -48,25 +48,22 @@ export class Credentials {
);
}

async getOctokit(): Promise<Octokit.Octokit> {
/** Octokit 인스턴스가 없다면, 인증 세션을 새로 만듦으로써, Octokit 인스턴스가 항상 존재하도록 보장한다. */
private async getOctokitInstance(): Promise<Octokit.Octokit> {
if (this.octokit) {
return this.octokit;
} else {
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
this.octokit = await this.createOctokit(session);
bbanderson marked this conversation as resolved.
Show resolved Hide resolved
return this.octokit;
bbanderson marked this conversation as resolved.
Show resolved Hide resolved
}
}

const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
this.octokit = await this.createOctokit(session);

return this.octokit;
async getOctokit(): Promise<Octokit.Octokit> {
return this.getOctokitInstance();
}

async getAuth(): Promise<OctokitAuth> {
bbanderson marked this conversation as resolved.
Show resolved Hide resolved
if (this.octokit) {
return this.octokit.auth() as Promise<OctokitAuth>;
}

const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
this.octokit = await this.createOctokit(session);

return this.octokit.auth() as Promise<OctokitAuth>;
return (await this.getOctokitInstance()).auth() as Promise<OctokitAuth>;
}
}
9 changes: 4 additions & 5 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository";
import { mapClusterNodesFrom } from "./utils/csm.mapper";
import {
findGit,
Expand All @@ -18,7 +18,7 @@ import {
import WebviewLoader from "./webview-loader";

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

function normalizeFsPath(fsPath: string) {
return fsPath.replace(/\\/g, "/");
Expand Down Expand Up @@ -58,12 +58,11 @@ export async function activate(context: vscode.ExtensionContext) {

const fetchBranches = async () => await getBranches(gitPath, currentWorkspacePath);
const fetchCurrentBranch = async () => {

let branchName;
try {
branchName = await getCurrentBranchName(gitPath, currentWorkspacePath)
branchName = await getCurrentBranchName(gitPath, currentWorkspacePath);
} catch (error) {
console.error(error);
console.error(error);
bbanderson marked this conversation as resolved.
Show resolved Hide resolved
}

if (!branchName) {
Expand Down
Loading