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

Commit 12932f7

Browse files
author
Noah Lee
authored
Fix the error handler of getting perm for stream API (#252)
* Fix handling error of getting perm * Fix error handling
1 parent a83ea9a commit 12932f7

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gitploy-io/gitploy/ent"
2020
"github.com/gitploy-io/gitploy/ent/event"
2121
gb "github.com/gitploy-io/gitploy/internal/server/global"
22+
"github.com/gitploy-io/gitploy/pkg/e"
2223
)
2324

2425
// GetEvents streams events of deployment, or review.
@@ -42,12 +43,12 @@ func (s *Stream) GetEvents(c *gin.Context) {
4243
return
4344
}
4445

45-
if ok, err := s.hasPermForEvent(ctx, u, e); !ok {
46-
s.log.Debug("Skip the event. The user has not the perm.")
47-
return
48-
} else if err != nil {
46+
if ok, err := s.hasPermForEvent(ctx, u, e); err != nil {
4947
s.log.Error("It has failed to check the perm.", zap.Error(err))
5048
return
49+
} else if !ok {
50+
s.log.Debug("Skip the event. The user has not the perm.")
51+
return
5152
}
5253

5354
events <- e
@@ -93,14 +94,14 @@ L:
9394
}
9495

9596
// hasPermForEvent checks the user has permission for the event.
96-
func (s *Stream) hasPermForEvent(ctx context.Context, u *ent.User, e *ent.Event) (bool, error) {
97-
if e.Kind == event.KindDeployment {
98-
d, err := s.i.FindDeploymentByID(ctx, e.DeploymentID)
97+
func (s *Stream) hasPermForEvent(ctx context.Context, u *ent.User, evt *ent.Event) (bool, error) {
98+
if evt.Kind == event.KindDeployment {
99+
d, err := s.i.FindDeploymentByID(ctx, evt.DeploymentID)
99100
if err != nil {
100101
return false, err
101102
}
102103

103-
if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); ent.IsNotFound(err) {
104+
if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
104105
return false, nil
105106
} else if err != nil {
106107
return false, err
@@ -109,8 +110,8 @@ func (s *Stream) hasPermForEvent(ctx context.Context, u *ent.User, e *ent.Event)
109110
return true, nil
110111
}
111112

112-
if e.Kind == event.KindReview {
113-
rv, err := s.i.FindReviewByID(ctx, e.ReviewID)
113+
if evt.Kind == event.KindReview {
114+
rv, err := s.i.FindReviewByID(ctx, evt.ReviewID)
114115
if err != nil {
115116
return false, err
116117
}
@@ -120,7 +121,7 @@ func (s *Stream) hasPermForEvent(ctx context.Context, u *ent.User, e *ent.Event)
120121
return false, err
121122
}
122123

123-
if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); ent.IsNotFound(err) {
124+
if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
124125
return false, nil
125126
} else if err != nil {
126127
return false, err

0 commit comments

Comments
 (0)