From 4f3297d37953c9c1f174a1e14e86a5c07b0174df Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 3 Mar 2021 14:56:18 +0100 Subject: [PATCH] Fix #200: Add support for mirrorOf attribute --- pkg/cmd/util_dependencies.go | 19 ++++++++++++++----- pkg/platform/defaults.go | 19 ++++++++++++++----- pkg/util/maven/maven_project.go | 19 +++++++++++++++++++ pkg/util/maven/maven_settings.go | 4 +++- pkg/util/maven/maven_settings_test.go | 18 +++++++++++++++--- pkg/util/maven/maven_types.go | 11 +++++++++++ 6 files changed, 76 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/util_dependencies.go b/pkg/cmd/util_dependencies.go index 4db062e201..0882edf0b0 100644 --- a/pkg/cmd/util_dependencies.go +++ b/pkg/cmd/util_dependencies.go @@ -127,15 +127,24 @@ func getTransitiveDependencies( if len(repositories) > 0 { var repoList []maven.Repository + var mirrors []maven.Mirror for i, repo := range repositories { - repository := maven.NewRepository(repo) - if repository.ID == "" { - repository.ID = fmt.Sprintf("repository-%03d", i) + if strings.Contains(repo, "@mirrorOf=") { + mirror := maven.NewMirror(repo) + if mirror.ID == "" { + mirror.ID = fmt.Sprintf("mirror-%03d", i) + } + mirrors = append(mirrors, mirror) + } else { + repository := maven.NewRepository(repo) + if repository.ID == "" { + repository.ID = fmt.Sprintf("repository-%03d", i) + } + repoList = append(repoList, repository) } - repoList = append(repoList, repository) } - settings := maven.NewDefaultSettings(repoList) + settings := maven.NewDefaultSettings(repoList, mirrors) settingsData, err := util.EncodeXML(settings) if err != nil { return nil, err diff --git a/pkg/platform/defaults.go b/pkg/platform/defaults.go index 9da4e883e0..af8f310c87 100644 --- a/pkg/platform/defaults.go +++ b/pkg/platform/defaults.go @@ -212,17 +212,26 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration if p.Status.Build.Maven.Settings.ConfigMapKeyRef == nil && p.Status.Build.Maven.Settings.SecretKeyRef == nil { var repositories []maven.Repository + var mirrors []maven.Mirror for i, c := range p.Status.Configuration { if c.Type == "repository" { - repository := maven.NewRepository(c.Value) - if repository.ID == "" { - repository.ID = fmt.Sprintf("repository-%03d", i) + if strings.Contains(c.Value, "@mirrorOf=") { + mirror := maven.NewMirror(c.Value) + if mirror.ID == "" { + mirror.ID = fmt.Sprintf("mirror-%03d", i) + } + mirrors = append(mirrors, mirror) + } else { + repository := maven.NewRepository(c.Value) + if repository.ID == "" { + repository.ID = fmt.Sprintf("repository-%03d", i) + } + repositories = append(repositories, repository) } - repositories = append(repositories, repository) } } - settings := maven.NewDefaultSettings(repositories) + settings := maven.NewDefaultSettings(repositories, mirrors) err := createDefaultMavenSettingsConfigMap(ctx, c, p, settings) if err != nil { diff --git a/pkg/util/maven/maven_project.go b/pkg/util/maven/maven_project.go index 4de5225852..068065b193 100644 --- a/pkg/util/maven/maven_project.go +++ b/pkg/util/maven/maven_project.go @@ -197,3 +197,22 @@ func NewRepository(repo string) Repository { return r } + +func NewMirror(repo string) Mirror{ + m := Mirror{} + if idx := strings.Index(repo, "@"); idx != -1 { + m.URL = repo[:idx] + + for _, attribute := range strings.Split(repo[idx+1:], "@") { + switch { + case strings.HasPrefix(attribute, "mirrorOf="): + m.MirrorOf = attribute[9:] + case strings.HasPrefix(attribute, "id="): + m.ID = attribute[3:] + case strings.HasPrefix(attribute, "name="): + m.Name = attribute[5:] + } + } + } + return m +} diff --git a/pkg/util/maven/maven_settings.go b/pkg/util/maven/maven_settings.go index 4bdf28232c..206b3ed221 100644 --- a/pkg/util/maven/maven_settings.go +++ b/pkg/util/maven/maven_settings.go @@ -42,7 +42,7 @@ func NewSettings() Settings { } // NewDefaultSettings -- -func NewDefaultSettings(repositories []Repository) Settings { +func NewDefaultSettings(repositories []Repository, mirrors []Mirror) Settings { settings := NewSettings() var additionalRepos []Repository @@ -66,6 +66,8 @@ func NewDefaultSettings(repositories []Repository) Settings { }, } + settings.Mirrors = mirrors + return settings } diff --git a/pkg/util/maven/maven_settings_test.go b/pkg/util/maven/maven_settings_test.go index a4a1776a83..cebada9944 100644 --- a/pkg/util/maven/maven_settings_test.go +++ b/pkg/util/maven/maven_settings_test.go @@ -52,6 +52,7 @@ const expectedSettings = ` + ` const expectedDefaultSettings = ` @@ -94,6 +95,7 @@ const expectedDefaultSettings = ` + ` const expectedDefaultSettingsWithExtraRepo = ` @@ -160,6 +162,13 @@ const expectedDefaultSettingsWithExtraRepo = ` + + + foo + https://foo.bar.org/repo + * + + ` func TestSettingsGeneration(t *testing.T) { @@ -198,7 +207,7 @@ func TestSettingsGeneration(t *testing.T) { } func TestDefaultSettingsGeneration(t *testing.T) { - settings := NewDefaultSettings([]Repository{}) + settings := NewDefaultSettings([]Repository{}, []Mirror{}) content, err := util.EncodeXML(settings) @@ -213,7 +222,10 @@ func TestDefaultSettingsGenerationWithAdditionalRepo(t *testing.T) { NewRepository("https://repo1.maven.org/maven2@id=central"), NewRepository("https://foo.bar.org/repo@id=foo"), } - settings := NewDefaultSettings(repositories) + mirrors := []Mirror{ + NewMirror("https://foo.bar.org/repo@id=foo@mirrorOf=*"), + } + settings := NewDefaultSettings(repositories, mirrors) content, err := util.EncodeXML(settings) @@ -224,7 +236,7 @@ func TestDefaultSettingsGenerationWithAdditionalRepo(t *testing.T) { } func TestCreateSettingsConfigMap(t *testing.T) { - settings := NewDefaultSettings([]Repository{}) + settings := NewDefaultSettings([]Repository{}, []Mirror{}) configMap, err := CreateSettingsConfigMap("foo", "bar", settings) assert.Nil(t, err) diff --git a/pkg/util/maven/maven_types.go b/pkg/util/maven/maven_types.go index 44cbf71c79..3b7dba5f1f 100644 --- a/pkg/util/maven/maven_types.go +++ b/pkg/util/maven/maven_types.go @@ -41,6 +41,14 @@ type RepositoryPolicy struct { ChecksumPolicy string `xml:"checksumPolicy,omitempty"` } +// Mirror -- +type Mirror struct { + ID string `xml:"id"` + Name string `xml:"name,omitempty"` + URL string `xml:"url"` + MirrorOf string `xml:"mirrorOf"` +} + // Build -- type Build struct { DefaultGoal string `xml:"defaultGoal,omitempty"` @@ -157,6 +165,7 @@ type Settings struct { XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"` LocalRepository string `xml:"localRepository"` Profiles []Profile `xml:"profiles>profile,omitempty"` + Mirrors []Mirror `xml:"mirrors>mirror,omitempty"` } // MarshalBytes -- @@ -235,3 +244,5 @@ type PropertyActivation struct { Name string `xml:"name"` Value string `xml:"value"` } + +