Skip to content

Commit

Permalink
Merge pull request #834 from snyk/fix/and-or-github-app-plugin
Browse files Browse the repository at this point in the history
fix: allow one or more matching connection types for plugin enablement
  • Loading branch information
soniqua authored Sep 5, 2024
2 parents d9668ed + 55e924a commit 491aa7d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ repos:
hooks:
- id: gitleaks
stages: [ commit ]
- repo: local
hooks:
- id: prettier-lint
language: system
stages: [pre-commit]
pass_filenames: true
name: prettier lint
entry: prettier --check
types_or:
- "javascript"
- "ts"
files: ^(lib|test|cli)/
- id: eslint
language: system
stages: [pre-commit]
pass_filenames: true
name: eslint
entry: npx eslint --color --cache
types_or:
- "javascript"
- "ts"
files: ^(lib|test|cli)/
12 changes: 3 additions & 9 deletions lib/client/brokerClientPlugins/abstractBrokerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ export default abstract class BrokerPlugin {
}

getApplicableTypes(): Array<string> {
const applicableTypes: Array<string> = [];
if (
this.applicableBrokerTypes.every((type) =>
this.brokerClientConfiguration.supportedBrokerTypes.includes(type),
)
) {
applicableTypes.push(...this.applicableBrokerTypes);
}
return applicableTypes;
return this.applicableBrokerTypes.filter((type) =>
this.brokerClientConfiguration.supportedBrokerTypes.includes(type),
);
}
isDisabled(config): boolean {
let isDisabled = false;
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/plugins/dummy-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BrokerPlugin from '../../../lib/client/brokerClientPlugins/abstractBrokerPlugin';

export class Plugin extends BrokerPlugin {
pluginCode = 'DUMMYMULTI';
pluginName = 'Dummy Plugin Multi';
description = `
dummy plugin multi 1 2
`;
version = '0.1';
applicableBrokerTypes = ['dummy-multi-1', 'dummy-multi-2'];

isPluginActive() {
if (this.brokerClientConfiguration.DISABLE_DUMMY_PLUGIN) {
this.logger.debug({ plugin: this.pluginName }, 'Disabling plugin');
return false;
}
return true;
}
async startUp(connectionConfig) {
this.logger.info({ plugin: this.pluginName }, 'Running Startup');
this.logger.info(
{ config: connectionConfig },
'Connection Config passed to the plugin',
);
connectionConfig['NEW_VAR_ADDED_TO_CONNECTION'] = 'access-token';
}

async preRequest(connectionConfiguration, postFilterPreparedRequest) {
this.logger.info({ plugin: this.pluginName }, 'Running prerequest plugin');
const customizedRequest = postFilterPreparedRequest;
customizedRequest.body['NEW_VAR_ADDED_TO_CONNECTION'] =
connectionConfiguration['NEW_VAR_ADDED_TO_CONNECTION'];
return customizedRequest;
}
}
15 changes: 15 additions & 0 deletions test/unit/plugins/pluginManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ describe('Plugin Manager', () => {
expect(plugins.get('dummy')[0].pluginName).toEqual('Dummy Plugin');
});

it('should load plugins if at least one supported broker type is present', async () => {
const clientOpts = {
config: {
universalBrokerEnabled: true,
supportedBrokerTypes: ['dummy-multi-1'],
connections: { 'my connection': { type: 'dummy-multi-1' } },
},
};
const plugins = await loadPlugins(pluginsFolderPath, clientOpts);
expect(plugins.get('dummy-multi-1').length).toBeGreaterThanOrEqual(1);
expect(plugins.get('dummy-multi-1')[0].pluginName).toEqual(
'Dummy Plugin Multi',
);
});

it('should load plugins no plugin successfully', async () => {
const clientOpts = {
config: {
Expand Down

0 comments on commit 491aa7d

Please sign in to comment.