Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 6e0bdf2

Browse files
authored
Merge pull request #55 from codiga/feat/add-user-agent-headers
feat: add user agent headers
2 parents cb44259 + 02a3dcb commit 6e0bdf2

12 files changed

Lines changed: 175 additions & 7 deletions

File tree

package-lock.json

Lines changed: 93 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"React",
3232
"Typescript"
3333
],
34-
"version": "0.8.5",
34+
"version": "0.8.7",
3535
"engines": {
3636
"vscode": "^1.70.0"
3737
},
@@ -205,6 +205,7 @@
205205
"react-markdown": "^8.0.3",
206206
"remark": "^14.0.2",
207207
"remark-html": "^15.0.1",
208+
"rollbar": "^2.26.0",
208209
"yaml": "^2.1.1"
209210
},
210211
"__metadata": {

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { Language } from "./graphql-api/types";
33
export const GRAPHQL_ENDPOINT_PROD = "https://api.codiga.io/graphql";
44
export const GRAPHQL_ENDPOINT_STAGING = "https://api-staging.codiga.io/graphql";
55

6+
export const CODIGA_EXTENSION_ID = "codiga.vscode-plugin";
7+
8+
export const API_TOKEN_HEADER_KEY = "X-Api-Token";
9+
export const USER_AGENT_HEADER_KEY = "User-Agent";
10+
export const USER_AGENT_HEADER_PRODUCT = "VsCode";
11+
612
export const CODING_ASSISTANT_WAIT_BEFORE_QUERYING_RESULTS_IN_MS = 200;
713

814
export const MESSAGE_STARTUP_SHOW_SHORTCUTS = "View Shortcuts";

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ import { provideInlineComplextion } from "./code-completion/inline-completion";
3939
import { AssistantRecipe } from "./graphql-api/types";
4040
import { subscribeToDocumentChanges } from "./diagnostics/diagnostics";
4141
import { applyFix, RosieFixAction } from "./rosie/rosiefix";
42-
import { RosieFix, Violation } from "./rosie/rosieTypes";
42+
import { RosieFix } from "./rosie/rosieTypes";
4343
import { refreshCachePeriodic } from "./rosie/rosieCache";
4444
import { recordLastActivity } from "./utils/activity";
4545
import {
4646
IgnoreViolation,
4747
ignoreViolation,
4848
} from "./diagnostics/ignore-violation";
4949
import { checkCodigaFileSuggestion } from "./utils/startupUtils";
50+
import { rollbarLogger } from "./utils/rollbarUtils";
5051

5152
// this method is called when your extension is activated
5253
// your extension is activated the very first time the command is executed
@@ -291,6 +292,7 @@ export async function activate(context: vscode.ExtensionContext) {
291292
} catch (e) {
292293
console.debug("Error when trying to fetch shortcuts");
293294
console.debug(e);
295+
rollbarLogger(e);
294296
}
295297
});
296298

@@ -305,6 +307,7 @@ export async function activate(context: vscode.ExtensionContext) {
305307
} catch (e) {
306308
console.debug("Error when trying to refresh the webview");
307309
console.debug(e);
310+
rollbarLogger(e);
308311
}
309312
});
310313

src/graphql-api/client.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import * as vscode from "vscode";
12
import { GraphQLClient } from "graphql-request";
2-
import { GRAPHQL_ENDPOINT_STAGING, GRAPHQL_ENDPOINT_PROD } from "../constants";
3+
import {
4+
GRAPHQL_ENDPOINT_PROD,
5+
API_TOKEN_HEADER_KEY,
6+
USER_AGENT_HEADER_KEY,
7+
USER_AGENT_HEADER_PRODUCT,
8+
} from "../constants";
39
import { getApiToken } from "./configuration";
10+
import { getExtensionVersion } from "../utils/extensionUtils";
11+
import { rollbarLogger } from "../utils/rollbarUtils";
412

513
let client: GraphQLClient;
614

@@ -15,16 +23,25 @@ export function initializeClient(): void {
1523
function generateHeaders(): Record<string, string> {
1624
const apiToken = getApiToken();
1725

26+
const userAgentHeader = {
27+
[USER_AGENT_HEADER_KEY]: `${USER_AGENT_HEADER_PRODUCT}/${
28+
getExtensionVersion() || ""
29+
}`,
30+
};
31+
1832
/**
1933
* First, check if there is a token. If that is the case,
2034
* prioritize its use.
2135
*/
2236
if (apiToken && apiToken.length > 20) {
2337
return {
24-
"X-Api-Token": apiToken,
38+
...userAgentHeader,
39+
[API_TOKEN_HEADER_KEY]: apiToken,
2540
};
2641
}
27-
return {};
42+
return {
43+
...userAgentHeader,
44+
};
2845
}
2946

3047
/**
@@ -45,6 +62,10 @@ export function doQuery(
4562
.catch((e) => {
4663
console.log("exception when querying the GraphQL API");
4764
console.log(e);
65+
// ignore user-not-logged errors
66+
if (!e.message.includes("user-not-logged")) {
67+
rollbarLogger(e, { variables });
68+
}
4869
return undefined;
4970
});
5071
return query;
@@ -65,6 +86,7 @@ export function doMutation(
6586
.catch((e) => {
6687
console.error("exception");
6788
console.debug(e);
89+
rollbarLogger(e, { variables });
6890
return undefined;
6991
});
7092
return query;

src/graphql-api/shortcut-cache.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { wasActiveRecently } from "../utils/activity";
77
import { getDependencies } from "../utils/dependencies/get-dependencies";
88
import { getLanguageForDocument } from "../utils/fileUtils";
9+
import { rollbarLogger } from "../utils/rollbarUtils";
910
import {
1011
getRecipesForClientByShorcut,
1112
getRecipesForClientByShorcutLastTimestamp,
@@ -204,6 +205,7 @@ export const fetchShortcuts = async () => {
204205
export const fetchPeriodicShortcuts = async () => {
205206
if (enablePeriodicPolling) {
206207
await fetchShortcuts().catch((e) => {
208+
rollbarLogger(e);
207209
console.error("error while fetching shortcuts");
208210
});
209211
garbageCollection(cache);

src/rosie/debug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import * as fs from "fs";
33
import { Rule, RuleSet } from "./rosieTypes";
44
import { CODIGA_RULES_DEBUGFILE } from "../constants";
5+
import { rollbarLogger } from "../utils/rollbarUtils";
56

67
/**
78
* Get the rules from the rulesets in JSON in the .codigadebug file
@@ -36,8 +37,9 @@ export const getRulesDebug = async (
3637
return undefined;
3738
}
3839
return getRulesFromDebugRulesets(rulesJson);
39-
} catch (err) {
40+
} catch (e) {
4041
console.debug("error when trying to read the rules");
42+
rollbarLogger(e);
4143
return undefined;
4244
}
4345
}

src/rosie/rosieCache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { wasActiveRecently } from "../utils/activity";
1212
import { getLanguageForDocument } from "../utils/fileUtils";
1313
import { GRAPHQL_LANGUAGE_TO_ROSIE_LANGUAGE } from "./rosieConstants";
1414
import { Rule } from "./rosieTypes";
15+
import { rollbarLogger } from "../utils/rollbarUtils";
1516

1617
interface CacheData {
1718
lastRefreshed: number; // last time we refreshed the data
@@ -27,6 +28,7 @@ const RULES_CACHE = new Map<vscode.WorkspaceFolder, CacheData>();
2728
*/
2829
export const refreshCachePeriodic = async (): Promise<void> => {
2930
await refreshCache(RULES_CACHE).catch((e) => {
31+
rollbarLogger(e);
3032
console.error("error while fetching shortcuts");
3133
});
3234
garbageCollection(RULES_CACHE);
@@ -116,6 +118,7 @@ const getRulesFromYamlFile = async (
116118
} catch (e) {
117119
console.log("error when reading the updating the rules");
118120
console.log(e);
121+
rollbarLogger(e);
119122
return [];
120123
}
121124
};
@@ -193,6 +196,7 @@ const updateCacheForWorkspace = async (
193196
} catch (e) {
194197
console.log("error when reading the updating the rules");
195198
console.log(e);
199+
rollbarLogger(e);
196200
}
197201
};
198202

src/utils/dependencies/get-dependencies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
NODE_PACKAGE_FILE,
1313
REQUIREMENTS_FILE,
1414
} from "../../constants";
15+
import { rollbarLogger } from "../rollbarUtils";
1516

1617
/**
1718
* Search from the top of the project a dependency file and walk backwards to the
@@ -58,6 +59,7 @@ export async function getDependenciesFromProject(
5859
return await functionToExecute(potentialDependencyFile).catch((e) => {
5960
console.debug("Codiga - error when fetching dependencies");
6061
console.debug(e);
62+
rollbarLogger(e, { packageFile: filename });
6163
return [];
6264
});
6365
}

src/utils/extensionUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as vscode from "vscode";
2+
import { CODIGA_EXTENSION_ID } from "../constants";
3+
4+
export function getExtensionVersion(): string | null {
5+
const codigaExtension = vscode.extensions.getExtension(CODIGA_EXTENSION_ID);
6+
7+
if (codigaExtension) {
8+
return codigaExtension.packageJSON.version;
9+
}
10+
return null;
11+
}

0 commit comments

Comments
 (0)