Skip to content

Commit

Permalink
feat: receive updated Snyk token by notification from clients VSCode …
Browse files Browse the repository at this point in the history
…Secret Storage (#243)

Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov authored Feb 29, 2024
1 parent 042c06b commit d013683
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/codeActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function getDiagnosticsCodeActions(diagnostics: Diagnostic[], uri: string): Code
hasRhdaDiagonostic ||= diagnostic.source === RHDA_DIAGNOSTIC_SOURCE;
}

if (globalConfig.triggerFullStackAnalysis && hasRhdaDiagonostic) {
if (globalConfig.stackAnalysisCommand && hasRhdaDiagonostic) {
codeActions.push(generateFullStackAnalysisAction());
}

Expand All @@ -78,7 +78,7 @@ function generateFullStackAnalysisAction(): CodeAction {
kind: CodeActionKind.QuickFix,
command: {
title: 'Analytics Report',
command: globalConfig.triggerFullStackAnalysis,
command: globalConfig.stackAnalysisCommand,
}
};
}
Expand Down Expand Up @@ -110,7 +110,7 @@ function generateSwitchToRecommendedVersionAction(title: string, versionReplacem
if (path.basename(uri) === 'pom.xml') {
codeAction.command = {
title: 'RedHat repository recommendation',
command: globalConfig.triggerRHRepositoryRecommendationNotification,
command: globalConfig.rhRepositoryRecommendationNotificationCommand,
};
}

Expand Down
17 changes: 12 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
class Config
{
triggerFullStackAnalysis: string;
triggerRHRepositoryRecommendationNotification: string;
stackAnalysisCommand: string;
rhRepositoryRecommendationNotificationCommand: string;
telemetryId: string;
utmSource: string;
exhortSnykToken: string;
Expand All @@ -33,8 +33,8 @@ class Config
}

load() {
this.triggerFullStackAnalysis = process.env.VSCEXT_TRIGGER_FULL_STACK_ANALYSIS || '';
this.triggerRHRepositoryRecommendationNotification = process.env.VSCEXT_TRIGGER_REDHAT_REPOSITORY_RECOMMENDATION_NOTIFICATION || '';
this.stackAnalysisCommand = process.env.VSCEXT_STACK_ANALYSIS_COMMAND || '';
this.rhRepositoryRecommendationNotificationCommand = process.env.VSCEXT_REDHAT_REPOSITORY_RECOMMENDATION_NOTIFICATION_COMMAND || '';
this.telemetryId = process.env.VSCEXT_TELEMETRY_ID || '';
this.utmSource = process.env.VSCEXT_UTM_SOURCE || '';
this.exhortSnykToken = process.env.VSCEXT_EXHORT_SNYK_TOKEN || '';
Expand All @@ -54,7 +54,6 @@ class Config
* @param data - The data from extension workspace settings to update the global configuration with.
*/
updateConfig( rhdaData: any ) {
this.exhortSnykToken = rhdaData.exhortSnykToken;
this.matchManifestVersions = rhdaData.matchManifestVersions ? 'true' : 'false';
this.vulnerabilityAlertSeverity = rhdaData.vulnerabilityAlertSeverity;
this.exhortMvnPath = rhdaData.mvn.executable.path || 'mvn';
Expand All @@ -65,6 +64,14 @@ class Config
this.exhortPythonPath = rhdaData.python.executable.path || 'python';
this.exhortPipPath = rhdaData.pip.executable.path || 'pip';
}

/**
* Sets the Snyk token.
* @param token The Snyk token to be set.
*/
setExhortSnykToken( token: string ) {
this.exhortSnykToken = token;
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ connection.onCodeAction((params): CodeAction[] => {
return getDiagnosticsCodeActions(params.context.diagnostics, params.textDocument.uri);
});

/**
* Registers a callback for the 'snykTokenModified' notification.
*
* @param notification The name of the notification.
* @param callback The callback function to be invoked when the notification is received.
* @param token The token received in the notification.
*/
connection.onNotification('snykTokenModified', token => {
globalConfig.setExhortSnykToken(token);
});

connection.listen();

export { connection };
20 changes: 10 additions & 10 deletions test/codeActionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Code Action Handler tests', () => {
it('should return an empty array if no RHDA diagnostics are present and full stack analysis action is provided', () => {
const diagnostics: Diagnostic[] = [];
let globalConfig = {
triggerFullStackAnalysis: 'mockTriggerFullStackAnalysis'
stackAnalysisCommand: 'mockStackAnalysisCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -123,7 +123,7 @@ describe('Code Action Handler tests', () => {
it('should return an empty array if no RHDA diagnostics are present and full stack analysis action is not provided', () => {
const diagnostics: Diagnostic[] = [];
let globalConfig = {
triggerFullStackAnalysis: ''
stackAnalysisCommand: ''
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -137,7 +137,7 @@ describe('Code Action Handler tests', () => {
codeActionHandler.registerCodeAction(mockUri, mockLoc, mockCodeAction);

let globalConfig = {
triggerFullStackAnalysis: 'mockTriggerFullStackAnalysis'
stackAnalysisCommand: 'mockStackAnalysisCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -151,7 +151,7 @@ describe('Code Action Handler tests', () => {
codeActionHandler.registerCodeAction(mockUri, mockLoc, mockCodeAction);

let globalConfig = {
triggerFullStackAnalysis: 'mockTriggerFullStackAnalysis'
stackAnalysisCommand: 'mockStackAnalysisCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -166,7 +166,7 @@ describe('Code Action Handler tests', () => {
codeActionHandler.registerCodeAction(mockUri, loc, mockCodeAction);

let globalConfig = {
triggerFullStackAnalysis: ''
stackAnalysisCommand: ''
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -181,7 +181,7 @@ describe('Code Action Handler tests', () => {
codeActionHandler.registerCodeAction(mockUri, loc, mockCodeAction);

let globalConfig = {
triggerFullStackAnalysis: 'mockTriggerFullStackAnalysis'
stackAnalysisCommand: 'mockStackAnalysisCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -196,7 +196,7 @@ describe('Code Action Handler tests', () => {
codeActionHandler.registerCodeAction(mockUri, loc, mockCodeAction);

let globalConfig = {
triggerFullStackAnalysis: 'mockTriggerFullStackAnalysis'
stackAnalysisCommand: 'mockStackAnalysisCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

Expand All @@ -210,7 +210,7 @@ describe('Code Action Handler tests', () => {
kind: CodeActionKind.QuickFix,
command: {
title: 'Analytics Report',
command: 'mockTriggerFullStackAnalysis'
command: 'mockStackAnalysisCommand'
}
}
]
Expand Down Expand Up @@ -268,15 +268,15 @@ describe('Code Action Handler tests', () => {
it('should return a switch to recommended version code action with RedHat repository recommendation', async () => {

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

const codeAction: CodeAction = codeActionHandler.generateSwitchToRecommendedVersionAction( 'mockTitle', 'mockVersionReplacementString', mockDiagnostic1[0], 'mock/path/pom.xml');
expect(codeAction).to.deep.equal(
{
"command": {
"command": 'mockTriggerRHRepositoryRecommendationNotification',
"command": 'mockRHRepositoryRecommendationNotificationCommand',
"title": "RedHat repository recommendation"
},
"diagnostics": [
Expand Down
16 changes: 12 additions & 4 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('Config tests', () => {
};

it('should initialize with default values when environment variables are not set', () => {
expect(mockConfig.triggerFullStackAnalysis).to.eq('');
expect(mockConfig.triggerRHRepositoryRecommendationNotification).to.eq('');
expect(mockConfig.stackAnalysisCommand).to.eq('');
expect(mockConfig.rhRepositoryRecommendationNotificationCommand).to.eq('');
expect(mockConfig.telemetryId).to.eq('');
expect(mockConfig.utmSource).to.eq('');
expect(mockConfig.exhortSnykToken).to.eq('');
Expand All @@ -87,7 +87,6 @@ describe('Config tests', () => {

mockConfig.updateConfig(rhdaData);

expect(mockConfig.exhortSnykToken).to.eq('mockToken');
expect(mockConfig.matchManifestVersions).to.eq('false');
expect(mockConfig.exhortMvnPath).to.eq('mockPath');
expect(mockConfig.exhortNpmPath).to.eq('mockPath');
Expand All @@ -102,7 +101,6 @@ describe('Config tests', () => {

mockConfig.updateConfig(partialRhdaData);

expect(mockConfig.exhortSnykToken).to.eq('mockToken');
expect(mockConfig.matchManifestVersions).to.eq('true');
expect(mockConfig.exhortMvnPath).to.eq('mvn');
expect(mockConfig.exhortNpmPath).to.eq('npm');
Expand All @@ -112,4 +110,14 @@ describe('Config tests', () => {
expect(mockConfig.exhortPythonPath).to.eq('python');
expect(mockConfig.exhortPipPath).to.eq('pip');
});

it('should set Exhort Snyk Token', () => {
const mockToken = 'mockToken';

expect(mockConfig.exhortSnykToken).to.not.eq(mockToken);

mockConfig.setExhortSnykToken(mockToken);

expect(mockConfig.exhortSnykToken).to.eq(mockToken);
});
});

0 comments on commit d013683

Please sign in to comment.