Skip to content

Commit 6ed199f

Browse files
🩹 [Patch]: Switch to use GITHUB_TOKEN for updates (#68)
## Description This pull request introduces significant updates to the workflow and script for automating Google Fonts data updates. Key changes include enhancements to permissions in the GitHub Actions workflow, improved repository management in the PowerShell script, and refactoring for better logging and error handling. ### Workflow Updates: * [`.github/workflows/Update-FontsData.yml`](diffhunk://#diff-5c6d6ac6744e613c22fe7a4b5bf6b9fbd4b3bf5548239191e4400a4b17440826L8-R10): Updated permissions to include `contents: write` and `pull-requests: write`, enabling the workflow to create pull requests and write changes to the repository. * [`.github/workflows/Update-FontsData.yml`](diffhunk://#diff-5c6d6ac6744e613c22fe7a4b5bf6b9fbd4b3bf5548239191e4400a4b17440826R20-L26): Added `fetch-depth: 0` to the checkout step to ensure complete history is fetched, supporting branch operations. Removed unused `ClientID` and `PrivateKey` inputs from the `Update-FontsData` job. ### Script Enhancements: * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L1-R129): Introduced the `Invoke-NativeCommand` function to standardize execution of native commands with improved error handling and logging. * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L1-R129): Refactored branch management logic to dynamically determine whether to create a new branch or use the current branch, ensuring compatibility with different workflow triggers. * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L1-R129): Improved logging for changes detection, commit creation, and push operations. Added detailed output for diffs and commit logs to enhance debugging and visibility. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --------- Co-authored-by: github-actions <41898282+github-actions@users.noreply.github.com>
1 parent 48cc86b commit 6ed199f

2 files changed

Lines changed: 96 additions & 46 deletions

File tree

.github/workflows/Update-FontsData.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ on:
55
schedule:
66
- cron: '0 0 * * *'
77

8-
permissions: {}
8+
permissions:
9+
contents: write
10+
pull-requests: write
911

1012
jobs:
1113
Update-FontsData:
@@ -15,13 +17,12 @@ jobs:
1517
- name: Checkout repository
1618
uses: actions/checkout@v4
1719
with:
20+
fetch-depth: 0
1821
persist-credentials: false
1922

2023
- name: Update-FontsData
2124
uses: PSModule/GitHub-Script@v1
2225
env:
2326
GOOGLE_DEVELOPER_API_KEY: ${{ secrets.GOOGLE_DEVELOPER_API_KEY }}
2427
with:
25-
ClientID: ${{ secrets.GOOGLEFONTS_UPDATER_BOT_CLIENT_ID }}
26-
PrivateKey: ${{ secrets.GOOGLEFONTS_UPDATER_BOT_PRIVATE_KEY }}
2728
Script: scripts/Update-FontsData.ps1

scripts/Update-FontsData.ps1

Lines changed: 92 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,106 @@
1-
Connect-GitHubApp -Organization PSModule -Default
1+
function Invoke-NativeCommand {
2+
<#
3+
.SYNOPSIS
4+
Executes a native command with arguments.
5+
#>
6+
[Alias('Exec', 'Run')]
7+
[CmdletBinding()]
8+
param (
9+
# The command to execute
10+
[Parameter(ValueFromRemainingArguments)]
11+
[string[]]$Command
12+
)
13+
$cmd = $Command[0]
14+
$arguments = $Command[1..$Command.Length]
15+
Write-Debug "Command: $cmd"
16+
Write-Debug "Arguments: $($arguments -join ', ')"
17+
$fullCommand = "$cmd $($arguments -join ' ')"
218

3-
git checkout main
4-
git pull
19+
try {
20+
Write-Verbose "Executing: $fullCommand"
21+
& $cmd @arguments
22+
if ($LASTEXITCODE -ne 0) {
23+
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
24+
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
25+
}
26+
} catch {
27+
throw
28+
}
29+
}
30+
31+
$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim()
32+
$defaultBranch = (Run git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() })
33+
Write-Output "Current branch: $currentBranch"
34+
Write-Output "Default branch: $defaultBranch"
35+
36+
Run git fetch origin
37+
Run git checkout $defaultBranch
38+
Run git pull origin $defaultBranch
539

6-
# 2. Retrieve the date-time to create a unique branch name.
740
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
8-
$branchName = "auto-font-update-$timeStamp"
9-
10-
# 3. Create a new branch for the changes.
11-
git checkout -b $branchName
12-
13-
# 4. Retrieve the latest font data from Google Fonts.
14-
$GOOGLE_DEVELOPER_API_KEY = $env:GOOGLE_DEVELOPER_API_KEY
15-
$fontList = Invoke-RestMethod -Uri "https://www.googleapis.com/webfonts/v1/webfonts?key=$GOOGLE_DEVELOPER_API_KEY"
16-
$fontFamilies = $fontList.items
17-
$fonts = @()
18-
19-
foreach ($fontFamily in $fontFamilies) {
20-
$variants = $fontFamily.files.PSObject.Properties
21-
foreach ($variant in $variants) {
22-
$fonts += [ordered]@{
23-
Name = $fontFamily.family
24-
Variant = $variant.Name
25-
URL = $variant.Value
41+
if ($currentBranch -eq $defaultBranch) {
42+
# Running on main/default branch - create new branch
43+
$targetBranch = "auto-update-font-$timeStamp"
44+
Write-Output "Running on default branch. Creating new branch: $targetBranch"
45+
Run git checkout -b $targetBranch
46+
} else {
47+
# Running on another branch (e.g., workflow_dispatch) - use current branch
48+
$targetBranch = $currentBranch
49+
Write-Output "Running on feature branch. Using existing branch: $targetBranch"
50+
Run git checkout $targetBranch
51+
# Merge latest changes from default branch
52+
Run git merge origin/$defaultBranch
53+
}
54+
55+
LogGroup 'Latest Fonts' {
56+
$fontList = Invoke-RestMethod -Uri "https://www.googleapis.com/webfonts/v1/webfonts?key=$env:GOOGLE_DEVELOPER_API_KEY"
57+
$fontFamilies = $fontList.items
58+
$fonts = @()
59+
60+
foreach ($fontFamily in $fontFamilies) {
61+
$variants = $fontFamily.files.PSObject.Properties
62+
foreach ($variant in $variants) {
63+
$fonts += [PSCustomObject]@{
64+
Name = $fontFamily.family
65+
Variant = $variant.Name
66+
URL = $variant.Value
67+
}
2668
}
2769
}
70+
71+
$fonts | Sort-Object Name | Format-Table -AutoSize | Out-String
2872
}
2973

30-
# 5. Write results to FontsData.json.
3174
$parentFolder = Split-Path -Path $PSScriptRoot -Parent
3275
$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'
33-
34-
# Make sure file exists (or overwrite).
3576
$null = New-Item -Path $filePath -ItemType File -Force
36-
$fonts | ConvertTo-Json -Depth 10 | Out-File -FilePath $filePath -Force
37-
38-
# 6. Check if anything actually changed.
39-
# If git status --porcelain is empty, there are no new changes to commit.
40-
$changes = git status --porcelain
41-
if (-not [string]::IsNullOrWhiteSpace($changes)) {
42-
# 7. Commit and push changes.
43-
git add .
44-
git commit -m "Update-FontsData via script on $timeStamp"
45-
git push --set-upstream origin $branchName
46-
47-
# 8. Create a PR via GitHub CLI.
48-
gh pr create `
49-
--base main `
50-
--head $branchName `
77+
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force
78+
79+
$changes = Run git status --porcelain
80+
if ([string]::IsNullOrWhiteSpace($changes)) {
81+
Write-Output 'No changes detected.'
82+
return
83+
}
84+
85+
Run git add .
86+
Run git commit -m "Update-FontsData via script on $timeStamp"
87+
Write-Output 'Changes in this commit:'
88+
Run git diff HEAD~1 HEAD -- src/FontsData.json
89+
90+
# Push behavior depends on branch type
91+
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
92+
# Push to existing branch
93+
Run git push origin $targetBranch
94+
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
95+
} else {
96+
# Push new branch and create PR
97+
Run git push --set-upstream origin $targetBranch
98+
99+
Run gh pr create `
100+
--base $defaultBranch `
101+
--head $targetBranch `
51102
--title "Auto-Update: Google Fonts Data ($timeStamp)" `
52103
--body 'This PR updates FontsData.json with the latest Google Fonts metadata.'
53104

54-
Write-Output 'Changes detected and PR opened.'
55-
} else {
56-
Write-Output 'No changes to commit.'
105+
Write-Output "Changes detected and PR opened for branch: $targetBranch"
57106
}

0 commit comments

Comments
 (0)