-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): Added Major Version Dependency check practice for Java
- Loading branch information
ryzzaki
committed
Dec 7, 2019
1 parent
3cb77c6
commit 258c44e
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters