Skip to content

Commit e7d9578

Browse files
authored
fix: add context to webhook creator (#39)
1 parent 1c098a5 commit e7d9578

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

pkg/server/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig,
1111

1212
srv := NewServer(cfg, clients)
1313
gracefulShutdownHandler := NewGracefulShutdown(ctx, stop)
14-
srv.Start()
14+
srv.Start(ctx)
1515

1616
gracefulShutdownHandler.Shutdown(srv)
1717

pkg/server/server.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"context"
45
"github.com/gin-gonic/gin"
56
"github.com/quickube/piper/pkg/clients"
67
"github.com/quickube/piper/pkg/conf"
@@ -28,6 +29,7 @@ func (s *Server) startServer() *http.Server {
2829
}
2930

3031
go func() {
32+
log.Printf("Server is listening on %s", s.httpServer.Addr)
3133
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
3234
log.Fatalf("listen: %s\n", err)
3335
}
@@ -53,18 +55,18 @@ func (s *Server) getRoutes() {
5355
routes.AddWebhookRoutes(s.config, s.clients, v1, s.webhookCreator)
5456
}
5557

56-
func (s *Server) startServices() {
57-
s.webhookCreator.Start()
58+
func (s *Server) startServices(ctx context.Context) {
59+
s.webhookCreator.Start(ctx)
5860
}
5961

60-
func (s *Server) Start() {
62+
func (s *Server) Start(ctx context.Context) {
6163

6264
s.registerMiddlewares()
6365

6466
s.getRoutes()
6567

6668
s.httpServer = s.startServer()
6769

68-
s.startServices()
70+
s.startServices(ctx)
6971

7072
}

pkg/webhook_creator/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func (wc *WebhookCreatorImpl) GetHooks() *map[int64]*git_provider.HookWithStatus
3535
return &wc.hooks
3636
}
3737

38-
func (wc *WebhookCreatorImpl) Start() {
38+
func (wc *WebhookCreatorImpl) Start(ctx context.Context) {
3939

40-
err := wc.initWebhooks()
40+
err := wc.initWebhooks(ctx)
4141
if err != nil {
4242
log.Print(err)
4343
panic("failed in initializing webhooks")
@@ -85,9 +85,8 @@ func (wc *WebhookCreatorImpl) setAllHooksHealth(status bool) {
8585
log.Printf("set all hooks health status for to %s", strconv.FormatBool(status))
8686
}
8787

88-
func (wc *WebhookCreatorImpl) initWebhooks() error {
88+
func (wc *WebhookCreatorImpl) initWebhooks(ctx context.Context) error {
8989

90-
ctx := context.Background()
9190
if wc.cfg.GitProviderConfig.OrgLevelWebhook && len(wc.cfg.GitProviderConfig.RepoList) != 0 {
9291
return fmt.Errorf("org level webhook wanted but provided repositories list")
9392
} else if !wc.cfg.GitProviderConfig.OrgLevelWebhook && len(wc.cfg.GitProviderConfig.RepoList) == 0 {

pkg/webhook_creator/tests_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func TestWebhookCreatorImpl_SetAllHooksHealth(t *testing.T) {
126126

127127
func TestWebhookCreatorImpl_InitWebhooks(t *testing.T) {
128128
assertion := assert.New(t)
129+
ctx := context.Background()
129130
// Mock the necessary methods of the GitProvider client
130131
mockClient := &MockGitProviderClient{
131132
SetWebhookFunc: func(ctx context.Context, repoName *string) (*git_provider.HookWithStatus, error) {
@@ -149,7 +150,7 @@ func TestWebhookCreatorImpl_InitWebhooks(t *testing.T) {
149150
})
150151

151152
// Initialize the webhooks
152-
err := wc.initWebhooks()
153+
err := wc.initWebhooks(ctx)
153154

154155
// Run tests
155156
assertion.NoError(err)

0 commit comments

Comments
 (0)