diff --git a/.github/workflows/Update-FontsData.yml b/.github/workflows/Update-FontsData.yml index e442043..73de107 100644 --- a/.github/workflows/Update-FontsData.yml +++ b/.github/workflows/Update-FontsData.yml @@ -5,9 +5,7 @@ on: schedule: - cron: '0 0 * * *' -permissions: - contents: write - pull-requests: write +permissions: {} jobs: Update-FontsData: @@ -25,4 +23,6 @@ jobs: env: GOOGLE_DEVELOPER_API_KEY: ${{ secrets.GOOGLE_DEVELOPER_API_KEY }} with: + ClientID: ${{ secrets.GOOGLEFONTS_UPDATER_BOT_CLIENT_ID }} + PrivateKey: ${{ secrets.GOOGLEFONTS_UPDATER_BOT_PRIVATE_KEY }} Script: scripts/Update-FontsData.ps1 diff --git a/scripts/Update-FontsData.ps1 b/scripts/Update-FontsData.ps1 index 0450c1f..d0ff0f0 100644 --- a/scripts/Update-FontsData.ps1 +++ b/scripts/Update-FontsData.ps1 @@ -18,41 +18,51 @@ try { Write-Verbose "Executing: $fullCommand" - & $cmd @arguments + $output = & $cmd @arguments if ($LASTEXITCODE -ne 0) { $errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand" Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand } + if ($output -is [array] -and $output.Count -gt 1) { + return $output -join "`n" + } else { + return $output + } } catch { throw } } -$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim() -$defaultBranch = (Run git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() }) -Write-Output "Current branch: $currentBranch" -Write-Output "Default branch: $defaultBranch" - -Run git fetch origin -Run git checkout $defaultBranch -Run git pull origin $defaultBranch - -$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss' -if ($currentBranch -eq $defaultBranch) { - # Running on main/default branch - create new branch - $targetBranch = "auto-update-font-$timeStamp" - Write-Output "Running on default branch. Creating new branch: $targetBranch" - Run git checkout -b $targetBranch -} else { - # Running on another branch (e.g., workflow_dispatch) - use current branch - $targetBranch = $currentBranch - Write-Output "Running on feature branch. Using existing branch: $targetBranch" - Run git checkout $targetBranch - # Merge latest changes from default branch - Run git merge origin/$defaultBranch +Connect-GitHubApp -Organization 'PSModule' -Default +$repo = Get-GitHubRepository -Owner 'PSModule' -Name 'GoogleFonts' + +LogGroup 'Checkout' { + $currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim() + $defaultBranch = $repo.DefaultBranch + + Write-Output "Current branch: $currentBranch" + Write-Output "Default branch: $defaultBranch" + Run git fetch origin + Run git checkout $defaultBranch + Run git pull origin $defaultBranch + + $timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss' + if ($currentBranch -eq $defaultBranch) { + # Running on main/default branch - create new branch + $targetBranch = "auto-update-font-$timeStamp" + Write-Output "Running on default branch. Creating new branch: $targetBranch" + Run git checkout -b $targetBranch + } else { + # Running on another branch (e.g., workflow_dispatch) - use current branch + $targetBranch = $currentBranch + Write-Output "Running on feature branch. Using existing branch: $targetBranch" + Run git checkout $targetBranch + # Merge latest changes from default branch + Run git merge origin/$defaultBranch + } } -LogGroup 'Latest Fonts' { +LogGroup 'Getting latest fonts' { $fontList = Invoke-RestMethod -Uri "https://www.googleapis.com/webfonts/v1/webfonts?key=$env:GOOGLE_DEVELOPER_API_KEY" $fontFamilies = $fontList.items $fonts = @() @@ -69,38 +79,56 @@ LogGroup 'Latest Fonts' { } $fonts | Sort-Object Name | Format-Table -AutoSize | Out-String + $parentFolder = Split-Path -Path $PSScriptRoot -Parent + $filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json' + $null = New-Item -Path $filePath -ItemType File -Force + $fonts | ConvertTo-Json | Set-Content -Path $filePath -Force } -$parentFolder = Split-Path -Path $PSScriptRoot -Parent -$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json' -$null = New-Item -Path $filePath -ItemType File -Force -$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force - $changes = Run git status --porcelain if ([string]::IsNullOrWhiteSpace($changes)) { - Write-Output 'No changes detected.' + Write-Output 'No updates available.' + Write-GitHubNotice 'No updates available.' return } +LogGroup 'Get changes' { + Run git add . + Run git commit -m "Update-FontsData via script on $timeStamp" + Write-Output 'Changes in this commit:' + $changes = Run git diff HEAD~1 HEAD -- src/FontsData.json + Write-Output $changes + Set-GitHubStepSummary @" +## Changes available + +
Details +

+ +``````diff +$changes +`````` + +

+
+"@ + +} -Run git add . -Run git commit -m "Update-FontsData via script on $timeStamp" -Write-Output 'Changes in this commit:' -Run git diff HEAD~1 HEAD -- src/FontsData.json - -# Push behavior depends on branch type -if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) { - # Push to existing branch - Run git push origin $targetBranch - Write-Output "Changes committed and pushed to existing branch: $targetBranch" -} else { - # Push new branch and create PR - Run git push --set-upstream origin $targetBranch - - Run gh pr create ` - --base $defaultBranch ` - --head $targetBranch ` - --title "Auto-Update: Google Fonts Data ($timeStamp)" ` - --body 'This PR updates FontsData.json with the latest Google Fonts metadata.' - - Write-Output "Changes detected and PR opened for branch: $targetBranch" +LogGroup 'Process changes' { + # Push behavior depends on branch type + if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) { + # Push to existing branch + Run git push origin $targetBranch + Write-Output "Changes committed and pushed to existing branch: $targetBranch" + } else { + # Push new branch and create PR + Run git push --set-upstream origin $targetBranch + + Run gh pr create ` + --base $defaultBranch ` + --head $targetBranch ` + --title "Auto-Update: Google Fonts Data ($timeStamp)" ` + --body 'This PR updates FontsData.json with the latest Google Fonts metadata.' + + Write-Output "Changes detected and PR opened for branch: $targetBranch" + } }