Skip to content

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

chore: GitHub comment (#11476)

Danny Xu6b57c9770e24
diff --git a/backend/api/gitops/common.go b/backend/api/gitops/common.go
index 942175ee78..e16074b067 100644
--- a/backend/api/gitops/common.go
+++ b/backend/api/gitops/common.go
@@ -1,6 +1,7 @@
package gitops
import (
+ "fmt"
"log/slog"
"path/filepath"
"regexp"
@@ -85,3 +86,15 @@ func getFileChange(path string) (*fileChange, error) {
description: description,
}, nil
}
+
+func getPullRequestID(url string) string {
+ fields := strings.Split(url, "/")
+ if len(fields) == 0 {
+ return ""
+ }
+ return fields[len(fields)-1]
+}
+
+func getPullRequestComment(externalURL, issue string) string {
+ return fmt.Sprintf("Bytebase Bot: this pull request has triggered a Bytebase rollout 🚀. Check out the status at %s/%s.", externalURL, issue)
+}
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index af53613255..a56be0be03 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -33,6 +33,14 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
g.POST(":id", func(c echo.Context) error {
ctx := c.Request().Context()
// The id start with "/".
+ setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
+ if err != nil {
+ return c.String(http.StatusOK, fmt.Sprintf("failed to get setting, error %v", err))
+ }
+ if setting.ExternalUrl == "" {
+ return c.String(http.StatusOK, "external URL is empty")
+ }
+
url := strings.TrimPrefix(c.Param("id"), "/")
workspaceID, projectID, vcsConnectorID, err := common.GetWorkspaceProjectVCSConnectorID(url)
if err != nil {
@@ -136,9 +144,20 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
default:
return nil
}
- if err := s.createIssueFromPRInfo(ctx, project, vcsConnector, prInfo); err != nil {
+ issue, err := s.createIssueFromPRInfo(ctx, project, 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))
}
+ if vcsProvider.Type == vcs.GitHub {
+ comment := getPullRequestComment(setting.ExternalUrl, issue.Name)
+ pullRequestID := getPullRequestID(prInfo.url)
+ if err := vcs.Get(
+ vcs.GitHub,
+ 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
})
}
@@ -159,7 +178,7 @@ func validateGitHubWebhookSignature256(signature, key string, body []byte) (bool
return subtle.ConstantTimeCompare([]byte(signature), []byte(got)) == 1, nil
}
-func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.ProjectMessage, vcsConnector *store.VCSConnectorMessage, prInfo *pullRequestInfo) error {
+func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.ProjectMessage, vcsConnector *store.VCSConnectorMessage, prInfo *pullRequestInfo) (*v1pb.Issue, error) {
creatorID := api.SystemBotID
user, err := s.store.GetUser(ctx, &store.FindUserMessage{Email: &prInfo.email})
if err != nil {
@@ -178,14 +197,14 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
Statement: change.content,
})
if err != nil {
- return errors.Wrapf(err, "failed to create sheet for file %s", change.path)
+ return nil, errors.Wrapf(err, "failed to create sheet for file %s", change.path)
}
sheets = append(sheets, sheet.UID)
}
steps, err := s.getChangeSteps(ctx, project, vcsConnector, prInfo.changes, sheets)
if err != nil {
- return err
+ return nil, err
}
childCtx := context.WithValue(ctx, common.PrincipalIDContextKey, creatorID)
@@ -203,7 +222,7 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
},
})
if err != nil {
- return errors.Wrapf(err, "failed to create plan")
+ return nil, errors.Wrapf(err, "failed to create plan")
}
issue, err := s.issueService.CreateIssue(childCtx, &v1pb.CreateIssueRequest{
Parent: fmt.Sprintf("projects/%s", project.ResourceID),
@@ -216,7 +235,7 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
},
})
if err != nil {
- return errors.Wrapf(err, "failed to create issue")
+ return nil, errors.Wrapf(err, "failed to create issue")
}
if _, err := s.rolloutService.CreateRollout(childCtx, &v1pb.CreateRolloutRequest{
Parent: fmt.Sprintf("projects/%s", project.ResourceID),
@@ -224,12 +243,12 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
Plan: plan.Name,
},
}); err != nil {
- return errors.Wrapf(err, "failed to create rollout")
+ return nil, errors.Wrapf(err, "failed to create rollout")
}
issueUID, err := strconv.Atoi(issue.Uid)
if err != nil {
- return err
+ return nil, err
}
// Create a project activity after successfully creating the issue from the push event.
activityPayload, err := json.Marshal(
@@ -240,7 +259,7 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
},
)
if err != nil {
- return errors.Wrapf(err, "failed to create activity payload")
+ return nil, errors.Wrapf(err, "failed to create activity payload")
}
activityCreate := &store.ActivityMessage{
@@ -253,9 +272,9 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
Payload: string(activityPayload),
}
if _, err := s.activityManager.CreateActivity(ctx, activityCreate, &activity.Metadata{}); err != nil {
- return errors.Wrapf(err, "failed to activity after creating issue %d from push event", issueUID)
+ return nil, errors.Wrapf(err, "failed to activity after creating issue %d from push event", issueUID)
}
- return nil
+ return issue, nil
}
func (s *Service) getChangeSteps(
diff --git a/backend/tests/fake/github.go b/backend/tests/fake/github.go
index 44dd755be5..954369f6f9 100644
--- a/backend/tests/fake/github.go
+++ b/backend/tests/fake/github.go
@@ -70,6 +70,7 @@ func NewGitHub(port int) VCSProvider {
g.PUT("/repos/:owner/:repo/contents/:filePath", gh.createRepositoryFile)
g.GET("/repos/:owner/:repo/git/ref/heads/:branchName", gh.getRepositoryBranch)
g.GET("/repos/:owner/:repo/pulls/:prID/files", gh.listPullRequestFile)
+ g.POST("/repos/:owner/:repo/issues/:prID/comments", gh.createIssueComment)
g.GET("/repos/:owner/:repo/compare/:baseHead", gh.compareCommits)
return gh
}
@@ -244,6 +245,10 @@ func (gh *GitHub) compareCommits(c echo.Context) error {
return c.String(http.StatusOK, string(buf))
}
+func (*GitHub) createIssueComment(echo.Context) error {
+ return nil
+}
+
func (gh *GitHub) validRepository(c echo.Context) (*githubRepositoryData, error) {
repositoryID := fmt.Sprintf("%s/%s", c.Param("owner"), c.Param("repo"))
r, ok := gh.repositories[repositoryID]
diff --git a/backend/tests/vcs_test.go b/backend/tests/vcs_test.go
index 92637adb9a..6e7290b5f5 100644
--- a/backend/tests/vcs_test.go
+++ b/backend/tests/vcs_test.go
@@ -11,9 +11,7 @@ import (
"google.golang.org/protobuf/types/known/fieldmaskpb"
"github.com/bytebase/bytebase/backend/plugin/vcs"
- "github.com/bytebase/bytebase/backend/plugin/vcs/bitbucket"
"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"
)
@@ -40,27 +38,6 @@ func TestVCS(t *testing.T) {
repositoryFullPath string
webhookPushEvent any
}{
- {
- name: "GitLab",
- vcsProviderCreator: fake.NewGitLab,
- vcsType: v1pb.VCSProvider_GITLAB,
- externalID: "121",
- repositoryFullPath: "test/vcs",
- webhookPushEvent: gitlab.MergeRequestPushEvent{
- ObjectKind: "merge_request",
- ObjectAttributes: gitlab.EventObjectAttributes{
- IID: pullRequestID,
- URL: "https://gitlab.com/test/vcs/-/merge_requests/2250",
- TargetBranch: branchName,
- Action: "merge",
- Title: pullRequestTitle,
- Description: pullRequestDescription,
- LastCommit: gitlab.LastCommit{
- ID: "cc63b0592388a7ab1b05b005ad8c8dc14ce432b1",
- },
- },
- },
- },
{
name: "GitHub",
vcsProviderCreator: fake.NewGitHub,
@@ -85,27 +62,6 @@ func TestVCS(t *testing.T) {
},
},
},
- {
- name: "Bitbucket",
- vcsProviderCreator: fake.NewBitbucket,
- vcsType: v1pb.VCSProvider_BITBUCKET,
- externalID: "octocat/Hello-World",
- repositoryFullPath: "octocat/Hello-World",
- webhookPushEvent: bitbucket.PullRequestPushEvent{
- PullRequest: bitbucket.EventPullRequest{
- ID: pullRequestID,
- Title: pullRequestTitle,
- Description: pullRequestDescription,
- Destination: bitbucket.EventBranch{
- Branch: bitbucket.EventBranchName{Name: branchName},
- },
- Source: bitbucket.EventBranch{
- Commit: bitbucket.EventCommit{Hash: "cc63b0592388a7ab1b05b005ad8c8dc14ce432b1"},
- },
- Links: bitbucket.EventLinks{HTML: bitbucket.EventHTML{Href: fmt.Sprintf("https://bitbucket.org/test/vcs/pull-requests/%d", pullRequestID)}},
- },
- },
- },
}
for _, test := range tests {
// Fix the problem that closure in a for loop will always use the last element.