Skip to content

Commit

Permalink
chore: track redhat version recommendation acceptance for telemetry (#…
Browse files Browse the repository at this point in the history
…248)

Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov authored Mar 26, 2024
1 parent fa9a7bb commit 345a388
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 64 deletions.
14 changes: 7 additions & 7 deletions src/codeActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ function generateFullStackAnalysisAction(): CodeAction {
/**
* Generates a code action to switch to the recommended version.
* @param title - The title of the code action.
* @param dependency - The dependency (package and version) provided by exhort.
* @param versionReplacementString - The version replacement string.
* @param diagnostic - The diagnostic information.
* @param uri - The URI of the file.
* @returns A CodeAction object for switching to the recommended version.
*/
function generateSwitchToRecommendedVersionAction(title: string, versionReplacementString: string, diagnostic: Diagnostic, uri: string): CodeAction {
function generateSwitchToRecommendedVersionAction(title: string, dependency: string, versionReplacementString: string, diagnostic: Diagnostic, uri: string): CodeAction {
const codeAction: CodeAction = {
title: title,
diagnostics: [diagnostic],
Expand All @@ -107,12 +108,11 @@ function generateSwitchToRecommendedVersionAction(title: string, versionReplacem
newText: versionReplacementString
}];

if (path.basename(uri) === 'pom.xml') {
codeAction.command = {
title: 'RedHat repository recommendation',
command: globalConfig.rhRepositoryRecommendationNotificationCommand,
};
}
codeAction.command = {
title: 'Track recommendation acceptance',
command: globalConfig.trackRecommendationAcceptanceCommand,
arguments: [dependency, path.basename(uri)],
};

return codeAction;
}
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Config
{
stackAnalysisCommand: string;
rhRepositoryRecommendationNotificationCommand: string;
trackRecommendationAcceptanceCommand: string;
telemetryId: string;
utmSource: string;
exhortSnykToken: string;
Expand All @@ -38,7 +38,7 @@ class Config

load() {
this.stackAnalysisCommand = process.env.VSCEXT_STACK_ANALYSIS_COMMAND || '';
this.rhRepositoryRecommendationNotificationCommand = process.env.VSCEXT_REDHAT_REPOSITORY_RECOMMENDATION_NOTIFICATION_COMMAND || '';
this.trackRecommendationAcceptanceCommand = process.env.VSCEXT_TRACK_RECOMMENDATION_ACCEPTANCE_COMMAND || '';
this.telemetryId = process.env.VSCEXT_TELEMETRY_ID || '';
this.utmSource = process.env.VSCEXT_UTM_SOURCE || '';
this.exhortSnykToken = process.env.VSCEXT_EXHORT_SNYK_TOKEN || '';
Expand Down
5 changes: 3 additions & 2 deletions src/diagnosticsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ class DiagnosticsPipeline implements IDiagnosticsPipeline {
* @private
*/
private createCodeAction(loc: string, ref: string, context: IPositionedContext, sourceId: string, vulnerabilityDiagnostic: Diagnostic) {
const switchToVersion = this.provider.resolveDependencyFromReference(ref).split('@')[1];
const dependency = this.provider.resolveDependencyFromReference(ref);
const switchToVersion = dependency.split('@')[1];
const versionReplacementString = context ? context.value.replace(VERSION_PLACEHOLDER, switchToVersion) : switchToVersion;
const title = `Switch to version ${switchToVersion} for ${sourceId}`;
const codeAction = generateSwitchToRecommendedVersionAction(title, versionReplacementString, vulnerabilityDiagnostic, this.diagnosticFilePath);
const codeAction = generateSwitchToRecommendedVersionAction(title, dependency, versionReplacementString, vulnerabilityDiagnostic, this.diagnosticFilePath);
registerCodeAction(this.diagnosticFilePath, loc, codeAction);
}
}
Expand Down
61 changes: 9 additions & 52 deletions test/codeActionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,66 +218,23 @@ describe('Code Action Handler tests', () => {
codeActionHandler.clearCodeActionsMap(mockUri);
});

it('should return a switch to recommended version code action without RedHat repository recommendation', async () => {

const codeAction: CodeAction = codeActionHandler.generateSwitchToRecommendedVersionAction( 'mockTitle', 'mockVersionReplacementString', mockDiagnostic0[0], 'mock/path/noPom.xml');
expect(codeAction).to.deep.equal(
{
"diagnostics": [
{
"message": "mock message",
"range": {
"end": {
"character": 456,
"line": 456
},
"start": {
"character": 123,
"line": 123
}
},
"severity": 1,
"source": RHDA_DIAGNOSTIC_SOURCE
}
],
"edit": {
"changes": {
"mock/path/noPom.xml": [
{
"newText": "mockVersionReplacementString",
"range": {
"end": {
"character": 456,
"line": 456
},
"start": {
"character": 123,
"line": 123
}
}
}
]
}
},
"kind": "quickfix",
"title": "mockTitle"
}
);
});

it('should return a switch to recommended version code action with RedHat repository recommendation', async () => {
it('should return a switch to recommended version code action', async () => {

let globalConfig = {
rhRepositoryRecommendationNotificationCommand: 'mockRHRepositoryRecommendationNotificationCommand'
trackRecommendationAcceptanceCommand: 'mockTrackRecommendationAcceptanceCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

const codeAction: CodeAction = codeActionHandler.generateSwitchToRecommendedVersionAction( 'mockTitle', 'mockVersionReplacementString', mockDiagnostic1[0], 'mock/path/pom.xml');
const codeAction: CodeAction = codeActionHandler.generateSwitchToRecommendedVersionAction( 'mockTitle', 'mockPackage@mockversion', 'mockVersionReplacementString', mockDiagnostic1[0], 'mock/path/pom.xml');
expect(codeAction).to.deep.equal(
{
"command": {
"command": 'mockRHRepositoryRecommendationNotificationCommand',
"title": "RedHat repository recommendation"
"command": 'mockTrackRecommendationAcceptanceCommand',
"title": "Track recommendation acceptance",
"arguments": [
"mockPackage@mockversion",
"pom.xml"
]
},
"diagnostics": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Config tests', () => {

it('should initialize with default values when environment variables are not set', () => {
expect(mockConfig.stackAnalysisCommand).to.eq('');
expect(mockConfig.rhRepositoryRecommendationNotificationCommand).to.eq('');
expect(mockConfig.trackRecommendationAcceptanceCommand).to.eq('');
expect(mockConfig.telemetryId).to.eq('');
expect(mockConfig.utmSource).to.eq('');
expect(mockConfig.exhortSnykToken).to.eq('');
Expand Down

0 comments on commit 345a388

Please sign in to comment.