Skip to content

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

chore: update event and add github test (#11416)

Danny Xufac504c83022
diff --git a/backend/api/gitops/github.go b/backend/api/gitops/github.go
index a9428019b9..cda3113002 100644
--- a/backend/api/gitops/github.go
+++ b/backend/api/gitops/github.go
@@ -25,7 +25,6 @@ func getGitHubPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
if err := json.Unmarshal(body, &pushEvent); err != nil {
return nil, errors.Errorf("failed to unmarshal push event, error %v", err)
}
- fmt.Printf("Barny1: %+v\n", pushEvent)
if pushEvent.Action != closeAction {
return nil, errors.Errorf("invalid webhook event action, got %s, want closed", pushEvent.Action)
}
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 5c407224fa..4ec5b58dfe 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -2110,7 +2110,6 @@ func createVCSWebhook(ctx context.Context, vcsType vcsplugin.Type, webhookEndpoi
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
SecretToken: secretToken,
MergeRequestsEvents: true,
- PushEvents: true,
NoteEvents: true,
EnableSSLVerification: false,
}
@@ -2126,7 +2125,7 @@ func createVCSWebhook(ctx context.Context, vcsType vcsplugin.Type, webhookEndpoi
Secret: secretToken,
InsecureSSL: 1,
},
- Events: []string{"push", "pull_request", "pull_request_review_comment"},
+ Events: []string{"pull_request", "pull_request_review_comment"},
}
webhookCreatePayload, err = json.Marshal(webhookPost)
if err != nil {
@@ -2137,7 +2136,7 @@ func createVCSWebhook(ctx context.Context, vcsType vcsplugin.Type, webhookEndpoi
Description: "Bytebase GitOps",
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
Active: true,
- Events: []string{"repo:push", "pullrequest:created", "pullrequest:updated", "pullrequest:comment_created"},
+ Events: []string{"pullrequest:created", "pullrequest:updated", "pullrequest:fulfilled", "pullrequest:comment_created"},
}
webhookCreatePayload, err = json.Marshal(webhookPost)
if err != nil {
@@ -2157,8 +2156,7 @@ func createVCSWebhook(ctx context.Context, vcsType vcsplugin.Type, webhookEndpoi
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
AcceptUntrustedCerts: true,
},
- EventType: "git.push",
- // TODO(d): We cannot create a few webhooks together with "git.pullrequest.created", "git.pullrequest.updated", and work items.
+ EventType: "git.pullrequest.merged",
PublisherID: "tfs",
PublisherInputs: azure.WebhookCreatePublisherInputs{
Repository: repositoryID,
diff --git a/backend/plugin/vcs/github/webhook_events.go b/backend/plugin/vcs/github/webhook_events.go
index 0098e20522..bb297eeead 100644
--- a/backend/plugin/vcs/github/webhook_events.go
+++ b/backend/plugin/vcs/github/webhook_events.go
@@ -3,9 +3,8 @@ package github
// PullRequestPushEvent is the json message for pull request push event.
type PullRequestPushEvent struct {
// opened, edited, closed.
- Action string `json:"action"`
- Number int `json:"number"`
-
+ Action string `json:"action"`
+ Number int `json:"number"`
PullRequest EventPullRequest `json:"pull_request"`
}
diff --git a/backend/tests/fake/bitbucket.go b/backend/tests/fake/bitbucket.go
index c652667353..440db735ae 100644
--- a/backend/tests/fake/bitbucket.go
+++ b/backend/tests/fake/bitbucket.go
@@ -287,7 +287,7 @@ func (bb *Bitbucket) SendWebhookPush(repositoryID string, payload []byte) error
if err != nil {
return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.URL)
}
- req.Header.Set("X-Event-Key", "repo:push")
+ req.Header.Set("X-Event-Key", "pullrequest:fulfilled")
resp, err := bb.client.Do(req)
if err != nil {
return errors.Wrapf(err, "failed to send POST request to %q", webhook.URL)
diff --git a/backend/tests/fake/github.go b/backend/tests/fake/github.go
index 046e252d4b..dd06ea0d96 100644
--- a/backend/tests/fake/github.go
+++ b/backend/tests/fake/github.go
@@ -430,7 +430,7 @@ func (gh *GitHub) SendWebhookPush(repositoryID string, payload []byte) error {
}
signature := "sha256=" + hex.EncodeToString(m.Sum(nil))
req.Header.Set("X-Hub-Signature-256", signature)
- req.Header.Set("X-GitHub-Event", "push")
+ req.Header.Set("X-GitHub-Event", "pull_request")
resp, err := gh.client.Do(req)
if err != nil {
diff --git a/backend/tests/vcs_test.go b/backend/tests/vcs_test.go
index a38cb5c95a..9485335c5e 100644
--- a/backend/tests/vcs_test.go
+++ b/backend/tests/vcs_test.go
@@ -11,6 +11,7 @@ import (
"google.golang.org/protobuf/types/known/fieldmaskpb"
"github.com/bytebase/bytebase/backend/plugin/vcs"
+ "github.com/bytebase/bytebase/backend/plugin/vcs/github"
"github.com/bytebase/bytebase/backend/plugin/vcs/gitlab"
"github.com/bytebase/bytebase/backend/tests/fake"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -59,6 +60,30 @@ func TestVCS(t *testing.T) {
},
},
},
+ {
+ name: "GitHub",
+ vcsProviderCreator: fake.NewGitHub,
+ vcsType: v1pb.VCSProvider_GITHUB,
+ externalID: "octocat/Hello-World",
+ repositoryFullPath: "octocat/Hello-World",
+ webhookPushEvent: github.PullRequestPushEvent{
+ Action: "closed",
+ Number: pullRequestID,
+ PullRequest: github.EventPullRequest{
+ URL: fmt.Sprintf("https://github.com/test/vcs/pull/%d", pullRequestID),
+ Title: pullRequestTitle,
+ Body: pullRequestDescription,
+ Base: github.EventBranch{
+ Ref: branchName,
+ SHA: "cc63b0592388a7ab1b05b005ad8c8dc14ce432b0",
+ },
+ Head: github.EventBranch{
+ Ref: "test-branch",
+ SHA: "cc63b0592388a7ab1b05b005ad8c8dc14ce432b1",
+ },
+ },
+ },
+ },
}
for _, test := range tests {
// Fix the problem that closure in a for loop will always use the last element.