Skip to content

Commit bcdb1af

Browse files
authored
Merge pull request #24 from GitGuardian/salomevoltz/standardize-api-dashboard-conversion
fix(remediation-message): convert dashboard to api
2 parents 683a6da + 377ca7c commit bcdb1af

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/lib/ggshield-api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import axios from 'axios';
1111
import { GGShieldConfiguration } from "./ggshield-configuration";
1212
import { GGShieldScanResults } from "./api-types";
1313
import * as os from "os";
14+
import { apiToDashboard, dasboardToApi } from "../utils";
1415

1516
/**
1617
* Run ggshield CLI application with specified arguments
@@ -97,9 +98,10 @@ export async function getAPIquota(
9798
export async function getRemediationMessage(
9899
configuration: GGShieldConfiguration
99100
): Promise<string> {
101+
const apiUrl = dasboardToApi(configuration.apiUrl);
100102
const path = require('node:path');
101103
try {
102-
const response = await axios.get(path.join(configuration.apiUrl,'v1/metadata'), {
104+
const response = await axios.get(path.join(apiUrl,'v1/metadata'), {
103105
headers: {
104106
'authorization': `Token ${configuration.apiKey}`
105107
}
@@ -305,7 +307,7 @@ export function ggshieldApiKey(
305307
const apiUrl = configuration.apiUrl;
306308
const re = /api/;
307309

308-
const regexInstanceSection = `\\[${apiUrl.replace(re, "dashboard")}\\]([\\s\\S]*?)(?=\\[|$)`;
310+
const regexInstanceSection = `\\[${apiToDashboard(apiUrl)}\\]([\\s\\S]*?)(?=\\[|$)`;
309311
const instanceSectionMatch = proc.stdout.match(regexInstanceSection);
310312

311313
if (instanceSectionMatch) {

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import * as vscode from "vscode";
22
import { spawnSync } from "child_process";
33
import path = require("path");
44

5+
const reDashboard = "dashboard";
6+
const reApi = "api";
7+
const gitGuardianDomains = ["gitguardian.tech", "gitguardian.com"];
8+
59
export async function isGitInstalled(): Promise<boolean> {
610
return new Promise((resolve) => {
711
let proc = spawnSync("git", ["--version"]);
@@ -22,3 +26,21 @@ export function getCurrentFile(): string {
2226
return "";
2327
}
2428
}
29+
30+
export function dasboardToApi(dashboardUrl: string): string {
31+
const domainMatch = gitGuardianDomains.some((domain) => dashboardUrl.includes(domain));
32+
if (domainMatch) {
33+
return dashboardUrl.replace(reDashboard, reApi);
34+
} else {
35+
return dashboardUrl;
36+
}
37+
}
38+
39+
export function apiToDashboard(apiUrl: string): string {
40+
const domainMatch = gitGuardianDomains.some((domain) => apiUrl.includes(domain));
41+
if (domainMatch) {
42+
return apiUrl.replace(reApi, reDashboard);
43+
} else {
44+
return apiUrl;
45+
}
46+
}

0 commit comments

Comments
 (0)