Skip to content

Commit 34cbcf7

Browse files
🩹 [Patch]: Use GitHub App again and some log and file formatting (#45)
This pull request updates the workflow and script for automating the FontsData update process. Key changes include integration with GitHub App credentials, enhanced error handling, and formatting updates for better readability. The changes also streamline the process for detecting updates, committing changes, and creating pull requests. ### Workflow updates: * [`.github/workflows/Update-FontsData.yml`](diffhunk://#diff-5c6d6ac6744e613c22fe7a4b5bf6b9fbd4b3bf5548239191e4400a4b17440826R24-R25): Added GitHub App credentials (`ClientID` and `PrivateKey`) for secure authentication during the update process. Removed permissions as these grant the GITHUB_TOKEN permissions, which is no longer used. ### Script enhancements: * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L21-R54): Improved command execution to capture output and handle multiline results, returning formatted output when applicable. * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L68-R129): Updated JSON formatting for `FontsData.json` to include indentation for better readability. * [`scripts/Update-FontsData.ps1`](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89R65-R69): Enhanced logging and messaging, including clearer notices for no updates, summary of changes, and improved pull request titles and descriptions. [[1]](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89R65-R69) [[2]](diffhunk://#diff-69d235ffacf154789a70d5e00a557ee83393c256cd9431a8b7be57cc41c3ad89L68-R129) ## 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: nerdfonts-updater <199731644+nerdfonts-updater@users.noreply.github.com>
1 parent 37db98e commit 34cbcf7

3 files changed

Lines changed: 366 additions & 339 deletions

File tree

.github/workflows/Update-FontsData.yml

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

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

1210
jobs:
1311
Update-FontsData:
@@ -23,4 +21,6 @@ jobs:
2321
- name: Update-FontsData
2422
uses: PSModule/GitHub-Script@v1
2523
with:
24+
ClientID: ${{ secrets.NERDFONTS_UPDATER_BOT_CLIENT_ID }}
25+
PrivateKey: ${{ secrets.NERDFONTS_UPDATER_BOT_PRIVATE_KEY }}
2626
Script: scripts/Update-FontsData.ps1

scripts/Update-FontsData.ps1

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,55 @@
1818

1919
try {
2020
Write-Verbose "Executing: $fullCommand"
21-
& $cmd @arguments
21+
$output = & $cmd @arguments
2222
if ($LASTEXITCODE -ne 0) {
2323
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
2424
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
2525
}
26+
if ($output -is [array] -and $output.Count -gt 1) {
27+
return $output -join "`n"
28+
} else {
29+
return $output
30+
}
2631
} catch {
2732
throw
2833
}
2934
}
3035

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
39-
40-
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
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
36+
Install-PSResource -Repository PSGallery -TrustRepository -Name 'Json'
37+
38+
Connect-GitHubApp -Organization 'PSModule' -Default
39+
$repo = Get-GitHubRepository -Owner 'PSModule' -Name 'NerdFonts'
40+
41+
LogGroup 'Checkout' {
42+
$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim()
43+
$defaultBranch = $repo.DefaultBranch
44+
45+
Write-Output "Current branch: $currentBranch"
46+
Write-Output "Default branch: $defaultBranch"
47+
Run git fetch origin
48+
Run git checkout $defaultBranch
49+
Run git pull origin $defaultBranch
50+
51+
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
52+
if ($currentBranch -eq $defaultBranch) {
53+
# Running on main/default branch - create new branch
54+
$targetBranch = "auto-update-$timeStamp"
55+
Write-Output "Running on default branch. Creating new branch: $targetBranch"
56+
Run git checkout -b $targetBranch
57+
} else {
58+
# Running on another branch (e.g., workflow_dispatch) - use current branch
59+
$targetBranch = $currentBranch
60+
Write-Output "Running on feature branch. Using existing branch: $targetBranch"
61+
Run git checkout $targetBranch
62+
# Merge latest changes from default branch
63+
Run git merge origin/$defaultBranch
64+
}
5365
}
5466

55-
LogGroup 'Latest Fonts' {
56-
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
67+
LogGroup 'Getting latest fonts' {
5768
$fonts = @()
69+
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
5870
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }
5971

6072
foreach ($fontArchive in $fontAssets) {
@@ -65,38 +77,53 @@ LogGroup 'Latest Fonts' {
6577
}
6678

6779
$fonts | Sort-Object Name | Format-Table -AutoSize | Out-String
80+
$parentFolder = Split-Path -Path $PSScriptRoot -Parent
81+
$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'
82+
$null = New-Item -Path $filePath -ItemType File -Force
83+
$fonts | ConvertTo-Json | Format-Json -IndentationType Spaces -IndentationSize 4 | Set-Content -Path $filePath -Force
6884
}
6985

70-
$parentFolder = Split-Path -Path $PSScriptRoot -Parent
71-
$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'
72-
$null = New-Item -Path $filePath -ItemType File -Force
73-
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force
74-
7586
$changes = Run git status --porcelain
7687
if ([string]::IsNullOrWhiteSpace($changes)) {
77-
Write-Output 'No changes detected.'
88+
Write-Output 'No updates available.'
89+
Write-GitHubNotice 'No updates available.'
7890
return
7991
}
92+
LogGroup 'Get changes' {
93+
Run git add .
94+
Run git commit -m 'Update FontsData.json'
95+
Write-Output 'Changes in this commit:'
96+
$changes = Run git diff HEAD~1 HEAD -- src/FontsData.json
97+
Write-Output $changes
98+
Set-GitHubStepSummary @"
99+
## Changes available
80100
81-
Run git add .
82-
Run git commit -m "Update-FontsData via script on $timeStamp"
83-
Write-Output 'Changes in this commit:'
84-
Run git diff HEAD~1 HEAD -- src/FontsData.json
85-
86-
# Push behavior depends on branch type
87-
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
88-
# Push to existing branch
89-
Run git push origin $targetBranch
90-
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
91-
} else {
92-
# Push new branch and create PR
93-
Run git push --set-upstream origin $targetBranch
94-
95-
Run gh pr create `
96-
--base $defaultBranch `
97-
--head $targetBranch `
98-
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
99-
--body 'This PR updates FontsData.json with the latest NerdFonts metadata.'
100-
101-
Write-Output "Changes detected and PR opened for branch: $targetBranch"
101+
<details><summary>Details</summary>
102+
<p>
103+
104+
``````diff
105+
$changes
106+
``````
107+
108+
</p>
109+
</details>
110+
"@
111+
112+
}
113+
114+
LogGroup 'Process changes' {
115+
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
116+
Run git push origin $targetBranch
117+
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
118+
} else {
119+
Run git push --set-upstream origin $targetBranch
120+
121+
Run gh pr create `
122+
--base $defaultBranch `
123+
--head $targetBranch `
124+
--title "Auto-Update $timeStamp" `
125+
--body 'This PR updates FontsData.json with the latest metadata.'
126+
127+
Write-Output "Changes detected and PR opened for branch: $targetBranch"
128+
}
102129
}

0 commit comments

Comments
 (0)