Skip to content

Commit

Permalink
Implement Gitlab event handling
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Sep 20, 2024
1 parent 6a3bef1 commit 73f6227
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/providers/gitlab/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/stacklok/minder/internal/config/server"
"github.com/stacklok/minder/internal/crypto"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/events"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/gitlab"
v1 "github.com/stacklok/minder/pkg/providers/v1"
Expand All @@ -41,6 +42,7 @@ type providerClassManager struct {
glpcfg *server.GitLabConfig
webhookURL string
parentContext context.Context
pub events.Publisher

// secrets for the webhook. These are stored in the
// structure to allow efficient fetching. Rotation
Expand All @@ -51,7 +53,8 @@ type providerClassManager struct {

// NewGitLabProviderClassManager creates a new provider class manager for the dockerhub provider
func NewGitLabProviderClassManager(
ctx context.Context, crypteng crypto.Engine, store db.Store, cfg *server.GitLabConfig, wgCfg server.WebhookConfig,
ctx context.Context, crypteng crypto.Engine, store db.Store, pub events.Publisher,
cfg *server.GitLabConfig, wgCfg server.WebhookConfig,
) (*providerClassManager, error) {
webhookURLBase := wgCfg.ExternalWebhookURL
if webhookURLBase == "" {
Expand Down Expand Up @@ -80,6 +83,7 @@ func NewGitLabProviderClassManager(
return &providerClassManager{
store: store,
crypteng: crypteng,
pub: pub,
glpcfg: cfg,
webhookURL: webhookURL,
parentContext: ctx,
Expand Down
22 changes: 20 additions & 2 deletions internal/providers/gitlab/manager/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/google/uuid"
"github.com/rs/zerolog"
gitlablib "github.com/xanzy/go-gitlab"

"github.com/stacklok/minder/internal/providers/gitlab/webhooksecret"
)
Expand All @@ -47,13 +48,30 @@ func (m *providerClassManager) GetWebhookHandler() http.Handler {
return
}

l.Debug().Msg("received webhook")
eventType := gitlablib.HookEventType(r)
if eventType == "" {
l.Error().Msg("missing X-Gitlab-Event header")
http.Error(w, "missing X-Gitlab-Event header", http.StatusBadRequest)
return
}

l = l.With().Str("event", string(eventType)).Logger()

disp := m.getWebhookEventDispatcher(eventType)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / compose-migrate / docker

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / build / Verify build

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / image-build / Image build

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / lint / Run golangci-lint

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher) (typecheck)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / lint / Run golangci-lint

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)) (typecheck)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / test / Coverage

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

Check failure on line 60 in internal/providers/gitlab/manager/webhook.go

View workflow job for this annotation

GitHub Actions / test / Unit testing

m.getWebhookEventDispatcher undefined (type *providerClassManager has no field or method getWebhookEventDispatcher)

if err := disp(l, r); err != nil {
l.Error().Err(err).Msg("error handling webhook event")
http.Error(w, "error handling webhook event", http.StatusInternalServerError)
return
}

l.Debug().Msg("processed webhook event successfully")
})
}

func (m *providerClassManager) validateRequest(r *http.Request) error {
// Validate the webhook secret
gltok := r.Header.Get("X-Gitlab-Token")
gltok := gitlablib.HookEventToken(r)
if gltok == "" {
return errors.New("missing X-Gitlab-Token header")
}
Expand Down
7 changes: 7 additions & 0 deletions internal/providers/gitlab/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func (c *gitlabClient) PropertiesToProtoMessage(
return repoV1FromProperties(props)
}

// FormatRepositoryUpstreamID returns the upstream ID for a gitlab project
// This is done so we don't have to deal with conversions in the provider
// when dealing with entities
func FormatRepositoryUpstreamID(id int) string {
return fmt.Sprintf("gitlab:%d", id)
}

func getStringProp(props *properties.Properties, key string) (string, error) {
value, err := props.GetProperty(key).AsString()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/providers/gitlab/repository_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func gitlabProjectToProperties(proj *gitlab.Project) (*properties.Properties, er
}

outProps, err := properties.NewProperties(map[string]any{
properties.PropertyUpstreamID: fmt.Sprintf("%d", proj.ID),
properties.PropertyUpstreamID: FormatRepositoryUpstreamID(proj.ID),
properties.PropertyName: formatRepoName(owner, proj.Name),
properties.RepoPropertyIsPrivate: proj.Visibility == gitlab.PrivateVisibility,
properties.RepoPropertyIsArchived: proj.Archived,
Expand Down
1 change: 1 addition & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func AllInOneServerService(
ctx,
cryptoEngine,
store,
evt,
cfg.Provider.GitLab,
cfg.WebhookConfig,
)
Expand Down

0 comments on commit 73f6227

Please sign in to comment.