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
···
}
}
-
func (ix *Indexer) NewPullMerged(ctx context.Context, pull *models.Pull) {
-
l := log.FromContext(ctx).With("notifier", "indexer", "pull", pull)
-
l.Debug("updating a pr")
-
err := ix.Pulls.Index(ctx, pull)
-
if err != nil {
-
l.Error("failed to index a pr", "err", err)
-
}
-
}
-
-
func (ix *Indexer) NewPullClosed(ctx context.Context, pull *models.Pull) {
+
func (ix *Indexer) NewPullState(ctx context.Context, pull *models.Pull) {
l := log.FromContext(ctx).With("notifier", "indexer", "pull", pull)
l.Debug("updating a pr")
err := ix.Pulls.Index(ctx, pull)
+12 -93
appview/notify/db/db.go
···
)
}
-
func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {
+
func (n *databaseNotifier) NewPullState(ctx context.Context, pull *models.Pull) {
// Get repo details
repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt)))
if err != nil {
-
log.Printf("NewPullMerged: failed to get repos: %v", err)
+
log.Printf("NewPullState: failed to get repos: %v", err)
return
}
···
}
actorDid := syntax.DID(repo.Did)
-
eventType := models.NotificationTypePullMerged
entityType := "pull"
entityId := pull.PullAt().String()
repoId := &repo.Id
var issueId *int64
-
p := int64(pull.ID)
-
pullId := &p
-
-
n.notifyEvent(
-
actorDid,
-
recipients,
-
eventType,
-
entityType,
-
entityId,
-
repoId,
-
issueId,
-
pullId,
-
)
-
}
-
-
func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
-
// Get repo details
-
repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt)))
-
if err != nil {
-
log.Printf("NewPullMerged: failed to get repos: %v", err)
-
return
-
}
-
-
// build up the recipients list:
-
// - repo owner
-
// - all pull participants
-
var recipients []syntax.DID
-
recipients = append(recipients, syntax.DID(repo.Did))
-
collaborators, err := db.GetCollaborators(n.db, db.FilterEq("repo_at", repo.RepoAt()))
-
if err != nil {
-
log.Printf("failed to fetch collaborators: %v", err)
-
return
-
}
-
for _, c := range collaborators {
-
recipients = append(recipients, c.SubjectDid)
-
}
-
for _, p := range pull.Participants() {
-
recipients = append(recipients, syntax.DID(p))
-
}
-
-
actorDid := syntax.DID(repo.Did)
-
eventType := models.NotificationTypePullClosed
-
entityType := "pull"
-
entityId := pull.PullAt().String()
-
repoId := &repo.Id
-
var issueId *int64
-
p := int64(pull.ID)
-
pullId := &p
-
-
n.notifyEvent(
-
actorDid,
-
recipients,
-
eventType,
-
entityType,
-
entityId,
-
repoId,
-
issueId,
-
pullId,
-
)
-
}
-
-
func (n *databaseNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) {
-
// Get repo details
-
repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt)))
-
if err != nil {
-
log.Printf("NewPullMerged: failed to get repos: %v", err)
-
return
-
}
-
-
// build up the recipients list:
-
// - repo owner
-
// - all pull participants
-
var recipients []syntax.DID
-
recipients = append(recipients, syntax.DID(repo.Did))
-
collaborators, err := db.GetCollaborators(n.db, db.FilterEq("repo_at", repo.RepoAt()))
-
if err != nil {
-
log.Printf("failed to fetch collaborators: %v", err)
+
var eventType models.NotificationType
+
switch pull.State {
+
case models.PullClosed:
+
eventType = models.NotificationTypePullClosed
+
case models.PullOpen:
+
eventType = models.NotificationTypePullReopen
+
case models.PullMerged:
+
eventType = models.NotificationTypePullMerged
+
default:
+
log.Println("NewPullState: unexpected new PR state:", pull.State)
return
}
-
for _, c := range collaborators {
-
recipients = append(recipients, c.SubjectDid)
-
}
-
for _, p := range pull.Participants() {
-
recipients = append(recipients, syntax.DID(p))
-
}
-
-
actorDid := syntax.DID(repo.Did)
-
eventType := models.NotificationTypePullReopen
-
entityType := "pull"
-
entityId := pull.PullAt().String()
-
repoId := &repo.Id
-
var issueId *int64
p := int64(pull.ID)
pullId := &p
+2 -10
appview/notify/merged_notifier.go
···
m.fanout("NewPullComment", ctx, comment)
}
-
func (m *mergedNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {
-
m.fanout("NewPullMerged", ctx, pull)
-
}
-
-
func (m *mergedNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
-
m.fanout("NewPullClosed", ctx, pull)
-
}
-
-
func (m *mergedNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) {
-
m.fanout("NewPullReopen", ctx, pull)
+
func (m *mergedNotifier) NewPullState(ctx context.Context, pull *models.Pull) {
+
m.fanout("NewPullState", ctx, pull)
}
func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
+2 -6
appview/notify/notifier.go
···
NewPull(ctx context.Context, pull *models.Pull)
NewPullComment(ctx context.Context, comment *models.PullComment)
-
NewPullMerged(ctx context.Context, pull *models.Pull)
-
NewPullClosed(ctx context.Context, pull *models.Pull)
-
NewPullReopen(ctx context.Context, pull *models.Pull)
+
NewPullState(ctx context.Context, pull *models.Pull)
UpdateProfile(ctx context.Context, profile *models.Profile)
···
func (m *BaseNotifier) NewPull(ctx context.Context, pull *models.Pull) {}
func (m *BaseNotifier) NewPullComment(ctx context.Context, models *models.PullComment) {}
-
func (m *BaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {}
-
func (m *BaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {}
-
func (m *BaseNotifier) NewPullReopen(ctx context.Context, pull *models.Pull) {}
+
func (m *BaseNotifier) NewPullState(ctx context.Context, pull *models.Pull) {}
func (m *BaseNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {}
+14 -2
appview/notify/posthog/notifier.go
···
}
}
-
func (n *posthogNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {
+
func (n *posthogNotifier) NewPullState(ctx context.Context, pull *models.Pull) {
+
var event string
+
switch pull.State {
+
case models.PullClosed:
+
event = "pull_closed"
+
case models.PullOpen:
+
event = "pull_reopen"
+
case models.PullMerged:
+
event = "pull_merged"
+
default:
+
log.Println("posthog: unexpected new PR state:", pull.State)
+
return
+
}
err := n.client.Enqueue(posthog.Capture{
DistinctId: pull.OwnerDid,
-
Event: "pull_merged",
+
Event: event,
Properties: posthog.Properties{
"repo_at": pull.RepoAt,
"pull_id": pull.PullId,