Skip to content

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

chore: optimize sql review comment content (#13024)

ecmadaoa1e219e872f8
diff --git a/backend/api/gitops/common.go b/backend/api/gitops/common.go
index a518d60dd9..9d2453d09d 100644
--- a/backend/api/gitops/common.go
+++ b/backend/api/gitops/common.go
@@ -109,8 +109,8 @@ func getPullRequestID(url string) string {
}
const (
- commentPrefixBytebaseBot = "[Bytebase Bot]"
- commentPrefixSQLReview = "[Bytebase SQL Review]"
+ commentPrefixBytebaseBot = "**[Bytebase Bot]**"
+ commentPrefixSQLReview = "**[Bytebase SQL Review]**"
)
func getPullRequestComment(externalURL, issue string) string {
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 621365a27e..66b1345eb3 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -195,6 +195,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
fmt.Sprintf("%s\n\n%s", commentPrefix, comment),
func(content string) bool { return strings.HasPrefix(content, commentPrefix) },
); err != nil {
+ slog.Error("failed to upsert comment", slog.String("pr", prInfo.url), log.BBError(err))
return c.String(http.StatusOK, fmt.Sprintf("failed to create pull request comment, error %v", err))
}
}
@@ -226,6 +227,9 @@ func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.Projec
}
content := []string{}
+ errorCount := 0
+ warnCount := 0
+
for _, change := range prInfo.changes {
adviceStatus, advices, err := s.sqlService.SQLReviewCheck(
ctx,
@@ -243,10 +247,14 @@ func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.Projec
continue
}
- // TODO(ed): better message format. Maybe add links for files?
adviceMessage := []string{}
for _, advice := range advices {
- message := fmt.Sprintf("- [%d] %s (line: %d)", advice.Code, advice.Title, advice.Line)
+ if advice.Status == v1pb.Advice_ERROR {
+ errorCount++
+ } else if advice.Status == v1pb.Advice_WARNING {
+ warnCount++
+ }
+ message := fmt.Sprintf("- **[%s]** %s (line: %d)", advice.Status.String(), advice.Title, advice.Line)
adviceMessage = append(adviceMessage, message)
}
@@ -254,12 +262,19 @@ func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.Projec
if len(content) > 0 {
content = append(content, "\n")
}
- content = append(content, fmt.Sprintf("SQL review for %s", change.path))
- content = append(content, strings.Join(adviceMessage, "\n"))
+ content = append(content, fmt.Sprintf("SQL review for %s\n", change.path))
+ // We have to use at least 2 \n for Bitbucket.
+ // The API docs for Bitbucket sucks.
+ // https://community.atlassian.com/t5/Bitbucket-questions/How-to-post-html-comments-on-pull-request-via-2-0-api/qaq-p/1066809
+ content = append(content, strings.Join(adviceMessage, "\n\n"))
}
}
- return strings.Join(content, "\n"), nil
+ if len(content) == 0 {
+ return "", nil
+ }
+
+ return fmt.Sprintf("\n%d errors, %d warnings\n\n---\n\n%s", errorCount, warnCount, strings.Join(content, "\n")), nil
}
func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.ProjectMessage, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, prInfo *pullRequestInfo) (*v1pb.Issue, error) {
diff --git a/backend/plugin/vcs/azure/azure.go b/backend/plugin/vcs/azure/azure.go
index fe281c046e..1df899dc15 100644
--- a/backend/plugin/vcs/azure/azure.go
+++ b/backend/plugin/vcs/azure/azure.go
@@ -385,8 +385,10 @@ type Comment struct {
}
type PullRequestThread struct {
+ ID int64 `json:"id"`
Comments []*Comment `json:"comments"`
- Status string `json:"status"`
+ // https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-threads/list?view=azure-devops-rest-7.1&tabs=HTTP#commentthreadstatus
+ Status string `json:"status"`
}
// CreatePullRequestComment creates a pull request comment.
@@ -428,14 +430,77 @@ func (p *Provider) CreatePullRequestComment(ctx context.Context, repositoryID, p
}
// ListPullRequestComments lists comments in a pull request.
-// TODO(ed): implement.
-func (*Provider) ListPullRequestComments(_ context.Context, _, _ string) ([]*vcs.PullRequestComment, error) {
- return nil, nil
+//
+// https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-threads/list?view=azure-devops-rest-7.1&tabs=HTTP
+func (p *Provider) ListPullRequestComments(ctx context.Context, repositoryID, pullRequestID string) ([]*vcs.PullRequestComment, error) {
+ apiURL, err := p.getRepositoryAPIURL(repositoryID)
+ if err != nil {
+ return nil, err
+ }
+
+ values := &url.Values{}
+ values.Set("api-version", "7.0")
+ url := fmt.Sprintf("%s/pullRequests/%s/threads?%s", apiURL, pullRequestID, values.Encode())
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
+ if err != nil {
+ return nil, errors.Wrapf(err, "GET %s", url)
+ }
+ if code != http.StatusOK {
+ return nil, errors.Errorf("failed to list thread, code: %v, body: %s", code, string(body))
+ }
+
+ var threads []PullRequestThread
+ if err := json.Unmarshal([]byte(body), &threads); err != nil {
+ return nil, err
+ }
+
+ var res []*vcs.PullRequestComment
+ for _, thread := range threads {
+ if len(thread.Comments) != 1 {
+ continue
+ }
+ res = append(res, &vcs.PullRequestComment{
+ ID: fmt.Sprintf("%d", thread.ID),
+ Content: thread.Comments[0].Content,
+ })
+ }
+ return res, nil
}
// UpdatePullRequestComment updates a comment in a pull request.
-// TODO(ed): implement.
-func (*Provider) UpdatePullRequestComment(_ context.Context, _, _ string, _ *vcs.PullRequestComment) error {
+//
+// https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-threads/update?view=azure-devops-rest-7.1
+func (p *Provider) UpdatePullRequestComment(ctx context.Context, repositoryID, pullRequestID string, comment *vcs.PullRequestComment) error {
+ thread := &PullRequestThread{
+ Status: "active",
+ Comments: []*Comment{
+ {
+ Content: comment.Content,
+ CommentType: "text",
+ },
+ },
+ }
+ commentCreatePayload, err := json.Marshal(thread)
+ if err != nil {
+ return errors.Wrap(err, "failed to marshal request body for creating pull request comment")
+ }
+
+ apiURL, err := p.getRepositoryAPIURL(repositoryID)
+ if err != nil {
+ return err
+ }
+
+ values := &url.Values{}
+ values.Set("api-version", "7.0")
+ url := fmt.Sprintf("%s/pullRequests/%s/threads/%s?%s", apiURL, pullRequestID, comment.ID, values.Encode())
+ code, body, err := internal.Patch(ctx, url, p.getAuthorization(), commentCreatePayload)
+ if err != nil {
+ return errors.Wrapf(err, "PATCH %s", url)
+ }
+ if code != http.StatusOK {
+ return errors.Errorf("failed to update thread, code: %v, body: %s", code, string(body))
+ }
+
return nil
}
diff --git a/backend/plugin/vcs/vcs.go b/backend/plugin/vcs/vcs.go
index 799c86810d..04d25ebe94 100644
--- a/backend/plugin/vcs/vcs.go
+++ b/backend/plugin/vcs/vcs.go
@@ -87,7 +87,7 @@ type Provider interface {
// GetBranch gets the given branch in the repository.
GetBranch(ctx context.Context, repositoryID, branchName string) (*BranchInfo, error)
- // CreatePullRequest creates the pull request in the repository.
+ // ListPullRequestFile lists changed files in a pull request.
ListPullRequestFile(ctx context.Context, repositoryID, pullRequestID string) ([]*PullRequestFile, error)
// CreatePullRequestComment creates a pull request comment.