Skip to content

Commit

Permalink
fix: dedup dependencies, ignore docker variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Jul 7, 2023
1 parent 7b3180b commit 7819bd9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 59 deletions.
55 changes: 0 additions & 55 deletions src/analyser/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ exports[`analyser > should register only component of the same tech 1`] = `
"pg",
"1.0.0",
],
[
"docker",
"postgres",
"15.1-alpine",
],
[
"npm",
"pg",
"1.0.0",
],
],
"edges": [],
"id": "6",
Expand Down Expand Up @@ -727,11 +717,6 @@ exports[`analyser > should run correctly 2`] = `
"react",
"4.17.0",
],
[
"npm",
"typescript",
"5.0.4",
],
[
"npm",
"vite",
Expand Down Expand Up @@ -767,46 +752,6 @@ exports[`analyser > should run correctly 2`] = `
"typescript",
"4.9.5",
],
[
"docker",
"postgres",
"14.5-alpine",
],
[
"docker",
"redis",
"7.0.4-alpine",
],
[
"docker",
"unknown",
"7.17.5",
],
[
"npm",
"@typescript-eslint/eslint-plugin",
"5.57.1",
],
[
"npm",
"@typescript-eslint/parser",
"5.57.1",
],
[
"npm",
"eslint",
"8.39.0",
],
[
"npm",
"prettier",
"2.8.7",
],
[
"npm",
"typescript",
"4.9.5",
],
],
"edges": [],
"id": "20",
Expand Down
8 changes: 5 additions & 3 deletions src/payload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { BaseProvider } from '../provider/base.js';
import { IGNORED_DIVE_PATHS } from '../provider/base.js';
import { rulesComponents, rulesTechs } from '../rules.js';
import { cleanPath } from '../tests/helpers.js';
import type { Analyser, AnalyserJson } from '../types/index.js';
import type { Analyser, AnalyserJson, Dependency } from '../types/index.js';
import type { AllowedKeys } from '../types/techs.js';

import '../rules/index.js';
Expand Down Expand Up @@ -240,8 +240,10 @@ export class Payload implements Analyser {
this.path = [...new Set([...this.path, ...pl.path])];

// Merge dependencies
// TODO: dedup
this.dependencies = [...this.dependencies, ...pl.dependencies];
const dedup = new Map<string, Dependency>();
this.dependencies.forEach((dep) => dedup.set(dep.join('_'), dep));
pl.dependencies.forEach((dep) => dedup.set(dep.join('_'), dep));
this.dependencies = Array.from(dedup.values());

for (const [lang, count] of Object.entries(pl.languages)) {
this.addLang(lang, count);
Expand Down
3 changes: 3 additions & 0 deletions src/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const IGNORED_DIVE_PATHS = [
'terraform.tfstate.d',
'migrations',
'tests',
'e2e',
'__fixtures__',
'__snapshots__',
'tmp',
Expand All @@ -47,6 +48,8 @@ export const IGNORED_DIVE_PATHS = [
'.npm',
'.nuxt',
'.react-email',
'.release',
'.semgrep',
'.serverless',
'.svn',
'.terraform',
Expand Down
3 changes: 3 additions & 0 deletions src/rules/spec/docker/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const detectDockerComponent: ComponentMatcher = async (
// It's better to have few false positive, than a lot of missing components
const matched = [...detect([service.image], 'docker')];
const [imageName, imageVersion] = service.image.split(':');
if (imageName.startsWith('$')) {
continue;
}

pl.addChild(
new Payload({
Expand Down
20 changes: 20 additions & 0 deletions src/rules/spec/docker/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ describe('docker', () => {
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual([]);
});

it('should not extract variables', async () => {
const res = await analyser({
provider: new FakeProvider({
paths: {
'/': ['docker-compose.yml'],
},
files: {
'/docker-compose.yml': `version: '3'
services:
boots:
image: $BOOTS_IMAGE`,
},
}),
});

expect(
Array.from(flatten(res, { merge: true }).dependencies).sort()
).toStrictEqual([]);
});
});
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface GraphEdge {
write: boolean;
}

export type Dependency = [SupportedDeps, string, string];

export interface Analyser {
/**
* Unique random id for this payload
Expand Down Expand Up @@ -67,7 +69,7 @@ export interface Analyser {
/**
* List all dependencies wether or not they matched a rule.
*/
dependencies: Array<[SupportedDeps, string, string]>;
dependencies: Dependency[];
}

export type AnalyserJson = Modify<
Expand Down

0 comments on commit 7819bd9

Please sign in to comment.