Skip to content

Commit 58ab797

Browse files
Extract version parsing to shared function and improve error handling
- Created Get-VersionChangeType function in scripts/utils.ps1 (DRY principle) - Updated vendor.yml and update.ps1 to use shared function - Fixed numeric comparison for COUNT_UPDATED (fromJSON for proper numeric eval) - Improved git operation error handling with proper exit code checking - Only reset commit if it succeeded but push failed Agent-Logs-Url: https://github.com/cmderdev/cmder/sessions/4511d497-599a-4a80-bc3c-12bd6f2d8191 Co-authored-by: DRSDavidSoft <4673812+DRSDavidSoft@users.noreply.github.com>
1 parent 09b0075 commit 58ab797

3 files changed

Lines changed: 139 additions & 94 deletions

File tree

.github/workflows/vendor.yml

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ jobs:
5757
Set-GHVariable -Name COUNT_UPDATED -Value $count
5858
5959
$newVersion = (Get-Content -Raw .\vendor\sources.json | ConvertFrom-Json)
60+
# Source utility functions
61+
. scripts/utils.ps1
62+
6063
$listUpdated = ""
6164
$updateMessage = "| Name | Old Version | New Version |`n| :--- | :---: | :---: |`n"
6265
$majorUpdates = @()
@@ -75,46 +78,11 @@ jobs:
7578
$singleDepNewVersion = $s.version
7679
}
7780
78-
# Determine change type and emoji
79-
$changeType = "unknown"
80-
$emoji = "🔄"
81-
$isMajor = $false
82-
try {
83-
# Handle versions with more than 4 parts
84-
$oldVerStr = $oldVersion.Split('-')[0]
85-
$newVerStr = $s.version.Split('-')[0]
86-
87-
# Split by dots and take only numeric parts, first 4 max
88-
$oldParts = $oldVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
89-
$newParts = $newVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
90-
91-
# Ensure we have at least 2 parts (major.minor)
92-
if ($oldParts.Count -ge 2 -and $newParts.Count -ge 2) {
93-
$oldVerParseable = $oldParts -join '.'
94-
$newVerParseable = $newParts -join '.'
95-
96-
$oldVer = [System.Version]::Parse($oldVerParseable)
97-
$newVer = [System.Version]::Parse($newVerParseable)
98-
99-
if ($newVer.Major -gt $oldVer.Major) {
100-
$changeType = "major"
101-
$emoji = "🔥"
102-
$isMajor = $true
103-
} elseif ($newVer.Minor -gt $oldVer.Minor) {
104-
$changeType = "minor"
105-
$emoji = "🚀"
106-
} elseif ($newVer -gt $oldVer) {
107-
$changeType = "patch"
108-
$emoji = "⬆️"
109-
} else {
110-
$changeType = "unknown"
111-
$emoji = "🔄"
112-
}
113-
}
114-
} catch {
115-
$changeType = "unknown"
116-
$emoji = "🔄"
117-
}
81+
# Determine change type and emoji using shared function
82+
$result = Get-VersionChangeType -OldVersion $oldVersion -NewVersion $s.version
83+
$changeType = $result.ChangeType
84+
$emoji = $result.Emoji
85+
$isMajor = $result.IsMajor
11886
11987
# Track major updates for changelog section
12088
if ($isMajor) {
@@ -229,18 +197,26 @@ jobs:
229197
# Commit the changes
230198
git add vendor/sources.json
231199
$commitResult = git commit -m "⬆️ Update dependencies ($env:LIST_UPDATED)"
200+
$commitSuccess = $LASTEXITCODE -eq 0
232201
233-
if ($commitResult) {
202+
if ($commitSuccess) {
234203
# Push directly to master
235204
git push origin HEAD:master
236-
237-
echo "" >> $env:GITHUB_STEP_SUMMARY
238-
echo "✅ **Success!** Updates have been automatically merged to master." >> $env:GITHUB_STEP_SUMMARY
239-
echo "" >> $env:GITHUB_STEP_SUMMARY
240-
echo "**Updated dependencies:** $env:LIST_UPDATED" >> $env:GITHUB_STEP_SUMMARY
241-
242-
# Set a flag to skip PR creation
243-
echo "AUTO_MERGED=true" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
205+
$pushSuccess = $LASTEXITCODE -eq 0
206+
207+
if ($pushSuccess) {
208+
echo "" >> $env:GITHUB_STEP_SUMMARY
209+
echo "✅ **Success!** Updates have been automatically merged to master." >> $env:GITHUB_STEP_SUMMARY
210+
echo "" >> $env:GITHUB_STEP_SUMMARY
211+
echo "**Updated dependencies:** $env:LIST_UPDATED" >> $env:GITHUB_STEP_SUMMARY
212+
213+
# Set a flag to skip PR creation
214+
echo "AUTO_MERGED=true" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
215+
} else {
216+
throw "Failed to push to master (exit code: $LASTEXITCODE)"
217+
}
218+
} else {
219+
throw "Failed to commit changes (exit code: $LASTEXITCODE)"
244220
}
245221
} catch {
246222
echo "" >> $env:GITHUB_STEP_SUMMARY
@@ -252,17 +228,24 @@ jobs:
252228
253229
Write-Warning "Failed to auto-merge: $($_.Exception.Message)"
254230
255-
# Reset the commit if one was made
256-
if ($commitResult) {
257-
git reset --hard HEAD~1
231+
# Only reset if commit was successful but push failed
232+
if ($commitSuccess -and -not $pushSuccess) {
233+
try {
234+
git reset --hard HEAD~1
235+
if ($LASTEXITCODE -ne 0) {
236+
Write-Warning "Failed to reset commit (exit code: $LASTEXITCODE), continuing with PR creation"
237+
}
238+
} catch {
239+
Write-Warning "Failed to reset commit: $($_.Exception.Message), continuing with PR creation"
240+
}
258241
}
259242
260243
# Set flag to create PR instead
261244
echo "AUTO_MERGED=false" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
262245
}
263246
264247
- uses: peter-evans/create-pull-request@v8
265-
if: env.COUNT_UPDATED > 0 && (env.HAS_BREAKING_CHANGES == 'True' || env.AUTO_MERGED == 'false')
248+
if: fromJSON(env.COUNT_UPDATED) > 0 && (env.HAS_BREAKING_CHANGES == 'True' || env.AUTO_MERGED == 'false')
266249
with:
267250
title: ${{ env.COUNT_UPDATED == 1 && format('⬆️ Update {0}', env.LIST_UPDATED) || format('⬆️ Update {0} vendored dependencies', env.COUNT_UPDATED) }}
268251
body: |

scripts/update.ps1

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -304,47 +304,15 @@ foreach ($s in $sources) {
304304

305305
$count++
306306

307-
# Analyze version change type
308-
$changeType = "unknown"
309-
try {
310-
# Try parsing as semantic version
311-
# Handle versions with more than 4 parts by taking only the first 3-4 parts
312-
$oldVerStr = $s.version.Split('-')[0]
313-
$newVerStr = $version.Split('-')[0]
314-
315-
# Split by dots and take only numeric parts, first 4 max
316-
$oldParts = $oldVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
317-
$newParts = $newVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
318-
319-
# Ensure we have at least 2 parts (major.minor)
320-
if ($oldParts.Count -ge 2 -and $newParts.Count -ge 2) {
321-
$oldVerParseable = $oldParts -join '.'
322-
$newVerParseable = $newParts -join '.'
323-
324-
$oldVer = [System.Version]::Parse($oldVerParseable)
325-
$newVer = [System.Version]::Parse($newVerParseable)
326-
327-
if ($newVer -lt $oldVer) {
328-
$changeType = "downgrade"
329-
$hasBreakingChanges = $true
330-
} elseif ($newVer.Major -gt $oldVer.Major) {
331-
$changeType = "major"
332-
$hasBreakingChanges = $true
333-
} elseif ($newVer.Minor -gt $oldVer.Minor) {
334-
$changeType = "minor"
335-
} elseif ($newVer.Build -gt $oldVer.Build) {
336-
$changeType = "patch"
337-
} else {
338-
# No version increase detected (could be equal or non-incremental change)
339-
$changeType = "unknown"
340-
}
341-
} else {
342-
# Not enough numeric parts for semantic versioning
343-
throw "Not enough numeric version parts"
344-
}
345-
} catch {
346-
# If semantic versioning fails, treat as unknown (potentially breaking)
347-
$changeType = "unknown"
307+
# Analyze version change type using shared function
308+
$result = Get-VersionChangeType -OldVersion $s.version -NewVersion $version
309+
$changeType = $result.ChangeType
310+
311+
# Determine if this is a breaking change
312+
if ($changeType -eq "downgrade" -or $changeType -eq "major") {
313+
$hasBreakingChanges = $true
314+
} elseif ($changeType -eq "unknown") {
315+
# If version parsing failed, treat as potentially breaking
348316
$hasBreakingChanges = $true
349317
Write-Verbose "Could not parse version as semantic version for dependency '$($s.name)' (old: '$($s.version)', new: '$version'), treating as potentially breaking"
350318
}

scripts/utils.ps1

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,100 @@ function Format-FileSize {
285285
}
286286
}
287287

288+
function Get-VersionChangeType {
289+
<#
290+
.SYNOPSIS
291+
Analyzes version changes using semantic versioning to determine the type of update and appropriate emoji.
292+
293+
.DESCRIPTION
294+
Parses old and new version strings, compares them using semantic versioning rules,
295+
and returns information about the change type, emoji indicator, and whether it's a major update.
296+
297+
.PARAMETER OldVersion
298+
The previous version string (e.g., "1.2.3" or "2.52.0.windows.1").
299+
300+
.PARAMETER NewVersion
301+
The new version string (e.g., "1.3.0" or "3.0.0.windows.1").
302+
303+
.OUTPUTS
304+
Returns a hashtable with the following keys:
305+
- ChangeType: "major", "minor", "patch", "downgrade", or "unknown"
306+
- Emoji: The corresponding emoji indicator (🔥, 🚀, ⬆️, or 🔄)
307+
- IsMajor: Boolean indicating if this is a major version update
308+
309+
.EXAMPLE
310+
$result = Get-VersionChangeType -OldVersion "1.2.3" -NewVersion "2.0.0"
311+
# Returns: @{ ChangeType = "major"; Emoji = "🔥"; IsMajor = $true }
312+
313+
.EXAMPLE
314+
$result = Get-VersionChangeType -OldVersion "1.2.3" -NewVersion "1.3.0"
315+
# Returns: @{ ChangeType = "minor"; Emoji = "🚀"; IsMajor = $false }
316+
#>
317+
param(
318+
[Parameter(Mandatory = $true)]
319+
[string]$OldVersion,
320+
321+
[Parameter(Mandatory = $true)]
322+
[string]$NewVersion
323+
)
324+
325+
$changeType = "unknown"
326+
$emoji = "🔄"
327+
$isMajor = $false
328+
329+
try {
330+
# Handle versions with more than 4 parts and strip pre-release identifiers
331+
$oldVerStr = $OldVersion.Split('-')[0]
332+
$newVerStr = $NewVersion.Split('-')[0]
333+
334+
# Split by dots and take only numeric parts, first 4 max
335+
$oldParts = $oldVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
336+
$newParts = $newVerStr.Split('.') | Where-Object { $_ -match '^\d+$' } | Select-Object -First 4
337+
338+
# Ensure we have at least 2 parts (major.minor) for semantic versioning
339+
if ($oldParts.Count -ge 2 -and $newParts.Count -ge 2) {
340+
$oldVerParseable = $oldParts -join '.'
341+
$newVerParseable = $newParts -join '.'
342+
343+
$oldVer = [System.Version]::Parse($oldVerParseable)
344+
$newVer = [System.Version]::Parse($newVerParseable)
345+
346+
if ($newVer -lt $oldVer) {
347+
$changeType = "downgrade"
348+
# Don't set emoji for downgrades in this function - caller handles it
349+
} elseif ($newVer.Major -gt $oldVer.Major) {
350+
$changeType = "major"
351+
$emoji = "🔥"
352+
$isMajor = $true
353+
} elseif ($newVer.Minor -gt $oldVer.Minor) {
354+
$changeType = "minor"
355+
$emoji = "🚀"
356+
} elseif ($newVer -gt $oldVer) {
357+
$changeType = "patch"
358+
$emoji = "⬆️"
359+
} else {
360+
# No version increase detected (versions are equal)
361+
$changeType = "unknown"
362+
$emoji = "🔄"
363+
}
364+
} else {
365+
# Not enough numeric parts for semantic versioning
366+
throw "Not enough numeric version parts"
367+
}
368+
} catch {
369+
# If semantic versioning fails, return unknown
370+
$changeType = "unknown"
371+
$emoji = "🔄"
372+
$isMajor = $false
373+
}
374+
375+
return @{
376+
ChangeType = $changeType
377+
Emoji = $emoji
378+
IsMajor = $isMajor
379+
}
380+
}
381+
288382
function Get-ArtifactDownloadUrl {
289383
<#
290384
.SYNOPSIS

0 commit comments

Comments
 (0)