Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit bfc13c7

Browse files
author
noah
committed
Fix the stream API to support DeploymentStatus
1 parent d776bb8 commit bfc13c7

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

internal/pkg/store/event.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func (s *Store) ListEventsGreaterThanTime(ctx context.Context, t time.Time) ([]*
2828
WithRepo().
2929
WithDeploymentStatuses()
3030
}).
31+
WithDeploymentStatus(func(dsq *ent.DeploymentStatusQuery) {
32+
dsq.
33+
WithDeployment()
34+
}).
3135
WithReview(func(rq *ent.ReviewQuery) {
3236
rq.
3337
WithUser().

internal/server/api/v1/stream/events.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *Stream) GetEvents(c *gin.Context) {
3939
}
4040

4141
if _, err := s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); err != nil {
42-
s.log.Debug("Skip the event. The permission is denied.")
42+
s.log.Debug("Skip the event. The permission is denied.", zap.Error(err))
4343
return
4444
}
4545

@@ -48,6 +48,30 @@ func (s *Stream) GetEvents(c *gin.Context) {
4848
Event: "deployment",
4949
Data: d,
5050
}
51+
case event.KindDeploymentStatus:
52+
ds, err := s.i.FindDeploymentStatusByID(ctx, e.DeploymentStatusID)
53+
if err != nil {
54+
s.log.Error("Failed to find the deployment status.", zap.Error(err))
55+
return
56+
}
57+
58+
// Ensure that a user has access to the repository of the deployment.
59+
d, err := s.i.FindDeploymentByID(ctx, ds.DeploymentID)
60+
if err != nil {
61+
s.log.Error("Failed to find the deployment.", zap.Error(err))
62+
return
63+
}
64+
65+
if _, err := s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); err != nil {
66+
s.log.Debug("Skip the event. The permission is denied.", zap.Error(err))
67+
return
68+
}
69+
70+
s.log.Debug("Dispatch a deployment_status event.", zap.Int("id", d.ID))
71+
events <- &sse.Event{
72+
Event: "deployment_status",
73+
Data: ds,
74+
}
5175

5276
case event.KindReview:
5377
r, err := s.i.FindReviewByID(ctx, e.ReviewID)

internal/server/api/v1/stream/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type (
1111
SubscribeEvent(fn func(e *ent.Event)) error
1212
UnsubscribeEvent(fn func(e *ent.Event)) error
1313
FindDeploymentByID(ctx context.Context, id int) (*ent.Deployment, error)
14+
FindDeploymentStatusByID(ctx context.Context, id int) (*ent.DeploymentStatus, error)
1415
FindReviewByID(ctx context.Context, id int) (*ent.Review, error)
1516
FindPermOfRepo(ctx context.Context, r *ent.Repo, u *ent.User) (*ent.Perm, error)
1617
}

openapi/v1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,12 @@ paths:
14381438
type: string
14391439
enum:
14401440
- deployment
1441+
- deployment_status
14411442
- review
14421443
data:
14431444
oneOf:
14441445
- $ref: '#/components/schemas/Deployment'
1446+
- $ref: '#/components/schemas/DeploymentStatus'
14451447
- $ref: '#/components/schemas/Review'
14461448
/sync:
14471449
post:

0 commit comments

Comments
 (0)