This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new cb2a6f8  support for jitpack dependencies #1407
cb2a6f8 is described below

commit cb2a6f81838c9972070a7c435618765b28edbb19
Author: lburgazzoli <[email protected]>
AuthorDate: Thu Jun 4 18:04:52 2020 +0200

    support for jitpack dependencies #1407
---
 pkg/builder/builder_steps.go     | 32 ++++++++++++++++++-
 pkg/util/jitpack/jitpack.go      | 67 ++++++++++++++++++++++++++++++++++++++++
 pkg/util/jitpack/jitpack_test.go | 61 ++++++++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+), 1 deletion(-)

diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index e33597a..a1ce547 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 0000000..4b07993
--- /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 0000000..777e7d5
--- /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)
+               })
+       }
+}

Reply via email to