Skip to content

Commit

Permalink
support for jitpack dependencies #1407
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jun 5, 2020
1 parent 25d31d0 commit cb2a6f8
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
}
}

Expand Down
67 changes: 67 additions & 0 deletions pkg/util/jitpack/jitpack.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions pkg/util/jitpack/jitpack_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit cb2a6f8

Please sign in to comment.