Skip to content

Commit 378130a

Browse files
authored
fix: support proper sorting for prereleases (#78)
* feat: support proper sorting for prereleases * chore: changelog * don't fail on tags that fail to parse as semver * skip updates if not updating to the actual latest version * fixes * more fixes * tests and other fixes * chore: update changelog
1 parent 7f18300 commit 378130a

File tree

6 files changed

+110
-85
lines changed

6 files changed

+110
-85
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Don't update from a manually-updated prerelease to a latest stable release that is earlier than the prerelease ([#78](https://github.com/getsentry/github-workflows/pull/78))
8+
39
## 2.11.0
410

511
### Features

updater/scripts/common.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
function GetComparableVersion([string] $value)
3+
{
4+
$value = $value -replace '^v', ''
5+
try {
6+
[System.Management.Automation.SemanticVersion]::Parse($value)
7+
} catch {
8+
Write-Warning "Failed to parse string '$value' as semantic version: $_"
9+
$null
10+
}
11+
}

updater/scripts/sort-versions.ps1

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,8 @@ param(
33
)
44

55
Set-StrictMode -Version latest
6+
. "$PSScriptRoot/common.ps1"
67

7-
function GetComparablePart([Parameter(Mandatory = $true)][string] $value)
8-
{
9-
$value.PadLeft(10, '0')
10-
}
11-
12-
function GetComparableVersion([Parameter(Mandatory = $true)][string] $value)
13-
{
14-
$value = $value -replace '^v', ''
15-
$output = ''
16-
$buffer = ''
17-
for ($i = 0; $i -lt $value.Length; $i++)
18-
{
19-
$char = $value[$i]
20-
if ("$char" -match '[^a-zA-Z0-9]')
21-
{
22-
# Found a separtor, update the current buffer
23-
$output += GetComparablePart $buffer
24-
$output += $char
25-
$buffer = ''
26-
}
27-
else
28-
{
29-
$buffer += $char
30-
}
31-
}
32-
if ($buffer.Length -gt 0)
33-
{
34-
$output += GetComparablePart $buffer
35-
}
36-
37-
$output
38-
}
39-
40-
$List | Sort-Object -Property @{Expression = { GetComparableVersion $_ } }
8+
$List `
9+
| Where-Object { $null -ne (GetComparableVersion $_) } `
10+
| Sort-Object -Property @{Expression = { GetComparableVersion $_ } }

updater/scripts/update-dependency.ps1

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ param(
1414
)
1515

1616
Set-StrictMode -Version latest
17+
. "$PSScriptRoot/common.ps1"
1718

1819
if (-not (Test-Path $Path ))
1920
{
@@ -45,8 +46,9 @@ if (-not $isSubmodule)
4546
if (Get-Command 'chmod' -ErrorAction SilentlyContinue)
4647
{
4748
chmod +x $Path
48-
if ($LastExitCode -ne 0) {
49-
throw "chmod failed";
49+
if ($LastExitCode -ne 0)
50+
{
51+
throw 'chmod failed';
5052
}
5153
}
5254
try
@@ -69,18 +71,18 @@ if (-not $isSubmodule)
6971
{
7072
switch ($action)
7173
{
72-
"get-version"
74+
'get-version'
7375
{
7476
return (Get-Content $Path -Raw | ConvertFrom-StringData).version
7577
}
76-
"get-repo"
78+
'get-repo'
7779
{
7880
return (Get-Content $Path -Raw | ConvertFrom-StringData).repo
7981
}
80-
"set-version"
82+
'set-version'
8183
{
8284
$content = Get-Content $Path
83-
$content = $content -replace "^(?<prop>version *= *).*$", "`${prop}$value"
85+
$content = $content -replace '^(?<prop>version *= *).*$', "`${prop}$value"
8486
$content | Out-File $Path
8587

8688
$readVersion = (Get-Content $Path -Raw | ConvertFrom-StringData).version
@@ -99,7 +101,7 @@ if (-not $isSubmodule)
99101
}
100102
}
101103

102-
if ("$Tag" -eq "")
104+
if ("$Tag" -eq '')
103105
{
104106
if ($isSubmodule)
105107
{
@@ -111,7 +113,7 @@ if ("$Tag" -eq "")
111113
git fetch --tags
112114
[string[]]$tags = $(git tag --list)
113115
$url = $(git remote get-url origin)
114-
$mainBranch = $(git remote show origin | Select-String "HEAD branch: (.*)").Matches[0].Groups[1].Value
116+
$mainBranch = $(git remote show origin | Select-String 'HEAD branch: (.*)').Matches[0].Groups[1].Value
115117
}
116118
finally
117119
{
@@ -125,9 +127,9 @@ if ("$Tag" -eq "")
125127

126128
# Get tags for a repo without cloning.
127129
[string[]]$tags = $(git ls-remote --refs --tags $url)
128-
$tags = $tags | ForEach-Object { ($_ -split "\s+")[1] -replace '^refs/tags/', '' }
130+
$tags = $tags | ForEach-Object { ($_ -split '\s+')[1] -replace '^refs/tags/', '' }
129131

130-
$headRef = ($(git ls-remote $url HEAD) -split "\s+")[0]
132+
$headRef = ($(git ls-remote $url HEAD) -split '\s+')[0]
131133
if ("$headRef" -eq '')
132134
{
133135
throw "Couldn't determine repository head (no ref returned by ls-remote HEAD"
@@ -155,18 +157,46 @@ if ("$Tag" -eq "")
155157

156158
Write-Host "Sorted tags: $tags"
157159
$latestTag = $tags[-1]
158-
$latestTagNice = ($latestTag -match "^[0-9]") ? "v$latestTag" : $latestTag
159160

160-
SetOutput "originalTag" $originalTag
161-
SetOutput "latestTag" $latestTag
162-
SetOutput "latestTagNice" $latestTagNice
163-
SetOutput "url" $url
164-
SetOutput "mainBranch" $mainBranch
161+
if (("$originalTag" -ne '') -and ("$latestTag" -ne '') -and ("$latestTag" -ne "$originalTag"))
162+
{
163+
do
164+
{
165+
# It's possible that the dependency was updated to a pre-release version manually in which case we don't want to
166+
# roll back, even though it's not the latest version matching the configured pattern.
167+
if ((GetComparableVersion $originalTag) -ge (GetComparableVersion $latestTag))
168+
{
169+
Write-Host "SemVer represented by the original tag '$originalTag' is newer than the latest tag '$latestTag'. Skipping update."
170+
$latestTag = $originalTag
171+
break
172+
}
173+
174+
# Verify that the latest tag actually points to a different commit. Otherwise, we don't need to update.
175+
$refs = $(git ls-remote --tags $url)
176+
$refOriginal = (($refs -match "refs/tags/$originalTag" ) -split '[ \t]') | Select-Object -First 1
177+
$refLatest = (($refs -match "refs/tags/$latestTag" ) -split '[ \t]') | Select-Object -First 1
178+
if ($refOriginal -eq $refLatest)
179+
{
180+
Write-Host "Latest tag '$latestTag' points to the same commit as the original tag '$originalTag'. Skipping update."
181+
$latestTag = $originalTag
182+
break
183+
}
184+
} while ($false)
185+
}
186+
187+
$latestTagNice = ($latestTag -match '^[0-9]') ? "v$latestTag" : $latestTag
188+
189+
SetOutput 'originalTag' $originalTag
190+
SetOutput 'latestTag' $latestTag
191+
SetOutput 'latestTagNice' $latestTagNice
192+
SetOutput 'url' $url
193+
SetOutput 'mainBranch' $mainBranch
165194

166195
if ("$originalTag" -eq "$latestTag")
167196
{
168197
return
169198
}
199+
170200
$Tag = $latestTag
171201
}
172202

updater/tests/sort-versions.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ RunTest "sort standard versions v2" {
2020
AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11', '5.5', 'v6.0') $sorted
2121
}
2222

23-
# TODO, currently doesn't respect (order) stuff like RC, Beta, etc.
24-
# RunTest "sort with pre-releases" {
25-
# $sorted = SortVersions @('3.0.0', '5.4.11', 'v1.2.3', '5.4.1', '5.4.11-rc.0')
26-
# AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11-rc.0', '5.4.11') $sorted
27-
# }
23+
# https://semver.org/#spec-item-11
24+
RunTest "sort with pre-releases" {
25+
$sorted = SortVersions @('1.0.0-rc.1', '1.0.0', '1.0.0-beta.11', '1.0.0-alpha.1', '1.0.0-beta', '1.0.0-alpha.beta', '1.0.0-alpha', '1.0.0-beta.2')
26+
AssertEqual @('1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-alpha.beta', '1.0.0-beta', '1.0.0-beta.2', '1.0.0-beta.11', '1.0.0-rc.1', '1.0.0') $sorted
27+
}

updater/tests/update-dependency.ps1

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ $repoUrl = 'https://github.com/getsentry/github-workflows'
2323
$currentVersion = (git -c 'versionsort.suffix=-' ls-remote --tags --sort='v:refname' $repoUrl `
2424
| Select-Object -Last 1 | Select-String -Pattern 'refs/tags/(.*)$').Matches.Groups[1].Value
2525

26-
RunTest "properties-file" {
26+
RunTest 'properties-file' {
2727
$testFile = "$testDir/test.properties"
28-
@("repo=$repoUrl", "version = none") | Out-File $testFile
28+
@("repo=$repoUrl", 'version = none') | Out-File $testFile
2929
UpdateDependency $testFile
3030
AssertEqual @("repo=$repoUrl", "version = $currentVersion") (Get-Content $testFile)
3131
}
3232

33-
RunTest "version pattern match" {
33+
RunTest 'version pattern match' {
3434
$testFile = "$testDir/test.properties"
3535
$repo = 'https://github.com/getsentry/sentry-cli'
36-
@("repo=$repo", "version=0") | Out-File $testFile
36+
@("repo=$repo", 'version=0') | Out-File $testFile
3737
UpdateDependency $testFile '^0\.'
38-
AssertEqual @("repo=$repo", "version=0.28.0") (Get-Content $testFile)
38+
AssertEqual @("repo=$repo", 'version=0.28.0') (Get-Content $testFile)
3939
}
4040

4141
function _testOutput([string[]] $output)
@@ -48,27 +48,27 @@ function _testOutput([string[]] $output)
4848
AssertContains $output 'mainBranch=master'
4949
}
5050

51-
RunTest "writes output" {
51+
RunTest 'writes output' {
5252
$testFile = "$testDir/test.properties"
5353
$repo = 'https://github.com/getsentry/sentry-cli'
54-
@("repo=$repo", "version=0") | Out-File $testFile
54+
@("repo=$repo", 'version=0') | Out-File $testFile
5555
$stdout = UpdateDependency $testFile '^0\.'
5656
_testOutput $stdout
5757
}
5858

59-
RunTest "writes to env:GITHUB_OUTPUT" {
59+
RunTest 'writes to env:GITHUB_OUTPUT' {
6060
$testFile = "$testDir/test.properties"
6161
$repo = 'https://github.com/getsentry/sentry-cli'
62-
@("repo=$repo", "version=0") | Out-File $testFile
62+
@("repo=$repo", 'version=0') | Out-File $testFile
6363
$outFile = "$testDir/outfile"
6464
New-Item $outFile -ItemType File | Out-Null
6565
try
6666
{
6767
$env:GITHUB_OUTPUT = $outFile
6868
$stdout = UpdateDependency $testFile '^0\.'
69-
Write-Host "Testing standard output"
69+
Write-Host 'Testing standard output'
7070
_testOutput $stdout
71-
Write-Host "Testing env:GITHUB_OUTPUT"
71+
Write-Host 'Testing env:GITHUB_OUTPUT'
7272
_testOutput (Get-Content $outFile)
7373
}
7474
finally
@@ -80,15 +80,23 @@ RunTest "writes to env:GITHUB_OUTPUT" {
8080
}
8181

8282
# Note: without custom sorting, this would have yielded 'v1.7.31_gradle_plugin'
83-
RunTest "version sorting must work properly" {
83+
RunTest 'version sorting must work properly' {
8484
$testFile = "$testDir/test.properties"
8585
$repo = 'https://github.com/getsentry/sentry-java'
86-
@("repo=$repo", "version=0") | Out-File $testFile
86+
@("repo=$repo", 'version=0') | Out-File $testFile
8787
UpdateDependency $testFile '^v?[123].*$'
88-
AssertEqual @("repo=$repo", "version=3.2.1") (Get-Content $testFile)
88+
AssertEqual @("repo=$repo", 'version=3.2.1') (Get-Content $testFile)
8989
}
9090

91-
RunTest "powershell-script" {
91+
RunTest 'will not update from a later release to an earlier release' {
92+
$testFile = "$testDir/test.properties"
93+
$repo = 'https://github.com/getsentry/sentry-java'
94+
@("repo=$repo", 'version=999.0.0-beta.1') | Out-File $testFile
95+
UpdateDependency $testFile
96+
AssertEqual @("repo=$repo", 'version=999.0.0-beta.1') (Get-Content $testFile)
97+
}
98+
99+
RunTest 'powershell-script' {
92100
$testFile = "$testDir/test.version"
93101
'' | Out-File $testFile
94102
$testScript = "$testDir/test.ps1"
@@ -109,7 +117,7 @@ switch ($action)
109117
AssertEqual $currentVersion (Get-Content $testFile)
110118
}
111119

112-
RunTest "bash-script" {
120+
RunTest 'bash-script' {
113121
$testFile = "$testDir/test.version"
114122
'' | Out-File $testFile
115123
$testScript = "$testDir/test.sh"
@@ -136,29 +144,29 @@ esac
136144
'@ | Out-File $testScript
137145
UpdateDependency $testScript
138146
AssertEqual $currentVersion (Get-Content $testFile)
139-
} -skipReason ($IsWindows ? "on Windows" : '')
147+
} -skipReason ($IsWindows ? 'on Windows' : '')
140148

141-
RunTest "powershell-script fails in get-version" {
149+
RunTest 'powershell-script fails in get-version' {
142150
$testScript = "$testDir/test.ps1"
143151
@'
144152
throw "Failure"
145153
'@ | Out-File $testScript
146154

147-
AssertFailsWith "get-version | output: Failure" { UpdateDependency $testScript }
155+
AssertFailsWith 'get-version | output: Failure' { UpdateDependency $testScript }
148156
}
149157

150-
RunTest "bash-script fails in get-version" {
158+
RunTest 'bash-script fails in get-version' {
151159
$testScript = "$testDir/test.sh"
152160
@'
153161
#!/usr/bin/env bash
154162
echo "Failure"
155163
exit 1
156164
'@ | Out-File $testScript
157165

158-
AssertFailsWith "get-version | output: Failure" { UpdateDependency $testScript }
159-
} -skipReason ($IsWindows ? "on Windows" : '')
166+
AssertFailsWith 'get-version | output: Failure' { UpdateDependency $testScript }
167+
} -skipReason ($IsWindows ? 'on Windows' : '')
160168

161-
RunTest "powershell-script fails in get-repo" {
169+
RunTest 'powershell-script fails in get-repo' {
162170
$testScript = "$testDir/test.ps1"
163171
@'
164172
param([string] $action, [string] $value)
@@ -168,10 +176,10 @@ if ($action -eq "get-repo")
168176
}
169177
'@ | Out-File $testScript
170178

171-
AssertFailsWith "get-repo | output: Failure" { UpdateDependency $testScript }
179+
AssertFailsWith 'get-repo | output: Failure' { UpdateDependency $testScript }
172180
}
173181

174-
RunTest "bash-script fails in get-repo" {
182+
RunTest 'bash-script fails in get-repo' {
175183
$testScript = "$testDir/test.sh"
176184
@'
177185
#!/usr/bin/env bash
@@ -186,10 +194,10 @@ get-repo)
186194
esac
187195
'@ | Out-File $testScript
188196

189-
AssertFailsWith "get-repo | output: Failure" { UpdateDependency $testScript }
190-
} -skipReason ($IsWindows ? "on Windows" : '')
197+
AssertFailsWith 'get-repo | output: Failure' { UpdateDependency $testScript }
198+
} -skipReason ($IsWindows ? 'on Windows' : '')
191199

192-
RunTest "powershell-script fails in set-version" {
200+
RunTest 'powershell-script fails in set-version' {
193201
$testScript = "$testDir/test.ps1"
194202
@'
195203
param([string] $action, [string] $value)
@@ -206,7 +214,7 @@ switch ($action)
206214
AssertFailsWith "set-version $currentVersion | output: Failure" { UpdateDependency $testScript }
207215
}
208216

209-
RunTest "bash-script fails in set-version" {
217+
RunTest 'bash-script fails in set-version' {
210218
$testScript = "$testDir/test.sh"
211219
@'
212220
#!/usr/bin/env bash
@@ -228,4 +236,4 @@ esac
228236
'@ | Out-File $testScript
229237

230238
AssertFailsWith "set-version $currentVersion | output: Failure" { UpdateDependency $testScript }
231-
} -skipReason ($IsWindows ? "on Windows" : '')
239+
} -skipReason ($IsWindows ? 'on Windows' : '')

0 commit comments

Comments
 (0)