Skip to content

Commit

Permalink
Add version parsing check skip malformed versions and avoid panic (#1469
Browse files Browse the repository at this point in the history
)

* Add version parsing check skip malformed versions and avoid panic

Signed-off-by: Anton Troshin <[email protected]>

* lint

Signed-off-by: Anton Troshin <[email protected]>

* do not return error on nil, skip bad versions

Signed-off-by: Anton Troshin <[email protected]>

* simplify condition to skip prerelease and versions with metadata
print warning on error and non-semver version tag

Signed-off-by: Anton Troshin <[email protected]>

---------

Signed-off-by: Anton Troshin <[email protected]>
  • Loading branch information
antontroshin authored Nov 22, 2024
1 parent 6d5e64d commit 25d9ece
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,18 @@ func GetLatestReleaseGithub(githubURL string) (string, error) {
latestVersion := defaultVersion

for _, release := range githubRepoReleases {
if !strings.Contains(release.TagName, "-rc") {
cur, _ := version.NewVersion(strings.TrimPrefix(release.TagName, "v"))
if cur.GreaterThan(latestVersion) {
latestVersion = cur
}
cur, err := version.NewVersion(strings.TrimPrefix(release.TagName, "v"))
if err != nil || cur == nil {
print.WarningStatusEvent(os.Stdout, "Malformed version %s, skipping", release.TagName)
continue
}
// Prerelease versions and versions with metadata are skipped.
if cur.Prerelease() != "" || cur.Metadata() != "" {
continue
}

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 25d9ece

Please sign in to comment.