|
| 1 | +// Copyright 2021 Gitploy.IO Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by the Gitploy Non-Commercial License |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// +build !oss |
| 6 | + |
| 7 | +package stream |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "math/rand" |
| 13 | + "net/http" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/gin-contrib/sse" |
| 17 | + "github.com/gin-gonic/gin" |
| 18 | + "go.uber.org/zap" |
| 19 | + |
| 20 | + "github.com/gitploy-io/gitploy/ent" |
| 21 | + "github.com/gitploy-io/gitploy/ent/event" |
| 22 | + gb "github.com/gitploy-io/gitploy/internal/server/global" |
| 23 | +) |
| 24 | + |
| 25 | +// GetEvents streams events of deployment, or approval. |
| 26 | +func (s *Stream) GetEvents(c *gin.Context) { |
| 27 | + ctx := c.Request.Context() |
| 28 | + |
| 29 | + v, _ := c.Get(gb.KeyUser) |
| 30 | + u, _ := v.(*ent.User) |
| 31 | + |
| 32 | + debugID := randstr() |
| 33 | + |
| 34 | + events := make(chan *ent.Event, 10) |
| 35 | + |
| 36 | + // Subscribe events |
| 37 | + // it'll unsubscribe after the connection is closed. |
| 38 | + sub := func(e *ent.Event) { |
| 39 | + |
| 40 | + // Deleted type is always propagated to all. |
| 41 | + if e.Type == event.TypeDeleted { |
| 42 | + events <- e |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + if ok, err := s.hasPermForEvent(ctx, u, e); !ok { |
| 47 | + s.log.Debug("Skip the event. The user has not the perm.") |
| 48 | + return |
| 49 | + } else if err != nil { |
| 50 | + s.log.Error("It has failed to check the perm.", zap.Error(err)) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + events <- e |
| 55 | + } |
| 56 | + if err := s.i.SubscribeEvent(sub); err != nil { |
| 57 | + s.log.Error("failed to subscribe notification events", zap.Error(err)) |
| 58 | + gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to connect.") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + defer func() { |
| 63 | + if err := s.i.UnsubscribeEvent(sub); err != nil { |
| 64 | + s.log.Error("failed to unsubscribe notification events.") |
| 65 | + } |
| 66 | + |
| 67 | + close(events) |
| 68 | + }() |
| 69 | + |
| 70 | + w := c.Writer |
| 71 | + |
| 72 | +L: |
| 73 | + for { |
| 74 | + select { |
| 75 | + case <-w.CloseNotify(): |
| 76 | + break L |
| 77 | + case <-time.After(time.Hour): |
| 78 | + break L |
| 79 | + case <-time.After(time.Second * 30): |
| 80 | + c.Render(-1, sse.Event{ |
| 81 | + Event: "ping", |
| 82 | + Data: "ping", |
| 83 | + }) |
| 84 | + w.Flush() |
| 85 | + case e := <-events: |
| 86 | + c.Render(-1, sse.Event{ |
| 87 | + Event: "event", |
| 88 | + Data: e, |
| 89 | + }) |
| 90 | + w.Flush() |
| 91 | + s.log.Debug("server sent event.", zap.Int("event_id", e.ID), zap.String("debug_id", debugID)) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// hasPermForEvent checks the user has permission for the event. |
| 97 | +func (s *Stream) hasPermForEvent(ctx context.Context, u *ent.User, e *ent.Event) (bool, error) { |
| 98 | + if e.Kind == event.KindDeployment { |
| 99 | + d, err := s.i.FindDeploymentByID(ctx, e.DeploymentID) |
| 100 | + if err != nil { |
| 101 | + return false, err |
| 102 | + } |
| 103 | + |
| 104 | + if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); ent.IsNotFound(err) { |
| 105 | + return false, nil |
| 106 | + } else if err != nil { |
| 107 | + return false, err |
| 108 | + } |
| 109 | + |
| 110 | + return true, nil |
| 111 | + } |
| 112 | + |
| 113 | + if e.Kind == event.KindApproval { |
| 114 | + a, err := s.i.FindApprovalByID(ctx, e.ApprovalID) |
| 115 | + if err != nil { |
| 116 | + return false, err |
| 117 | + } |
| 118 | + |
| 119 | + d, err := s.i.FindDeploymentByID(ctx, a.DeploymentID) |
| 120 | + if err != nil { |
| 121 | + return false, err |
| 122 | + } |
| 123 | + |
| 124 | + if _, err = s.i.FindPermOfRepo(ctx, d.Edges.Repo, u); ent.IsNotFound(err) { |
| 125 | + return false, nil |
| 126 | + } else if err != nil { |
| 127 | + return false, err |
| 128 | + } |
| 129 | + |
| 130 | + return true, nil |
| 131 | + } |
| 132 | + |
| 133 | + return false, fmt.Errorf("The type of event is not \"deployment\" or \"approval\".") |
| 134 | +} |
| 135 | + |
| 136 | +func randstr() string { |
| 137 | + var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 138 | + |
| 139 | + b := make([]rune, 4) |
| 140 | + for i := range b { |
| 141 | + b[i] = letterRunes[rand.Intn(len(letterRunes))] |
| 142 | + } |
| 143 | + return string(b) |
| 144 | +} |
0 commit comments