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 output builder for vcs sql review (#2951)
ecmadaoddf740e4a81f
diff --git a/server/webhook.go b/server/webhook.goindex 1b878b94f8..d85dbe6dd1 100644--- a/server/webhook.go+++ b/server/webhook.go@@ -22,6 +22,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/common/log"+ "github.com/bytebase/bytebase/plugin/advisor""github.com/bytebase/bytebase/plugin/db""github.com/bytebase/bytebase/plugin/parser""github.com/bytebase/bytebase/plugin/parser/differ"@@ -30,6 +31,15 @@ import ("github.com/bytebase/bytebase/plugin/vcs/gitlab")+// nolint:unused+type vcsSQLReviewResult struct {+ Status advisor.Status `json:"status"`+ Content []string `json:"content"`+}++// sqlReviewDocs is the URL for SQL review doc.+const sqlReviewDocs = "https://www.bytebase.com/docs/reference/error-code/advisor"+func (s *Server) registerWebhookRoutes(g *echo.Group) {g.POST("/gitlab/:id", func(c echo.Context) error {ctx := c.Request().Context()@@ -827,3 +837,131 @@ func (s *Server) computeDatabaseSchemaDiff(ctx context.Context, database *api.Da}return diff, nil}++// convertSQLAdviceToGitLabCIResult will convert SQL advice map to GitLab test output format.+// GitLab test report: https://docs.gitlab.com/ee/ci/testing/unit_test_reports.html+// junit XML format: https://llg.cubic.org/docs/junit/+// nolint:unused+func convertSQLAdviceToGitLabCIResult(adviceMap map[string][]advisor.Advice) *vcsSQLReviewResult {+ testsuiteList := []string{}+ status := advisor.Success++ fileList := []string{}+ for filePath := range adviceMap {+ fileList = append(fileList, filePath)+ }+ sort.Strings(fileList)++ for _, filePath := range fileList {+ adviceList := adviceMap[filePath]+ testcaseList := []string{}+ for _, advice := range adviceList {+ if advice.Code == 0 {+ continue+ }++ line := advice.Line+ if line <= 0 {+ line = 1+ }++ if advice.Status == advisor.Error {+ status = advice.Status+ } else if advice.Status == advisor.Warn && status != advisor.Error {+ status = advice.Status+ }++ content := fmt.Sprintf(`Error: %s+You can check the docs at %s#%d`,+ advice.Content,+ sqlReviewDocs,+ advice.Code,+ )++ testcase := fmt.Sprintf(+ "<testcase name=\"%s\" classname=\"%s\" file=\"%s#L%d\">\n<failure>\n%s\n</failure>\n</testcase>",+ advice.Title,+ filePath,+ filePath,+ line,+ content,+ )++ testcaseList = append(testcaseList, testcase)+ }++ if len(testcaseList) > 0 {+ testsuiteList = append(+ testsuiteList,+ fmt.Sprintf("<testsuite name=\"%s\">\n%s\n</testsuite>", filePath, strings.Join(testcaseList, "\n")),+ )+ }+ }++ return &vcsSQLReviewResult{+ Status: status,+ Content: []string{+ fmt.Sprintf(+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites name=\"SQL Review\">\n%s\n</testsuites>",+ strings.Join(testsuiteList, "\n"),+ ),+ },+ }+}++// convertSQLAdiceToGitHubActionResult will convert SQL advice map to GitHub action output format.+// GitHub action output message: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions+// nolint:unused+func convertSQLAdiceToGitHubActionResult(adviceMap map[string][]advisor.Advice) *vcsSQLReviewResult {+ messageList := []string{}+ status := advisor.Success++ fileList := []string{}+ for filePath := range adviceMap {+ fileList = append(fileList, filePath)+ }+ sort.Strings(fileList)++ for _, filePath := range fileList {+ adviceList := adviceMap[filePath]+ for _, advice := range adviceList {+ if advice.Code == 0 || advice.Status == advisor.Success {+ continue+ }++ line := advice.Line+ if line <= 0 {+ line = 1+ }++ prefix := ""+ if advice.Status == advisor.Error {+ prefix = "error"+ status = advice.Status+ } else {+ prefix = "warning"+ if status != advisor.Error {+ status = advice.Status+ }+ }++ msg := fmt.Sprintf(+ "::%s file=%s,line=%d,col=1,endColumn=2,title=%s (%d)::%s\nDoc: %s#%d",+ prefix,+ filePath,+ line,+ advice.Title,+ advice.Code,+ advice.Content,+ sqlReviewDocs,+ advice.Code,+ )+ // To indent the output message in action+ messageList = append(messageList, strings.ReplaceAll(msg, "\n", "%0A"))+ }+ }+ return &vcsSQLReviewResult{+ Status: status,+ Content: messageList,+ }+}diff --git a/server/webhook_test.go b/server/webhook_test.goindex dcd2fe9423..408ffcac5d 100644--- a/server/webhook_test.go+++ b/server/webhook_test.go@@ -4,6 +4,8 @@ import ("testing""github.com/stretchr/testify/assert"++ "github.com/bytebase/bytebase/plugin/advisor")// TODO(d): fix the double underscore "__".@@ -78,3 +80,102 @@ func TestParseBranchNameFromGitHubRefs(t *testing.T) {}}}++var mockSQLAdviceMap = map[string][]advisor.Advice{+ "file1.sql": {+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ {+ Status: advisor.Warn,+ Code: advisor.ColumnCanNotNull,+ Title: "column.no-null",+ Content: `Column "id" in "public"."book" can not have NULL value`,+ Line: 1,+ },+ {+ Status: advisor.Error,+ Code: advisor.NamingIndexConventionMismatch,+ Title: "naming.index.idx",+ Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Line: 2,+ },+ },+ "file2.sql": {+ {+ Status: advisor.Warn,+ Code: advisor.NamingTableConventionMismatch,+ Title: "naming.table",+ Content: "\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",+ Line: 1,+ },+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ {+ Status: advisor.Error,+ Code: advisor.NamingUKConventionMismatch,+ Title: "naming.index.uk",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Line: 4,+ },+ },+}++func TestVCSSQLReview_ConvertSQLAdviceToGitLabCIResult(t *testing.T) {+ expect :=+ `<?xml version="1.0" encoding="UTF-8"?>+<testsuites name="SQL Review">+<testsuite name="file1.sql">+<testcase name="column.no-null" classname="file1.sql" file="file1.sql#L1">+<failure>+Error: Column "id" in "public"."book" can not have NULL value+You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#402+</failure>+</testcase>+<testcase name="naming.index.idx" classname="file1.sql" file="file1.sql#L2">+<failure>+Error: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "tech_book_id_name"+You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#303+</failure>+</testcase>+</testsuite>+<testsuite name="file2.sql">+<testcase name="naming.table" classname="file2.sql" file="file2.sql#L1">+<failure>+Error: "techBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"+You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#301+</failure>+</testcase>+<testcase name="naming.index.uk" classname="file2.sql" file="file2.sql#L4">+<failure>+Error: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "tech_book_id_name"+You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#304+</failure>+</testcase>+</testsuite>+</testsuites>`+ res := convertSQLAdviceToGitLabCIResult(mockSQLAdviceMap)+ assert.Equal(t, advisor.Error, res.Status)+ assert.Equal(t, 1, len(res.Content))+ assert.Equal(t, expect, res.Content[0])+}++func TestVCSSQLReview_ConvertSQLAdiceToGitHubActionResult(t *testing.T) {+ expect := []string{+ "::warning file=file1.sql,line=1,col=1,endColumn=2,title=column.no-null (402)::Column \"id\" in \"public\".\"book\" can not have NULL value%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#402",+ "::error file=file1.sql,line=2,col=1,endColumn=2,title=naming.index.idx (303)::Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#303",+ "::warning file=file2.sql,line=1,col=1,endColumn=2,title=naming.table (301)::\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#301",+ "::error file=file2.sql,line=4,col=1,endColumn=2,title=naming.index.uk (304)::Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#304",+ }+ res := convertSQLAdiceToGitHubActionResult(mockSQLAdviceMap)+ assert.Equal(t, advisor.Error, res.Status)+ assert.Equal(t, 4, len(res.Content))+ assert.Equal(t, expect, res.Content)+}