Skip to content

Commit c310eaf

Browse files
committed
feat(ggshield-configuration): fetch API key from config list
1 parent 8568061 commit c310eaf

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/extension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ export function activate(context: ExtensionContext) {
167167
updateStatusBarItem(StatusBarStatus.unauthenticated, statusBar);
168168
} else {
169169
commands.executeCommand('setContext', 'isAuthenticated', true);
170-
}
170+
updateStatusBarItem(StatusBarStatus.ready, statusBar);
171+
const ggshieldApi = ggshieldApiKey(configuration);
172+
setApiKey(configuration, ggshieldApi);
173+
}
171174
})
172175
.then(async () => {
173176
// Check if git is installed
@@ -235,7 +238,8 @@ export function activate(context: ExtensionContext) {
235238
authStatus = true;
236239
updateStatusBarItem(StatusBarStatus.ready, statusBar);
237240
commands.executeCommand('setContext', 'isAuthenticated', true);
238-
setApiKey(configuration, ggshieldApiKey(configuration));
241+
const ggshieldApi = ggshieldApiKey(configuration);
242+
setApiKey(configuration, ggshieldApi);
239243
ggshieldViewProvider.refresh();
240244
ggshieldQuotaViewProvider.refresh();
241245
} else {

src/lib/ggshield-api.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
12
import {
23
SpawnOptionsWithoutStdio,
34
SpawnSyncOptionsWithStringEncoding,
@@ -22,17 +23,25 @@ export function runGGShieldCommand(
2223
args: string[]
2324
): SpawnSyncReturns<string> {
2425
const { ggshieldPath, apiUrl, apiKey } = configuration;
26+
let env: {
27+
GITGUARDIAN_API_URL: string;
28+
GG_USER_AGENT: string;
29+
GITGUARDIAN_API_KEY?: string; // Note the ? to indicate this property is optional
30+
} = {
31+
GITGUARDIAN_API_URL: apiUrl,
32+
GG_USER_AGENT: "gitguardian-vscode",
33+
};
34+
35+
if (apiKey) {
36+
env = {
37+
...env,
38+
// eslint-disable-next-line @typescript-eslint/naming-convention
39+
GITGUARDIAN_API_KEY: apiKey,
40+
};
41+
}
2542

2643
let options: SpawnSyncOptionsWithStringEncoding = {
2744
cwd: os.tmpdir(),
28-
env: {
29-
// eslint-disable-next-line @typescript-eslint/naming-convention
30-
GITGUARDIAN_API_URL: apiUrl,
31-
// eslint-disable-next-line @typescript-eslint/naming-convention
32-
GG_USER_AGENT: "gitguardian-vscode",
33-
//eslint-disable-next-line @typescript-eslint/naming-convention
34-
GITGUARDIAN_API_KEY: apiKey,
35-
},
3645
encoding: "utf-8",
3746
windowsHide: true,
3847
};
@@ -249,9 +258,18 @@ export function ggshieldApiKey(
249258
return undefined;
250259
} else {
251260
console.log(proc.stdout);
252-
const regexToken = /token: ([a-zA-Z0-9]+)/;
253-
const matchToken = proc.stdout.match(regexToken);
254-
255-
return matchToken ? matchToken[1].trim() : undefined;
261+
const apiUrl = configuration.apiUrl;
262+
const re = /api/;
263+
264+
const regexInstanceSection = `\\[${apiUrl.replace(re, "dashboard")}\\]([\\s\\S]*?)(?=\\[|$)`;
265+
const instanceSectionMatch = proc.stdout.match(regexInstanceSection);
266+
267+
if (instanceSectionMatch) {
268+
const instanceSection = instanceSectionMatch[0];
269+
const regexToken = /token:\s([a-zA-Z0-9]+)/;
270+
const matchToken = instanceSection.match(regexToken);
271+
272+
return matchToken ? matchToken[1].trim() : undefined;
273+
}
256274
}
257275
}

src/lib/ggshield-configuration.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getBinaryAbsolutePath } from "./ggshield-resolver-utils";
2-
import { ExtensionContext, workspace } from "vscode";
2+
import { ConfigurationTarget, ExtensionContext, workspace } from "vscode";
33
import * as os from "os";
44

55
const apiUrlDefault = "https://api.gitguardian.com/";
@@ -43,6 +43,7 @@ export function setApiKey(configuration: GGShieldConfiguration, apiKey: string |
4343
if (!apiKey) {
4444
throw new Error("Missing API Key");
4545
}
46+
4647
configuration.apiKey = apiKey;
47-
config.update("apiKey", apiKey);
48+
config.update("apiKey", apiKey, ConfigurationTarget.Global);
4849
}

0 commit comments

Comments
 (0)