Skip to content

Commit

Permalink
Merge pull request #354 from DXHeroes/feature/correct-component-paths
Browse files Browse the repository at this point in the history
Feature/correct component paths
  • Loading branch information
prokopsimek authored Jul 2, 2020
2 parents 6b1dbaa + c230730 commit 0332091
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 80 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"@typescript-eslint/no-use-before-define": [
"off"
],
"@typescript-eslint/explicit-module-boundary-types": [
"off"
],
"@typescript-eslint/no-angle-bracket-type-assertion": [
"off"
],
Expand Down
7 changes: 2 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ module.exports = {
coverageDirectory: 'coverage',

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: [
"/node_modules/",
"__MOCKS__",
],
coveragePathIgnorePatterns: ['/node_modules/', '__MOCKS__'],

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
Expand Down Expand Up @@ -171,7 +168,7 @@ module.exports = {
// verbose: null,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
watchPathIgnorePatterns: ['__MOCKS__'],
watchPathIgnorePatterns: ['node_modules', '__MOCKS__'],

// Whether to use watchman for file crawling
// watchman: true,
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/scanner/scannerContextBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BitbucketService } from '../../services/bitbucket/BitbucketService';
import { GitLabService } from '../../services/gitlab/GitLabService';
import { PythonLanguageDetector } from '../../detectors/Python/PythonLanguageDetector';
import { ArgumentsProvider } from '../../scanner';
import { IReporter, FixReporter, JSONReporter, CLIReporter, CIReporter, HTMLReporter, EnterpriseReporter } from '../../reporters';
import { IReporter, FixReporter, JSONReporter, CLIReporter, CIReporter, HTMLReporter, DashboardReporter } from '../../reporters';
import { ServiceType, AccessType } from '../../detectors/IScanningStrategy';

export const bindScanningContext = (container: Container) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ const bindReporters = (container: Container, args: ArgumentsProvider, accessType
}

if (accessType !== AccessType.private || (accessType === AccessType.private && args.apiToken)) {
container.bind<IReporter>(Types.IReporter).to(EnterpriseReporter);
container.bind<IReporter>(Types.IReporter).to(DashboardReporter);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/detectors/ScanningStrategyDetector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('ScanningStrategyDetector', () => {
getRemotes: () => [{ name: 'origin', refs: { fetch: repoPath, push: repoPath } }],
};
});
new GitHubNock('1', 'DXHeroes', 1, 'dx-scanner').getRepo('').reply(200);

const scanningStrategyDetector = await createScanningStrategyDetector({ uri: '/local/rootDir/subDir' });
const result = await scanningStrategyDetector.detect();
Expand Down
11 changes: 7 additions & 4 deletions src/detectors/ScanningStrategyDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ScanningStrategyDetectorUtils } from './utils/ScanningStrategyDetectorU
import { ErrorFactory } from '../lib/errors';
import { AccessType, ServiceType } from './IScanningStrategy';
import git from 'simple-git/promise';
import nodePath from 'path';

@injectable()
export class ScanningStrategyDetector implements IDetector<string, ScanningStrategy> {
Expand Down Expand Up @@ -38,9 +39,10 @@ export class ScanningStrategyDetector implements IDetector<string, ScanningStrat
}

async detect(): Promise<ScanningStrategy> {
let accessType: AccessType | undefined = undefined;
let remoteUrl: RemoteUrl = undefined;
let accessType: AccessType | undefined;
let remoteUrl: RemoteUrl;
let rootPath: string | undefined;
let localPath: string | undefined;

const path = ScanningStrategyDetectorUtils.normalizePath(this.argumentsProvider.uri);

Expand All @@ -60,6 +62,7 @@ export class ScanningStrategyDetector implements IDetector<string, ScanningStrat

if (ScanningStrategyDetectorUtils.isLocalPath(path)) {
rootPath = path;
localPath = path;

if (await git(path).checkIsRepo()) {
rootPath = await git(path).revparse(['--show-toplevel']);
Expand All @@ -70,8 +73,8 @@ export class ScanningStrategyDetector implements IDetector<string, ScanningStrat
serviceType,
accessType,
remoteUrl: this.repositoryConfig.remoteUrl,
localPath: ScanningStrategyDetectorUtils.isLocalPath(path) ? path : undefined,
rootPath,
localPath: localPath && nodePath.resolve(localPath),
rootPath: rootPath && nodePath.resolve(rootPath),
isOnline: this.isOnline,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/inspectors/FileInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class FileInspector implements IFileInspector {
path = `${this.basePath}/${path}`;
}

return nodePath.normalize(path);
return nodePath.normalize(nodePath.resolve(path));
}

async scanFor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GitignoreIsPresentPractice } from './GitignoreIsPresentPractice';
import { PracticeEvaluationResult, ProgrammingLanguage } from '../../model';
import { TestContainerContext, createTestContainer } from '../../inversify.config';
import nock from 'nock';

describe('GitignoreIsPresentPractice', () => {
let practice: GitignoreIsPresentPractice;
Expand Down Expand Up @@ -56,7 +57,10 @@ describe('GitignoreIsPresentPractice', () => {
'package.json': '{}',
});
containerCtx.fixerContext.projectComponent.language = ProgrammingLanguage.Java;

nock('https://api.github.com')
.get('/repos/github/gitignore/contents')
.reply(200, [{ name: 'Java.gitignore' }]);
nock('https://raw.githubusercontent.com').get('/github/gitignore/master/Java.gitignore').reply(200, '*.log');
await practice.fix(containerCtx.fixerContext);

const exists = await containerCtx.virtualFileSystemService.exists('.gitignore');
Expand Down
4 changes: 2 additions & 2 deletions src/reporters/CLIReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class CLIReporter implements IReporter {
buildReport(practicesAndComponents: PracticeWithContextForReporter[]): string {
const lines: string[] = [];

const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents);
const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents, this.scanningStrategy);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents, this.scanningStrategy);

lines.push(bold(blue('----------------------------')));
lines.push(bold(blue('| |')));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { PracticeEvaluationResult } from '../model';
import { argumentsProviderFactory } from '../test/factories/ArgumentsProviderFactory';
import { practiceWithContextFactory } from '../test/factories/PracticeWithContextFactory';
import { EnterpriseReporter } from './EnterpriseReporter';
import { DashboardReporter } from './DashboardReporter';
import { AccessType, ServiceType } from '../detectors/IScanningStrategy';

describe('EnterpriseReporter', () => {
describe('DashboardReporter', () => {
const practicingHighImpactPracticeWithCtx = practiceWithContextFactory();
const notPracticingHighImpactPracticeWithCtx = practiceWithContextFactory({ evaluation: PracticeEvaluationResult.notPracticing });
const scanningStrategy = {
accessType: AccessType.public,
localPath: '.',
rootPath: undefined,
remoteUrl: 'https://github.com/DXHeroes/dx-scanner',
isOnline: true,
serviceType: ServiceType.github,
};

describe('#report', () => {
it('one practicing practice', async () => {
const result = new EnterpriseReporter(argumentsProviderFactory()).buildReport([practicingHighImpactPracticeWithCtx]);
const result = new DashboardReporter(argumentsProviderFactory(), scanningStrategy).buildReport([practicingHighImpactPracticeWithCtx]);

await expect(result.componentsWithDxScore).toContainObject({
dxScore: { points: { total: 100, max: 100, percentage: 100 }, value: '100% | 1/1' },
Expand All @@ -19,7 +28,7 @@ describe('EnterpriseReporter', () => {
});

it('one practicing practice and one not practicing in two components', async () => {
const result = new EnterpriseReporter(argumentsProviderFactory()).buildReport([
const result = new DashboardReporter(argumentsProviderFactory(), scanningStrategy).buildReport([
practicingHighImpactPracticeWithCtx,
notPracticingHighImpactPracticeWithCtx,
]);
Expand Down Expand Up @@ -47,7 +56,9 @@ describe('EnterpriseReporter', () => {
});

it('one not practicing practice', async () => {
const result = new EnterpriseReporter(argumentsProviderFactory()).buildReport([notPracticingHighImpactPracticeWithCtx]);
const result = new DashboardReporter(argumentsProviderFactory(), scanningStrategy).buildReport([
notPracticingHighImpactPracticeWithCtx,
]);

await expect(result.componentsWithDxScore).toContainObject({
dxScore: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import { PracticeWithContextForReporter, IReporter } from './IReporter';
import { ProjectComponent } from '../model';
import axios from 'axios';
import * as uuid from 'uuid';
import { ScanningStrategy } from '../detectors';
import { inspect } from 'util';
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const pjson = require('../../package.json');

@injectable()
export class EnterpriseReporter implements IReporter {
export class DashboardReporter implements IReporter {
private readonly argumentsProvider: ArgumentsProvider;
private readonly scanningStrategy: ScanningStrategy;

constructor(@inject(Types.ArgumentsProvider) argumentsProvider: ArgumentsProvider) {
constructor(
@inject(Types.ArgumentsProvider) argumentsProvider: ArgumentsProvider,
@inject(Types.ScanningStrategy) scanningStrategy: ScanningStrategy,
) {
this.argumentsProvider = argumentsProvider;
this.scanningStrategy = scanningStrategy;
}

async report(practicesAndComponents: PracticeWithContextForReporter[]): Promise<void> {
const reportData = this.buildReport(practicesAndComponents);

try {
// send data
await axios.post('https://provider.dxscanner.io/api/v1/data-report', reportData, {
Expand All @@ -33,9 +39,9 @@ export class EnterpriseReporter implements IReporter {
}

buildReport(practicesAndComponents: PracticeWithContextForReporter[]): DataReportDto {
const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents);
const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents, this.scanningStrategy);

const dxScore = ReporterUtils.computeDXScore(practicesAndComponents);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents, this.scanningStrategy);

const report: DataReportDto = {
componentsWithDxScore: [],
Expand Down
8 changes: 4 additions & 4 deletions src/reporters/FixReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export class FixReporter implements IReporter {
): string {
const lines: string[] = [];

const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents);
const componentsWithPracticesAfterFix = ReporterUtils.getComponentsWithPractices(practicesAndComponentsAfterFix);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents);
const dxScoreAfterFix = ReporterUtils.computeDXScore(practicesAndComponentsAfterFix);
const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents, this.scanningStrategy);
const componentsWithPracticesAfterFix = ReporterUtils.getComponentsWithPractices(practicesAndComponentsAfterFix, this.scanningStrategy);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents, this.scanningStrategy);
const dxScoreAfterFix = ReporterUtils.computeDXScore(practicesAndComponentsAfterFix, this.scanningStrategy);

lines.push(bold(blue('----------------------------')));
lines.push(bold(blue('| |')));
Expand Down
7 changes: 4 additions & 3 deletions src/reporters/HTMLReporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { argumentsProviderFactory } from '../test/factories/ArgumentsProviderFac
import { FileSystemService } from '../services';
import path from 'path';
import { DirectoryJSON } from 'memfs/lib/volume';
import { scanningStrategy } from '../scanner/__MOCKS__/ScanningStrategy.mock';

describe('HTMLReporter', () => {
const practicingHighImpactPracticeWithCtx = practiceWithContextFactory();
Expand All @@ -24,7 +25,7 @@ describe('HTMLReporter', () => {

describe('#report', () => {
it('one practicing practice', async () => {
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService).report([
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService, scanningStrategy).report([
practicingHighImpactPracticeWithCtx,
]);

Expand All @@ -34,7 +35,7 @@ describe('HTMLReporter', () => {
});

it('one practicing practice and one not practicing', async () => {
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService).report([
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService, scanningStrategy).report([
practicingHighImpactPracticeWithCtx,
notPracticingHighImpactPracticeWithCtx,
]);
Expand All @@ -45,7 +46,7 @@ describe('HTMLReporter', () => {
});

it('all impacted practices', async () => {
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService).report([
await new HTMLReporter(argumentsProviderFactory({ html: true }), virtualFileSystemService, scanningStrategy).report([
practicingHighImpactPracticeWithCtx,
notPracticingHighImpactPracticeWithCtx,
practiceWithContextFactory({
Expand Down
8 changes: 6 additions & 2 deletions src/reporters/HTMLReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import { ArgumentsProvider } from '../scanner';
import { FileSystemService, GitServiceUtils } from '../services';
import { Types } from '../types';
import path from 'path';
import { ScanningStrategy } from '../detectors';

@injectable()
export class HTMLReporter implements IReporter {
private readonly argumentsProvider: ArgumentsProvider;
private readonly fileSystemService: FileSystemService;
private readonly scanningStrategy: ScanningStrategy;

constructor(
@inject(Types.ArgumentsProvider) argumentsProvider: ArgumentsProvider,
@inject(FileSystemService) fileSystemService: FileSystemService,
@inject(Types.ScanningStrategy) scanningStrategy: ScanningStrategy,
) {
this.argumentsProvider = argumentsProvider;
this.scanningStrategy = scanningStrategy;
this.fileSystemService = fileSystemService;
}

Expand All @@ -37,8 +41,8 @@ export class HTMLReporter implements IReporter {
buildReport(practicesAndComponents: PracticeWithContextForReporter[]): string {
const lines: string[] = [];

const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents);
const componentsWithPractices = ReporterUtils.getComponentsWithPractices(practicesAndComponents, this.scanningStrategy);
const dxScore = ReporterUtils.computeDXScore(practicesAndComponents, this.scanningStrategy);

lines.push('<h1 style="text-align: center">DX Scanner Result</h1>');

Expand Down
2 changes: 1 addition & 1 deletion src/reporters/IReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PracticeEvaluationResult,
} from '../model';
import { PracticeData } from '../practices/IPractice';
import { DataReportDto } from './EnterpriseReporter';
import { DataReportDto } from './DashboardReporter';

export interface IReporter {
report(
Expand Down
Loading

0 comments on commit 0332091

Please sign in to comment.