summaryrefslogtreecommitdiff
path: root/app/init.go
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-24 17:40:45 -0600
committermo khan <mo@mokhan.ca>2025-07-24 17:40:45 -0600
commitd48fe690c3c071cb5c8e3aa4d4672a32230a5e2d (patch)
tree414f9e91877e901cb3de12be6f466cb4929f55ab /app/init.go
parent7257c213887c6a80f727642b016606ec10340ed9 (diff)
refactor: extract job to process relationship updates in background
Diffstat (limited to 'app/init.go')
-rw-r--r--app/init.go57
1 files changed, 7 insertions, 50 deletions
diff --git a/app/init.go b/app/init.go
index c22628c..ea67e48 100644
--- a/app/init.go
+++ b/app/init.go
@@ -2,12 +2,9 @@ package app
import (
"context"
- "fmt"
"net/http"
"os"
- "strings"
- v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"github.com/rs/zerolog"
"github.com/xlgmokha/x/pkg/env"
@@ -20,6 +17,7 @@ import (
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/jobs"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/authz"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web"
)
@@ -83,52 +81,11 @@ func init() {
}
})
- ioc.MustResolve[*event.TypedAggregator[*domain.Sparkle]](c).SubscribeTo("after.create", func(item *domain.Sparkle) {
- client := ioc.MustResolve[*authzed.Client](c)
+ logger := ioc.MustResolve[*zerolog.Logger](c)
+ ctx := logger.WithContext(context.Background())
+ client := ioc.MustResolve[*authzed.Client](c)
- resource := item.ToGID().ToObjectReference()
-
- response, err := client.WriteRelationships(context.Background(), &v1.WriteRelationshipsRequest{
- Updates: []*v1.RelationshipUpdate{
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "sparkler",
- Subject: item.Author.ToSubjectReference(),
- },
- },
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "sparklee",
- Subject: &v1.SubjectReference{
- Object: &v1.ObjectReference{
- ObjectType: "user",
- ObjectId: strings.TrimPrefix(item.Sparklee, "@"),
- },
- },
- },
- },
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "reader",
- Subject: &v1.SubjectReference{
- Object: &v1.ObjectReference{
- ObjectType: "user",
- ObjectId: "*",
- },
- },
- },
- },
- },
- })
- if err != nil {
- fmt.Printf("%v\n", err)
- }
- fmt.Printf("%v\n", response)
- })
+ ioc.
+ MustResolve[*event.TypedAggregator[*domain.Sparkle]](c).
+ SubscribeTo("after.create", jobs.NewCreateSparkle(ctx, client).Run)
}