appview/notify: merge NewPullClosed/Merged/Reopen to NewPullState #711

merged
opened by boltless.me targeting master from boltless.me/core: feat/search

same to NewIssueState, we can determine the detailed event type from latest pull state

Signed-off-by: Seongmin Lee git@boltless.me

Changed files
+31 -121
appview
+1 -10
appview/indexer/notifier.go
··· 46 46 } 47 47 } 48 48 49 - func (ix *Indexer) NewPullMerged(ctx context.Context, pull *models.Pull) { 50 - l := log.FromContext(ctx).With("notifier", "indexer", "pull", pull) 51 - l.Debug("updating a pr") 52 - err := ix.Pulls.Index(ctx, pull) 53 - if err != nil { 54 - l.Error("failed to index a pr", "err", err) 55 - } 56 - } 57 - 58 - func (ix *Indexer) NewPullClosed(ctx context.Context, pull *models.Pull) { 49 + func (ix *Indexer) NewPullState(ctx context.Context, pull *models.Pull) { 59 50 l := log.FromContext(ctx).With("notifier", "indexer", "pull", pull) 60 51 l.Debug("updating a pr") 61 52 err := ix.Pulls.Index(ctx, pull)
+12 -93
appview/notify/db/db.go
··· 328 328 ) 329 329 } 330 330 331 - func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { 331 + func (n *databaseNotifier) NewPullState(ctx context.Context, pull *models.Pull) { 332 332 // Get repo details 333 333 repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) 334 334 if err != nil { 335 - log.Printf("NewPullMerged: failed to get repos: %v", err) 335 + log.Printf("NewPullState: failed to get repos: %v", err) 336 336 return 337 337 } 338 338 ··· 354 354 } 355 355 356 356 actorDid := syntax.DID(repo.Did) 357 - eventType := models.NotificationTypePullMerged 358 357 entityType := "pull" 359 358 entityId := pull.PullAt().String() 360 359 repoId := &repo.Id 361 360 var issueId *int64 362 - p := int64(pull.ID) 363 - pullId := &p 364 - 365 - n.notifyEvent( 366 - actorDid, 367 - recipients, 368 - eventType, 369 - entityType, 370 - entityId, 371 - repoId, 372 - issueId, 373 - pullId, 374 - ) 375 - } 376 - 377 - func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { 378 - // Get repo details 379 - repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) 380 - if err != nil { 381 - log.Printf("NewPullMerged: failed to get repos: %v", err) 382 - return 383 - } 384 - 385 - // build up the recipients list: 386 - // - repo owner 387 - // - all pull participants 388 - var recipients []syntax.DID 389 - recipients = append(recipients, syntax.DID(repo.Did)) 390 - collaborators, err := db.GetCollaborators(n.db, db.FilterEq("repo_at", repo.RepoAt())) 391 - if err != nil { 392 - log.Printf("failed to fetch collaborators: %v", err) 393 - return 394 - } 395 - for _, c := range collaborators { 396 - recipients = append(recipients, c.SubjectDid) 397 - } 398 - for _, p := range pull.Participants() { 399 - recipients = append(recipients, syntax.DID(p)) 400 - } 401 - 402 - actorDid := syntax.DID(repo.Did) 403 - eventType := models.NotificationTypePullClosed 404 - entityType := "pull" 405 - entityId := pull.PullAt().String() 406 - repoId := &repo.Id 407 - var issueId *int64 408 - p := int64(pull.ID) 409 - pullId := &p 410 - 411 - n.notifyEvent( 412 - actorDid, 413 - recipients, 414 - eventType, 415 - entityType, 416 - entityId, 417 - repoId, 418 - issueId, 419 - pullId, 420 - ) 421 - } 422 - 423 - func (n *databaseNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) { 424 - // Get repo details 425 - repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) 426 - if err != nil { 427 - log.Printf("NewPullMerged: failed to get repos: %v", err) 428 - return 429 - } 430 - 431 - // build up the recipients list: 432 - // - repo owner 433 - // - all pull participants 434 - var recipients []syntax.DID 435 - recipients = append(recipients, syntax.DID(repo.Did)) 436 - collaborators, err := db.GetCollaborators(n.db, db.FilterEq("repo_at", repo.RepoAt())) 437 - if err != nil { 438 - log.Printf("failed to fetch collaborators: %v", err) 361 + var eventType models.NotificationType 362 + switch pull.State { 363 + case models.PullClosed: 364 + eventType = models.NotificationTypePullClosed 365 + case models.PullOpen: 366 + eventType = models.NotificationTypePullReopen 367 + case models.PullMerged: 368 + eventType = models.NotificationTypePullMerged 369 + default: 370 + log.Println("NewPullState: unexpected new PR state:", pull.State) 439 371 return 440 372 } 441 - for _, c := range collaborators { 442 - recipients = append(recipients, c.SubjectDid) 443 - } 444 - for _, p := range pull.Participants() { 445 - recipients = append(recipients, syntax.DID(p)) 446 - } 447 - 448 - actorDid := syntax.DID(repo.Did) 449 - eventType := models.NotificationTypePullReopen 450 - entityType := "pull" 451 - entityId := pull.PullAt().String() 452 - repoId := &repo.Id 453 - var issueId *int64 454 373 p := int64(pull.ID) 455 374 pullId := &p 456 375
+2 -10
appview/notify/merged_notifier.go
··· 85 85 m.fanout("NewPullComment", ctx, comment) 86 86 } 87 87 88 - func (m *mergedNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { 89 - m.fanout("NewPullMerged", ctx, pull) 90 - } 91 - 92 - func (m *mergedNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { 93 - m.fanout("NewPullClosed", ctx, pull) 94 - } 95 - 96 - func (m *mergedNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) { 97 - m.fanout("NewPullReopen", ctx, pull) 88 + func (m *mergedNotifier) NewPullState(ctx context.Context, pull *models.Pull) { 89 + m.fanout("NewPullState", ctx, pull) 98 90 } 99 91 100 92 func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
+2 -6
appview/notify/notifier.go
··· 22 22 23 23 NewPull(ctx context.Context, pull *models.Pull) 24 24 NewPullComment(ctx context.Context, comment *models.PullComment) 25 - NewPullMerged(ctx context.Context, pull *models.Pull) 26 - NewPullClosed(ctx context.Context, pull *models.Pull) 27 - NewPullReopen(ctx context.Context, pull *models.Pull) 25 + NewPullState(ctx context.Context, pull *models.Pull) 28 26 29 27 UpdateProfile(ctx context.Context, profile *models.Profile) 30 28 ··· 53 51 54 52 func (m *BaseNotifier) NewPull(ctx context.Context, pull *models.Pull) {} 55 53 func (m *BaseNotifier) NewPullComment(ctx context.Context, models *models.PullComment) {} 56 - func (m *BaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {} 57 - func (m *BaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {} 58 - func (m *BaseNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) {} 54 + func (m *BaseNotifier) NewPullState(ctx context.Context, pull *models.Pull) {} 59 55 60 56 func (m *BaseNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {} 61 57
+14 -2
appview/notify/posthog/notifier.go
··· 210 210 } 211 211 } 212 212 213 - func (n *posthogNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { 213 + func (n *posthogNotifier) NewPullState(ctx context.Context, pull *models.Pull) { 214 + var event string 215 + switch pull.State { 216 + case models.PullClosed: 217 + event = "pull_closed" 218 + case models.PullOpen: 219 + event = "pull_reopen" 220 + case models.PullMerged: 221 + event = "pull_merged" 222 + default: 223 + log.Println("posthog: unexpected new PR state:", pull.State) 224 + return 225 + } 214 226 err := n.client.Enqueue(posthog.Capture{ 215 227 DistinctId: pull.OwnerDid, 216 - Event: "pull_merged", 228 + Event: event, 217 229 Properties: posthog.Properties{ 218 230 "repo_at": pull.RepoAt, 219 231 "pull_id": pull.PullId,