-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Timoni release type (#97)
- Loading branch information
Showing
9 changed files
with
348 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package providers | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/input-output-hk/catalyst-forge/cli/pkg/events" | ||
"github.com/input-output-hk/catalyst-forge/cli/pkg/executor" | ||
"github.com/input-output-hk/catalyst-forge/cli/pkg/run" | ||
"github.com/input-output-hk/catalyst-forge/lib/project/project" | ||
"github.com/input-output-hk/catalyst-forge/lib/project/schema" | ||
) | ||
|
||
const ( | ||
TIMONI_BINARY = "timoni" | ||
) | ||
|
||
type TimoniReleaserConfig struct { | ||
Container string `json:"container"` | ||
Tag string `json:"tag"` | ||
} | ||
|
||
type TimoniReleaser struct { | ||
config TimoniReleaserConfig | ||
force bool | ||
handler events.EventHandler | ||
logger *slog.Logger | ||
project project.Project | ||
release schema.Release | ||
releaseName string | ||
timoni executor.WrappedExecuter | ||
} | ||
|
||
func (r *TimoniReleaser) Release() error { | ||
if !r.handler.Firing(&r.project, r.project.GetReleaseEvents(r.releaseName)) && !r.force { | ||
r.logger.Info("No release event is firing, skipping release") | ||
return nil | ||
} | ||
|
||
registries := r.project.Blueprint.Global.CI.Providers.Timoni.Registries | ||
if len(registries) == 0 { | ||
return fmt.Errorf("must specify at least one Timoni registry") | ||
} | ||
|
||
container := r.config.Container | ||
if container == "" { | ||
r.logger.Debug("Defaulting container name") | ||
container = fmt.Sprintf("%s-%s", r.project.Name, "deployment") | ||
} | ||
|
||
tag := r.config.Tag | ||
if tag == "" { | ||
return fmt.Errorf("no tag specified") | ||
} | ||
|
||
for _, registry := range registries { | ||
fullContainer := fmt.Sprintf("oci://%s/%s", registry, container) | ||
path, err := r.project.GetRelativePath() | ||
if err != nil { | ||
return fmt.Errorf("failed to get relative path: %w", err) | ||
} | ||
|
||
r.logger.Info("Publishing module", "path", path, "container", fullContainer, "tag", tag) | ||
out, err := r.timoni.Execute("mod", "push", "--version", tag, "--latest=false", path, fullContainer) | ||
if err != nil { | ||
r.logger.Error("Failed to push module", "module", fullContainer, "error", err, "output", string(out)) | ||
return fmt.Errorf("failed to push module: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewTimoniReleaser creates a new Timoni release provider. | ||
func NewTimoniReleaser(ctx run.RunContext, | ||
project project.Project, | ||
name string, | ||
force bool, | ||
) (*TimoniReleaser, error) { | ||
release, ok := project.Blueprint.Project.Release[name] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown release: %s", name) | ||
} | ||
|
||
exec := executor.NewLocalExecutor(ctx.Logger) | ||
if _, ok := exec.LookPath(TIMONI_BINARY); ok != nil { | ||
return nil, fmt.Errorf("failed to find Timoni binary: %w", ok) | ||
} | ||
|
||
var config TimoniReleaserConfig | ||
if err := parseConfig(&project, name, &config); err != nil { | ||
return nil, fmt.Errorf("failed to parse release config: %w", err) | ||
} | ||
|
||
timoni := executor.NewLocalWrappedExecutor(exec, "timoni") | ||
handler := events.NewDefaultEventHandler(ctx.Logger) | ||
return &TimoniReleaser{ | ||
config: config, | ||
force: force, | ||
handler: &handler, | ||
logger: ctx.Logger, | ||
project: project, | ||
release: release, | ||
releaseName: name, | ||
timoni: timoni, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package providers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/input-output-hk/catalyst-forge/lib/project/project" | ||
"github.com/input-output-hk/catalyst-forge/lib/project/schema" | ||
"github.com/input-output-hk/catalyst-forge/lib/tools/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimoniReleaserRelease(t *testing.T) { | ||
newProject := func( | ||
name string, | ||
registries []string, | ||
) project.Project { | ||
return project.Project{ | ||
Name: name, | ||
Blueprint: schema.Blueprint{ | ||
Global: schema.Global{ | ||
CI: schema.GlobalCI{ | ||
Providers: schema.Providers{ | ||
Timoni: schema.TimoniProvider{ | ||
Registries: registries, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
project project.Project | ||
release schema.Release | ||
config TimoniReleaserConfig | ||
firing bool | ||
force bool | ||
failOn string | ||
validate func(t *testing.T, calls []string, err error) | ||
}{ | ||
{ | ||
name: "full", | ||
project: newProject("test", []string{"test.com"}), | ||
release: schema.Release{}, | ||
config: TimoniReleaserConfig{ | ||
Container: "test", | ||
Tag: "test", | ||
}, | ||
firing: true, | ||
force: false, | ||
failOn: "", | ||
validate: func(t *testing.T, calls []string, err error) { | ||
require.NoError(t, err) | ||
assert.Contains(t, calls, "mod push --version test --latest=false . oci://test.com/test") | ||
}, | ||
}, | ||
{ | ||
name: "no container", | ||
project: newProject("test", []string{"test.com"}), | ||
release: schema.Release{}, | ||
config: TimoniReleaserConfig{ | ||
Tag: "test", | ||
}, | ||
firing: true, | ||
force: false, | ||
failOn: "", | ||
validate: func(t *testing.T, calls []string, err error) { | ||
require.NoError(t, err) | ||
assert.Contains(t, calls, "mod push --version test --latest=false . oci://test.com/test-deployment") | ||
}, | ||
}, | ||
{ | ||
name: "not firing", | ||
project: newProject("test", []string{"test.com"}), | ||
firing: false, | ||
force: false, | ||
failOn: "", | ||
validate: func(t *testing.T, calls []string, err error) { | ||
require.NoError(t, err) | ||
assert.Len(t, calls, 0) | ||
}, | ||
}, | ||
{ | ||
name: "forced", | ||
project: newProject("test", []string{"test.com"}), | ||
release: schema.Release{}, | ||
config: TimoniReleaserConfig{ | ||
Container: "test", | ||
Tag: "test", | ||
}, | ||
firing: false, | ||
force: true, | ||
failOn: "", | ||
validate: func(t *testing.T, calls []string, err error) { | ||
require.NoError(t, err) | ||
assert.Contains(t, calls, "mod push --version test --latest=false . oci://test.com/test") | ||
}, | ||
}, | ||
{ | ||
name: "push fails", | ||
project: newProject("test", []string{"test.com"}), | ||
release: schema.Release{}, | ||
config: TimoniReleaserConfig{ | ||
Container: "test", | ||
Tag: "test", | ||
}, | ||
firing: true, | ||
force: false, | ||
failOn: "mod push", | ||
validate: func(t *testing.T, calls []string, err error) { | ||
require.Error(t, err) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var calls []string | ||
timoni := TimoniReleaser{ | ||
config: tt.config, | ||
force: tt.force, | ||
handler: newReleaseEventHandlerMock(tt.firing), | ||
logger: testutils.NewNoopLogger(), | ||
project: tt.project, | ||
release: tt.release, | ||
timoni: newWrappedExecuterMock(&calls, tt.failOn), | ||
} | ||
|
||
err := timoni.Release() | ||
|
||
tt.validate(t, calls, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.