Skip to content

Commit

Permalink
Fix debricked config regex list (#221)
Browse files Browse the repository at this point in the history
* Change test to correct format

* Change regex format and fix test
  • Loading branch information
filip-debricked authored Apr 5, 2024
1 parent 1c93113 commit 816fcce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
28 changes: 17 additions & 11 deletions internal/upload/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ func (boolOrString *boolOrString) MarshalJSON() ([]byte, error) {
}

type purlConfig struct {
PackageURL string `json:"pURL" yaml:"pURL"`
Version boolOrString `json:"version" yaml:"version"` // Either false or version string
FileRegex string `json:"fileRegex" yaml:"fileRegex"`
PackageURL string `json:"pURL" yaml:"pURL"`
Version boolOrString `json:"version" yaml:"version"` // Either false or version string
FileRegexes []string `json:"fileRegexes" yaml:"fileRegexes"`
}

type DebrickedConfig struct {
Expand Down Expand Up @@ -332,8 +332,14 @@ func printSuccessfulUpload(f string) {
fmt.Printf("Successfully uploaded: %s\n", color.YellowString(f))
}

type pURLConfigYAML struct {
PackageURL string `yaml:"pURL"`
Version *string `yaml:"version"`
FileRegexes []string `yaml:"fileRegexes"`
}

type DebrickedConfigYAML struct {
Overrides []map[string]string `yaml:"overrides"`
Overrides []pURLConfigYAML `yaml:"overrides"`
}

func GetDebrickedConfig(path string) DebrickedConfig {
Expand All @@ -354,16 +360,16 @@ func GetDebrickedConfig(path string) DebrickedConfig {
for _, entry := range yamlConfig.Overrides {
var version string
var exist bool
pURL := entry["pURL"]
fileRegex := entry["fileRegex"]
if _, exist = entry["version"]; exist {
version = entry["version"]
exist = true
} else {
pURL := entry.PackageURL
fileRegexes := entry.FileRegexes
if entry.Version == nil {
version = ""
exist = false
} else {
version = *entry.Version
exist = true
}
overrides = append(overrides, purlConfig{PackageURL: pURL, Version: boolOrString{Version: version, HasVersion: exist}, FileRegex: fileRegex})
overrides = append(overrides, purlConfig{PackageURL: pURL, Version: boolOrString{Version: version, HasVersion: exist}, FileRegexes: fileRegexes})
}

return DebrickedConfig{
Expand Down
26 changes: 13 additions & 13 deletions internal/upload/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ func TestGetDebrickedConfig(t *testing.T) {
expectedJSON, err := json.Marshal(DebrickedConfig{
Overrides: []purlConfig{
{
PackageURL: "pkg:npm/lodash",
Version: boolOrString{Version: "1.0.0", HasVersion: true},
FileRegex: ".*/lodash/.*",
PackageURL: "pkg:npm/lodash",
Version: boolOrString{Version: "1.0.0", HasVersion: true},
FileRegexes: []string{".*/lodash/.*"},
},
{
PackageURL: "pkg:maven/org.openjfx/javafx-base",
Version: boolOrString{Version: "", HasVersion: false},
FileRegex: "subpath/org.openjfx/.*",
PackageURL: "pkg:maven/org.openjfx/javafx-base",
Version: boolOrString{Version: "", HasVersion: false},
FileRegexes: []string{"subpath/org.openjfx/.*"},
},
},
})
Expand Down Expand Up @@ -180,18 +180,18 @@ func TestMarshalJSONDebrickedConfig(t *testing.T) {
config, err := json.Marshal(DebrickedConfig{
Overrides: []purlConfig{
{
PackageURL: "pkg:npm/lodash",
Version: boolOrString{Version: "1.0.0", HasVersion: true},
FileRegex: ".*/lodash/.*",
PackageURL: "pkg:npm/lodash",
Version: boolOrString{Version: "1.0.0", HasVersion: true},
FileRegexes: []string{".*/lodash/.*"},
},
{
PackageURL: "pkg:maven/org.openjfx/javafx-base",
Version: boolOrString{Version: "", HasVersion: false},
FileRegex: "subpath/org.openjfx/.*",
PackageURL: "pkg:maven/org.openjfx/javafx-base",
Version: boolOrString{Version: "", HasVersion: false},
FileRegexes: []string{"subpath/org.openjfx/.*"},
},
},
})
expectedJSON := "{\"overrides\":[{\"pURL\":\"pkg:npm/lodash\",\"version\":\"1.0.0\",\"fileRegex\":\".*/lodash/.*\"},{\"pURL\":\"pkg:maven/org.openjfx/javafx-base\",\"version\":false,\"fileRegex\":\"subpath/org.openjfx/.*\"}]}"
expectedJSON := "{\"overrides\":[{\"pURL\":\"pkg:npm/lodash\",\"version\":\"1.0.0\",\"fileRegexes\":[\".*/lodash/.*\"]},{\"pURL\":\"pkg:maven/org.openjfx/javafx-base\",\"version\":false,\"fileRegexes\":[\"subpath/org.openjfx/.*\"]}]}"
assert.Nil(t, err)
assert.Equal(t, []byte(expectedJSON), config)
}
6 changes: 4 additions & 2 deletions internal/upload/testdata/debricked-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
overrides:
- pURL: "pkg:npm/lodash"
version: "1.0.0" # optional: if left out, we will decide version
fileRegex: ".*/lodash/.*" # PCRE2
fileRegexes:
- ".*/lodash/.*" # PCRE2
- pURL: "pkg:maven/org.openjfx/javafx-base"
fileRegex: "subpath/org.openjfx/.*"
fileRegexes:
- "subpath/org.openjfx/.*"

0 comments on commit 816fcce

Please sign in to comment.