Skip to content

Commit

Permalink
Add version parsing check skip malformed versions and avoid panic
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Troshin <anton@diagrid.io>
  • Loading branch information
antontroshin committed Nov 22, 2024
1 parent 6d5e64d commit a422d13
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ func GetLatestReleaseGithub(githubURL string) (string, error) {

for _, release := range githubRepoReleases {
if !strings.Contains(release.TagName, "-rc") {
cur, _ := version.NewVersion(strings.TrimPrefix(release.TagName, "v"))
cur, err := version.NewVersion(strings.TrimPrefix(release.TagName, "v"))
if err != nil {
if strings.HasPrefix(err.Error(), "Malformed version") {
continue
}
return "", err
}
// just a safety check to make sure we don't get a nil version.
// all errors should be handled above

Check failure on line 131 in pkg/version/version.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Comment should end in a period (godot)
if cur == nil {
return "", fmt.Errorf("failed to parse version %s, resulted in nil version", release.TagName)
}
if cur.GreaterThan(latestVersion) {
latestVersion = cur
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,52 @@ func TestGetVersionsGithub(t *testing.T) {
"no releases",
"",
},
{
"Malformed version no releases",
"/malformed_version_no_releases",
`[
{
"url": "https://api.github.com/repos/dapr/dapr/releases/186741665",
"html_url": "https://github.com/dapr/dapr/releases/tag/vedge",
"id": 186741665,
"tag_name": "vedge",
"target_commitish": "master",
"name": "Dapr Runtime vedge",
"draft": false,
"prerelease": false
}
] `,
"no releases",
"",
},
{
"Malformed version with latest",
"/malformed_version_with_latest",
`[
{
"url": "https://api.github.com/repos/dapr/dapr/releases/186741665",
"html_url": "https://github.com/dapr/dapr/releases/tag/vedge",
"id": 186741665,
"tag_name": "vedge",
"target_commitish": "master",
"name": "Dapr Runtime vedge",
"draft": false,
"prerelease": false
},
{
"url": "https://api.github.com/repos/dapr/dapr/releases/44766923",
"html_url": "https://github.com/dapr/dapr/releases/tag/v1.5.1",
"id": 44766923,
"tag_name": "v1.5.1",
"target_commitish": "master",
"name": "Dapr Runtime v1.5.1",
"draft": false,
"prerelease": false
}
] `,
"",
"1.5.1",
},
}
m := http.NewServeMux()
s := http.Server{Addr: ":12345", Handler: m, ReadHeaderTimeout: time.Duration(5) * time.Second}
Expand Down

0 comments on commit a422d13

Please sign in to comment.