Monorepo for Tangled tangled.org

appview/state: integrate webhooks with gitRefUpdate events #1068

merged opened by anirudh.fi targeting master from icy/qlyxxp

Trigger webhooks on git push events from knotstream.

Signed-off-by: Anirudh Oppiliappan anirudh@tangled.org

Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:hwevmowznbiukdf6uk5dwrrq/sh.tangled.repo.pull/3menyebs7t622
+58 -23
Diff #5
+42 -11
appview/state/knotstream.go
··· 8 8 "slices" 9 9 "time" 10 10 11 + "tangled.org/core/appview/notify" 12 + 11 13 "tangled.org/core/api/tangled" 12 14 "tangled.org/core/appview/cache" 13 15 "tangled.org/core/appview/config" ··· 25 27 "github.com/posthog/posthog-go" 26 28 ) 27 29 28 - func Knotstream(ctx context.Context, c *config.Config, d *db.DB, enforcer *rbac.Enforcer, posthog posthog.Client) (*ec.Consumer, error) { 30 + func Knotstream(ctx context.Context, c *config.Config, d *db.DB, enforcer *rbac.Enforcer, posthog posthog.Client, notifier notify.Notifier) (*ec.Consumer, error) { 29 31 logger := log.FromContext(ctx) 30 32 logger = log.SubLogger(logger, "knotstream") 31 33 ··· 48 50 49 51 cfg := ec.ConsumerConfig{ 50 52 Sources: srcs, 51 - ProcessFunc: knotIngester(d, enforcer, posthog, c.Core.Dev), 53 + ProcessFunc: knotIngester(d, enforcer, posthog, notifier, c.Core.Dev), 52 54 RetryInterval: c.Knotstream.RetryInterval, 53 55 MaxRetryInterval: c.Knotstream.MaxRetryInterval, 54 56 ConnectionTimeout: c.Knotstream.ConnectionTimeout, ··· 62 64 return ec.NewConsumer(cfg), nil 63 65 } 64 66 65 - func knotIngester(d *db.DB, enforcer *rbac.Enforcer, posthog posthog.Client, dev bool) ec.ProcessFunc { 67 + func knotIngester(d *db.DB, enforcer *rbac.Enforcer, posthog posthog.Client, notifier notify.Notifier, dev bool) ec.ProcessFunc { 66 68 return func(ctx context.Context, source ec.Source, msg ec.Message) error { 67 69 switch msg.Nsid { 68 70 case tangled.GitRefUpdateNSID: 69 - return ingestRefUpdate(d, enforcer, posthog, dev, source, msg) 71 + return ingestRefUpdate(d, enforcer, posthog, notifier, dev, source, msg, ctx) 70 72 case tangled.PipelineNSID: 71 73 return ingestPipeline(d, source, msg) 72 74 } ··· 75 77 } 76 78 } 77 79 78 - func ingestRefUpdate(d *db.DB, enforcer *rbac.Enforcer, pc posthog.Client, dev bool, source ec.Source, msg ec.Message) error { 80 + func ingestRefUpdate(d *db.DB, enforcer *rbac.Enforcer, pc posthog.Client, notifier notify.Notifier, dev bool, source ec.Source, msg ec.Message, ctx context.Context) error { 81 + logger := log.FromContext(ctx) 82 + 79 83 var record tangled.GitRefUpdate 80 84 err := json.Unmarshal(msg.EventJson, &record) 81 85 if err != nil { ··· 90 94 return fmt.Errorf("%s does not belong to %s, something is fishy", record.CommitterDid, source.Key()) 91 95 } 92 96 93 - err1 := populatePunchcard(d, record) 94 - err2 := updateRepoLanguages(d, record) 97 + logger.Info("processing gitRefUpdate event", 98 + "repo_did", record.RepoDid, 99 + "repo_name", record.RepoName, 100 + "ref", record.Ref, 101 + "old_sha", record.OldSha, 102 + "new_sha", record.NewSha) 103 + 104 + // trigger webhook notifications first (before other ops that might fail) 105 + var errWebhook error 106 + repos, err := db.GetRepos( 107 + d, 108 + 0, 109 + orm.FilterEq("did", record.RepoDid), 110 + orm.FilterEq("name", record.RepoName), 111 + ) 112 + if err != nil { 113 + errWebhook = fmt.Errorf("failed to lookup repo for webhooks: %w", err) 114 + } else if len(repos) == 1 { 115 + notifier.Push(ctx, &repos[0], record.Ref, record.OldSha, record.NewSha, record.CommitterDid) 116 + } else if len(repos) == 0 { 117 + errWebhook = fmt.Errorf("no repo found for webhooks: %s/%s", record.RepoDid, record.RepoName) 118 + } 119 + 120 + errPunchcard := populatePunchcard(d, record) 121 + errLanguages := updateRepoLanguages(d, record) 95 122 96 - var err3 error 97 - if !dev { 98 - err3 = pc.Enqueue(posthog.Capture{ 123 + var errPosthog error 124 + if !dev && record.CommitterDid != "" { 125 + errPosthog = pc.Enqueue(posthog.Capture{ 99 126 DistinctId: record.CommitterDid, 100 127 Event: "git_ref_update", 101 128 }) 102 129 } 103 130 104 - return errors.Join(err1, err2, err3) 131 + return errors.Join(errWebhook, errPunchcard, errLanguages, errPosthog) 105 132 } 106 133 107 134 func populatePunchcard(d *db.DB, record tangled.GitRefUpdate) error { 135 + if record.CommitterDid == "" { 136 + return nil 137 + } 138 + 108 139 knownEmails, err := db.GetAllEmails(d, record.CommitterDid) 109 140 if err != nil { 110 141 return err
+16 -12
appview/state/state.go
··· 151 151 return nil, fmt.Errorf("failed to start jetstream watcher: %w", err) 152 152 } 153 153 154 - knotstream, err := Knotstream(ctx, config, d, enforcer, posthog) 155 - if err != nil { 156 - return nil, fmt.Errorf("failed to start knotstream consumer: %w", err) 157 - } 158 - knotstream.Start(ctx) 159 - 160 - spindlestream, err := Spindlestream(ctx, config, d, enforcer) 161 - if err != nil { 162 - return nil, fmt.Errorf("failed to start spindlestream consumer: %w", err) 163 - } 164 - spindlestream.Start(ctx) 165 - 166 154 var notifiers []notify.Notifier 167 155 168 156 // Always add the database notifier ··· 173 161 notifiers = append(notifiers, phnotify.NewPosthogNotifier(posthog)) 174 162 } 175 163 notifiers = append(notifiers, indexer) 164 + 165 + // Add webhook notifier 166 + notifiers = append(notifiers, notify.NewWebhookNotifier(d)) 167 + 176 168 notifier := notify.NewMergedNotifier(notifiers) 177 169 notifier = notify.NewLoggingNotifier(notifier, tlog.SubLogger(logger, "notify")) 178 170 171 + knotstream, err := Knotstream(ctx, config, d, enforcer, posthog, notifier) 172 + if err != nil { 173 + return nil, fmt.Errorf("failed to start knotstream consumer: %w", err) 174 + } 175 + knotstream.Start(ctx) 176 + 177 + spindlestream, err := Spindlestream(ctx, config, d, enforcer) 178 + if err != nil { 179 + return nil, fmt.Errorf("failed to start spindlestream consumer: %w", err) 180 + } 181 + spindlestream.Start(ctx) 182 + 179 183 state := &State{ 180 184 d, 181 185 notifier,

History

6 rounds 1 comment
sign up or login to add to the discussion
1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 0 comments
pull request successfully merged
1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 0 comments
1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 0 comments
1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 0 comments
1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 1 comment
  • here why was the knownKnots check removed?
  • the other ops can fail, but they are non-returning fails, the errors are Joined, we can do the same here (collect the error and join in the final statement

changeset lgtm otherwise!

1 commit
expand
appview/state: integrate webhooks with gitRefUpdate events
3/3 success
expand
expand 0 comments