diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go index e33597a81f..a1ce547105 100644 --- a/pkg/builder/builder_steps.go +++ b/pkg/builder/builder_steps.go @@ -25,6 +25,10 @@ import ( "reflect" "strings" + "github.com/apache/camel-k/pkg/util/jitpack" + + "github.com/rs/xid" + "github.com/apache/camel-k/pkg/util/controller" "k8s.io/apimachinery/pkg/selection" @@ -184,7 +188,33 @@ func injectDependencies(ctx *Context) error { ctx.Maven.Project.AddEncodedDependencyGAV(gav) default: - return fmt.Errorf("unknown dependency type: %s", d) + if dep := jitpack.ToDependency(d); dep != nil { + ctx.Maven.Project.AddDependency(*dep) + + addRepo := true + for _, repo := range ctx.Maven.Project.Repositories { + if repo.URL == jitpack.RepoURL { + addRepo = false + break + } + } + if addRepo { + ctx.Maven.Project.Repositories = append(ctx.Maven.Project.Repositories, maven.Repository{ + ID: "jitpack.io-" + xid.New().String(), + URL: jitpack.RepoURL, + Releases: maven.RepositoryPolicy{ + Enabled: true, + ChecksumPolicy: "fail", + }, + Snapshots: maven.RepositoryPolicy{ + Enabled: true, + ChecksumPolicy: "fail", + }, + }) + } + } else { + return fmt.Errorf("unknown dependency type: %s", d) + } } } diff --git a/pkg/util/jitpack/jitpack.go b/pkg/util/jitpack/jitpack.go new file mode 100644 index 0000000000..4b079930e7 --- /dev/null +++ b/pkg/util/jitpack/jitpack.go @@ -0,0 +1,67 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package jitpack + +import ( + "strings" + + "github.com/apache/camel-k/pkg/util/maven" +) + +const RepoURL = "https://jitpack.io" +const LatestVersion = "master-SNAPSHOT" + +func ToDependency(dependencyID string) *maven.Dependency { + gav := "" + + switch { + case strings.HasPrefix(dependencyID, "github:"): + gav = strings.TrimPrefix(dependencyID, "github:") + gav = "com.github." + gav + case strings.HasPrefix(dependencyID, "gitlab:"): + gav = strings.TrimPrefix(dependencyID, "gitlab:") + gav = "com.gitlab." + gav + case strings.HasPrefix(dependencyID, "bitbucket:"): + gav = strings.TrimPrefix(dependencyID, "bitbucket:") + gav = "org.bitbucket." + gav + case strings.HasPrefix(dependencyID, "gitee:"): + gav = strings.TrimPrefix(dependencyID, "gitee:") + gav = "com.gitee." + gav + case strings.HasPrefix(dependencyID, "azure:"): + gav = strings.TrimPrefix(dependencyID, "azure:") + gav = "com.azure." + gav + } + + if gav == "" { + return nil + } + + gav = strings.Replace(gav, "/", ":", -1) + dep, err := maven.ParseGAV(gav) + if err != nil { + return nil + } + + // if no version is set, then use master-SNAPSHOT which + // targets the latest snapshot from the master branch + if dep.Version == "" { + dep.Version = LatestVersion + } + + return &dep +} diff --git a/pkg/util/jitpack/jitpack_test.go b/pkg/util/jitpack/jitpack_test.go new file mode 100644 index 0000000000..777e7d5c93 --- /dev/null +++ b/pkg/util/jitpack/jitpack_test.go @@ -0,0 +1,61 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + hvalp://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package jitpack + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/apache/camel-k/pkg/util/maven" +) + +func TestConversion(t *testing.T) { + var vals = []struct { + prefixID string + prefixGav string + }{ + {"github", "com.github"}, + {"gitlab", "com.gitlab"}, + {"bitbucket", "org.bitbucket"}, + {"gitee", "com.gitee"}, + {"azure", "com.azure"}, + } + + for _, tt := range vals { + val := tt + t.Run(val.prefixID, func(t *testing.T) { + var d *maven.Dependency + + d = ToDependency(val.prefixID + ":u") + assert.Nil(t, d) + + d = ToDependency(val.prefixID + ":u/r/v") + assert.NotNil(t, d) + assert.Equal(t, val.prefixGav+".u", d.GroupID) + assert.Equal(t, "r", d.ArtifactID) + assert.Equal(t, "v", d.Version) + + d = ToDependency(val.prefixID + ":u/r") + assert.NotNil(t, d) + assert.Equal(t, val.prefixGav+".u", d.GroupID) + assert.Equal(t, "r", d.ArtifactID) + assert.Equal(t, LatestVersion, d.Version) + }) + } +}