[Windows] Add some helpers to work with checksums (#8262)

This commit is contained in:
Erik Bershel
2023-09-18 12:00:27 +02:00
committed by GitHub
parent 824743a429
commit 68e600ace0
2 changed files with 59 additions and 0 deletions

View File

@@ -630,3 +630,60 @@ function Get-GitHubPackageDownloadUrl {
return $downloadUrl
}
function Use-ChecksumСomparison {
param (
[Parameter(Mandatory=$true)]
[string]$LocalFileHash,
[Parameter(Mandatory=$true)]
[string]$DistributorFileHash
)
Write-Verbose "Performing checksum verification"
if ($LocalFileHash -ne $DistributorFileHash) {
throw "Checksum verification failed. Expected hash: $DistributorFileHash; Actual hash: $LocalFileHash."
} else {
Write-Verbose "Checksum verification passed"
}
}
function Get-HashFromGitHubReleaseBody {
param (
[string]$RepoOwner,
[string]$RepoName,
[Parameter(Mandatory=$true)]
[string]$FileName,
[string]$Url,
[string]$Version = "latest",
[boolean]$IsPrerelease = $false,
[int]$SearchInCount = 100,
[string]$Delimiter = '|',
[int]$WordNumber = 1
)
if ($Url) {
$releaseUrl = $Url
} else {
if ($Version -eq "latest") {
$releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/latest"
} else {
$json = Invoke-RestMethod -Uri "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases?per_page=${SearchInCount}"
$tags = $json.Where{ $_.prerelease -eq $IsPrerelease }.tag_name
$tag = $tags -match $Version
if (-not $tag) {
throw "Failed to get a tag name for version $Version."
}
$releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/tag/$tag"
}
}
$body = (Invoke-RestMethod -Uri $releaseUrl).body -replace('`', "") -join "`n"
$matchingLine = $body.Split("`n") | Where-Object { $_ -like "*$FileName*" }
if ([string]::IsNullOrEmpty($matchingLine)) {
throw "File name '$FileName' not found in release body."
}
$result = $matchingLine.Split($Delimiter)[$WordNumber] -replace "[^a-zA-Z0-9]", ""
if ([string]::IsNullOrEmpty($result)) {
throw "Empty result. Check Split method parameters (delimiter and/or word number) for the matching line."
}
return $result
}