Skip to content

Commit

Permalink
fix: Supports adding third-party dependencies from git repo with the …
Browse files Browse the repository at this point in the history
…version field

Signed-off-by: Akash Kumar <[email protected]>
  • Loading branch information
AkashKumar7902 committed Apr 4, 2024
1 parent ec6f1f2 commit 62ff3e6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
7 changes: 4 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,13 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
return nil, err
}
dep.FullName = dep.GenDepFullName()
// If the dependency is from git commit, the version is the commit id.
// If the dependency is from git tag, the version is the tag.
dep.Version, err = dep.Source.Git.GetValidGitReference()

modFile, err := c.LoadModFile(localPath)
if err != nil {
return nil, err
}
dep.Version = modFile.Pkg.Version
dep.Source.Git.Version = modFile.Pkg.Version
}

if dep.Source.Oci != nil {
Expand Down
29 changes: 17 additions & 12 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/dominikbraun/graph"
"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"golang.org/x/mod/module"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/git"
Expand Down Expand Up @@ -181,28 +182,32 @@ func TestDependencyGraph(t *testing.T) {
adjMap, err := depGraph.AdjacencyMap()
assert.Equal(t, err, nil)

m := func(Path, Version string) module.Version {
return module.Version{Path, Version}
}

edgeProp := graph.EdgeProperties{
Attributes: map[string]string{},
Weight: 0,
Data: nil,
}
assert.Equal(t, adjMap,
map[string]map[string]graph.Edge[string]{
"dependency_graph@0.0.1": {
"teleport@0.1.0": {Source: "dependency_graph@0.0.1", Target: "teleport@0.1.0", Properties: edgeProp},
"rabbitmq@0.0.1": {Source: "dependency_graph@0.0.1", Target: "rabbitmq@0.0.1", Properties: edgeProp},
"agent@0.1.0": {Source: "dependency_graph@0.0.1", Target: "agent@0.1.0", Properties: edgeProp},
map[module.Version]map[module.Version]graph.Edge[module.Version]{
m("dependency_graph", "0.0.1"): {
m("teleport", "0.1.0"): {Source: m("dependency_graph", "0.0.1"), Target: m("teleport", "0.1.0"), Properties: edgeProp},
m("rabbitmq", "0.0.1"): {Source: m("dependency_graph", "0.0.1"), Target: m("rabbitmq", "0.0.1"), Properties: edgeProp},
m("agent", "0.1.0"): {Source: m("dependency_graph", "0.0.1"), Target: m("agent", "0.1.0"), Properties: edgeProp},
},
"teleport@0.1.0": {
"k8s@1.28": {Source: "teleport@0.1.0", Target: "k8s@1.28", Properties: edgeProp},
m("teleport", "0.1.0"): {
m("k8s", "1.28"): {Source: m("teleport", "0.1.0"), Target: m("k8s", "1.28"), Properties: edgeProp},
},
"rabbitmq@0.0.1": {
"k8s@1.28": {Source: "rabbitmq@0.0.1", Target: "k8s@1.28", Properties: edgeProp},
m("rabbitmq", "0.0.1"): {
m("k8s", "1.28"): {Source: m("rabbitmq", "0.0.1"), Target: m("k8s", "1.28"), Properties: edgeProp},
},
"agent@0.1.0": {
"k8s@1.28": {Source: "agent@0.1.0", Target: "k8s@1.28", Properties: edgeProp},
m("agent", "0.1.0"): {
m("k8s", "1.28"): {Source: m("agent", "0.1.0"), Target: m("k8s", "1.28"), Properties: edgeProp},
},
"k8s@1.28": {},
m("k8s", "1.28"): {},
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" }
catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db", version = "0.1.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
[dependencies.catalog]
name = "catalog"
full_name = "catalog_a29e3db"
version = "a29e3db"
version = "0.1.0"
sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks="
url = "https://github.com/KusionStack/catalog.git"
commit = "a29e3db"
version = "0.1.0"
9 changes: 5 additions & 4 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ func (oci *Oci) FromString(ociUrl string) (*Oci, error) {

// Git is the package source from git registry.
type Git struct {
Url string `toml:"url,omitempty"`
Branch string `toml:"branch,omitempty"`
Commit string `toml:"commit,omitempty"`
Tag string `toml:"git_tag,omitempty"`
Url string `toml:"url,omitempty"`
Branch string `toml:"branch,omitempty"`
Commit string `toml:"commit,omitempty"`
Tag string `toml:"git_tag,omitempty"`
Version string `toml:"version,omitempty"`
}

// GetValidGitReference will get the valid git reference from git source.
Expand Down
5 changes: 5 additions & 0 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (source *Source) MarshalTOML() string {
const GIT_URL_PATTERN = "git = \"%s\""
const TAG_PATTERN = "tag = \"%s\""
const GIT_COMMIT_PATTERN = "commit = \"%s\""
const VERSION_PATTERN = "version = \"%s\""
const SEPARATOR = ", "

func (git *Git) MarshalTOML() string {
Expand All @@ -133,6 +134,10 @@ func (git *Git) MarshalTOML() string {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(GIT_COMMIT_PATTERN, git.Commit))
}
if len(git.Version) != 0 {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(VERSION_PATTERN, git.Version))
}
return sb.String()
}

Expand Down

0 comments on commit 62ff3e6

Please sign in to comment.