Skip to content

Instantly share code, notes, and snippets.

@awakecoding
Created September 16, 2021 14:52
Show Gist options
  • Save awakecoding/927139cc500655c83f8af17c3b4baabc to your computer and use it in GitHub Desktop.
Save awakecoding/927139cc500655c83f8af17c3b4baabc to your computer and use it in GitHub Desktop.
Fetch PowerShell release information and convert to an ORAS artifact manifest
# Fetch latest PowerShell release information from GitHub and export ORAS manifest
$Headers = @{
Accept = "application/vnd.github.v3+json";
}
$RequestParams = @{
Method = 'GET';
Headers = $Headers;
UseBasicParsing = $true;
Uri = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest";
}
$Response = $(Invoke-WebRequest @RequestParams).Content | ConvertFrom-Json
# GitHub release tag
$TagName = $Response.tag_name
# extract PowerShell artifact hashes from textual release description
$Hashes = @{}
$HashMatches = $($Response.body | Select-String -AllMatches -Pattern '\- (.*)\n \- (.*)\n').Matches
foreach ($HashMatch in $HashMatches) {
$FileName = $($HashMatch.Groups[1].Value | Out-String).Trim()
$FileHash = $($HashMatch.Groups[2].Value | Out-String).Trim()
$Hashes[$FileName] = $FileHash
}
# create array of blob artifacts
$blobs = @()
$Response.assets | ForEach-Object {
$filename = $_.name
$mediaType = "application/octet-stream"
$digest = "sha256`:$($Hashes[$filename])"
$url = $_.browser_download_url
$blob = [PSCustomObject]@{
mediaType = $mediaType
digest = $digest
urls = @($url)
filename = $filename
}
$blobs += $blob
}
$Manifest = [PSCustomObject]@{
artifactType = "org.cncf.oras.manifest.annotations"
blobs = $blobs
}
# Convert to JSON and export manifest
$json = $Manifest | ConvertTo-Json -Depth 8
Write-Host "PowerShell $TagName"
Write-Host $json
Set-Content -Path "powershell-$TagName.json" -Value $json -Encoding utf8NoBOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment