2020-01-05 12:55:59 +01:00
|
|
|
import * as httpm from 'typed-rest-client/HttpClient'
|
|
|
|
|
2024-01-31 18:45:32 +01:00
|
|
|
import fileWrapperChecksums from './wrapper-checksums.json'
|
|
|
|
|
2021-05-14 17:15:39 +08:00
|
|
|
const httpc = new httpm.HttpClient(
|
|
|
|
'gradle/wrapper-validation-action',
|
|
|
|
undefined,
|
|
|
|
{allowRetries: true, maxRetries: 3}
|
|
|
|
)
|
2020-01-05 12:55:59 +01:00
|
|
|
|
2024-01-31 18:45:32 +01:00
|
|
|
function getKnownValidChecksums(): Map<string, Set<string>> {
|
|
|
|
const versionsMap = new Map<string, Set<string>>()
|
|
|
|
for (const entry of fileWrapperChecksums) {
|
|
|
|
const checksum = entry.checksum
|
|
|
|
|
|
|
|
let versionNames = versionsMap.get(checksum)
|
|
|
|
if (versionNames === undefined) {
|
|
|
|
versionNames = new Set()
|
|
|
|
versionsMap.set(checksum, versionNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
versionNames.add(entry.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionsMap
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Known checksums from previously published Wrapper versions.
|
|
|
|
*
|
|
|
|
* Maps from the checksum to the names of the Gradle versions whose wrapper has this checksum.
|
|
|
|
*/
|
|
|
|
export const KNOWN_VALID_CHECKSUMS = getKnownValidChecksums()
|
|
|
|
|
2020-01-06 11:37:12 +01:00
|
|
|
export async function fetchValidChecksums(
|
|
|
|
allowSnapshots: boolean
|
2024-01-31 18:45:32 +01:00
|
|
|
): Promise<Set<string>> {
|
2020-04-14 23:10:58 +02:00
|
|
|
const all = await httpGetJsonArray('https://services.gradle.org/versions/all')
|
2020-09-22 18:02:50 +02:00
|
|
|
const withChecksum = all.filter(
|
|
|
|
entry =>
|
|
|
|
typeof entry === 'object' &&
|
|
|
|
entry != null &&
|
|
|
|
entry.hasOwnProperty('wrapperChecksumUrl')
|
2020-01-06 11:37:12 +01:00
|
|
|
)
|
|
|
|
const allowed = withChecksum.filter(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(entry: any) => allowSnapshots || !entry.snapshot
|
|
|
|
)
|
2020-04-14 23:10:58 +02:00
|
|
|
const checksumUrls = allowed.map(
|
2020-01-06 11:37:12 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(entry: any) => entry.wrapperChecksumUrl as string
|
|
|
|
)
|
|
|
|
const checksums = await Promise.all(
|
|
|
|
checksumUrls.map(async (url: string) => httpGetText(url))
|
|
|
|
)
|
2024-01-31 18:45:32 +01:00
|
|
|
return new Set(checksums)
|
2020-01-05 12:55:59 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 18:02:50 +02:00
|
|
|
async function httpGetJsonArray(url: string): Promise<unknown[]> {
|
2020-01-06 11:37:12 +01:00
|
|
|
return JSON.parse(await httpGetText(url))
|
2020-01-05 12:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function httpGetText(url: string): Promise<string> {
|
2020-01-06 11:37:12 +01:00
|
|
|
const response = await httpc.get(url)
|
|
|
|
return await response.readBody()
|
2020-01-05 12:55:59 +01:00
|
|
|
}
|