Skip to content

Commit

Permalink
feat(java): Added Major Version Dependency check practice for Java
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzzaki committed Dec 7, 2019
1 parent 3cb77c6 commit 258c44e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/practices/Java/JavaDependenciesVersionMajorLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { PracticeContext } from '../../contexts/practice/PracticeContext';
import { Package } from '../../inspectors/IPackageInspector';
import { PackageInspectorBase, SemverLevel } from '../../inspectors/package/PackageInspectorBase';
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model';
import { DxPractice } from '../DxPracticeDecorator';
import { IPractice } from '../IPractice';
import * as axios from 'axios';

@DxPractice({
id: 'Java.DependenciesVersionMajorLevel',
name: 'Update Dependencies of Major Level',
impact: PracticeImpact.medium,
suggestion: 'Keep the dependencies updated to have all possible features. Use, for example, Renovate Bot.',
reportOnlyOnce: true,
url: 'https://renovatebot.com/',
})
export class 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 === undefined || ctx.packageInspector === undefined) {
return PracticeEvaluationResult.unknown;
}

const pkgs = ctx.packageInspector.packages;

if (pkgs === undefined) {
return PracticeEvaluationResult.unknown;
}

const result = await JavaDependenciesVersionMajorLevel.searchMavenCentral(pkgs, 5);
const practiceEvaluationResult = JavaDependenciesVersionMajorLevel.isPracticing(result, SemverLevel.major, pkgs);

return practiceEvaluationResult || PracticeEvaluationResult.practicing;
}

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

static isPracticing(
result: { [key: string]: string },
semverVersion: SemverLevel,
pkgs: Package[],
): PracticeEvaluationResult | undefined {
for (const packageName in result) {
const parsedVersion = PackageInspectorBase.semverToPackageVersion(result[packageName]);
if (parsedVersion) {
for (const pkg of pkgs) {
if (pkg.name === packageName) {
if (parsedVersion[semverVersion] > pkg.lockfileVersion[semverVersion]) {
return PracticeEvaluationResult.notPracticing;
}
}
}
}
}
return undefined;
}
}
2 changes: 2 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { DependenciesVersionMajorLevel } from './JavaScript/DependenciesVersionM
import { ESLintWithoutErrorsPractice } from './JavaScript/ESLintWithoutErrorsPractice';
import { TsGitignoreCorrectlySetPractice } from './TypeScript/TsGitignoreCorrectlySetPractice';
import { DependenciesVersionMinorPatchLevel } from './JavaScript/DependenciesVersionMinorPatchLevel';
import { JavaDependenciesVersionMajorLevel } from './Java/JavaDependenciesVersionMajorLevel';

// register practices here
export const practices = [
Expand All @@ -42,6 +43,7 @@ export const practices = [
DockerizationUsedPractice,
EditorConfigIsPresentPractice,
DependenciesVersionMajorLevel,
JavaDependenciesVersionMajorLevel,
DependenciesVersionMinorPatchLevel,
GitignoreIsPresentPractice,
JsGitignoreCorrectlySetPractice,
Expand Down

0 comments on commit 258c44e

Please sign in to comment.