Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhooks: handle ref and tag deletion events #794

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/controller/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (c *Controller) WebhookHandler(w http.ResponseWriter, r *http.Request) {
go c.processJobEvent(ctx, *event)
case *gitlab.DeploymentEvent:
go c.processDeploymentEvent(ctx, *event)
case *gitlab.PushEvent:
go c.processPushEvent(ctx, *event)
case *gitlab.TagEvent:
go c.processTagEvent(ctx, *event)
default:
logger.
WithField("event-type", reflect.TypeOf(event).String()).
Expand Down
56 changes: 56 additions & 0 deletions pkg/controller/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,62 @@
))
}

func (c *Controller) processPushEvent(ctx context.Context, e goGitlab.PushEvent) {
if e.CheckoutSHA == "" {
var (
refKind = schemas.RefKindBranch
refName string
)

// branch refs in push events have "refs/heads/" prefix
if branch, found := strings.CutPrefix(e.Ref, "refs/heads/"); found {
refName = branch
} else {
log.WithContext(ctx).
WithFields(log.Fields{
"project-name": e.Project.Name,
"ref": e.Ref,
}).
Error("extracting branch name from ref")
return

Check failure on line 86 in pkg/controller/webhooks.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

return with no blank line before (nlreturn)
}

deleteRef(ctx, c.Store, schemas.NewRef(
schemas.NewProject(e.Project.PathWithNamespace),
refKind,
refName,
), "received branch deletion push event from webhook")
}
}

func (c *Controller) processTagEvent(ctx context.Context, e goGitlab.TagEvent) {
if e.CheckoutSHA == "" {
var (
refKind = schemas.RefKindTag
refName string
)

// tags refs in tag events have "refs/tags/" prefix
if tag, found := strings.CutPrefix(e.Ref, "refs/tags/"); found {
refName = tag
} else {
log.WithContext(ctx).
WithFields(log.Fields{
"project-name": e.Project.Name,
"ref": e.Ref,
}).
Error("extracting tag name from ref")
return

Check failure on line 114 in pkg/controller/webhooks.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

return with no blank line before (nlreturn)
}

deleteRef(ctx, c.Store, schemas.NewRef(
schemas.NewProject(e.Project.PathWithNamespace),
refKind,
refName,
), "received tag deletion tag event from webhook")
}
}

func (c *Controller) triggerRefMetricsPull(ctx context.Context, ref schemas.Ref) {
logFields := log.Fields{
"project-name": ref.Project.Name,
Expand Down
Loading