Skip to content

Commit 4a2197b

Browse files
vaindclaude
andcommitted
refactor: clean up GitHub release filtering code
Simplify the implementation with: - Consolidated URL validation with single regex match - Cleaner variable assignment using tuple unpacking - Simplified array handling by wrapping API result in @() - Removed unnecessary null/single object checks - More concise comments and clearer logic flow 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1510bc2 commit 4a2197b

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

updater/scripts/update-dependency.ps1

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -195,67 +195,46 @@ if ("$Tag" -eq '')
195195
{
196196
Write-Host "Filtering tags by GitHub release title pattern '$GhTitlePattern'"
197197

198-
# Check if URL is a GitHub URL first
199-
if ($url -notmatch 'github\.com')
198+
# Parse GitHub repo owner/name from URL
199+
if ($url -notmatch 'github\.com[:/]([^/]+)/([^/]+?)(?:\.git)?$')
200200
{
201201
throw "Could not parse GitHub owner/repo from URL: $url"
202202
}
203203

204-
# Parse GitHub repo owner/name from URL
205-
if ($url -match 'github\.com[:/]([^/]+)/([^/]+?)(?:\.git)?$')
204+
$owner, $repo = $Matches[1], $Matches[2]
205+
206+
try
206207
{
207-
$owner = $Matches[1]
208-
$repo = $Matches[2]
208+
# Fetch releases from GitHub API
209+
$releases = @(gh api "repos/$owner/$repo/releases" --jq '.[] | {tag_name: .tag_name, name: .name}' | ConvertFrom-Json)
209210

210-
try
211+
# Find tags that have matching release titles
212+
$validTags = @{}
213+
foreach ($release in $releases)
211214
{
212-
# Fetch releases from GitHub API (first 30 releases should be sufficient for most cases)
213-
$releases = gh api "repos/$owner/$repo/releases" --jq '.[] | {tag_name: .tag_name, name: .name}' | ConvertFrom-Json
214-
215-
# Handle case where releases might be null or single object
216-
if ($null -eq $releases)
215+
if ($release.name -match $GhTitlePattern)
217216
{
218-
$releases = @()
219-
}
220-
elseif ($releases -isnot [array])
221-
{
222-
$releases = @($releases)
223-
}
224-
225-
# Create a hashtable of tag->title mappings for releases that match the pattern
226-
$validTags = @{}
227-
foreach ($release in $releases)
228-
{
229-
if ($release.name -match $GhTitlePattern)
230-
{
231-
$validTags[$release.tag_name] = $true
232-
}
217+
$validTags[$release.tag_name] = $true
233218
}
219+
}
234220

235-
# Filter tags to only include those with matching release titles
236-
$originalTagCount = $tags.Length
237-
$tags = @($tags | Where-Object { $validTags.ContainsKey($_) })
238-
Write-Host "GitHub release title filtering: $originalTagCount -> $($tags.Count) tags"
221+
# Filter tags to only include those with matching release titles
222+
$originalTagCount = $tags.Length
223+
$tags = @($tags | Where-Object { $validTags.ContainsKey($_) })
224+
Write-Host "GitHub release title filtering: $originalTagCount -> $($tags.Count) tags"
239225

240-
if ($tags.Count -le 0)
241-
{
242-
throw "Found no tags with GitHub releases matching title pattern '$GhTitlePattern'"
243-
}
244-
}
245-
catch
226+
if ($tags.Count -eq 0)
246227
{
247-
# If it's our specific "no matching tags" error, re-throw it
248-
if ($_.Exception.Message -like "*Found no tags with GitHub releases matching title pattern*")
249-
{
250-
throw
251-
}
252-
# Otherwise, wrap it as a GitHub API error
253-
throw "Failed to fetch GitHub releases for $owner/$repo`: $($_.Exception.Message)"
228+
throw "Found no tags with GitHub releases matching title pattern '$GhTitlePattern'"
254229
}
255230
}
256-
else
231+
catch
257232
{
258-
throw "Could not parse GitHub owner/repo from URL: $url"
233+
if ($_.Exception.Message -like "*Found no tags with GitHub releases matching title pattern*")
234+
{
235+
throw
236+
}
237+
throw "Failed to fetch GitHub releases for $owner/$repo`: $($_.Exception.Message)"
259238
}
260239
}
261240

0 commit comments

Comments
 (0)