Skip to content

Commit

Permalink
Fix apache#200: Add support for mirrorOf attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
bouskaJ committed Mar 3, 2021
1 parent 786db59 commit 4f3297d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
19 changes: 14 additions & 5 deletions pkg/cmd/util_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions pkg/platform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion pkg/util/maven/maven_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,6 +66,8 @@ func NewDefaultSettings(repositories []Repository) Settings {
},
}

settings.Mirrors = mirrors

return settings
}

Expand Down
18 changes: 15 additions & 3 deletions pkg/util/maven/maven_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?>
<pluginRepositories></pluginRepositories>
</profile>
</profiles>
<mirrors></mirrors>
</settings>`

const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -94,6 +95,7 @@ const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
</pluginRepositories>
</profile>
</profiles>
<mirrors></mirrors>
</settings>`

const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -160,6 +162,13 @@ const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-
</pluginRepositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>`

func TestSettingsGeneration(t *testing.T) {
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/maven/maven_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 --
Expand Down Expand Up @@ -235,3 +244,5 @@ type PropertyActivation struct {
Name string `xml:"name"`
Value string `xml:"value"`
}


0 comments on commit 4f3297d

Please sign in to comment.