Skip to content

Commit

Permalink
Fix bug with newly created .weaudit files and process review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdprng committed Sep 30, 2024
1 parent ff4f6c2 commit 0f500bf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
27 changes: 16 additions & 11 deletions src/multiConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
import { userInfo } from "os";
import { SERIALIZED_FILE_EXTENSION } from "./codeMarker";
import { ConfigurationEntry, WorkspaceRootEntry, ConfigTreeEntry, isConfigurationEntry, isWorkspaceRootEntry, configEntryEquals } from "./types";
import {
ConfigurationEntry,
WorkspaceRootEntry,
ConfigTreeEntry,
isConfigurationEntry,
isWorkspaceRootEntry,
configEntryEquals,
RootPathAndLabel,
} from "./types";

let lightEyePath: vscode.Uri;
let darkEyePath: vscode.Uri;

export class MultipleSavedFindingsTree implements vscode.TreeDataProvider<ConfigTreeEntry> {
private configurationEntries: ConfigurationEntry[];
private rootEntries: WorkspaceRootEntry[];
private rootPathsAndLabels: [string, string][];
private rootPathsAndLabels: RootPathAndLabel[];
private activeConfigs: ConfigurationEntry[];
private username: string;

private _onDidChangeTreeDataEmitter = new vscode.EventEmitter<ConfigTreeEntry | undefined | void>();
readonly onDidChangeTreeData = this._onDidChangeTreeDataEmitter.event;
Expand All @@ -29,10 +35,8 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider<Config
this.rootEntries = [];
this.activeConfigs = [];

this.username = vscode.workspace.getConfiguration("weAudit").get("general.username") || userInfo().username;

// register a command that sets the root paths and labels
vscode.commands.registerCommand("weAudit.setMultiConfigRoots", (rootPathsAndLabels: [string, string][]) => {
vscode.commands.registerCommand("weAudit.setMultiConfigRoots", (rootPathsAndLabels: RootPathAndLabel[]) => {
this.rootPathsAndLabels = rootPathsAndLabels;
});

Expand All @@ -43,7 +47,8 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider<Config
});

// register a command that calls findAndLoadConfigurationFiles
vscode.commands.registerCommand("weAudit.findAndLoadConfigurationFiles", () => {
vscode.commands.registerCommand("weAudit.findAndLoadConfigurationFiles", async () => {
await vscode.commands.executeCommand("weAudit.getMultiConfigRoots");
this.findAndLoadConfigurationFiles();
this.refresh();
});
Expand All @@ -52,13 +57,13 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider<Config
findAndLoadConfigurationFiles() {
this.configurationEntries = [];
this.rootEntries = [];
for (const [rootPath, rootLabel] of this.rootPathsAndLabels) {
const vscodeFolder = path.join(rootPath, ".vscode");
for (const rootPathAndLabel of this.rootPathsAndLabels) {
const vscodeFolder = path.join(rootPathAndLabel.rootPath, ".vscode");
if (!fs.existsSync(vscodeFolder)) {
continue;
}

const rootEntry = { label: rootLabel } as WorkspaceRootEntry;
const rootEntry = { label: rootPathAndLabel.rootLabel } as WorkspaceRootEntry;
this.rootEntries.push(rootEntry);

fs.readdirSync(vscodeFolder).forEach((file) => {
Expand Down
42 changes: 26 additions & 16 deletions src/panels/gitConfigPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as crypto from "crypto";

import { getUri } from "../utilities/getUri";
import { WebviewMessage, UpdateRepositoryMessage, SetWorkspaceRootsMessage } from "../webview/webviewMessageTypes";
import { RootPathAndLabel } from "../types";

export function activateGitConfigWebview(context: vscode.ExtensionContext) {
const provider = new GitConfigProvider(context.extensionUri);
Expand All @@ -13,22 +14,22 @@ export function activateGitConfigWebview(context: vscode.ExtensionContext) {
class GitConfigProvider implements vscode.WebviewViewProvider {
public static readonly viewType = "gitConfig";
private _disposables: vscode.Disposable[] = [];
private currentRootPathAndLabel: [string, string];
private currentRootPathAndLabel: RootPathAndLabel;
private dirToPathMap: Map<string, string>;

private _view?: vscode.WebviewView;

constructor(private readonly _extensionUri: vscode.Uri) {
this.currentRootPathAndLabel = ["", ""];
this.currentRootPathAndLabel = { rootPath: "", rootLabel: "" } as RootPathAndLabel;
this.dirToPathMap = new Map<string, string>();

vscode.commands.registerCommand(
"weAudit.setGitConfigView",
(rootPathAndLabel: [string, string], clientRepo: string, auditRepo: string, commitHash: string) => {
(rootPathAndLabel: RootPathAndLabel, clientRepo: string, auditRepo: string, commitHash: string) => {
this.currentRootPathAndLabel = rootPathAndLabel;
const msg: UpdateRepositoryMessage = {
command: "update-repository-config",
rootLabel: rootPathAndLabel[1],
rootLabel: rootPathAndLabel.rootLabel,
clientURL: clientRepo,
auditURL: auditRepo,
commitHash,
Expand All @@ -37,36 +38,42 @@ class GitConfigProvider implements vscode.WebviewViewProvider {
},
);

vscode.commands.registerCommand("weAudit.setGitConfigRoots", (rootPathsAndLabels: [string, string][]) => {
if (!rootPathsAndLabels.includes(this.currentRootPathAndLabel)) {
vscode.commands.registerCommand("weAudit.setGitConfigRoots", (rootPathsAndLabels: RootPathAndLabel[]) => {
const idx = rootPathsAndLabels.findIndex((rootPathAndLabel) => {
return (
rootPathAndLabel.rootPath === this.currentRootPathAndLabel.rootPath && rootPathAndLabel.rootLabel === this.currentRootPathAndLabel.rootLabel
);
});

if (idx === -1) {
if (rootPathsAndLabels.length > 0) {
this.currentRootPathAndLabel = rootPathsAndLabels[0];
}
}
this.dirToPathMap.clear();
for (const [rootPath, rootLabel] of rootPathsAndLabels) {
this.dirToPathMap.set(rootLabel, rootPath);
for (const rootPathAndLabel of rootPathsAndLabels) {
this.dirToPathMap.set(rootPathAndLabel.rootLabel, rootPathAndLabel.rootPath);
}

const msg: SetWorkspaceRootsMessage = {
command: "set-workspace-roots",
rootLabels: rootPathsAndLabels.map((rootPathAndLabel) => rootPathAndLabel[1]),
rootLabels: rootPathsAndLabels.map((rootPathAndLabel) => rootPathAndLabel.rootLabel),
};
this._view?.webview.postMessage(msg);
});

vscode.commands.registerCommand("weAudit.nextGitConfig", async () => {
const nextRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel[0], true);
const nextRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel.rootPath, true);
vscode.commands.executeCommand("weAudit.pushGitConfigView", nextRootPath);
});

vscode.commands.registerCommand("weAudit.prevGitConfig", async () => {
const prevRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel[0], false);
const prevRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel.rootPath, false);
vscode.commands.executeCommand("weAudit.pushGitConfigView", prevRootPath);
});

vscode.commands.registerCommand("weAudit.setupRepositoriesCurrent", () => {
vscode.commands.executeCommand("weAudit.setupRepositoriesOne", this.currentRootPathAndLabel[0]);
vscode.commands.executeCommand("weAudit.setupRepositoriesOne", this.currentRootPathAndLabel.rootPath);
});
}

Expand All @@ -87,7 +94,7 @@ class GitConfigProvider implements vscode.WebviewViewProvider {
webviewView.onDidChangeVisibility(() => {
if (webviewView.visible) {
vscode.commands.executeCommand("weAudit.getGitConfigRoots");
vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel[0]);
vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel.rootPath);
}
});
}
Expand Down Expand Up @@ -140,16 +147,19 @@ class GitConfigProvider implements vscode.WebviewViewProvider {
vscode.commands.executeCommand("weAudit.updateGitConfig", rootPath, message.clientURL, message.auditURL, message.commitHash);
return;
case "choose-workspace-root":
rootPath = this.dirToPathMap.get(message.rootDir);
rootPath = this.dirToPathMap.get(message.rootLabel);
if (rootPath === undefined) {
vscode.window.showErrorMessage(`weAudit: Error choosing workspace root. Directory: ${message.rootDir} is not a workspace root.`);
vscode.window.showErrorMessage(`weAudit: Error choosing workspace root. Directory: ${message.rootLabel} is not a workspace root.`);
return;
}
vscode.commands.executeCommand("weAudit.pushGitConfigView", rootPath);
return;
case "webview-ready":
vscode.commands.executeCommand("weAudit.getGitConfigRoots");
vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel[0] ? this.currentRootPathAndLabel[0] : null);
vscode.commands.executeCommand(
"weAudit.pushGitConfigView",
this.currentRootPathAndLabel.rootPath ? this.currentRootPathAndLabel.rootPath : null,
);
return;
}
},
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ export interface FullPath {
path: string;
}

/**
* A tuple containing a root path and a unique label for that root path
*/
export interface RootPathAndLabel {
rootPath: string;
rootLabel: string;
}
/**
* Creates a PathOrganizer entry.
* @param path the path of the file
Expand Down
2 changes: 1 addition & 1 deletion src/webview/gitConfigMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function handleDropdownChange(_e: Event): void {

const message: ChooseWorkspaceRootMessage = {
command: "choose-workspace-root",
rootDir: rootDropdown.value,
rootLabel: rootDropdown.value,
};
vscode.postMessage(message);
}
2 changes: 1 addition & 1 deletion src/webview/webviewMessageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface UpdateRepositoryMessage {

export interface ChooseWorkspaceRootMessage {
command: "choose-workspace-root";
rootDir: string;
rootLabel: string;
}

export interface SetWorkspaceRootsMessage {
Expand Down

0 comments on commit 0f500bf

Please sign in to comment.