2020-01-06 10:37:12 +00:00
|
|
|
import * as find from './find'
|
|
|
|
import * as checksums from './checksums'
|
|
|
|
import * as hash from './hash'
|
2020-01-05 11:55:59 +00:00
|
|
|
|
2020-01-06 10:37:12 +00:00
|
|
|
export async function findInvalidWrapperJars(
|
|
|
|
gitRepoRoot: string,
|
2020-01-10 16:54:22 +00:00
|
|
|
minWrapperCount: number,
|
2020-01-06 12:36:28 +00:00
|
|
|
allowSnapshots: boolean,
|
|
|
|
allowChecksums: string[]
|
2020-01-11 14:35:09 +00:00
|
|
|
): Promise<ValidationResult> {
|
2020-01-06 10:37:12 +00:00
|
|
|
const wrapperJars = await find.findWrapperJars(gitRepoRoot)
|
2020-01-11 14:35:09 +00:00
|
|
|
const result = new ValidationResult([], [])
|
2020-01-10 16:54:22 +00:00
|
|
|
if (wrapperJars.length < minWrapperCount) {
|
2020-01-11 14:35:09 +00:00
|
|
|
result.errors.push(
|
2020-01-10 16:59:23 +00:00
|
|
|
`Expected to find at least ${minWrapperCount} Gradle Wrapper JARs but got only ${wrapperJars.length}`
|
2020-01-10 16:54:22 +00:00
|
|
|
)
|
|
|
|
}
|
2020-01-06 10:37:12 +00:00
|
|
|
if (wrapperJars.length > 0) {
|
|
|
|
const validChecksums = await checksums.fetchValidChecksums(allowSnapshots)
|
2020-01-06 12:36:28 +00:00
|
|
|
validChecksums.push(...allowChecksums)
|
2020-01-06 10:37:12 +00:00
|
|
|
for (const wrapperJar of wrapperJars) {
|
|
|
|
const sha = await hash.sha256File(wrapperJar)
|
|
|
|
if (!validChecksums.includes(sha)) {
|
2020-01-11 14:35:09 +00:00
|
|
|
result.invalid.push(new WrapperJar(wrapperJar, sha))
|
|
|
|
} else {
|
|
|
|
result.valid.push(new WrapperJar(wrapperJar, sha))
|
2020-01-06 10:37:12 +00:00
|
|
|
}
|
2020-01-05 11:55:59 +00:00
|
|
|
}
|
2020-01-06 10:37:12 +00:00
|
|
|
}
|
2020-01-11 14:35:09 +00:00
|
|
|
return result
|
2020-01-05 11:55:59 +00:00
|
|
|
}
|
2020-01-06 12:50:19 +00:00
|
|
|
|
2020-01-11 14:35:09 +00:00
|
|
|
export class ValidationResult {
|
|
|
|
valid: WrapperJar[]
|
|
|
|
invalid: WrapperJar[]
|
|
|
|
errors: string[] = []
|
|
|
|
|
|
|
|
constructor(valid: WrapperJar[], invalid: WrapperJar[]) {
|
|
|
|
this.valid = valid
|
|
|
|
this.invalid = invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid(): boolean {
|
|
|
|
return this.invalid.length === 0 && this.errors.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
toDisplayString(): string {
|
|
|
|
let displayString = ''
|
|
|
|
if (this.invalid.length > 0) {
|
2020-01-13 09:29:20 +00:00
|
|
|
displayString += `✗ Found unknown Gradle Wrapper JAR files:\n${ValidationResult.toDisplayList(
|
2020-01-11 14:35:09 +00:00
|
|
|
this.invalid
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
if (this.errors.length > 0) {
|
|
|
|
if (displayString.length > 0) displayString += '\n'
|
2020-01-13 09:29:20 +00:00
|
|
|
displayString += `✗ Other validation errors:\n ${this.errors.join(
|
2020-01-11 14:35:09 +00:00
|
|
|
`\n `
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
if (this.valid.length > 0) {
|
|
|
|
if (displayString.length > 0) displayString += '\n'
|
2020-01-13 09:29:20 +00:00
|
|
|
displayString += `✓ Found known Gradle Wrapper JAR files:\n${ValidationResult.toDisplayList(
|
2020-01-11 14:35:09 +00:00
|
|
|
this.valid
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
return displayString
|
|
|
|
}
|
|
|
|
|
|
|
|
private static toDisplayList(wrapperJars: WrapperJar[]): string {
|
|
|
|
return ` ${wrapperJars.map(wj => wj.toDisplayString()).join(`\n `)}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class WrapperJar {
|
2020-01-06 12:50:19 +00:00
|
|
|
path: string
|
|
|
|
checksum: string
|
2020-01-11 14:35:09 +00:00
|
|
|
|
2020-01-06 12:50:19 +00:00
|
|
|
constructor(path: string, checksum: string) {
|
|
|
|
this.path = path
|
|
|
|
this.checksum = checksum
|
|
|
|
}
|
2020-01-11 14:35:09 +00:00
|
|
|
|
|
|
|
toDisplayString(): string {
|
|
|
|
return `${this.checksum} ${this.path}`
|
|
|
|
}
|
2020-01-06 12:50:19 +00:00
|
|
|
}
|