Skip to content

Commit

Permalink
fix pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Nov 12, 2021
1 parent 3ebf7a6 commit 6cb07eb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 24 deletions.
7 changes: 5 additions & 2 deletions example/.git-ops-update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ policies:
pattern: '^(?P<all>.*)$'
extracts:
- value: '<all>'
semver: {}
semver:
pinMajor: yes
pinMinor: yes
ubuntu:
pattern: '^(?P<year>\d+)\.(?P<month>\d+)$'
extracts:
- value: '<year>'
numeric: {}
numeric:
pin: yes
- value: '<month>'
numeric: {}
15 changes: 12 additions & 3 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,20 @@ func LoadGitOpsUpdaterConfig(yaml []byte) (*GitOpsUpdaterConfig, error) {
extracts := []Extract{}
for ei, e := range p.Extracts {
if e.Lexicographic != nil {
extracts = append(extracts, Extract{Value: e.Value, Strategy: LexicographicExtractStrategy{}})
extracts = append(extracts, Extract{Value: e.Value, Strategy: LexicographicExtractStrategy{
Pin: e.Lexicographic.Pin,
}})
} else if e.Numeric != nil {
extracts = append(extracts, Extract{Value: e.Value, Strategy: NumericExtractStrategy{}})
extracts = append(extracts, Extract{Value: e.Value, Strategy: NumericExtractStrategy{
Pin: e.Numeric.Pin,
}})
} else if e.Semver != nil {
extracts = append(extracts, Extract{Value: e.Value, Strategy: SemverExtractStrategy{}})
extracts = append(extracts, Extract{Value: e.Value, Strategy: SemverExtractStrategy{
PinMajor: e.Semver.PinMajor,
PinMinor: e.Semver.PinMinor,
PinPatch: e.Semver.PinPatch,
AllowPrereleases: e.Semver.AllowPrereleases,
}})
} else {
return nil, fmt.Errorf("policy %s strategy %d is invalid", pn, ei)
}
Expand Down
33 changes: 24 additions & 9 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ policies:
pattern: '^(?P<all>.*)$'
extracts:
- value: '<all>'
lexicographic: {}
lexicographic:
pin: yes
numeric:
pattern: '^(?P<all>.*)$'
extracts:
- value: '<all>'
numeric: {}
numeric:
pin: yes
semver:
pattern: '^(?P<all>.*)$'
extracts:
- value: '<all>'
semver: {}
semver:
pinMajor: yes
pinMinor: yes
pinPatch: yes
allowPrereleases: yes
`

c1, err := LoadGitOpsUpdaterConfig([]byte(yaml))
Expand Down Expand Up @@ -79,26 +85,35 @@ policies:
Pattern: regexp.MustCompile(`^(?P<all>.*)$`),
Extracts: []Extract{
{
Value: "<all>",
Strategy: LexicographicExtractStrategy{},
Value: "<all>",
Strategy: LexicographicExtractStrategy{
Pin: true,
},
},
},
},
"numeric": {
Pattern: regexp.MustCompile(`^(?P<all>.*)$`),
Extracts: []Extract{
{
Value: "<all>",
Strategy: NumericExtractStrategy{},
Value: "<all>",
Strategy: NumericExtractStrategy{
Pin: true,
},
},
},
},
"semver": {
Pattern: regexp.MustCompile(`^(?P<all>.*)$`),
Extracts: []Extract{
{
Value: "<all>",
Strategy: SemverExtractStrategy{},
Value: "<all>",
Strategy: SemverExtractStrategy{
PinMajor: true,
PinMinor: true,
PinPatch: true,
AllowPrereleases: true,
},
},
},
},
Expand Down
9 changes: 7 additions & 2 deletions internal/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ type ExtractStrategy interface {
}

type LexicographicExtractStrategy struct {
Pin bool
}

type NumericExtractStrategy struct {
Pin bool
}

type SemverExtractStrategy struct {
Expand Down Expand Up @@ -160,7 +162,7 @@ func (str LexicographicExtractStrategy) Compare(v1 string, v2 string) int {
}

func (str LexicographicExtractStrategy) IsCompatible(v1 string, v2 string) bool {
return true
return !str.Pin || v1 == v2
}

func (str NumericExtractStrategy) Compare(v1 string, v2 string) int {
Expand All @@ -185,7 +187,7 @@ func (str NumericExtractStrategy) Compare(v1 string, v2 string) int {
}

func (str NumericExtractStrategy) IsCompatible(v1 string, v2 string) bool {
return true
return !str.Pin || v1 == v2
}

func (str SemverExtractStrategy) Compare(v1 string, v2 string) int {
Expand All @@ -209,5 +211,8 @@ func (str SemverExtractStrategy) IsCompatible(v1 string, v2 string) bool {
if str.PinPatch && (v1sv.Major != v2sv.Major || v1sv.Minor != v2sv.Minor || v1sv.Patch != v2sv.Patch) {
return false
}
if !str.AllowPrereleases && len(v2sv.Pre) > 0 {
return false
}
return true
}
33 changes: 25 additions & 8 deletions internal/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ func TestLexicographicSortStrategyCompare(t *testing.T) {
assert.Equal(t, 1, str.Compare("2", "10"))
assert.Equal(t, 1, str.Compare("10", "1"))
assert.Equal(t, -1, str.Compare("10", "2"))
}

func TestLexicographicSortStrategyIsCompatible(t *testing.T) {
assert.Equal(t, true, LexicographicExtractStrategy{}.IsCompatible("1", "1"))
assert.Equal(t, true, LexicographicExtractStrategy{}.IsCompatible("1", "2"))
assert.Equal(t, true, LexicographicExtractStrategy{Pin: true}.IsCompatible("1", "1"))
assert.Equal(t, false, LexicographicExtractStrategy{Pin: true}.IsCompatible("1", "2"))
}

func TestNumericSortStrategyCompare(t *testing.T) {
Expand All @@ -156,6 +162,13 @@ func TestNumericSortStrategyCompare(t *testing.T) {
assert.Equal(t, 1, str.Compare("10", "2"))
}

func TestNumericSortStrategyIsCompatible(t *testing.T) {
assert.Equal(t, true, NumericExtractStrategy{}.IsCompatible("1", "1"))
assert.Equal(t, true, NumericExtractStrategy{}.IsCompatible("1", "2"))
assert.Equal(t, true, NumericExtractStrategy{Pin: true}.IsCompatible("1", "1"))
assert.Equal(t, false, NumericExtractStrategy{Pin: true}.IsCompatible("1", "2"))
}

func TestSemverSortStrategyCompare(t *testing.T) {
str := SemverExtractStrategy{}

Expand All @@ -182,13 +195,17 @@ func TestSemverSortStrategyIsCompatible(t *testing.T) {
assert.Equal(t, true, SemverExtractStrategy{PinMajor: true}.IsCompatible("1.0.0", "1.1.0"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true}.IsCompatible("1.0.0", "2.0.0"))

assert.Equal(t, true, SemverExtractStrategy{PinMinor: true}.IsCompatible("1.0.0", "1.0.0"))
assert.Equal(t, true, SemverExtractStrategy{PinMinor: true}.IsCompatible("1.0.0", "1.0.1"))
assert.Equal(t, false, SemverExtractStrategy{PinMinor: true}.IsCompatible("1.0.0", "1.1.0"))
assert.Equal(t, false, SemverExtractStrategy{PinMinor: true}.IsCompatible("1.0.0", "2.0.0"))
assert.Equal(t, true, SemverExtractStrategy{PinMajor: true, PinMinor: true}.IsCompatible("1.0.0", "1.0.0"))
assert.Equal(t, true, SemverExtractStrategy{PinMajor: true, PinMinor: true}.IsCompatible("1.0.0", "1.0.1"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true, PinMinor: true}.IsCompatible("1.0.0", "1.1.0"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true, PinMinor: true}.IsCompatible("1.0.0", "2.0.0"))

assert.Equal(t, true, SemverExtractStrategy{PinMajor: true, PinMinor: true, PinPatch: true}.IsCompatible("1.0.0", "1.0.0"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true, PinMinor: true, PinPatch: true}.IsCompatible("1.0.0", "1.0.1"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true, PinMinor: true, PinPatch: true}.IsCompatible("1.0.0", "1.1.0"))
assert.Equal(t, false, SemverExtractStrategy{PinMajor: true, PinMinor: true, PinPatch: true}.IsCompatible("1.0.0", "2.0.0"))

assert.Equal(t, true, SemverExtractStrategy{PinPatch: true}.IsCompatible("1.0.0", "1.0.0"))
assert.Equal(t, false, SemverExtractStrategy{PinPatch: true}.IsCompatible("1.0.0", "1.0.1"))
assert.Equal(t, false, SemverExtractStrategy{PinPatch: true}.IsCompatible("1.0.0", "1.1.0"))
assert.Equal(t, false, SemverExtractStrategy{PinPatch: true}.IsCompatible("1.0.0", "2.0.0"))
assert.Equal(t, false, SemverExtractStrategy{AllowPrereleases: false}.IsCompatible("1.0.0", "2.0.0-pre"))
assert.Equal(t, true, SemverExtractStrategy{AllowPrereleases: false}.IsCompatible("1.0.0", "2.0.0"))
assert.Equal(t, true, SemverExtractStrategy{AllowPrereleases: true}.IsCompatible("1.0.0", "2.0.0-pre"))
}

0 comments on commit 6cb07eb

Please sign in to comment.