Skip to content

Commit b9c16bb

Browse files
chore: remove async on synchronous functions
1 parent 9625776 commit b9c16bb

10 files changed

+134
-173
lines changed

src/extension.ts

Lines changed: 89 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
cleanUpFileDiagnostics,
33
createDiagnosticCollection,
4-
ggshieldApiKey,
54
ggshieldAuthStatus,
65
ignoreLastFound,
76
ignoreSecret,
@@ -21,7 +20,7 @@ import {
2120
WebviewView,
2221
} from "vscode";
2322
import { GGShieldResolver } from "./lib/ggshield-resolver";
24-
import { getCurrentFile, isGitInstalled } from "./utils";
23+
import { getCurrentFile, checkGitInstalled } from "./utils";
2524
import { GitGuardianWebviewProvider } from "./ggshield-webview/gitguardian-webview-view";
2625
import {
2726
createStatusBarItem,
@@ -67,13 +66,6 @@ function registerOpenViewsCommands(
6766
);
6867
}
6968

70-
function registerQuotaViewCommands(view: GitGuardianQuotaWebviewProvider) {
71-
commands.registerCommand(
72-
"gitguardian.refreshQuota",
73-
async () => await view.refresh()
74-
);
75-
}
76-
7769
export function activate(context: ExtensionContext) {
7870
// Check if ggshield if available
7971
commands.executeCommand("setContext", "isAuthenticated", false);
@@ -122,123 +114,107 @@ export function activate(context: ExtensionContext) {
122114

123115
//generic commands to open correct view on status bar click
124116
registerOpenViewsCommands(context, outputChannel);
125-
registerQuotaViewCommands(ggshieldQuotaViewProvider);
117+
commands.registerCommand(
118+
"gitguardian.refreshQuota",
119+
ggshieldQuotaViewProvider.refresh
120+
);
126121

127122
context.subscriptions.push(
128123
languages.registerHoverProvider("*", new GitGuardianSecretHoverProvider())
129124
);
130125

131-
ggshieldResolver
132-
.checkGGShieldConfiguration()
133-
.then(() => {
134-
// Check if ggshield is authenticated
135-
ggshieldAuthStatus(configuration, context);
136-
if (context.globalState.get("isAuthenticated", false)) {
137-
updateStatusBarItem(StatusBarStatus.ready);
138-
ggshieldViewProvider.refresh();
139-
ggshieldRemediationMessageViewProvider.refresh();
140-
ggshieldQuotaViewProvider.refresh();
141-
} else {
142-
updateStatusBarItem(StatusBarStatus.unauthenticated);
143-
}
144-
})
145-
.then(async () => {
146-
// Check if git is installed
147-
console.log("git is installed and configured");
148-
const gitInstallation = await isGitInstalled();
149-
if (!gitInstallation) {
150-
window.showErrorMessage(
151-
`GGShield requires git to work correctly. Please install git.`
126+
checkGitInstalled();
127+
128+
ggshieldResolver.checkGGShieldConfiguration();
129+
130+
// Check if ggshield is authenticated
131+
ggshieldAuthStatus(configuration, context).then(() => {
132+
if (context.globalState.get("isAuthenticated", false)) {
133+
updateStatusBarItem(StatusBarStatus.ready);
134+
ggshieldViewProvider.refresh();
135+
ggshieldRemediationMessageViewProvider.refresh();
136+
ggshieldQuotaViewProvider.refresh();
137+
} else {
138+
updateStatusBarItem(StatusBarStatus.unauthenticated);
139+
}
140+
});
141+
142+
// Start scanning documents on activation events
143+
// (i.e. when a new document is opened or when the document is saved)
144+
createDiagnosticCollection(context);
145+
context.subscriptions.push(
146+
workspace.onDidSaveTextDocument((textDocument) => {
147+
// Check if the document is inside the workspace
148+
const workspaceFolder = workspace.getWorkspaceFolder(textDocument.uri);
149+
if (
150+
context.globalState.get("isAuthenticated", false) &&
151+
workspaceFolder
152+
) {
153+
scanFile(
154+
textDocument.fileName,
155+
textDocument.uri,
156+
ggshieldResolver.configuration
152157
);
153158
}
154-
})
155-
.then(() => {
156-
// Start scanning documents on activation events
157-
// (i.e. when a new document is opened or when the document is saved)
158-
createDiagnosticCollection(context);
159-
context.subscriptions.push(
160-
workspace.onDidSaveTextDocument((textDocument) => {
161-
// Check if the document is inside the workspace
162-
const workspaceFolder = workspace.getWorkspaceFolder(
163-
textDocument.uri
164-
);
165-
if (
166-
context.globalState.get("isAuthenticated", false) &&
167-
workspaceFolder
168-
) {
169-
scanFile(
170-
textDocument.fileName,
171-
textDocument.uri,
172-
ggshieldResolver.configuration
173-
);
174-
}
175-
}),
176-
workspace.onDidCloseTextDocument((textDocument) =>
177-
cleanUpFileDiagnostics(textDocument.uri)
178-
),
179-
commands.registerCommand("gitguardian.quota", () => {
180-
showAPIQuota(ggshieldResolver.configuration);
181-
}),
182-
commands.registerCommand("gitguardian.ignore", () => {
183-
ignoreLastFound(ggshieldResolver.configuration);
184-
if (window.activeTextEditor) {
185-
cleanUpFileDiagnostics(window.activeTextEditor?.document.uri);
186-
}
187-
}),
188-
commands.registerCommand(
189-
"gitguardian.ignoreSecret",
190-
(diagnosticData) => {
191-
window.showInformationMessage("Secret ignored.");
192-
let currentFile = getCurrentFile();
193-
let secretName = generateSecretName(currentFile, diagnosticData);
194-
195-
ignoreSecret(
196-
ggshieldResolver.configuration,
197-
diagnosticData.secretSha,
198-
secretName
199-
);
200-
scanFile(
201-
currentFile,
202-
Uri.file(currentFile),
203-
ggshieldResolver.configuration
204-
);
159+
}),
160+
workspace.onDidCloseTextDocument((textDocument) =>
161+
cleanUpFileDiagnostics(textDocument.uri)
162+
),
163+
commands.registerCommand("gitguardian.quota", () => {
164+
showAPIQuota(ggshieldResolver.configuration);
165+
}),
166+
commands.registerCommand("gitguardian.ignore", () => {
167+
ignoreLastFound(ggshieldResolver.configuration);
168+
if (window.activeTextEditor) {
169+
cleanUpFileDiagnostics(window.activeTextEditor?.document.uri);
170+
}
171+
}),
172+
commands.registerCommand("gitguardian.ignoreSecret", (diagnosticData) => {
173+
window.showInformationMessage("Secret ignored.");
174+
let currentFile = getCurrentFile();
175+
let secretName = generateSecretName(currentFile, diagnosticData);
176+
177+
ignoreSecret(
178+
ggshieldResolver.configuration,
179+
diagnosticData.secretSha,
180+
secretName
181+
);
182+
scanFile(
183+
currentFile,
184+
Uri.file(currentFile),
185+
ggshieldResolver.configuration
186+
);
187+
}),
188+
commands.registerCommand("gitguardian.authenticate", async () => {
189+
commands.executeCommand("gitguardian.openSidebar");
190+
await loginGGShield(
191+
ggshieldResolver.configuration,
192+
outputChannel,
193+
ggshieldViewProvider.getView() as WebviewView,
194+
context
195+
)
196+
.then(() => {
197+
if (context.globalState.get("isAuthenticated", false)) {
198+
updateStatusBarItem(StatusBarStatus.ready);
199+
} else {
200+
updateStatusBarItem(StatusBarStatus.unauthenticated);
205201
}
206-
),
207-
commands.registerCommand("gitguardian.authenticate", async () => {
208-
commands.executeCommand("gitguardian.openSidebar");
209-
await loginGGShield(
210-
ggshieldResolver.configuration,
211-
outputChannel,
212-
ggshieldViewProvider.getView() as WebviewView,
213-
context
214-
)
215-
.then(() => {
216-
if (context.globalState.get("isAuthenticated", false)) {
217-
updateStatusBarItem(StatusBarStatus.ready);
218-
} else {
219-
updateStatusBarItem(StatusBarStatus.unauthenticated);
220-
}
221-
ggshieldViewProvider.refresh();
222-
ggshieldRemediationMessageViewProvider.refresh();
223-
ggshieldQuotaViewProvider.refresh();
224-
})
225-
.catch((err) => {
226-
outputChannel.appendLine(`Authentication failed: ${err.message}`);
227-
});
228-
}),
229-
commands.registerCommand("gitguardian.logout", async () => {
230-
logoutGGShield(ggshieldResolver.configuration, context);
231-
updateStatusBarItem(StatusBarStatus.unauthenticated);
232202
ggshieldViewProvider.refresh();
233203
ggshieldRemediationMessageViewProvider.refresh();
234204
ggshieldQuotaViewProvider.refresh();
235205
})
236-
);
206+
.catch((err) => {
207+
outputChannel.appendLine(`Authentication failed: ${err.message}`);
208+
});
209+
}),
210+
commands.registerCommand("gitguardian.logout", async () => {
211+
logoutGGShield(ggshieldResolver.configuration, context);
212+
updateStatusBarItem(StatusBarStatus.unauthenticated);
213+
ggshieldViewProvider.refresh();
214+
ggshieldRemediationMessageViewProvider.refresh();
215+
ggshieldQuotaViewProvider.refresh();
237216
})
238-
.catch((error) => {
239-
outputChannel.appendLine(`Error: ${error.message}`);
240-
updateStatusBarItem(StatusBarStatus.error);
241-
});
217+
);
242218
}
243219

244220
export function deactivate() {}

src/ggshield-webview/gitguardian-quota-webview.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class GitGuardianQuotaWebviewProvider
2020
this.updateQuota();
2121
}
2222

23-
public async resolveWebviewView(
23+
public resolveWebviewView(
2424
webviewView: vscode.WebviewView,
2525
context: vscode.WebviewViewResolveContext,
2626
_token: vscode.CancellationToken
@@ -36,16 +36,16 @@ export class GitGuardianQuotaWebviewProvider
3636
});
3737
}
3838

39-
private async checkAuthenticationStatus() {
39+
private checkAuthenticationStatus() {
4040
this.isAuthenticated = this.context.globalState.get(
4141
"isAuthenticated",
4242
false
4343
);
4444
}
4545

46-
private async updateQuota() {
46+
private updateQuota() {
4747
if (this.isAuthenticated) {
48-
this.quota = await getAPIquota(this.ggshieldConfiguration);
48+
this.quota = getAPIquota(this.ggshieldConfiguration);
4949
}
5050
}
5151

@@ -85,12 +85,12 @@ export class GitGuardianQuotaWebviewProvider
8585
}
8686
}
8787

88-
public async refresh() {
88+
public refresh() {
8989
this.isLoading = true;
9090
this.updateWebViewContent(this._view);
9191

92-
await this.checkAuthenticationStatus();
93-
await this.updateQuota();
92+
this.checkAuthenticationStatus();
93+
this.updateQuota();
9494

9595
this.isLoading = false;
9696
this.updateWebViewContent(this._view);

src/ggshield-webview/gitguardian-remediation-message-view.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class GitGuardianRemediationMessageWebviewProvider
2020
this.updateRemediationMessage();
2121
}
2222

23-
public async resolveWebviewView(
23+
public resolveWebviewView(
2424
webviewView: vscode.WebviewView,
2525
context: vscode.WebviewViewResolveContext,
2626
_token: vscode.CancellationToken
@@ -36,14 +36,14 @@ export class GitGuardianRemediationMessageWebviewProvider
3636
});
3737
}
3838

39-
private async checkAuthenticationStatus() {
39+
private checkAuthenticationStatus() {
4040
this.isAuthenticated = this.context.globalState.get(
4141
"isAuthenticated",
4242
false
4343
);
4444
}
4545

46-
private async updateRemediationMessage() {
46+
private updateRemediationMessage() {
4747
if (this.isAuthenticated) {
4848
//TODO: Get remediation message
4949
this.remediationMessage = "";
@@ -97,13 +97,13 @@ ${this.escapeHtml(this.remediationMessage)}
9797
}
9898
}
9999

100-
public async refresh() {
100+
public refresh() {
101101
this.isLoading = true;
102102
this.updateWebViewContent(this._view);
103103

104-
await this.checkAuthenticationStatus();
104+
this.checkAuthenticationStatus();
105105
console.log("Well authenticated");
106-
await this.updateRemediationMessage();
106+
this.updateRemediationMessage();
107107

108108
this.isLoading = false;
109109
this.updateWebViewContent(this._view);

src/ggshield-webview/gitguardian-webview-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
2424
this.checkAuthenticationStatus();
2525
}
2626

27-
public async resolveWebviewView(
27+
public resolveWebviewView(
2828
webviewView: vscode.WebviewView,
2929
context: vscode.WebviewViewResolveContext,
3030
_token: vscode.CancellationToken
@@ -52,7 +52,7 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
5252
return this._view;
5353
}
5454

55-
private async checkAuthenticationStatus() {
55+
private checkAuthenticationStatus() {
5656
this.isAuthenticated = this.context.globalState.get(
5757
"isAuthenticated",
5858
false

src/lib/ggshield-api.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ export function showAPIQuota(configuration: GGShieldConfiguration): undefined {
4848
}
4949
}
5050

51-
export async function getAPIquota(
52-
configuration: GGShieldConfiguration
53-
): Promise<number> {
51+
export function getAPIquota(configuration: GGShieldConfiguration): number {
5452
try {
5553
const proc = runGGShieldCommand(configuration, ["quota", "--json"]);
5654
return JSON.parse(proc.stdout).remaining;
@@ -137,12 +135,11 @@ export function cleanUpFileDiagnostics(fileUri: Uri): void {
137135
* @param filePath path to file
138136
* @param fileUri file uri
139137
*/
140-
export async function scanFile(
141-
this: any,
138+
export function scanFile(
142139
filePath: string,
143140
fileUri: Uri,
144141
configuration: GGShieldConfiguration
145-
): Promise<void> {
142+
): void {
146143
if (isFileGitignored(filePath)) {
147144
updateStatusBarItem(StatusBarStatus.ignoredFile);
148145
return;

0 commit comments

Comments
 (0)