-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Relax version requirements to allow patch version mismatch #4080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d393d27
d45ff14
873b782
2e6ad48
19da1e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package v1alpha1 | ||
|
|
||
| import "strings" | ||
|
|
||
| func IsVersionAllowed(resourceVersion, buildVersion string) bool { | ||
| if buildVersion == "dev" || resourceVersion == buildVersion || strings.HasPrefix(buildVersion, "canary-") { | ||
| return true | ||
| } | ||
|
|
||
| rv, ok := parseSemver(resourceVersion) | ||
| if !ok { | ||
| return false | ||
| } | ||
| bv, ok := parseSemver(buildVersion) | ||
| if !ok { | ||
| return false | ||
| } | ||
| return rv.major == bv.major && rv.minor == bv.minor | ||
| } | ||
|
|
||
| type semver struct { | ||
| major string | ||
| minor string | ||
| } | ||
|
|
||
| func parseSemver(v string) (p semver, ok bool) { | ||
| if v == "" { | ||
| return | ||
| } | ||
| p.major, v, ok = parseInt(v) | ||
| if !ok { | ||
| return p, false | ||
| } | ||
| if v == "" { | ||
| p.minor = "0" | ||
| return p, true | ||
| } | ||
| if v[0] != '.' { | ||
| return p, false | ||
| } | ||
| p.minor, v, ok = parseInt(v[1:]) | ||
| if !ok { | ||
| return p, false | ||
| } | ||
| if v == "" { | ||
| return p, true | ||
| } | ||
| if v[0] != '.' { | ||
| return p, false | ||
| } | ||
| if _, _, ok = parseInt(v[1:]); !ok { | ||
| return p, false | ||
| } | ||
| return p, true | ||
| } | ||
|
|
||
| func parseInt(v string) (t, rest string, ok bool) { | ||
| if v == "" { | ||
| return | ||
| } | ||
| if v[0] < '0' || '9' < v[0] { | ||
| return | ||
| } | ||
| i := 1 | ||
| for i < len(v) && '0' <= v[i] && v[i] <= '9' { | ||
| i++ | ||
| } | ||
| if v[0] == '0' && i != 1 { | ||
| return | ||
| } | ||
| return v[:i], v[i:], true | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||
| package v1alpha1_test | ||||
|
|
||||
| import ( | ||||
| "testing" | ||||
|
|
||||
| "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" | ||||
| "github.com/stretchr/testify/assert" | ||||
| ) | ||||
|
|
||||
| func TestIsVersionAllowed(t *testing.T) { | ||||
| t.Parallel() | ||||
| tt := map[string]struct { | ||||
| resourceVersion string | ||||
| buildVersion string | ||||
| want bool | ||||
| }{ | ||||
| "dev should always be allowed": { | ||||
| resourceVersion: "0.11.0", | ||||
| buildVersion: "dev", | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need to accept
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I thought this one was covered by the exact match, but I guess I can add |
||||
| want: true, | ||||
| }, | ||||
| "resourceVersion is not semver": { | ||||
| resourceVersion: "dev", | ||||
| buildVersion: "0.11.0", | ||||
| want: false, | ||||
| }, | ||||
| "buildVersion is not semver": { | ||||
| resourceVersion: "0.11.0", | ||||
| buildVersion: "NA", | ||||
| want: false, | ||||
| }, | ||||
| "major version mismatch": { | ||||
| resourceVersion: "0.11.0", | ||||
| buildVersion: "1.11.0", | ||||
| want: false, | ||||
| }, | ||||
| "minor version mismatch": { | ||||
| resourceVersion: "0.11.0", | ||||
| buildVersion: "0.10.0", | ||||
| want: false, | ||||
| }, | ||||
| "patch version mismatch": { | ||||
| resourceVersion: "0.11.1", | ||||
| buildVersion: "0.11.0", | ||||
| want: true, | ||||
| }, | ||||
| "arbitrary version match": { | ||||
| resourceVersion: "abc", | ||||
| buildVersion: "abc", | ||||
| want: true, | ||||
| }, | ||||
| } | ||||
|
|
||||
| for name, tc := range tt { | ||||
| t.Run(name, func(t *testing.T) { | ||||
| got := v1alpha1.IsVersionAllowed(tc.resourceVersion, tc.buildVersion) | ||||
| assert.Equal(t, tc.want, got) | ||||
| }) | ||||
| } | ||||
| } | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding a comment here to clarify that patch version differences are intentionally ignored.