Skip to content

Commit

Permalink
fix: Don't create duplicated IaC projects when sharing results
Browse files Browse the repository at this point in the history
  • Loading branch information
francescomari committed May 4, 2022
1 parent 61b826d commit 041ed24
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
@@ -1,21 +1,14 @@
import {
IacFileScanResult,
IacShareResultsFormat,
IaCTestFlags,
} from '../types';
import { computePaths } from './utils';
import { IacFileScanResult, IacShareResultsFormat } from '../types';
import * as path from 'path';
import * as fs from 'fs';

export function formatShareResults(
scanResults: IacFileScanResult[],
options: IaCTestFlags,
): IacShareResultsFormat[] {
const resultsGroupedByFilePath = groupByFilePath(scanResults);

return resultsGroupedByFilePath.map((result) => {
const { projectName, targetFile } = computePaths(
result.filePath,
options.path,
);
const { projectName, targetFile } = computePaths(result.filePath);

return {
projectName,
Expand Down Expand Up @@ -45,3 +38,54 @@ function groupByFilePath(scanResults: IacFileScanResult[]) {

return Object.values(groupedByFilePath);
}

function computePaths(
filePath: string,
): { targetFilePath: string; projectName: string; targetFile: string } {
const currentDirectory = getGitRootOrCwd(path.resolve(filePath));
const currentDirectoryName = path.basename(currentDirectory);
const absoluteFilePath = path.resolve(filePath);
const relativeFilePath = path.relative(currentDirectory, absoluteFilePath);

return {
targetFilePath: absoluteFilePath,
projectName: currentDirectoryName,
targetFile: relativeFilePath,
};
}

function getGitRootOrCwd(currentPath: string): string {
const gitRoot = getGitRoot(currentPath);

if (gitRoot) {
return gitRoot;
}

return process.cwd();
}

function getGitRoot(currentPath: string): string | null {
if (isGitRoot(currentPath)) {
return currentPath;
}

const parentPath = path.dirname(currentPath);

if (parentPath === currentPath) {
return null;
}

return getGitRoot(parentPath);
}

function isGitRoot(currentPath: string): boolean {
return isDirectory(path.join(currentPath, '.git'));
}

function isDirectory(path): boolean {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}
Expand Up @@ -29,7 +29,7 @@ export async function formatAndShareResults({
throw new FeatureFlagError('report', 'iacCliShareResults');
}

const formattedResults = formatShareResults(results, options);
const formattedResults = formatShareResults(results);

return await shareResults({
results: formattedResults,
Expand Down
13 changes: 9 additions & 4 deletions test/jest/acceptance/iac/cli-share-results.spec.ts
@@ -1,5 +1,10 @@
import { FakeServer } from '../../../acceptance/fake-server';
import { startMockServer } from './helpers';
import * as path from 'path';

const projectDirectoryName = path.basename(
path.resolve(__dirname, '..', '..', '..', '..'),
);

jest.setTimeout(50000);

Expand Down Expand Up @@ -73,12 +78,12 @@ describe('CLI Share Results', () => {
{
identity: {
type: 'armconfig',
targetFile: './iac/arm/rule_test.json',
targetFile: 'test/fixtures/iac/arm/rule_test.json',
},
facts: [],
findings: expect.any(Array),
policy: '',
name: 'arm',
name: projectDirectoryName,
target: {
remoteUrl: 'http://github.com/snyk/cli.git',
},
Expand Down Expand Up @@ -198,11 +203,11 @@ describe('CLI Share Results', () => {
{
identity: {
type: 'armconfig',
targetFile: './iac/arm/rule_test.json',
targetFile: 'test/fixtures/iac/arm/rule_test.json',
},
facts: [],
findings: expect.arrayContaining([]),
name: 'arm',
name: projectDirectoryName,
target: {
remoteUrl: 'http://github.com/snyk/cli.git',
},
Expand Down
9 changes: 7 additions & 2 deletions test/jest/acceptance/iac/report.spec.ts
@@ -1,6 +1,11 @@
import { startMockServer } from './helpers';
import { FakeServer } from '../../../acceptance/fake-server';
import { EOL } from 'os';
import * as path from 'path';

const projectDirectoryName = path.basename(
path.resolve(__dirname, '..', '..', '..', '..'),
);

jest.setTimeout(50_000);

Expand Down Expand Up @@ -54,12 +59,12 @@ describe('iac report', () => {
{
identity: {
type: 'armconfig',
targetFile: './iac/arm/rule_test.json',
targetFile: 'test/fixtures/iac/arm/rule_test.json',
},
facts: [],
findings: expect.any(Array),
policy: '',
name: 'arm',
name: projectDirectoryName,
target: {
remoteUrl: 'http://github.com/snyk/cli.git',
},
Expand Down
Expand Up @@ -6,7 +6,6 @@ describe('formatShareResults', () => {
it('returns the formatted results', () => {
const IacShareResultsFormatResults = formatShareResults(
generateScanResults(),
{},
);
expect(IacShareResultsFormatResults).toStrictEqual(
expectedFormattedResultsForShareResults,
Expand Down

0 comments on commit 041ed24

Please sign in to comment.