Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(VCS): leave PushEventsBranchFilter blank when creating webhook in GitLab repository (#2639)

h3n4l551e77ffdeea
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 02a280af98..39f8d6f792 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -58,14 +58,7 @@ type WebhookCreate struct {
// So we can't possibly display useful info when reviewing a MR, thus we don't enable this event.
// Saying that, delivering a souding dry run solution would be great and hopefully we can achieve that one day.
// MergeRequestsEvents bool `json:"merge_requests_events"`
- PushEventsBranchFilter string `json:"push_events_branch_filter"`
- EnableSSLVerification bool `json:"enable_ssl_verification"`
-}
-
-// WebhookUpdate represents a GitLab API request for updating a new webhook.
-type WebhookUpdate struct {
- URL string `json:"url"`
- PushEventsBranchFilter string `json:"push_events_branch_filter"`
+ EnableSSLVerification bool `json:"enable_ssl_verification"`
}
// WebhookProject is the API message for webhook project.
diff --git a/server/project.go b/server/project.go
index 4a4143c9e7..69a30b183e 100644
--- a/server/project.go
+++ b/server/project.go
@@ -260,11 +260,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
switch vcs.Type {
case vcsPlugin.GitLabSelfHost:
webhookCreate := gitlab.WebhookCreate{
- URL: fmt.Sprintf("%s/%s/%s", s.profile.ExternalURL, gitlabWebhookPath, repositoryCreate.WebhookEndpointID),
- SecretToken: repositoryCreate.WebhookSecretToken,
- PushEvents: true,
- PushEventsBranchFilter: repositoryCreate.BranchFilter,
- EnableSSLVerification: false, // TODO(tianzhou): This is set to false, be lax to not enable_ssl_verification
+ URL: fmt.Sprintf("%s/%s/%s", s.profile.ExternalURL, gitlabWebhookPath, repositoryCreate.WebhookEndpointID),
+ SecretToken: repositoryCreate.WebhookSecretToken,
+ PushEvents: true,
+ EnableSSLVerification: false, // TODO(tianzhou): This is set to false, be lax to not enable_ssl_verification
}
webhookCreatePayload, err = json.Marshal(webhookCreate)
if err != nil {
@@ -430,65 +429,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update repository for project ID: %d", projectID)).SetInternal(err)
}
- if repoPatch.BranchFilter != nil {
- vcs, err := s.store.GetVCSByID(ctx, repo.VCSID)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update repository for project ID: %d", projectID)).SetInternal(err)
- }
- if vcs == nil {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found with ID: %d", repo.VCSID))
- }
- // Update the webhook after we successfully update the repository.
- // This is because in case the webhook update fails, we can still have a reconcile process to reconcile the webhook state.
- // If we update it before we update the repository, then if the repository update fails, then the reconcile process will reconcile the webhook to the pre-update state which is likely not intended.
- var webhookUpdatePayload []byte
- switch vcs.Type {
- case vcsPlugin.GitLabSelfHost:
- webhookUpdate := gitlab.WebhookUpdate{
- URL: fmt.Sprintf("%s/%s/%s", s.profile.ExternalURL, gitlabWebhookPath, updatedRepo.WebhookEndpointID),
- PushEventsBranchFilter: *repoPatch.BranchFilter,
- }
- webhookUpdatePayload, err = json.Marshal(webhookUpdate)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal request body for updating webhook %s for project ID: %v", repo.ExternalWebhookID, projectID)).SetInternal(err)
- }
- case vcsPlugin.GitHubCom:
- webhookUpdate := github.WebhookCreateOrUpdate{
- Config: github.WebhookConfig{
- URL: fmt.Sprintf("%s/%s/%s", s.profile.ExternalURL, githubWebhookPath, updatedRepo.WebhookEndpointID),
- ContentType: "json",
- Secret: updatedRepo.WebhookSecretToken,
- InsecureSSL: 1, // TODO: Allow user to specify this value through api.RepositoryPatch
- },
- Events: []string{"push"},
- }
- webhookUpdatePayload, err = json.Marshal(webhookUpdate)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal request body for updating webhook %s for project ID: %v", repo.ExternalWebhookID, projectID)).SetInternal(err)
- }
- }
-
- err = vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{}).PatchWebhook(
- ctx,
- common.OauthContext{
- // Need to get ApplicationID, Secret from vcs instead of repository.vcs since the latter is not composed.
- ClientID: vcs.ApplicationID,
- ClientSecret: vcs.Secret,
- AccessToken: repo.AccessToken,
- RefreshToken: repo.RefreshToken,
- Refresher: s.refreshToken(ctx, repo.ID),
- },
- vcs.InstanceURL,
- repo.ExternalID,
- repo.ExternalWebhookID,
- webhookUpdatePayload,
- )
-
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update webhook ID %s for project ID: %v", repo.ExternalWebhookID, projectID)).SetInternal(err)
- }
- }
-
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, updatedRepo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project repository response: %v", projectID)).SetInternal(err)
diff --git a/server/webhook.go b/server/webhook.go
index 9ab04aa57d..63620a5cfc 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -63,6 +63,12 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Webhook endpoint not found: %v", webhookEndpointID))
}
+ if branch, err := parseBranchNameFromRefs(pushEvent.Ref); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid ref").SetInternal(err)
+ } else if branch != repo.BranchFilter {
+ return c.String(http.StatusOK, "")
+ }
+
if repo.VCS == nil {
err := errors.Errorf("VCS not found for ID: %v", repo.VCSID)
return echo.NewHTTPError(http.StatusInternalServerError, err).SetInternal(err)
@@ -176,7 +182,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed push event").SetInternal(err)
}
- if branch, err := parseBranchNameFromGitHubRefs(pushEvent.Ref); err != nil {
+ if branch, err := parseBranchNameFromRefs(pushEvent.Ref); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ref").SetInternal(err)
} else if branch != repo.BranchFilter {
return c.String(http.StatusOK, "")
@@ -284,10 +290,10 @@ func validateGitHubWebhookSignature256(signature, key string, body []byte) (bool
return subtle.ConstantTimeCompare([]byte(signature), []byte(got)) == 1, nil
}
-// parseBranchNameFromGitHubRefs is for GitHub.
-// GitLab webhook has the option to listen to push events to a certain branch while GitHub doesn't. So we need to manually parse the branch name from the refs field in the request.
+// parseBranchNameFromRefs parses the branch name from the refs field in the request.
// https://docs.github.com/en/rest/git/refs
-func parseBranchNameFromGitHubRefs(ref string) (string, error) {
+// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#push-events
+func parseBranchNameFromRefs(ref string) (string, error) {
expectedPrefix := "refs/heads/"
if !strings.HasPrefix(ref, expectedPrefix) || len(expectedPrefix) == len(ref) {
log.Debug("ref is not prefix with expected prefix", zap.String("escaped ref", common.EscapeForLogging(ref)), zap.String("expected prefix", expectedPrefix))
diff --git a/server/webhook_test.go b/server/webhook_test.go
index 5a500e378e..0015eb09b7 100644
--- a/server/webhook_test.go
+++ b/server/webhook_test.go
@@ -507,7 +507,7 @@ func TestParseBranchNameFromGitHubRefs(t *testing.T) {
},
}
for _, test := range tests {
- branch, err := parseBranchNameFromGitHubRefs(test.refs)
+ branch, err := parseBranchNameFromRefs(test.refs)
if test.err {
assert.Error(t, err)
} else {