From df0f25975a85ad9a39e17550849f53cf6b1d31ed Mon Sep 17 00:00:00 2001 From: James Ruskin Date: Thu, 4 Jul 2024 10:51:27 +0100 Subject: [PATCH 1/3] (maint) Adds Get-GitHubRepositoryFileContent script This can be used to improve performance of any update that requires you to grab the content of a file from a repository, e.g. a changelog. --- scripts/Get-GitHubRelease.ps1 | 2 +- scripts/Get-GitHubRepositoryFileContent.ps1 | 65 +++++++++++++++++++++ scripts/au_extensions.psm1 | 1 + 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 scripts/Get-GitHubRepositoryFileContent.ps1 diff --git a/scripts/Get-GitHubRelease.ps1 b/scripts/Get-GitHubRelease.ps1 index a71bd922dc4..1084cb82ee9 100644 --- a/scripts/Get-GitHubRelease.ps1 +++ b/scripts/Get-GitHubRelease.ps1 @@ -16,7 +16,7 @@ [Parameter(Mandatory, Position = 1)] [string]$Name, - # The Name of the tag to get the relase for. Will default to the latest release. + # The Name of the tag to get the release for. Will default to the latest release. [string]$TagName, # GitHub token, used to reduce rate-limiting or access private repositories (needs repo scope) diff --git a/scripts/Get-GitHubRepositoryFileContent.ps1 b/scripts/Get-GitHubRepositoryFileContent.ps1 new file mode 100644 index 00000000000..a70a762ec3e --- /dev/null +++ b/scripts/Get-GitHubRepositoryFileContent.ps1 @@ -0,0 +1,65 @@ +function Get-GitHubRepositoryFileContent { + <# + .Synopsis + Returns the content of a given file in a repository via the REST API + + .Link + https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28 + + .Example + Get-GitHubRepositoryFileContent Kubernetes Kubernetes CHANGELOG/README.md + + .Notes + Seems to be a lot faster than IWRing raw files. + #> + [CmdletBinding()] + param( + # The owner of the repository + [Parameter(Mandatory)] + [string]$Owner, + + # The repository containing the file + [Parameter(Mandatory)] + [string]$Repository, + + # The path to the file within the repository + [Parameter(ValueFromPipeline)] + [string]$Path, + + # The branch, tag, or reference to get the content from. Defaults to the repository's default branch. + [Alias('ref', 'Tag')] + [string]$Branch, + + # Returns the raw response + [switch]$Raw + ) + process { + $restArgs = @{ + Uri = "https://api.github.com/repos/$($Owner)/$($Repository)/contents/$($Path.TrimStart('/'))" + Headers = @{ + 'Accept' = 'application/vnd.github+json' + 'X-GitHub-Api-Version' = '2022-11-28' + } + } + if ($Branch) { + $restArgs.Body = @{ref = $Branch} + } + if ($env:github_api_key) { + $restArgs.Headers.Authorization = "Bearer $($env:github_api_key)" + } + + $Result = Invoke-RestMethod @restArgs -UseBasicParsing + + if ($Raw) { + $Result + } elseif ($Result.encoding -eq 'base64') { + # Assumption made about the file being UTF8, here + [System.Text.Encoding]::UTF8.GetString( + [System.Convert]::FromBase64String($Result.content) + ) + } else { + Write-Warning "$($Path) encoded as '$($Result.encoding)'" + $Result.content + } + } +} diff --git a/scripts/au_extensions.psm1 b/scripts/au_extensions.psm1 index aa8af62f54a..3053bbac914 100644 --- a/scripts/au_extensions.psm1 +++ b/scripts/au_extensions.psm1 @@ -9,6 +9,7 @@ $funcs = @( 'Clear-DependenciesList' 'Get-AllGitHubReleases' 'Get-GitHubRelease' + 'Get-GitHubRepositoryFileContent' 'Set-DescriptionFromReadme' 'Update-ChangelogVersion' 'Update-OnETagChanged' From 48f08b76bec3d30d38ca2b9dc42cf844de7a6b0f Mon Sep 17 00:00:00 2001 From: James Ruskin Date: Thu, 4 Jul 2024 11:14:34 +0100 Subject: [PATCH 2/3] (kubernetes-cli) Fixes Update.ps1 (GetLatest returns nothing) GetLatest was failing to return anything. On inspection, it seems that a latest -alpha release was causing get latest to return immediately instead of listing the rest of the available streams. Continuing rather than returning when a URL isn't matched fixes that. --- automatic/kubernetes-cli/update.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/automatic/kubernetes-cli/update.ps1 b/automatic/kubernetes-cli/update.ps1 index bc3fb7b3ef7..f9880de8246 100644 --- a/automatic/kubernetes-cli/update.ps1 +++ b/automatic/kubernetes-cli/update.ps1 @@ -42,7 +42,7 @@ function global:au_GetLatest { foreach ($minor_version in $minor_version_changelogs) { if ($streams.ContainsKey($minor_version)) { - return + continue } $minor_changelog_page = Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/kubernetes/kubernetes/master/CHANGELOG/CHANGELOG-$($minor_version).md" @@ -52,7 +52,7 @@ function global:au_GetLatest { | Select-Object -First 1 if (!$url64) { - return + continue } if ($url64 -match "/v(?\d+(\.\d+)+)/kubernetes-client-windows-amd64.tar.gz") { From 675ea2ba363e4495152b0f1ca61b3f2d00232c08 Mon Sep 17 00:00:00 2001 From: James Ruskin Date: Thu, 4 Jul 2024 10:48:57 +0100 Subject: [PATCH 3/3] (kubernetes-cli) Updates to use GitHub REST API for ChangeLog Retrieval This improves performance for updating this package from ~164 seconds to ~86. --- automatic/kubernetes-cli/update.ps1 | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/automatic/kubernetes-cli/update.ps1 b/automatic/kubernetes-cli/update.ps1 index f9880de8246..2a747490f1d 100644 --- a/automatic/kubernetes-cli/update.ps1 +++ b/automatic/kubernetes-cli/update.ps1 @@ -3,7 +3,10 @@ param($IncludeStream, [switch] $Force) Import-Module Chocolatey-AU -$changelogs = 'https://raw.githubusercontent.com/kubernetes/kubernetes/master/CHANGELOG/README.md' +$changelogRepository = @{ + Owner = 'kubernetes' + Repository = 'kubernetes' +} function global:au_BeforeUpdate { Get-RemoteFiles -Purge -NoSuffix } @@ -27,7 +30,7 @@ function global:au_SearchReplace { function global:au_GetLatest { # Only report the supported Kubernetes streams. - $changelogs = (Invoke-WebRequest -Uri $changelogs -UseBasicParsing).content + $changelogs = Get-GitHubRepositoryFileContent @changelogRepository CHANGELOG/README.md # There is quite a few versions that do not exist on chocolatey.org # and since the limit of pushed packages is 10, we need to limit the amount @@ -45,8 +48,8 @@ function global:au_GetLatest { continue } - $minor_changelog_page = Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/kubernetes/kubernetes/master/CHANGELOG/CHANGELOG-$($minor_version).md" - $url64 = $minor_changelog_page.content ` + $minor_changelog_page = Get-GitHubRepositoryFileContent @changelogRepository "CHANGELOG/CHANGELOG-$($minor_version).md" + $url64 = $minor_changelog_page ` | Select-String -Pattern "(?<=\[.+\]\()(?.+/v(?\d+(\.\d+)+)/kubernetes-client-windows-amd64\.tar\.gz)\)" ` | ForEach-Object {$_.Matches.Groups.Where{$_.Name -eq 'href'}.value} ` | Select-Object -First 1 @@ -59,7 +62,7 @@ function global:au_GetLatest { $patch_version = $matches.version } - $url32 = $minor_changelog_page.content ` + $url32 = $minor_changelog_page ` | Select-String -Pattern "(?<=\[.+\]\()(?.+/v(?\d+(\.\d+)+)/kubernetes-client-windows-386\.tar\.gz)\)" ` | ForEach-Object {$_.Matches.Groups.Where{$_.Name -eq 'href'}.value} ` | Select-Object -First 1