diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-24 17:40:45 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-24 17:40:45 -0600 |
| commit | d48fe690c3c071cb5c8e3aa4d4672a32230a5e2d (patch) | |
| tree | 414f9e91877e901cb3de12be6f466cb4929f55ab /app/jobs | |
| parent | 7257c213887c6a80f727642b016606ec10340ed9 (diff) | |
refactor: extract job to process relationship updates in background
Diffstat (limited to 'app/jobs')
| -rw-r--r-- | app/jobs/create_sparkle.go | 75 | ||||
| -rw-r--r-- | app/jobs/job.go | 5 |
2 files changed, 80 insertions, 0 deletions
diff --git a/app/jobs/create_sparkle.go b/app/jobs/create_sparkle.go new file mode 100644 index 0000000..3a03b1f --- /dev/null +++ b/app/jobs/create_sparkle.go @@ -0,0 +1,75 @@ +package jobs + +import ( + "context" + "strings" + + v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" + "github.com/authzed/authzed-go/v1" + "github.com/containerd/log" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" +) + +type CreateSparkle struct { + client *authzed.Client + ctx context.Context +} + +func NewCreateSparkle(ctx context.Context, client *authzed.Client) Job[*domain.Sparkle] { + return &CreateSparkle{ + client: client, + ctx: ctx, + } +} + +func (job *CreateSparkle) Run(item *domain.Sparkle) { + response, err := job.client.WriteRelationships(job.ctx, job.requestFor(item)) + if err != nil { + pls.LogErrorNow(job.ctx, err) + } + pls.LogNow(job.ctx, log.Fields{"response": response}) +} + +func (job *CreateSparkle) requestFor(sparkle *domain.Sparkle) *v1.WriteRelationshipsRequest { + resource := sparkle.ToGID().ToObjectReference() + + return &v1.WriteRelationshipsRequest{ + Updates: []*v1.RelationshipUpdate{ + &v1.RelationshipUpdate{ + Operation: v1.RelationshipUpdate_OPERATION_CREATE, + Relationship: &v1.Relationship{ + Resource: resource, + Relation: "sparkler", + Subject: sparkle.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(sparkle.Sparklee, "@"), + }, + }, + }, + }, + &v1.RelationshipUpdate{ + Operation: v1.RelationshipUpdate_OPERATION_CREATE, + Relationship: &v1.Relationship{ + Resource: resource, + Relation: "reader", + Subject: &v1.SubjectReference{ + Object: &v1.ObjectReference{ + ObjectType: "user", + ObjectId: "*", + }, + }, + }, + }, + }, + } +} diff --git a/app/jobs/job.go b/app/jobs/job.go new file mode 100644 index 0000000..3864c76 --- /dev/null +++ b/app/jobs/job.go @@ -0,0 +1,5 @@ +package jobs + +type Job[T any] interface { + Run(T) +} |
