Skip to content

Commit 2e5a103

Browse files
authored
refactor: moves fetch bundle to deployment (#154)
1 parent 1a56719 commit 2e5a103

File tree

4 files changed

+303
-105
lines changed

4 files changed

+303
-105
lines changed

lib/project/deployment/deployer/deployer.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77

88
"cuelang.org/go/cue"
9-
"cuelang.org/go/cue/cuecontext"
109
"github.com/input-output-hk/catalyst-forge/lib/project/deployment"
1110
"github.com/input-output-hk/catalyst-forge/lib/project/deployment/generator"
1211
"github.com/input-output-hk/catalyst-forge/lib/project/project"
@@ -182,40 +181,6 @@ func (d *Deployer) CreateDeployment(
182181
}, nil
183182
}
184183

185-
// FetchBundle fetches a deployment bundle from the given project and repository.
186-
func (d *Deployer) FetchBundle(url, ref, projectPath string, opts ...CloneOption) (deployment.ModuleBundle, error) {
187-
options := CloneOptions{
188-
fs: billy.NewInMemoryFs(),
189-
}
190-
for _, o := range opts {
191-
o(&options)
192-
}
193-
194-
r, err := d.clone(url, ref, options.fs)
195-
if err != nil {
196-
return deployment.ModuleBundle{}, err
197-
}
198-
199-
exists, err := r.Exists(projectPath)
200-
if err != nil {
201-
return deployment.ModuleBundle{}, fmt.Errorf("could not check if project path exists: %w", err)
202-
} else if !exists {
203-
return deployment.ModuleBundle{}, fmt.Errorf("project path does not exist: %s", projectPath)
204-
}
205-
206-
loader := project.NewDefaultProjectLoader(cuecontext.New(), d.ss, d.logger, project.WithFs(r.WorkFs()))
207-
p, err := loader.Load("/" + projectPath)
208-
if err != nil {
209-
return deployment.ModuleBundle{}, fmt.Errorf("could not load project: %w", err)
210-
}
211-
212-
if p.Blueprint.Project == nil || p.Blueprint.Project.Deployment == nil {
213-
return deployment.ModuleBundle{}, fmt.Errorf("project does not have a deployment bundle")
214-
}
215-
216-
return deployment.NewModuleBundle(&p), nil
217-
}
218-
219184
// Commit commits the deployment to the GitOps repository.
220185
func (d *Deployment) Commit() error {
221186
d.logger.Info("Committing changes")

lib/project/deployment/deployer/deployer_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,6 @@ import (
2020
"github.com/stretchr/testify/require"
2121
)
2222

23-
func TestDeployerFetchBundle(t *testing.T) {
24-
tests := []struct {
25-
name string
26-
cfg DeployerConfig
27-
files map[string]string
28-
validate func(*testing.T, deployment.ModuleBundle, error)
29-
}{
30-
{
31-
name: "success",
32-
cfg: makeConfig(),
33-
files: map[string]string{
34-
"project/blueprint.cue": makeBlueprint(),
35-
},
36-
validate: func(t *testing.T, bundle deployment.ModuleBundle, err error) {
37-
require.NoError(t, err)
38-
39-
b := bundle.Bundle
40-
assert.Equal(t, "test", b.Env)
41-
assert.Len(t, b.Modules, 1)
42-
assert.Equal(t, "module", b.Modules["main"].Name)
43-
assert.Equal(t, "v1.0.0", b.Modules["main"].Version)
44-
},
45-
},
46-
{
47-
name: "no bundle",
48-
cfg: makeConfig(),
49-
files: map[string]string{
50-
"project/blueprint.cue": `version: "1.0"`,
51-
},
52-
validate: func(t *testing.T, bundle deployment.ModuleBundle, err error) {
53-
require.Error(t, err)
54-
require.Equal(t, "project does not have a deployment bundle", err.Error())
55-
},
56-
},
57-
{
58-
name: "no project",
59-
cfg: makeConfig(),
60-
files: map[string]string{
61-
"project1/blueprint.cue": `version: "1.0"`,
62-
},
63-
validate: func(t *testing.T, bundle deployment.ModuleBundle, err error) {
64-
require.Error(t, err)
65-
require.Equal(t, "project path does not exist: project", err.Error())
66-
},
67-
},
68-
}
69-
70-
for _, tt := range tests {
71-
t.Run(tt.name, func(t *testing.T) {
72-
fs := billy.NewInMemoryFs()
73-
74-
remote, _, err := tu.NewMockGitRemoteInterface(tt.files)
75-
require.NoError(t, err)
76-
77-
ss := tu.NewMockSecretStore(map[string]string{"token": "password"})
78-
d := Deployer{
79-
cfg: tt.cfg,
80-
ctx: cuecontext.New(),
81-
fs: fs,
82-
logger: testutils.NewNoopLogger(),
83-
remote: remote,
84-
ss: ss,
85-
}
86-
87-
bundle, err := d.FetchBundle("github.com/org/repo", "main", "project")
88-
tt.validate(t, bundle, err)
89-
})
90-
}
91-
}
92-
9323
func TestDeployerCreateDeployment(t *testing.T) {
9424
type testResult struct {
9525
cloneOpts *gg.CloneOptions

lib/project/deployment/module.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package deployment
22

33
import (
44
"fmt"
5+
"log/slog"
56

67
"cuelang.org/go/cue"
8+
"cuelang.org/go/cue/cuecontext"
79
"cuelang.org/go/cue/format"
810
"github.com/input-output-hk/catalyst-forge/lib/project/project"
11+
"github.com/input-output-hk/catalyst-forge/lib/project/secrets"
912
sp "github.com/input-output-hk/catalyst-forge/lib/schema/blueprint/project"
13+
"github.com/input-output-hk/catalyst-forge/lib/tools/git/repo"
1014
)
1115

1216
// ModuleBundle represents a deployment module bundle.
@@ -25,6 +29,28 @@ func (d *ModuleBundle) Dump() ([]byte, error) {
2529
return src, nil
2630
}
2731

32+
// FetchBundle fetches a deployment bundle from the given project and repository.
33+
func FetchBundle(r repo.GitRepo, projectPath string, store secrets.SecretStore, logger *slog.Logger) (ModuleBundle, error) {
34+
exists, err := r.Exists(projectPath)
35+
if err != nil {
36+
return ModuleBundle{}, fmt.Errorf("could not check if project path exists: %w", err)
37+
} else if !exists {
38+
return ModuleBundle{}, fmt.Errorf("project path does not exist: %s", projectPath)
39+
}
40+
41+
loader := project.NewDefaultProjectLoader(cuecontext.New(), store, logger, project.WithFs(r.WorkFs()))
42+
p, err := loader.Load("/" + projectPath)
43+
if err != nil {
44+
return ModuleBundle{}, fmt.Errorf("could not load project: %w", err)
45+
}
46+
47+
if p.Blueprint.Project == nil || p.Blueprint.Project.Deployment == nil {
48+
return ModuleBundle{}, fmt.Errorf("project does not have a deployment bundle")
49+
}
50+
51+
return NewModuleBundle(&p), nil
52+
}
53+
2854
// NewModuleBundle creates a new deployment module bundle from a project.
2955
func NewModuleBundle(p *project.Project) ModuleBundle {
3056
bundle := p.Blueprint.Project.Deployment.Bundle

0 commit comments

Comments
 (0)