diff --git a/.github/workflows/build-and-package-application.yml b/.github/workflows/build-and-package-application.yml index 2d5e39bbf..4c7de8b16 100644 --- a/.github/workflows/build-and-package-application.yml +++ b/.github/workflows/build-and-package-application.yml @@ -147,19 +147,31 @@ jobs: build-package-windows: name: Build and package windows runs-on: windows-latest - env: - GH_TOKEN: ${{ secrets.GH_TOKEN }} steps: - name: Checkout code uses: actions/checkout@v6 with: fetch-depth: 0 + - name: Detect NowPlaying-related changes + id: nowplaying_changes + if: ${{ github.event_name == 'pull_request' }} + shell: bash + run: | + changed_files="$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}")" + + if printf '%s\n' "$changed_files" | grep -Eq '^(\.github/workflows/build-and-package-application\.yml|data/module/media/media_windows\.go|\.scripts/windows/(create-installer\.ps1|download-now-playing\.ps1|installer\.nsi\.template))$'; then + echo "changed=true" >> "$GITHUB_OUTPUT" + else + echo "changed=false" >> "$GITHUB_OUTPUT" + fi + - name: Download NowPlaying helper shell: pwsh env: - GH_TOKEN: ${{ secrets.GH_TOKEN }} - if: ${{ env.GH_TOKEN != '' }} + GH_TOKEN: ${{ github.token }} + ALLOW_RATE_LIMIT_SKIP: ${{ github.event_name == 'pull_request' && steps.nowplaying_changes.outputs.changed != 'true' && 'true' || 'false' }} + if: ${{ github.token != '' }} run: ./.scripts/windows/download-now-playing.ps1 - name: Setup Go diff --git a/.scripts/windows/download-now-playing.ps1 b/.scripts/windows/download-now-playing.ps1 index fbf436408..2da9c40dd 100644 --- a/.scripts/windows/download-now-playing.ps1 +++ b/.scripts/windows/download-now-playing.ps1 @@ -1,6 +1,7 @@ $ErrorActionPreference = 'Stop' $ghToken = $env:GH_TOKEN +$allowRateLimitSkip = $env:ALLOW_RATE_LIMIT_SKIP -eq 'true' if (-not $ghToken -and $env:GITHUB_TOKEN) { $env:GH_TOKEN = $env:GITHUB_TOKEN $ghToken = $env:GH_TOKEN @@ -16,12 +17,30 @@ try { Write-Error 'Error calling gh auth status. Please check if the GH_TOKEN is set correctly.' } +function Test-RateLimitError { + param( + [string]$Message + ) + + return $Message -match 'rate limit|secondary rate limit|api rate limit exceeded|http 429|too many requests' +} + $outDir = Join-Path (Get-Location) '.scripts/windows/now-playing' $temp = Join-Path $env:TEMP ("npdl_" + [guid]::NewGuid()) New-Item -ItemType Directory -Path $temp -Force | Out-Null try { - gh release download -R timmo001/dotnet-now-playing -p '*.zip' -D $temp --clobber | Out-Null + $downloadOutput = & gh release download -R timmo001/dotnet-now-playing -p '*.zip' -D $temp --clobber 2>&1 + if ($LASTEXITCODE -ne 0) { + $message = ($downloadOutput | Out-String).Trim() + if ($allowRateLimitSkip -and (Test-RateLimitError -Message $message)) { + Write-Host '::warning title=NowPlaying download skipped::GitHub rate limit hit while downloading the NowPlaying helper. Continuing without it for this pull request.' + return + } + + throw "Failed to download NowPlaying helper. $message" + } + $zip = Get-ChildItem -Path $temp -Filter *.zip | Select-Object -First 1 if (-not $zip) { throw 'No zip asset found' }