Skip to content

Commit

Permalink
feat(deployed_version): regex templating (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav authored Jan 9, 2024
1 parent 22e7caf commit 249379b
Show file tree
Hide file tree
Showing 38 changed files with 343 additions and 248 deletions.
2 changes: 1 addition & 1 deletion config/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func testServiceURL(id string) *service.Service {
boolPtr(false),
nil, nil,
"version",
nil, "", nil,
nil, "", nil, nil,
"https://valid.release-argus.io/json",
&deployedver.LookupDefaults{}, &deployedver.LookupDefaults{}),
Dashboard: *service.NewDashboardOptions(
Expand Down
2 changes: 1 addition & 1 deletion service/deployed_version/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func testLookup() (lookup *Lookup) {
nil, "", boolPtr(true),
&opt.OptionsDefaults{},
opt.NewDefaults("", boolPtr(true))),
"",
"", nil,
&svcstatus.Status{
ServiceID: stringPtr("test")},
"https://invalid.release-argus.io/json",
Expand Down
9 changes: 3 additions & 6 deletions service/deployed_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,17 @@ func (l *Lookup) query(logFrom *util.LogFrom) (string, error) {
// If a regex is provided, use it to extract the version.
if l.Regex != "" {
re := regexp.MustCompile(l.Regex)
texts := re.FindStringSubmatch(version)
index := 1
texts := re.FindAllStringSubmatch(version, 1)

if len(texts) == 0 {
err := fmt.Errorf("regex %q didn't find a match on %q",
l.Regex, version)
jLog.Warn(err, *logFrom, true)
return "", err
} else if len(texts) == 1 {
// no capture group in regex
index = 0
}

version = texts[index]
regexMatches := texts[0]
version = util.RegexTemplate(regexMatches, l.RegexTemplate)
}

// If semantic versioning is enabled, check that the version is in the correct format.
Expand Down
10 changes: 10 additions & 0 deletions service/deployed_version/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestLookup_Query(t *testing.T) {
headers []Header
json string
regex string
regexTemplate *string
errRegex string
wantVersion string
}{
Expand Down Expand Up @@ -112,6 +113,14 @@ func TestLookup_Query(t *testing.T) {
url: "https://release-argus.io",
regex: "[0-9]{4}",
},
"regex with template": {
noSemanticVersioning: true,
errRegex: "^$",
url: "https://release-argus.io",
regex: "([0-9]+) (The) (Argus) (Developers)",
regexTemplate: stringPtr("$2 $1 $4, $3"),
wantVersion: "The [0-9]+ Developers, Argus",
},
"failing regex": {
errRegex: "regex .* didn't find a match on",
url: "https://release-argus.io",
Expand Down Expand Up @@ -165,6 +174,7 @@ func TestLookup_Query(t *testing.T) {
dvl.Headers = tc.headers
dvl.JSON = tc.json
dvl.Regex = tc.regex
dvl.RegexTemplate = tc.regexTemplate
*dvl.Options.SemanticVersioning = !tc.noSemanticVersioning

// WHEN Query is called on it
Expand Down
8 changes: 7 additions & 1 deletion service/deployed_version/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (l *Lookup) applyOverrides(
headers *string,
json *string,
regex *string,
regexTemplate *string,
semanticVersioning *string,
url *string,
serviceID *string,
Expand All @@ -55,6 +56,7 @@ func (l *Lookup) applyOverrides(
useJSON := util.PtrValueOrValue(json, l.JSON)
// regex
useRegex := util.PtrValueOrValue(regex, l.Regex)
useRegexTemplate := util.PtrValueOrValue(regexTemplate, util.DefaultIfNil(l.RegexTemplate))
// semantic_versioning
var useSemanticVersioning *bool
if semanticVersioning != nil {
Expand All @@ -78,6 +80,7 @@ func (l *Lookup) applyOverrides(
useJSON,
options,
useRegex,
&useRegexTemplate,
&svcstatus.Status{},
useURL,
l.Defaults,
Expand All @@ -101,6 +104,7 @@ func (l *Lookup) Refresh(
headers *string,
json *string,
regex *string,
regexTemplate *string,
semanticVersioning *string,
url *string,
) (version string, announceUpdate bool, err error) {
Expand All @@ -114,6 +118,7 @@ func (l *Lookup) Refresh(
headers,
json,
regex,
regexTemplate,
semanticVersioning,
url,
&serviceID,
Expand All @@ -134,7 +139,8 @@ func (l *Lookup) Refresh(
l.Options.GetSemanticVersioning() != lookup.Options.GetSemanticVersioning() ||
url != nil ||
json != nil ||
regex != nil
regex != nil ||
regexTemplate != nil

// Query the lookup.
version, err = lookup.Query(!overrides, &logFrom)
Expand Down
40 changes: 32 additions & 8 deletions service/deployed_version/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ func TestLookup_ApplyOverrides(t *testing.T) {
headers *string
json *string
regex *string
regexTemplate *string
semanticVersioning *string
url *string
previous *Lookup
previousRegex string
errRegex string
want *Lookup
}{
Expand All @@ -174,7 +176,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
nil, nil,
test.JSON,
test.Options,
"",
"", nil,
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -190,7 +192,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
nil,
test.JSON,
test.Options,
"",
"", nil,
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -207,7 +209,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
{Key: "bosh", Value: "bosh"}},
"version",
test.Options,
"",
"", nil,
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -221,7 +223,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
nil, nil,
"bish", // JSON
test.Options,
"",
"", nil,
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -235,7 +237,23 @@ func TestLookup_ApplyOverrides(t *testing.T) {
nil, nil,
"version",
test.Options,
"bish", // RegEx
"bish", nil, // RegEx
&svcstatus.Status{},
test.URL,
nil, nil),
},
"regex template": {
regexTemplate: stringPtr("$1.$4"),

previous: testLookup(),
previousRegex: "([0-9]+)",
want: New(
test.AllowInvalidCerts,
nil, nil,
"version",
test.Options,
"([0-9]+)",
stringPtr("$1.$4"), // RegEx Template
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -251,7 +269,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
opt.New(
boolPtr(false), "", nil,
nil, nil),
"",
"", nil,
&svcstatus.Status{},
test.URL,
nil, nil),
Expand All @@ -265,7 +283,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
nil, nil,
test.JSON,
test.Options,
"",
"", nil,
&svcstatus.Status{},
"https://valid.release-argus.io/json", // URL
nil, nil),
Expand All @@ -290,6 +308,9 @@ func TestLookup_ApplyOverrides(t *testing.T) {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if tc.previousRegex != "" {
tc.previous.Regex = tc.previousRegex
}

// WHEN we call applyOverrides
got, err := tc.previous.applyOverrides(
Expand All @@ -298,6 +319,7 @@ func TestLookup_ApplyOverrides(t *testing.T) {
tc.headers,
tc.json,
tc.regex,
tc.regexTemplate,
tc.semanticVersioning,
tc.url,
&name,
Expand Down Expand Up @@ -340,6 +362,7 @@ func TestLookup_Refresh(t *testing.T) {
headers *string
json *string
regex *string
regexTemplate *string
semanticVersioning *string
url *string
lookup *Lookup
Expand Down Expand Up @@ -377,7 +400,7 @@ func TestLookup_Refresh(t *testing.T) {
nil, nil,
test.JSON,
test.Options,
"",
"", nil,
&svcstatus.Status{},
test.URL,
test.Defaults,
Expand Down Expand Up @@ -417,6 +440,7 @@ func TestLookup_Refresh(t *testing.T) {
tc.headers,
tc.json,
tc.regex,
tc.regexTemplate,
tc.semanticVersioning,
tc.url)

Expand Down
31 changes: 17 additions & 14 deletions service/deployed_version/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ func NewDefaults(

// Lookup the deployed version of the service.
type Lookup struct {
URL string `yaml:"url,omitempty" json:"url,omitempty"` // URL to query.
LookupBase `yaml:",inline" json:",inline"`
BasicAuth *BasicAuth `yaml:"basic_auth,omitempty" json:"basic_auth,omitempty"` // Basic Auth for the HTTP(S) request.
Headers []Header `yaml:"headers,omitempty" json:"headers,omitempty"` // Headers for the HTTP(S) request.
JSON string `yaml:"json,omitempty" json:"json,omitempty"` // JSON key to use e.g. version_current.
Regex string `yaml:"regex,omitempty" json:"regex,omitempty"` // Regex to get the DeployedVersion
URL string `yaml:"url,omitempty" json:"url,omitempty"` // URL to query.
LookupBase `yaml:",inline" json:",inline"`
BasicAuth *BasicAuth `yaml:"basic_auth,omitempty" json:"basic_auth,omitempty"` // Basic Auth for the HTTP(S) request.
Headers []Header `yaml:"headers,omitempty" json:"headers,omitempty"` // Headers for the HTTP(S) request.
JSON string `yaml:"json,omitempty" json:"json,omitempty"` // JSON key to use e.g. version_current.
Regex string `yaml:"regex,omitempty" json:"regex,omitempty"` // RegEx to get the DeployedVersion
RegexTemplate *string `yaml:"regex_template,omitempty" json:"regex_template,omitempty"` // RegEx template to apply to the RegEx match.

Options *opt.Options `yaml:"-" json:"-"` // Options for the lookups
Status *svcstatus.Status `yaml:"-" json:"-"` // Service Status
Expand All @@ -67,6 +68,7 @@ func New(
json string,
options *opt.Options,
regex string,
regexTemplate *string,
status *svcstatus.Status,
url string,
defaults *LookupDefaults,
Expand All @@ -75,14 +77,15 @@ func New(
lookup = &Lookup{
LookupBase: LookupBase{
AllowInvalidCerts: allowInvalidCerts},
BasicAuth: basicAuth,
JSON: json,
Options: options,
Regex: regex,
Status: status,
URL: url,
Defaults: defaults,
HardDefaults: hardDefaults}
BasicAuth: basicAuth,
JSON: json,
Options: options,
Regex: regex,
RegexTemplate: regexTemplate,
Status: status,
URL: url,
Defaults: defaults,
HardDefaults: hardDefaults}
if headers != nil {
lookup.Headers = *headers
}
Expand Down
15 changes: 8 additions & 7 deletions service/deployed_version/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestLookup_String(t *testing.T) {
opt.New(
boolPtr(true), "9m", boolPtr(false),
nil, nil),
"v([0-9.]+)",
"v([0-9.]+)", stringPtr("$1"),
&svcstatus.Status{},
"https://example.com",
NewDefaults(
Expand All @@ -68,14 +68,15 @@ headers:
- key: X-Another
value: val2
json: value.version
regex: v([0-9.]+)`,
regex: v([0-9.]+)
regex_template: $1`,
},
"quotes otherwise invalid yaml strings": {
lookup: New(
nil,
&BasicAuth{
Username: ">123", Password: "{pass}"},
nil, "", nil, "", &svcstatus.Status{}, "", nil, nil),
nil, "", nil, "", nil, &svcstatus.Status{}, "", nil, nil),
want: `
basic_auth:
username: '>123'
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestLookup_IsEqual(t *testing.T) {
opt.New(
nil, "", nil,
nil, nil),
"v([0-9.]+)",
"v([0-9.]+)", stringPtr("$1"),
&svcstatus.Status{},
"https://example.com",
NewDefaults(
Expand All @@ -166,7 +167,7 @@ func TestLookup_IsEqual(t *testing.T) {
opt.New(
nil, "", boolPtr(true),
nil, nil),
"v([0-9.]+)",
"v([0-9.]+)", stringPtr("$1"),
&svcstatus.Status{},
"https://example.com",
NewDefaults(
Expand All @@ -187,7 +188,7 @@ func TestLookup_IsEqual(t *testing.T) {
opt.New(
nil, "", boolPtr(true),
nil, nil),
"v([0-9.]+)",
"v([0-9.]+)", stringPtr("$1"),
&svcstatus.Status{},
"https://example.com",
NewDefaults(
Expand All @@ -205,7 +206,7 @@ func TestLookup_IsEqual(t *testing.T) {
opt.New(
nil, "", boolPtr(true),
nil, nil),
"v([0-9.]+)",
"v([0-9.]+)", stringPtr("$1"),
&svcstatus.Status{},
"https://example.com/other",
NewDefaults(
Expand Down
4 changes: 4 additions & 0 deletions service/deployed_version/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (l *Lookup) CheckValues(prefix string) (errs error) {
errs = fmt.Errorf("%s%s regex: %q <invalid>\\",
util.ErrorToString(errs), prefix, l.Regex)
}
// Remove the RegExTemplate if empty or no RegEx.
if l.Regex == "" || util.DefaultIfNil(l.RegexTemplate) == "" {
l.RegexTemplate = nil
}

if errs != nil {
errs = fmt.Errorf("%sdeployed_version:\\%w",
Expand Down
Loading

0 comments on commit 249379b

Please sign in to comment.