Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add file url in the sql review comment (#13039)
ecmadao740eebb8f767
diff --git a/backend/api/gitops/common.go b/backend/api/gitops/common.goindex 9d2453d09d..e34032cfe2 100644--- a/backend/api/gitops/common.go+++ b/backend/api/gitops/common.go@@ -10,6 +10,7 @@ import ("github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/utils"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")@@ -39,6 +40,7 @@ type fileChange struct {changeType v1pb.Plan_ChangeDatabaseConfig_Typedescription stringcontent string+ webURL string}func getChangesByFileList(files []*vcs.PullRequestFile, rootDir string) []*fileChange {@@ -60,6 +62,7 @@ func getChangesByFileList(files []*vcs.PullRequestFile, rootDir string) []*fileC}if change != nil {change.path = v.Path+ change.webURL = v.WebURLchanges = append(changes, change)}}@@ -125,3 +128,16 @@ func convertFileContentToUTF8String(content string) string {}return convertedContent}++func getFileWebURLInPR(webURL string, line int32, vcsType storepb.VCSType) string {+ switch vcsType {+ case storepb.VCSType_GITHUB:+ return fmt.Sprintf("%sR%d", webURL, line)+ case storepb.VCSType_GITLAB:+ return fmt.Sprintf("%s_0_%d", webURL, line)+ case storepb.VCSType_BITBUCKET:+ return fmt.Sprintf("%sT%d", webURL, line)+ default:+ return webURL+ }+}diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.goindex 66b1345eb3..2571c95c10 100644--- a/backend/api/gitops/webhook.go+++ b/backend/api/gitops/webhook.go@@ -178,10 +178,11 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {comment = getPullRequestComment(setting.ExternalUrl, issue.Name)commentPrefix = commentPrefixBytebaseBotcase webhookActionSQLReview:- comment, err = s.sqlReviewWithPRInfo(childCtx, project, vcsConnector, prInfo)+ comment, err = s.sqlReviewWithPRInfo(childCtx, project, vcsConnector, vcsProvider.Type, prInfo)if err != nil {return c.String(http.StatusOK, fmt.Sprintf("failed to exec sql review for pull request %s, error %v", prInfo.url, err))}+ comment = fmt.Sprintf("%s\n\n---\n\nClick [here](%s) to check the SQL review config", comment, fmt.Sprintf("%s/sql-review", setting.ExternalUrl))commentPrefix = commentPrefixSQLReviewdefault:}@@ -220,7 +221,7 @@ func validateGitHubWebhookSignature256(signature, key string, body []byte) (boolreturn subtle.ConstantTimeCompare([]byte(signature), []byte(got)) == 1, nil}-func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.ProjectMessage, vcsConnector *store.VCSConnectorMessage, prInfo *pullRequestInfo) (string, error) {+func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.ProjectMessage, vcsConnector *store.VCSConnectorMessage, vcsType storepb.VCSType, prInfo *pullRequestInfo) (string, error) {instance, database, err := s.getDatabaseSample(ctx, project, vcsConnector)if err != nil {return "", errors.Wrapf(err, "failed to get database sample")@@ -254,7 +255,7 @@ func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.Projec} else if advice.Status == v1pb.Advice_WARNING {warnCount++}- message := fmt.Sprintf("- **[%s]** %s (line: %d)", advice.Status.String(), advice.Title, advice.Line)+ message := fmt.Sprintf("- **[%s]** %s ([line%d](%s))", advice.Status.String(), advice.Title, advice.Line, getFileWebURLInPR(change.webURL, advice.Line, vcsType))adviceMessage = append(adviceMessage, message)}@@ -262,7 +263,7 @@ func (s *Service) sqlReviewWithPRInfo(ctx context.Context, project *store.Projecif len(content) > 0 {content = append(content, "\n")}- content = append(content, fmt.Sprintf("SQL review for %s\n", change.path))+ content = append(content, fmt.Sprintf("SQL review for [%s](%s)\n", change.path, change.webURL))// 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/1066809diff --git a/backend/plugin/vcs/bitbucket/bitbucket.go b/backend/plugin/vcs/bitbucket/bitbucket.goindex b08bcb4e13..120e5fdd3f 100644--- a/backend/plugin/vcs/bitbucket/bitbucket.go+++ b/backend/plugin/vcs/bitbucket/bitbucket.go@@ -303,12 +303,14 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRereturn fields[0]}+ prURL := fmt.Sprintf("%s/%s/pull-requests/%s", p.instanceURL, repositoryID, pullRequestID)var files []*vcs.PullRequestFilefor _, d := range bbcDiffs {file := &vcs.PullRequestFile{Path: d.New.Path,LastCommitID: extractCommitIDFromLinkSelf(d.New.Links.Self.Href),IsDeleted: d.Status == "removed",+ WebURL: fmt.Sprintf("%s/diff#L%s", prURL, d.New.Path),}files = append(files, file)}diff --git a/backend/plugin/vcs/github/github.go b/backend/plugin/vcs/github/github.goindex 311c9f41a3..94ac1b5e65 100644--- a/backend/plugin/vcs/github/github.go+++ b/backend/plugin/vcs/github/github.go@@ -3,6 +3,8 @@ package githubimport ("context"+ "crypto/sha256"+ "encoding/hex""encoding/json""fmt""log/slog"@@ -269,6 +271,7 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRepage++}+ prURL := fmt.Sprintf("%s/%s/pull/%s", p.instanceURL, repositoryID, pullRequestID)var res []*vcs.PullRequestFilefor _, file := range allPRFiles {u, err := url.Parse(file.ContentsURL)@@ -295,10 +298,19 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRecontinue}+ // Get web url for changed file in the PR.+ // https://github.com/orgs/community/discussions/55764+ var fileHash string+ hash := sha256.New()+ if _, err := hash.Write([]byte(file.FileName)); err == nil {+ fileHash = hex.EncodeToString(hash.Sum(nil))+ }+res = append(res, &vcs.PullRequestFile{Path: file.FileName,LastCommitID: refs[0],IsDeleted: file.Status == "removed",+ WebURL: fmt.Sprintf("%s/files#diff-%s", prURL, fileHash),})}diff --git a/backend/plugin/vcs/gitlab/gitlab.go b/backend/plugin/vcs/gitlab/gitlab.goindex 9d60927d1a..bb83ba72a2 100644--- a/backend/plugin/vcs/gitlab/gitlab.go+++ b/backend/plugin/vcs/gitlab/gitlab.go@@ -3,6 +3,8 @@ package gitlabimport ("context"+ "crypto/sha1"+ "encoding/hex""encoding/json""fmt""net/http"@@ -242,6 +244,7 @@ func (p *Provider) ReadFileContent(ctx context.Context, repositoryID, filePath stype MergeRequestChange struct {SHA string `json:"sha"`Changes []MergeRequestFile `json:"changes"`+ WebURL string `json:"web_url"`}// MergeRequestFile is the API message for files in GitLab merge request.@@ -279,10 +282,19 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRevar res []*vcs.PullRequestFilefor _, file := range pr.Changes {+ // Get web url for changed file in the PR.+ // https://web.archive.org/web/20200922103150/https://github.community/t/getting-the-anchor-to-files-diffs-in-a-pull-request/14235+ var fileHash string+ hash := sha1.New()+ if _, err := hash.Write([]byte(file.NewPath)); err == nil {+ fileHash = hex.EncodeToString(hash.Sum(nil))+ }+res = append(res, &vcs.PullRequestFile{Path: file.NewPath,LastCommitID: pr.SHA,IsDeleted: file.DeletedFile,+ WebURL: fmt.Sprintf("%s/diffs#%s", pr.WebURL, fileHash),})}diff --git a/backend/plugin/vcs/vcs.go b/backend/plugin/vcs/vcs.goindex 04d25ebe94..7d9d15cd03 100644--- a/backend/plugin/vcs/vcs.go+++ b/backend/plugin/vcs/vcs.go@@ -60,6 +60,8 @@ type PullRequestFile struct {Path stringLastCommitID stringIsDeleted bool+ // WebURL is the changed file web url in the pull request.+ WebURL string}// BranchInfo is the API message for repository branch.