Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Updated Dependencies for Java & Kotlin Support (Issue #168) #177

Merged
merged 16 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/detectors/Java/JavaLanguageDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ export class JavaLanguageDetector implements ILanguageDetector {
const result: LanguageAtPath[] = [];
let packageFiles: Metadata[] = await this.fileInspector.scanFor(fileNameRegExp('pom.xml'), '/');
const isMaven: boolean = packageFiles.length > 0;
const ktFiles: Metadata[] = await this.fileInspector.scanFor(fileExtensionRegExp(['kt', 'kts']), '/');
const hasKtFiles = ktFiles.length > 0;
if (!isMaven) {
packageFiles = await this.fileInspector.scanFor(fileNameRegExp('build.gradle'), '/');
}
if (packageFiles.length > 0) {
for (const path of packageFiles.map((file) => nodePath.dirname(file.path))) {
result.push({ language: ProgrammingLanguage.Java, path });
result.push({ language: hasKtFiles ? ProgrammingLanguage.Kotlin : ProgrammingLanguage.Java, path });
}
} else {
const javaFiles: Metadata[] = await this.fileInspector.scanFor(fileExtensionRegExp(['java']), '/');
if (javaFiles.length === 0) {
if (javaFiles.length === 0 && ktFiles.length === 0) {
return result;
}
const dirsWithProjects = uniq(javaFiles.map((f) => nodePath.dirname(f.path)));
const javaOrKtFiles = javaFiles.concat(ktFiles);
const dirsWithProjects = uniq(javaOrKtFiles.map((f) => nodePath.dirname(f.path)));
const commonPath = sharedSubpath(dirsWithProjects);
result.push({ language: ProgrammingLanguage.Java, path: commonPath });
result.push({ language: hasKtFiles ? ProgrammingLanguage.Kotlin : ProgrammingLanguage.Java, path: commonPath });
}
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion src/inspectors/package/JavaPackageInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export class JavaPackageInspector extends PackageInspectorBase {
for (const xmlDependency of xmlDependencies) {
const dependencyAttributes = xmlDependency.dependency.values();
for (const attribute of dependencyAttributes) {
this.parsedDependencies.push({ packageName: String(attribute.artifactId.pop()), version: String(attribute.version.pop()) });
const packageName = `${attribute.groupId.pop()}:${attribute.artifactId.pop()}`;
this.parsedDependencies.push({ packageName, version: String(attribute.version.pop()) });
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/inspectors/package/JavaPackageInspectorMaven.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ describe('JavaPackageInspector Maven', () => {

it('Parses the packages correctly', async () => {
await inspector.init();
const pkg = inspector.findPackage('spring-boot-starter-actuator');
const pkg = inspector.findPackage('org.springframework.boot:spring-boot-starter-actuator');

if (!pkg) {
fail();
} else {
expect(pkg.name).toEqual('spring-boot-starter-actuator');
expect(pkg.name).toEqual('org.springframework.boot:spring-boot-starter-actuator');
expect(pkg.requestedVersion.value).toEqual('2.1.8');
expect(pkg.requestedVersion.major).toEqual('2');
expect(pkg.requestedVersion.minor).toEqual('1');
Expand All @@ -50,11 +50,11 @@ describe('JavaPackageInspector Maven', () => {
});

it('Returns true package if it exists', () => {
expect(inspector.hasPackage('mysql-connector-java')).toBe(true);
expect(inspector.hasPackage('mysql:mysql-connector-java')).toBe(true);
});

it('Returns undefined if the package does not exist', async () => {
const pkg = inspector.findPackage('spring-boot-autoconfigure');
const pkg = inspector.findPackage('org.springframework.boot:spring-boot-autoconfigure');
expect(pkg).toBeUndefined();
});
});
Expand Down
46 changes: 46 additions & 0 deletions src/practices/Java/JavaDependenciesVersionMajorLevel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PracticeEvaluationResult } from '../../model';
import { JavaDependenciesVersionMajorLevel } from './JavaDependenciesVersionMajorLevel';
import { createTestContainer, TestContainerContext } from '../../inversify.config';
import { mockPackage } from '../../test/helpers/mockPackage';
import { JavaPackageInspector } from '../../inspectors';
import * as axios from 'axios';
jest.mock('axios');

describe('JavaDependenciesVersionPractice of Major Level', () => {
let practice: JavaDependenciesVersionMajorLevel;
let containerCtx: TestContainerContext;
const MockedJavaPackageInspector = <jest.Mock<JavaPackageInspector>>(<unknown>JavaPackageInspector);
let mockJavaPackageInspector: JavaPackageInspector;
const mockedAxios = <jest.Mock>axios.default.get;

beforeAll(async () => {
containerCtx = createTestContainer();
containerCtx.container.bind('JavaDependenciesVersionMajorLevel').to(JavaDependenciesVersionMajorLevel);
practice = containerCtx.container.get('JavaDependenciesVersionMajorLevel');
mockJavaPackageInspector = new MockedJavaPackageInspector();
});

afterEach(async () => {
containerCtx.virtualFileSystemService.clearFileSystem();
containerCtx.practiceContext.fileInspector!.purgeCache();
});

it('not practicing if newer dependency versions of major level exists', async () => {
mockedAxios.mockResolvedValueOnce({ data: { response: { docs: [{ latestVersion: '2.2.1.RELEASE' }] } } });
mockJavaPackageInspector.packages = [mockPackage('org.springframework.boot:spring-boot-starter-actuator')];
containerCtx.practiceContext.packageInspector!.packages = mockJavaPackageInspector.packages;

const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.notPracticing);
});

it('practicing if newest dependency version of major level does not exist', async () => {
mockedAxios.mockResolvedValueOnce({ data: { response: { docs: [{ latestVersion: '' }] } } });

mockJavaPackageInspector.packages = [mockPackage('org.springframework.boot:spring-boot-starter-actuator')];
containerCtx.practiceContext.packageInspector!.packages = mockJavaPackageInspector.packages;

const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});
});
59 changes: 59 additions & 0 deletions src/practices/Java/JavaDependenciesVersionMajorLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PracticeContext } from '../../contexts/practice/PracticeContext';
import { Package } from '../../inspectors/IPackageInspector';
import { SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import * as axios from 'axios';
import { DependenciesVersionEvaluationUtils, PkgToUpdate } from '../utils/DependenciesVersionEvaluationUtils';
import { PracticeBase } from '../PracticeBase';
import { ReportDetailType } from '../../reporters/ReporterData';
import qs from 'qs';

@DxPractice({
id: 'Java.DependenciesVersionMajorLevel',
name: 'Update Dependencies of Major Level',
impact: PracticeImpact.small,
suggestion: 'Keep the dependencies updated to have all possible features. Use, for example, versions-maven-plugin',
reportOnlyOnce: true,
url: 'https://www.mojohaus.org/versions-maven-plugin/',
})
export class JavaDependenciesVersionMajorLevel extends PracticeBase {
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return ctx.projectComponent.language === ProgrammingLanguage.Java || ctx.projectComponent.language === ProgrammingLanguage.Kotlin;
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
if (!ctx.fileInspector || !ctx.packageInspector || !ctx.packageInspector.packages) {
return PracticeEvaluationResult.unknown;
}

const pkgs = ctx.packageInspector.packages;

const result = await JavaDependenciesVersionMajorLevel.searchMavenCentral(pkgs, 5);
const pkgsToUpdate = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.major, pkgs);
this.setData(pkgsToUpdate);

if (pkgsToUpdate.length > 0) return PracticeEvaluationResult.notPracticing;
return PracticeEvaluationResult.practicing;
}

static async searchMavenCentral(pkgs: Package[] | undefined, rows: number) {
const latestVersionsJson: { [key: string]: string } = {};
const URL = 'http://search.maven.org/solrsearch/select?';
if (pkgs) {
for (const p of pkgs) {
const listOfIds = p.name.split(':', 2);
const queryRequest = qs.stringify({ q: `${listOfIds[0]}+AND+a:${listOfIds[1]}`, rows, wt: 'json' }, { encode: false });
const listVersionsEndpoint = `${URL}${queryRequest}`;
await axios.default.get(listVersionsEndpoint).then((response) => {
latestVersionsJson[p.name] = `${response.data.response.docs.pop().latestVersion}`;
});
}
}
return latestVersionsJson;
}

setData(pkgsToUpdate: PkgToUpdate[]): void {
this.data.details = [{ type: ReportDetailType.table, headers: ['Name', 'New', 'Current'], data: pkgsToUpdate }];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { mockPackage } from '../../test/helpers/mockPackage';
import { createTestContainer, TestContainerContext } from '../../inversify.config';
import { PracticeEvaluationResult } from '../../model';
import { JavaDependenciesVersionMinorPatchLevel } from './JavaDependenciesVersionMinorPatchLevel';
import { JavaPackageInspector } from '../../inspectors';
import * as axios from 'axios';
jest.mock('axios');

describe('JavaDependenciesVersionPractice of Minor and Patch Level', () => {
let practice: JavaDependenciesVersionMinorPatchLevel;
let containerCtx: TestContainerContext;
const MockedJavaPackageInspector = <jest.Mock<JavaPackageInspector>>(<unknown>JavaPackageInspector);
let mockJavaPackageInspector: JavaPackageInspector;
const mockedAxios = <jest.Mock>axios.default.get;
ryzzaki marked this conversation as resolved.
Show resolved Hide resolved

beforeAll(async () => {
containerCtx = createTestContainer();
containerCtx.container.bind('JavaDependenciesVersionMinorPatchLevel').to(JavaDependenciesVersionMinorPatchLevel);
practice = containerCtx.container.get('JavaDependenciesVersionMinorPatchLevel');
mockJavaPackageInspector = new MockedJavaPackageInspector();
});

afterEach(async () => {
containerCtx.virtualFileSystemService.clearFileSystem();
containerCtx.practiceContext.fileInspector!.purgeCache();
});

it('not practicing if newer dependency versions of minor or patch level exists', async () => {
mockedAxios.mockResolvedValueOnce({ data: { response: { docs: [{ latestVersion: '2.2.1.RELEASE' }] } } });

mockJavaPackageInspector.packages = [mockPackage('org.springframework.boot:spring-boot-starter-actuator')];
containerCtx.practiceContext.packageInspector!.packages = mockJavaPackageInspector.packages;

const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.notPracticing);
});

it('practicing if newest dependency version dependency of minor or patch level does not exist', async () => {
mockedAxios.mockResolvedValueOnce({ data: { response: { docs: [{ latestVersion: '' }] } } });

mockJavaPackageInspector.packages = [mockPackage('org.springframework.boot:spring-boot-starter-actuator')];
containerCtx.practiceContext.packageInspector!.packages = mockJavaPackageInspector.packages;

const evaluated = await practice.evaluate(containerCtx.practiceContext);
expect(evaluated).toEqual(PracticeEvaluationResult.practicing);
});
});
41 changes: 41 additions & 0 deletions src/practices/Java/JavaDependenciesVersionMinorPatchLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { PracticeContext } from '../../contexts/practice/PracticeContext';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { IPractice } from '../IPractice';
import { JavaDependenciesVersionMajorLevel } from './JavaDependenciesVersionMajorLevel';
import { DependenciesVersionEvaluationUtils } from '../utils/DependenciesVersionEvaluationUtils';
import { SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { flatten } from 'lodash';

@DxPractice({
id: 'Java.DependenciesVersionMinorPatchLevel',
name: 'Update Dependencies of Minor and Patch Level',
impact: PracticeImpact.high,
suggestion: 'Keep the dependencies updated to have all possible features. Use, for example, versions-maven-plugin',
reportOnlyOnce: true,
url: 'https://www.mojohaus.org/versions-maven-plugin/',
})
export class JavaDependenciesVersionMinorPatchLevel extends JavaDependenciesVersionMajorLevel implements IPractice {
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return ctx.projectComponent.language === ProgrammingLanguage.Java || ctx.projectComponent.language === ProgrammingLanguage.Kotlin;
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
if (!ctx.fileInspector || !ctx.packageInspector || !ctx.packageInspector.packages) {
return PracticeEvaluationResult.unknown;
}

const pkgs = ctx.packageInspector.packages;

const result = await JavaDependenciesVersionMajorLevel.searchMavenCentral(pkgs, 5);

const patchLevelPkgs = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.patch, pkgs);
const minorLevelPkgs = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.minor, pkgs);
this.setData(flatten([patchLevelPkgs, minorLevelPkgs]));

if (patchLevelPkgs.length > 0 || minorLevelPkgs.length > 0) {
return PracticeEvaluationResult.notPracticing;
}
return PracticeEvaluationResult.practicing;
}
}
27 changes: 3 additions & 24 deletions src/practices/JavaScript/DependenciesVersionMajorLevel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ncu from 'npm-check-updates';
import { PracticeContext } from '../../contexts/practice/PracticeContext';
import { Package } from '../../inspectors/IPackageInspector';
import { PackageInspectorBase, SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { PracticeBase } from '../PracticeBase';
import { ReportDetailType } from '../../reporters/ReporterData';
import { DependenciesVersionEvaluationUtils, PkgToUpdate } from '../utils/DependenciesVersionEvaluationUtils';

@DxPractice({
id: 'JavaScript.DependenciesVersionMajorLevel',
Expand All @@ -29,7 +30,7 @@ export class DependenciesVersionMajorLevelPractice extends PracticeBase {
const pkgs = ctx.packageInspector.packages;

const result = await this.runNcu(pkgs);
const pkgsToUpdate = this.packagesToBeUpdated(result, SemverLevel.major, pkgs);
const pkgsToUpdate = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.major, pkgs);
this.setData(pkgsToUpdate);

if (pkgsToUpdate.length > 0) return PracticeEvaluationResult.notPracticing;
Expand All @@ -51,29 +52,7 @@ export class DependenciesVersionMajorLevelPractice extends PracticeBase {
return pkgsToBeUpdated;
}

packagesToBeUpdated(pkgsWithNewVersion: { [key: string]: string }, semverLevel: SemverLevel, pkgs: Package[]) {
// packages with Major level to be updated
const pkgsToUpdate: PkgToUpdate[] = [];

for (const packageName in pkgsWithNewVersion) {
const parsedVersion = PackageInspectorBase.semverToPackageVersion(pkgsWithNewVersion[packageName]);
if (parsedVersion) {
for (const pkg of pkgs) {
if (pkg.name === packageName) {
if (parsedVersion[semverLevel] > pkg.lockfileVersion[semverLevel]) {
pkgsToUpdate.push({ name: pkg.name, newVersion: parsedVersion.value, currentVersion: pkg.lockfileVersion.value });
}
}
}
}
}

return pkgsToUpdate;
}

setData(pkgsToUpdate: PkgToUpdate[]): void {
this.data.details = [{ type: ReportDetailType.table, headers: ['Name', 'New', 'Current'], data: pkgsToUpdate }];
}
}

export type PkgToUpdate = { name: string; newVersion: string; currentVersion: string };
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '.
import { DxPractice } from '../DxPracticeDecorator';
import { IPractice } from '../IPractice';
import { DependenciesVersionMajorLevelPractice } from './DependenciesVersionMajorLevel';
import { DependenciesVersionEvaluationUtils } from '../utils/DependenciesVersionEvaluationUtils';
import { SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { flatten } from 'lodash';

Expand Down Expand Up @@ -30,8 +31,8 @@ export class DependenciesVersionMinorPatchLevelPractice extends DependenciesVers

const result = await this.runNcu(pkgs);

const patchLevelPkgs = this.packagesToBeUpdated(result, SemverLevel.patch, pkgs);
const minorLevelPkgs = this.packagesToBeUpdated(result, SemverLevel.minor, pkgs);
const patchLevelPkgs = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.patch, pkgs);
const minorLevelPkgs = DependenciesVersionEvaluationUtils.packagesToBeUpdated(result, SemverLevel.minor, pkgs);
this.setData(flatten([patchLevelPkgs, minorLevelPkgs]));

if (patchLevelPkgs.length > 0 || minorLevelPkgs.length > 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { TsGitignoreCorrectlySetPractice } from './TypeScript/TsGitignoreCorrect
import { DoesPullRequestsPractice } from './LanguageIndependent/DoesPullRequests';
import { DependenciesVersionMinorPatchLevelPractice } from './JavaScript/DependenciesVersionMinorPatchLevel';
import { CorrectCommitMessagesPractice } from './LanguageIndependent/CorrectCommitMessagesPractice';
import { JavaDependenciesVersionMajorLevel } from './Java/JavaDependenciesVersionMajorLevel';
import { JavaDependenciesVersionMinorPatchLevel } from './Java/JavaDependenciesVersionMinorPatchLevel';
import { TimeToSolvePullRequestsPractice } from './LanguageIndependent/TimeToSolvePullRequestsPractice';
import { ThinPullRequestsPractice } from './LanguageIndependent/ThinPullRequestsPractice';

Expand All @@ -47,6 +49,8 @@ export const practices = [
EditorConfigIsPresentPractice,
DependenciesVersionMajorLevelPractice,
DependenciesVersionMinorPatchLevelPractice,
JavaDependenciesVersionMajorLevel,
JavaDependenciesVersionMinorPatchLevel,
GitignoreIsPresentPractice,
JsGitignoreCorrectlySetPractice,
JavaGitignoreCorrectlySetPractice,
Expand Down
24 changes: 24 additions & 0 deletions src/practices/utils/DependenciesVersionEvaluationUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SemverLevel, Package, PackageInspectorBase } from '../../inspectors';

export class DependenciesVersionEvaluationUtils {
static packagesToBeUpdated(pkgsWithNewVersion: { [key: string]: string }, semverLevel: SemverLevel, pkgs: Package[]) {
const pkgsToUpdate: PkgToUpdate[] = [];

for (const packageName in pkgsWithNewVersion) {
const parsedVersion = PackageInspectorBase.semverToPackageVersion(pkgsWithNewVersion[packageName]);
if (parsedVersion) {
for (const pkg of pkgs) {
if (pkg.name === packageName) {
if (parsedVersion[semverLevel] > pkg.lockfileVersion[semverLevel]) {
pkgsToUpdate.push({ name: pkg.name, newVersion: parsedVersion.value, currentVersion: pkg.lockfileVersion.value });
}
}
}
}
}

return pkgsToUpdate;
}
}

export type PkgToUpdate = { name: string; newVersion: string; currentVersion: string };