Skip to content

Commit

Permalink
chore(webhook): abstract event processing into generic function
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Dec 29, 2024
1 parent 3647d52 commit b382b31
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions webhook/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,31 @@ func (d *Dispatcher) RegisterBugCreateListener(listeners ...BugCreateListener) {
d.bugCreateListeners = append(d.bugCreateListeners, listeners...)
}

func (d *Dispatcher) processStoryCreate(ctx context.Context, event *StoryCreateEvent) error {
func processEvent[L, E any](ctx context.Context, listeners []L, event E, fn func(L, E) error) error {

Check warning on line 95 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L95

Added line #L95 was not covered by tests
eg, ctx := errgroup.WithContext(ctx)

Check failure on line 96 in webhook/dispatcher.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to ctx (ineffassign)
for _, listener := range d.storyCreateListeners {
for _, listener := range listeners {
listener := listener

Check failure on line 98 in webhook/dispatcher.go

View workflow job for this annotation

GitHub Actions / lint

The copy of the 'for' variable "listener" can be deleted (Go 1.22+) (copyloopvar)

Check warning on line 98 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L97-L98

Added lines #L97 - L98 were not covered by tests
eg.Go(func() error {
return listener.OnStoryCreate(ctx, event)
return fn(listener, event)

Check warning on line 100 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L100

Added line #L100 was not covered by tests
})
}
return eg.Wait()
}

func (d *Dispatcher) processStoryCreate(ctx context.Context, event *StoryCreateEvent) error {
return processEvent(ctx, d.storyCreateListeners, event, func(listener StoryCreateListener, event *StoryCreateEvent) error {
return listener.OnStoryCreate(ctx, event)
})

Check warning on line 109 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L106-L109

Added lines #L106 - L109 were not covered by tests
}

func (d *Dispatcher) processStoryUpdate(ctx context.Context, event *StoryUpdateEvent) error {
eg, ctx := errgroup.WithContext(ctx)
for _, listener := range d.storyUpdateListeners {
eg.Go(func() error {
return listener.OnStoryUpdate(ctx, event)
})
}
return eg.Wait()
return processEvent(ctx, d.storyUpdateListeners, event, func(listener StoryUpdateListener, event *StoryUpdateEvent) error {
return listener.OnStoryUpdate(ctx, event)
})

Check warning on line 115 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L113-L115

Added lines #L113 - L115 were not covered by tests
}

func (d *Dispatcher) processBugCreate(ctx context.Context, event *BugCreateEvent) error {
eg, ctx := errgroup.WithContext(ctx)
for _, listener := range d.bugCreateListeners {
eg.Go(func() error {
return listener.OnBugCreate(ctx, event)
})
}
return eg.Wait()
return processEvent(ctx, d.bugCreateListeners, event, func(listener BugCreateListener, event *BugCreateEvent) error {
return listener.OnBugCreate(ctx, event)
})

Check warning on line 121 in webhook/dispatcher.go

View check run for this annotation

Codecov / codecov/patch

webhook/dispatcher.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}

0 comments on commit b382b31

Please sign in to comment.