Refactor GitHub Actions workflow for changelog updates

This commit is contained in:
Dimitrij
2025-09-10 00:07:43 +01:00
committed by GitHub
parent aafb608149
commit 9d173d0bdc

View File

@@ -3,7 +3,7 @@ name: Update Changelog After Renovate PR Merge
on:
push:
branches:
- v1.78.2-dev # ← replace with your default branch
- v1.78.2-dev # ← replace with your repos default branch
workflow_dispatch:
inputs:
@@ -24,93 +24,95 @@ jobs:
id: check-renovate
shell: bash
run: |
# Always true for manual runs
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "isRenovate=true" >> $GITHUB_OUTPUT
exit 0
fi
# Inspect the last commit message
msg=$(git log -1 --pretty=%B)
msg="$(git log -1 --pretty=%B)"
if echo "$msg" | grep -q 'chore(deps): update dependency'; then
echo "isRenovate=true" >> $GITHUB_OUTPUT
else
echo "isRenovate=false" >> $GITHUB_OUTPUT
fi
- name: Skip if not a Renovate PR
- name: Abort if not a Renovate PR
if: steps.check-renovate.outputs.isRenovate == 'false'
run: |
echo " No Renovate dependency update found in the last commit. Skipping changelog update."
echo " Last commit is not a Renovate dependency update—skipping."
- name: Parse Renovate PR info
if: steps.check-renovate.outputs.isRenovate == 'true'
id: extract
shell: pwsh
run: |
# Determine dryRun
# 1) Determine dryRun
$dryRun = '${{ github.event.inputs.dryRun }}'
if (-not $dryRun) { $dryRun = 'false' }
Write-Host "🔍 dryRun = $dryRun"
# Grab full commit message
# 2) Read full commit message
$fullMsg = git log -1 --pretty=%B
Write-Host "📝 Commit message:"
Write-Host $fullMsg
# Extract PR number (#1234)
if ($fullMsg -match '#(\d+)\s*$') {
# 3) Extract PR number from merge subject
if ($fullMsg -match 'Merge pull request #(\d+)') {
$prNumber = $matches[1]
} else {
throw "❌ No PR number found in commit message"
throw "❌ Could not locate PR number in merge commit"
}
# Extract dependency name & version
# 4) Extract dependency update from body
if ($fullMsg -match 'chore\(deps\): update dependency ([\w\.\-]+) to ([\d\.]+)') {
$depName = $matches[1]
$depVersion = $matches[2]
} else {
throw "❌ Could not parse dependency info"
throw "❌ Could not parse dependency name/version"
}
echo "pr=$prNumber" >> $env:GITHUB_OUTPUT
echo "depName=$depName" >> $env:GITHUB_OUTPUT
# 5) Persist outputs
echo "pr=$prNumber" >> $env:GITHUB_OUTPUT
echo "depName=$depName" >> $env:GITHUB_OUTPUT
echo "depVersion=$depVersion" >> $env:GITHUB_OUTPUT
echo "dryRun=$dryRun" >> $env:GITHUB_OUTPUT
echo "dryRun=$dryRun" >> $env:GITHUB_OUTPUT
- name: Update CHANGELOG.md
if: steps.check-renovate.outputs.isRenovate == 'true'
shell: pwsh
run: |
$path = "$env:GITHUB_WORKSPACE/CHANGELOG.md"
$path = "$env:GITHUB_WORKSPACE/CHANGELOG.md"
if (-not (Test-Path $path)) { throw "❌ CHANGELOG.md not found" }
$lines = Get-Content $path
$pr = '${{ steps.extract.outputs.pr }}'
$dep = '${{ steps.extract.outputs.depName }}'
$ver = '${{ steps.extract.outputs.depVersion }}'
$entry = "- #$pr: update $dep to $ver"
$lines = Get-Content $path
$pr = '${{ steps.extract.outputs.pr }}'
$dep = '${{ steps.extract.outputs.depName }}'
$ver = '${{ steps.extract.outputs.depVersion }}'
$entry = "- #$pr: update $dep to $ver"
# Locate latest version header: ## [x.y.z]
# Find latest version header: ## [x.y.z]
$vIndex = $lines.FindIndex({ $_ -match '^## \[\d+\.\d+\.\d+\]' })
if ($vIndex -lt 0) { throw "❌ No version header found" }
# Find or insert '### Dependency update'
# Locate or create '### Dependency update'
$depIndex = -1
for ($i = $vIndex+1; $i -lt $lines.Count; $i++) {
for ($i = $vIndex + 1; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match '^### Dependency update') {
$depIndex = $i; break
$depIndex = $i
break
}
if ($lines[$i] -match '^## ') { break }
}
if ($depIndex -eq -1) {
$lines.Insert($vIndex+1, '### Dependency update')
$depIndex = $vIndex+1
$lines.Insert($vIndex + 1, '### Dependency update')
$depIndex = $vIndex + 1
}
# Insert the new line
$lines.Insert($depIndex+1, $entry)
# Insert the entry
$lines.Insert($depIndex + 1, $entry)
Set-Content -Path $path -Value $lines
Write-Host "✅ Changelog entry inserted:"
Write-Host "✅ Inserted in CHANGELOG.md:"
Write-Host " $entry"
- name: Commit & push