Skip to content

Commit

Permalink
webhooks: handle ref and tag deletion events (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysedwinistrator authored Mar 4, 2024
1 parent 5dcfba6 commit 5aca2a0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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) processJobEvent(ctx context.Context, e goGitlab.JobEvent) {
))
}

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

0 comments on commit 5aca2a0

Please sign in to comment.