Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/keboola/go-cloud-encrypt v0.0.0-20250422071622-41a5d5547c43
github.com/keboola/go-utils v1.4.0
github.com/keboola/keboola-sdk-go/v2 v2.12.0
github.com/keboola/keboola-sdk-go/v2 v2.12.1-0.20260223100110-432345f77343
github.com/klauspost/compress v1.18.4
github.com/klauspost/pgzip v1.2.6
github.com/kylelemons/godebug v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ github.com/keboola/go-oauth2-proxy/v7 v7.13.1-0.20251120082210-251fbcb18c16 h1:m
github.com/keboola/go-oauth2-proxy/v7 v7.13.1-0.20251120082210-251fbcb18c16/go.mod h1:2KeAM0/QPbyUAoky+PXVgQDt/5m0qNcn30z9jh4ig8A=
github.com/keboola/go-utils v1.4.0 h1:WTyj95yrr8O8HxtC8TSTyUcElZiRGDeEdVvDpFo6HUo=
github.com/keboola/go-utils v1.4.0/go.mod h1:IopwJzFz2gh0Yj3fUbIe2eamRoDKzbXvjqFjQyw3ZdQ=
github.com/keboola/keboola-sdk-go/v2 v2.12.0 h1:jU9trH7EW3MzRI4Yz/wt5qlXHBo3guTe9mxGQ8OB/v4=
github.com/keboola/keboola-sdk-go/v2 v2.12.0/go.mod h1:N/PkJnEHcyHMbVjHPjTdQwj5b9Iajl7PEaFUVtywHKU=
github.com/keboola/keboola-sdk-go/v2 v2.12.1-0.20260223100110-432345f77343 h1:xcHtXh3n6F2f3nBqGf+/AwdEKV06zAXXYlKZwVcOgI8=
github.com/keboola/keboola-sdk-go/v2 v2.12.1-0.20260223100110-432345f77343/go.mod h1:N/PkJnEHcyHMbVjHPjTdQwj5b9Iajl7PEaFUVtywHKU=
github.com/keybase/go-keychain v0.0.1 h1:way+bWYa6lDppZoZcgMbYsvC7GxljxrskdNInRtuthU=
github.com/keybase/go-keychain v0.0.1/go.mod h1:PdEILRW3i9D8JcdM+FmY6RwkHGnhHxXwkPPMeUgOK1k=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
97 changes: 97 additions & 0 deletions internal/pkg/mapper/notification/local_load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package notification

import (
"context"
"path/filepath"

"github.com/keboola/keboola-sdk-go/v2/pkg/keboola"

"github.com/keboola/keboola-as-code/internal/pkg/encoding/json"
"github.com/keboola/keboola-as-code/internal/pkg/filesystem"
"github.com/keboola/keboola-as-code/internal/pkg/model"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
)

// MapAfterLocalLoad loads notifications from notifications/ subdirectories.
func (m *mapper) MapAfterLocalLoad(ctx context.Context, recipe *model.LocalLoadRecipe) error {
configManifest, ok := recipe.ObjectManifest.(*model.ConfigManifest)
if !ok {
return nil
}

fs := m.state.LocalManager().Fs()
configDir := configManifest.Path()
notificationsDir := filesystem.Join(configDir, "notifications")

if !fs.IsDir(ctx, notificationsDir) {
return nil
}

entries, err := fs.ReadDir(ctx, notificationsDir)
if err != nil {
return errors.Errorf(`cannot read notifications directory "%s": %w`, notificationsDir, err)
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

subscriptionID := keboola.NotificationSubscriptionID(entry.Name())
metaFile := filesystem.Join(notificationsDir, entry.Name(), "meta.json")

var metaContent struct {
Event keboola.NotificationEvent `json:"event"`
Filters []keboola.NotificationFilter `json:"filters,omitempty"`
Recipient keboola.NotificationRecipient `json:"recipient"`
ExpiresAt *keboola.NotificationExpiration `json:"expiresAt,omitempty"`
}

file, err := fs.ReadFile(ctx, filesystem.NewFileDef(metaFile).SetDescription("notification meta"))
if err != nil {
m.logger.Warnf(ctx, `cannot read notification meta file "%s": %s`, metaFile, err)
continue
}

if err := json.DecodeString(file.Content, &metaContent); err != nil {
m.logger.Warnf(ctx, `cannot decode notification meta file "%s": %s`, metaFile, err)
continue
}

for _, filter := range metaContent.Filters {
if err := filter.Validate(); err != nil {
return errors.Errorf(`invalid filter in notification "%s": %w`, subscriptionID, err)
}
}

notification := &model.Notification{
NotificationKey: model.NotificationKey{
BranchID: configManifest.BranchID,
ComponentID: configManifest.ComponentID,
ConfigID: configManifest.ID,
ID: subscriptionID,
},
Event: metaContent.Event,
Filters: metaContent.Filters,
Recipient: metaContent.Recipient,
ExpiresAt: metaContent.ExpiresAt,
}

notificationManifest := &model.NotificationManifest{
NotificationKey: notification.NotificationKey,
}
notificationManifest.SetParentPath(configDir)
notificationManifest.SetRelativePath(filepath.Join("notifications", entry.Name()))

notificationState := &model.NotificationState{
NotificationManifest: notificationManifest,
Local: notification,
}

if err := m.state.Set(notificationState); err != nil {
return err
}
}

return nil
}
46 changes: 46 additions & 0 deletions internal/pkg/mapper/notification/local_save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package notification

import (
"context"

"github.com/keboola/go-utils/pkg/orderedmap"

"github.com/keboola/keboola-as-code/internal/pkg/filesystem"
"github.com/keboola/keboola-as-code/internal/pkg/model"
)

// MapBeforeLocalSave creates notification subdirectory and meta.json file.
func (m *mapper) MapBeforeLocalSave(_ context.Context, recipe *model.LocalSaveRecipe) error {
notification, ok := recipe.Object.(*model.Notification)
if !ok {
return nil
}

manifest, ok := recipe.ObjectManifest.(*model.NotificationManifest)
if !ok {
return nil
}

// Set relative path for notification
manifest.SetRelativePath(filesystem.Join("notifications", string(notification.ID)))

// Create meta.json with notification details
metaFile := filesystem.Join(recipe.Path(), "meta.json")
metaContent := orderedmap.New()
metaContent.Set("event", notification.Event)
metaContent.Set("recipient", notification.Recipient)
if len(notification.Filters) > 0 {
metaContent.Set("filters", notification.Filters)
}
if notification.ExpiresAt != nil {
metaContent.Set("expiresAt", notification.ExpiresAt)
}

jsonFile := filesystem.NewJSONFile(metaFile, metaContent)
recipe.Files.
Add(jsonFile).
AddTag(model.FileTypeJSON).
AddTag(model.FileKindObjectMeta)

return nil
}
15 changes: 15 additions & 0 deletions internal/pkg/mapper/notification/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package notification

import (
"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/state"
)

type mapper struct {
logger log.Logger
state *state.State
}

func NewMapper(s *state.State) *mapper {
return &mapper{logger: s.Logger(), state: s}
}
75 changes: 60 additions & 15 deletions internal/pkg/model/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import (
)

const (
BranchKind = "branch"
ComponentKind = "component"
ConfigKind = "config"
ConfigRowKind = "config row"
BlockKind = "block"
CodeKind = "code"
PhaseKind = "phase"
TaskKind = "task"
BranchAbbr = "B"
ConfigAbbr = "C"
RowAbbr = "R"
BlockAbbr = "b"
CodeAbbr = "c"
PhaseAbbr = "p"
TaskAbbr = "t"
BranchKind = "branch"
ComponentKind = "component"
ConfigKind = "config"
ConfigRowKind = "config row"
BlockKind = "block"
CodeKind = "code"
PhaseKind = "phase"
TaskKind = "task"
NotificationKind = "notification"
BranchAbbr = "B"
ConfigAbbr = "C"
RowAbbr = "R"
BlockAbbr = "b"
CodeAbbr = "c"
PhaseAbbr = "p"
TaskAbbr = "t"
NotificationAbbr = "N"
)

type Key interface {
Expand Down Expand Up @@ -82,6 +84,13 @@ type TaskKey struct {
Index int `json:"-" validate:"min=0"`
}

type NotificationKey struct {
BranchID keboola.BranchID `json:"branchId,omitempty"`
ComponentID keboola.ComponentID `json:"componentId,omitempty"`
ConfigID keboola.ConfigID `json:"configId,omitempty"`
ID keboola.NotificationSubscriptionID `json:"id" validate:"required"`
}

func (k BranchKey) Kind() Kind {
return Kind{Name: BranchKind, Abbr: BranchAbbr}
}
Expand Down Expand Up @@ -110,6 +119,10 @@ func (k TaskKey) Kind() Kind {
return Kind{Name: TaskKind, Abbr: TaskAbbr}
}

func (k NotificationKey) Kind() Kind {
return Kind{Name: NotificationKind, Abbr: NotificationAbbr}
}

func (k BranchKey) ObjectID() string {
return k.ID.String()
}
Expand Down Expand Up @@ -138,6 +151,10 @@ func (k TaskKey) ObjectID() string {
return cast.ToString(k.Index)
}

func (k NotificationKey) ObjectID() string {
return string(k.ID)
}

func (k BranchKey) Level() int {
return 1
}
Expand Down Expand Up @@ -166,6 +183,10 @@ func (k TaskKey) Level() int {
return 6
}

func (k NotificationKey) Level() int {
return 4
}

func (k BranchKey) Key() Key {
return k
}
Expand All @@ -190,6 +211,10 @@ func (k TaskKey) Key() Key {
return k
}

func (k NotificationKey) Key() Key {
return k
}

func (k BlockKey) ConfigKey() Key {
return ConfigKey{
BranchID: k.BranchID,
Expand Down Expand Up @@ -263,6 +288,10 @@ func (k TaskKey) Desc() string {
return fmt.Sprintf(`%s "branch:%d/component:%s/config:%s/phase:%d/task:%d"`, k.Kind().Name, k.BranchID, k.ComponentID, k.ConfigID, k.PhaseKey.Index, k.Index)
}

func (k NotificationKey) Desc() string {
return fmt.Sprintf(`%s "branch:%d/component:%s/config:%s/notification:%s"`, k.Kind().Name, k.BranchID, k.ComponentID, k.ConfigID, k.ID)
}

func (k BranchKey) String() string {
return fmt.Sprintf("%02d_%d_branch", k.Level(), k.ID)
}
Expand Down Expand Up @@ -295,6 +324,10 @@ func (k TaskKey) String() string {
return fmt.Sprintf("%02d_%d_%s_%s_%03d_%03d_task", k.Level(), k.BranchID, k.ComponentID, k.ConfigID, k.PhaseKey.Index, k.Index)
}

func (k NotificationKey) String() string {
return fmt.Sprintf("%02d_%d_%s_%s_%s_notification", k.Level(), k.BranchID, k.ComponentID, k.ConfigID, k.ID)
}

func (k ConfigKey) BranchKey() BranchKey {
return BranchKey{ID: k.BranchID}
}
Expand Down Expand Up @@ -347,6 +380,18 @@ func (k TaskKey) ParentKey() (Key, error) {
return k.PhaseKey, nil
}

func (k NotificationKey) ConfigKey() ConfigKey {
return ConfigKey{
BranchID: k.BranchID,
ComponentID: k.ComponentID,
ID: k.ConfigID,
}
}

func (k NotificationKey) ParentKey() (Key, error) {
return k.ConfigKey(), nil
}

type ConfigIDMetadata struct {
IDInTemplate keboola.ConfigID `json:"idInTemplate"`
}
Expand Down
Loading
Loading