Skip to content

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

chore: prepare for sql review ci (#12966)

ecmadao16c4cbb948c9
diff --git a/backend/api/gitops/azure.go b/backend/api/gitops/azure.go
index baf72a2127..309077220e 100644
--- a/backend/api/gitops/azure.go
+++ b/backend/api/gitops/azure.go
@@ -48,6 +48,7 @@ func getAzurePullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvider
}
prInfo := &pullRequestInfo{
+ action: webhookActionCreateIssue,
// TODO(ed): get the email.
url: pushEvent.Resource.Links.Web.Href,
title: pushEvent.Resource.Title,
diff --git a/backend/api/gitops/bitbucket.go b/backend/api/gitops/bitbucket.go
index 422037ac2a..3b6fc3fc89 100644
--- a/backend/api/gitops/bitbucket.go
+++ b/backend/api/gitops/bitbucket.go
@@ -29,6 +29,7 @@ func getBitBucketPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProv
}
prInfo := &pullRequestInfo{
+ action: webhookActionCreateIssue,
// email. How do we determine the user for BitBucket user?
url: pushEvent.PullRequest.Links.HTML.Href,
title: pushEvent.PullRequest.Title,
diff --git a/backend/api/gitops/common.go b/backend/api/gitops/common.go
index b291ec61dd..eaa8309714 100644
--- a/backend/api/gitops/common.go
+++ b/backend/api/gitops/common.go
@@ -17,7 +17,15 @@ var (
versionRE = regexp.MustCompile(`^[0-9]+`)
)
+type webhookAction int
+
+const (
+ webhookActionCreateIssue webhookAction = iota
+ webhookActionSQLReview
+)
+
type pullRequestInfo struct {
+ action webhookAction
email string
title string
description string
diff --git a/backend/api/gitops/github.go b/backend/api/gitops/github.go
index 4c71eed684..a18d5d1a73 100644
--- a/backend/api/gitops/github.go
+++ b/backend/api/gitops/github.go
@@ -13,17 +13,23 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-const (
- closeAction = "closed"
-)
-
func getGitHubPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, body []byte) (*pullRequestInfo, error) {
var pushEvent github.PullRequestPushEvent
if err := json.Unmarshal(body, &pushEvent); err != nil {
return nil, errors.Errorf("failed to unmarshal push event, error %v", err)
}
- if pushEvent.Action != closeAction || !pushEvent.PullRequest.Merged {
- return nil, errors.Errorf("skip webhook event action, got %s, want closed with merged", pushEvent.Action)
+
+ var actionType webhookAction
+ switch pushEvent.Action {
+ case github.PullRequestEventOpened, github.PullRequestEventSynchronize:
+ actionType = webhookActionSQLReview
+ case github.PullRequestEventClosed:
+ if !pushEvent.PullRequest.Merged {
+ return nil, errors.Errorf("skip pull request close action, pull request is not merged")
+ }
+ actionType = webhookActionCreateIssue
+ default:
+ return nil, errors.Errorf(`skip webhook event action "%s"`, pushEvent.Action)
}
if pushEvent.PullRequest.Base.Ref != vcsConnector.Payload.Branch {
@@ -36,6 +42,7 @@ func getGitHubPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
}
prInfo := &pullRequestInfo{
+ action: actionType,
// email. How do we determine the user for GitHub user?
url: pushEvent.PullRequest.HTMLURL,
title: pushEvent.PullRequest.Title,
diff --git a/backend/api/gitops/gitlab.go b/backend/api/gitops/gitlab.go
index 4e84c49822..34b39ea4f8 100644
--- a/backend/api/gitops/gitlab.go
+++ b/backend/api/gitops/gitlab.go
@@ -15,7 +15,6 @@ import (
const (
mergeRequestObjectKind = "merge_request"
- mergeAction = "merge"
)
func getGitLabPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, body []byte) (*pullRequestInfo, error) {
@@ -26,8 +25,15 @@ func getGitLabPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
if pushEvent.ObjectKind != mergeRequestObjectKind {
return nil, errors.Errorf("skip webhook event type, got %s, want %s", pushEvent.ObjectKind, mergeRequestObjectKind)
}
- if pushEvent.ObjectAttributes.Action != mergeAction {
- return nil, errors.Errorf("skip webhook event action, got %s, want merge", pushEvent.ObjectAttributes.Action)
+
+ var actionType webhookAction
+ switch pushEvent.ObjectAttributes.Action {
+ case gitlab.MergeRequestOpen, gitlab.MergeRequestUpdate:
+ actionType = webhookActionSQLReview
+ case gitlab.MergeRequestMerge:
+ actionType = webhookActionCreateIssue
+ default:
+ return nil, errors.Errorf("skip webhook event action %v", pushEvent.ObjectAttributes.Action)
}
if pushEvent.ObjectAttributes.TargetBranch != vcsConnector.Payload.Branch {
@@ -40,6 +46,7 @@ func getGitLabPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
}
prInfo := &pullRequestInfo{
+ action: actionType,
email: pushEvent.User.Email,
url: pushEvent.ObjectAttributes.URL,
title: pushEvent.ObjectAttributes.Title,
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 43f4f8ecf5..89ae87a779 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -100,7 +100,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
return c.String(http.StatusOK, "OK")
case "pull_request":
default:
- return c.String(http.StatusOK, "OK")
+ return c.String(http.StatusOK, fmt.Sprintf(`skip webhook event "%v"`, eventType))
}
prInfo, err = getGitHubPullRequestInfo(ctx, vcsProvider, vcsConnector, body)
@@ -119,19 +119,23 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
}
case storepb.VCSType_BITBUCKET:
eventType := c.Request().Header.Get("X-Event-Key")
+ var action webhookAction
switch eventType {
case "pullrequest:created", "pullrequest:updated":
- return c.String(http.StatusOK, "OK")
+ action = webhookActionSQLReview
case "pullrequest:fulfilled":
+ action = webhookActionCreateIssue
default:
- return c.String(http.StatusOK, "OK")
+ return c.String(http.StatusOK, fmt.Sprintf(`skip webhook event "%v"`, eventType))
}
prInfo, err = getBitBucketPullRequestInfo(ctx, vcsProvider, vcsConnector, body)
if err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to get pr info from pull request, error %v", err))
}
+ prInfo.action = action
case storepb.VCSType_AZURE_DEVOPS:
+ // TODO(ed): support pr update/create event.
secretToken := c.Request().Header.Get("X-Azure-Token")
if secretToken != vcsConnector.Payload.WebhookSecretToken {
return c.String(http.StatusOK, fmt.Sprintf("invalid webhook secret token %q", secretToken))
@@ -147,19 +151,32 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
if len(prInfo.changes) == 0 {
return c.String(http.StatusOK, fmt.Sprintf("no relevant file change directly under the base directory %q for pull request %q", vcsConnector.Payload.BaseDirectory, prInfo.url))
}
- issue, err := s.createIssueFromPRInfo(ctx, project, vcsProvider, vcsConnector, prInfo)
- if err != nil {
- return c.String(http.StatusOK, fmt.Sprintf("failed to create issue from pull request %s, error %v", prInfo.url, err))
+
+ var comment string
+ switch prInfo.action {
+ case webhookActionCreateIssue:
+ issue, err := s.createIssueFromPRInfo(ctx, project, vcsProvider, vcsConnector, prInfo)
+ if err != nil {
+ return c.String(http.StatusOK, fmt.Sprintf("failed to create issue from pull request %s, error %v", prInfo.url, err))
+ }
+ comment = getPullRequestComment(setting.ExternalUrl, issue.Name)
+ case webhookActionSQLReview:
+ // TODO(ed): sql review.
+ default:
}
- comment := getPullRequestComment(setting.ExternalUrl, issue.Name)
- pullRequestID := getPullRequestID(prInfo.url)
- if err := vcs.Get(
- vcsProvider.Type,
- vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken},
- ).CreatePullRequestComment(ctx, vcsConnector.Payload.ExternalId, pullRequestID, comment); err != nil {
- return c.String(http.StatusOK, fmt.Sprintf("failed to create pull request comment, error %v", err))
+
+ if comment != "" {
+ pullRequestID := getPullRequestID(prInfo.url)
+ // TODO(ed): upsert the commont.
+ if err := vcs.Get(
+ vcsProvider.Type,
+ vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken},
+ ).CreatePullRequestComment(ctx, vcsConnector.Payload.ExternalId, pullRequestID, comment); err != nil {
+ return c.String(http.StatusOK, fmt.Sprintf("failed to create pull request comment, error %v", err))
+ }
}
- return nil
+
+ return c.String(http.StatusOK, fmt.Sprintf("successfully handle the pull request %v", prInfo.url))
})
}
diff --git a/backend/api/v1/vcs_connector_service.go b/backend/api/v1/vcs_connector_service.go
index 4a516a5411..6491e0190e 100644
--- a/backend/api/v1/vcs_connector_service.go
+++ b/backend/api/v1/vcs_connector_service.go
@@ -425,6 +425,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
var err error
switch vcsProvider.Type {
case storepb.VCSType_GITLAB:
+ // https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#push-events
webhookCreate := gitlab.WebhookCreate{
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
SecretToken: webhookSecretToken,
@@ -444,6 +445,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
Secret: webhookSecretToken,
InsecureSSL: 1,
},
+ // https://docs.github.com/en/webhooks/webhook-events-and-payloads
Events: []string{"pull_request", "pull_request_review_comment"},
}
webhookCreatePayload, err = json.Marshal(webhookPost)
@@ -455,7 +457,8 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
Description: "Bytebase GitOps",
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
Active: true,
- Events: []string{"pullrequest:created", "pullrequest:updated", "pullrequest:fulfilled", "pullrequest:comment_created"},
+ // https://support.atlassian.com/bitbucket-cloud/docs/event-payloads
+ Events: []string{"pullrequest:created", "pullrequest:updated", "pullrequest:fulfilled", "pullrequest:comment_created"},
}
webhookCreatePayload, err = json.Marshal(webhookPost)
if err != nil {
@@ -468,6 +471,11 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
}
projectID, repositoryID := part[1], part[2]
+ // https://learn.microsoft.com/en-us/azure/devops/service-hooks/events?view=azure-devops
+ // TODO(ed): Azure doesn't support multiply events in a single webhook, but we need:
+ // - git.pullrequest.merged: A merge commit was created on a pull request.
+ // - git.pullrequest.created: A pull request is created in a Git repository.
+ // - git.pullrequest.updated: A pull request is updated; status, review list, reviewer vote changed, or the source branch is updated with a push.
webhookPost := azure.WebhookCreateOrUpdate{
ConsumerActionID: "httpRequest",
ConsumerID: "webHooks",
diff --git a/backend/plugin/vcs/bitbucket/bitbucket.go b/backend/plugin/vcs/bitbucket/bitbucket.go
index 59dec1b57c..7fb799f5bb 100644
--- a/backend/plugin/vcs/bitbucket/bitbucket.go
+++ b/backend/plugin/vcs/bitbucket/bitbucket.go
@@ -400,10 +400,17 @@ type WebhookPushEvent struct {
// WebhookCreateOrUpdate represents a Bitbucket API request for creating or
// updating a webhook.
type WebhookCreateOrUpdate struct {
- Description string `json:"description"`
- URL string `json:"url"`
- Active bool `json:"active"`
- Events []string `json:"events"`
+ Description string `json:"description"`
+ URL string `json:"url"`
+ Active bool `json:"active"`
+ // Docs for pr events: https://support.atlassian.com/bitbucket-cloud/docs/event-payloads/#Pull-request-events
+ // - pullrequest:created: A user creates a pull request for a repository.
+ // - pullrequest:updated: A user updates a pull request for a repository.
+ // - pullrequest:changes_request_created: A user requests a change for a pull request for a repository.
+ // - pullrequest:fulfilled: A user merges a pull request for a repository.
+ // - pullrequest:comment_created: A user comments on a pull request.
+ // - pullrequest:comment_updated: A user updates a comment on a pull request.
+ Events []string `json:"events"`
}
// Webhook represents a Bitbucket Cloud API response for the webhook
diff --git a/backend/plugin/vcs/github/webhook_events.go b/backend/plugin/vcs/github/webhook_events.go
index 5d160e69f9..70b0fbe1f7 100644
--- a/backend/plugin/vcs/github/webhook_events.go
+++ b/backend/plugin/vcs/github/webhook_events.go
@@ -1,11 +1,22 @@
package github
+type PullRequestEventType string
+
+const (
+ // A pull request was created.
+ PullRequestEventOpened PullRequestEventType = "opened"
+ // A pull request was closed. If merged is false in the webhook payload, the pull request was closed with unmerged commits. If merged is true in the webhook payload, the pull request was merged.
+ PullRequestEventClosed PullRequestEventType = "closed"
+ // A pull request's head branch was updated. For example, the head branch was updated from the base branch or new commits were pushed to the head branch.
+ PullRequestEventSynchronize PullRequestEventType = "synchronize"
+)
+
// PullRequestPushEvent is the json message for pull request push event.
+// Docs: https://docs.github.com/en/webhooks/webhook-events-and-payloads#pull_request
type PullRequestPushEvent struct {
- // opened, edited, closed.
- // PR close will also send webhook event with "closed" action, so we need to check the "merged" field.
- Action string `json:"action"`
- Number int `json:"number"`
+ Action PullRequestEventType `json:"action"`
+ Number int `json:"number"`
+ // PR close will also send webhook event with "closed" action, so we need to check the "merged" field in "pull_request".
PullRequest EventPullRequest `json:"pull_request"`
}
diff --git a/backend/plugin/vcs/gitlab/webhook_events.go b/backend/plugin/vcs/gitlab/webhook_events.go
index 74540c597b..71920ab48a 100644
--- a/backend/plugin/vcs/gitlab/webhook_events.go
+++ b/backend/plugin/vcs/gitlab/webhook_events.go
@@ -1,6 +1,7 @@
package gitlab
// MergeRequestPushEvent is the json message for merge request push event.
+// Docs: https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#merge-request-events
type MergeRequestPushEvent struct {
ObjectKind string `json:"object_kind"`
User EventUser `json:"user"`
@@ -11,15 +12,26 @@ type EventUser struct {
Email string `json:"email"`
}
+// The available values for object_attributes.action in the payload are:
+// open, close, reopen, update, approved, unapproved, approval, unapproval, merge.
+// Docs: https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#merge-request-events
+type MergeRequestAction string
+
+const (
+ MergeRequestOpen MergeRequestAction = "open"
+ // TODO(ed): code update? or just pr title/comment update.
+ MergeRequestUpdate MergeRequestAction = "update"
+ MergeRequestMerge MergeRequestAction = "merge"
+)
+
type EventObjectAttributes struct {
- IID int `json:"iid"`
- URL string `json:"url"`
- TargetBranch string `json:"target_branch"`
- // open, merge.
- Action string `json:"action"`
- Title string `json:"title"`
- Description string `json:"description"`
- LastCommit LastCommit `json:"last_commit"`
+ IID int `json:"iid"`
+ URL string `json:"url"`
+ TargetBranch string `json:"target_branch"`
+ Action MergeRequestAction `json:"action"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ LastCommit LastCommit `json:"last_commit"`
}
type LastCommit struct {