Skip to content

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

feat(action): --output writes check results (#18010)

p0ny87396389d936
diff --git a/action/README.md b/action/README.md
index 30e6279264..ac43b05c35 100644
--- a/action/README.md
+++ b/action/README.md
@@ -10,7 +10,7 @@ This action provides several subcommands to interact with Bytebase.
Usage: `bytebase-action check [global flags]`
-Checks the SQL files matching the `--file-pattern`. This is typically used for linting or pre-deployment validation within a CI pipeline. It utilizes global flags like `--url`, `--service-account`, `--service-account-secret`, `--file-pattern`, and `--declarative`.
+Checks the SQL files matching the `--file-pattern`. This is typically used for linting or pre-deployment validation within a CI pipeline. Use `--output` to save check results to a JSON file.
### `rollout`
@@ -30,8 +30,10 @@ This action is configured via command-line flags. Global flags apply to all comm
These flags apply to the main `bytebase-action` command and its subcommands (`check`, `rollout`).
-- **`--output`**: The output file location. The output file is a JSON file with the created resource names.
+- **`--output`**: The output file location. The output file is a JSON file with the created resource names and check results.
- Default: `""` (empty string)
+ - For `check` command: outputs detailed check results including advices, affected rows, and risk levels
+ - For `rollout` command: outputs created resource names (release, plan, rollout)
- **`--url`**: The Bytebase instance URL.
- Default: `https://demo.bytebase.com`
@@ -97,7 +99,6 @@ These flags are specific to the `check` subcommand (`bytebase-action check`).
- `FAIL_ON_ERROR`: Fail only if there are errors in the check results.
- Default: `SKIP`
- Note: Platform-specific outputs (GitHub comments, GitLab reports, etc.) are always generated before evaluating whether to fail.
-
### `rollout` Command Specific Flags
These flags are specific to the `rollout` subcommand (`bytebase-action rollout`).
diff --git a/action/command/check.go b/action/command/check.go
index 52e8dff7b9..06456e09f3 100644
--- a/action/command/check.go
+++ b/action/command/check.go
@@ -7,6 +7,7 @@ import (
"github.com/bytebase/bytebase/action/args"
"github.com/bytebase/bytebase/action/azure"
"github.com/bytebase/bytebase/action/bitbucket"
+ "github.com/bytebase/bytebase/action/command/output"
"github.com/bytebase/bytebase/action/github"
"github.com/bytebase/bytebase/action/gitlab"
"github.com/bytebase/bytebase/action/world"
@@ -46,6 +47,9 @@ func validateCheckFlags(w *world.World) func(*cobra.Command, []string) error {
func runCheck(w *world.World) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, _ []string) error {
+ defer func() {
+ output.WriteOutput(w)
+ }()
platform := w.Platform
w.Logger.Info("running on platform", "platform", platform.String())
client, err := NewClient(w.URL, w.ServiceAccount, w.ServiceAccountSecret)
@@ -69,6 +73,9 @@ func runCheck(w *world.World) func(*cobra.Command, []string) error {
return err
}
+ // Store check results in OutputMap for file output
+ w.OutputMap.CheckResults = checkReleaseResponse
+
w.Logger.Info("check release response", "resultCount", len(checkReleaseResponse.Results))
// Generate platform-specific outputs
diff --git a/action/command/output/output.go b/action/command/output/output.go
index 19ed4e4f59..90ca992719 100644
--- a/action/command/output/output.go
+++ b/action/command/output/output.go
@@ -6,6 +6,7 @@ import (
"path/filepath"
"github.com/pkg/errors"
+ "google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/action/github"
"github.com/bytebase/bytebase/action/world"
@@ -42,7 +43,31 @@ func writeOutputJSON(w *world.World) error {
}
defer f.Close()
- j, err := json.Marshal(w.OutputMap)
+ // Marshal OutputMap with protojson support for CheckResults
+ outputData := make(map[string]any)
+ if w.OutputMap.Release != "" {
+ outputData["release"] = w.OutputMap.Release
+ }
+ if w.OutputMap.Plan != "" {
+ outputData["plan"] = w.OutputMap.Plan
+ }
+ if w.OutputMap.Rollout != "" {
+ outputData["rollout"] = w.OutputMap.Rollout
+ }
+ if w.OutputMap.CheckResults != nil {
+ // Use protojson to marshal CheckResults with camelCase keys
+ checkResultsJSON, err := protojson.Marshal(w.OutputMap.CheckResults)
+ if err != nil {
+ return errors.Wrapf(err, "failed to marshal check results")
+ }
+ var checkResultsMap map[string]any
+ if err := json.Unmarshal(checkResultsJSON, &checkResultsMap); err != nil {
+ return errors.Wrapf(err, "failed to unmarshal check results")
+ }
+ outputData["checkResults"] = checkResultsMap
+ }
+
+ j, err := json.MarshalIndent(outputData, "", " ")
if err != nil {
return errors.Wrapf(err, "failed to marshal output map")
}
diff --git a/action/command/output/output_test.go b/action/command/output/output_test.go
new file mode 100644
index 0000000000..c10d5654c8
--- /dev/null
+++ b/action/command/output/output_test.go
@@ -0,0 +1,192 @@
+package output
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/action/world"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+)
+
+func TestWriteOutputJSON_EmptyOutputPath(t *testing.T) {
+ w := world.NewWorld()
+ w.Output = ""
+ w.OutputMap.Release = "projects/test/releases/123"
+
+ err := writeOutputJSON(w)
+
+ require.NoError(t, err)
+}
+
+func TestWriteOutputJSON_StringFieldsOnly(t *testing.T) {
+ tmpDir := t.TempDir()
+ outputFile := filepath.Join(tmpDir, "output.json")
+
+ w := world.NewWorld()
+ w.Output = outputFile
+ w.OutputMap.Release = "projects/test/releases/123"
+ w.OutputMap.Plan = "projects/test/plans/456"
+ w.OutputMap.Rollout = "projects/test/rollouts/789"
+
+ err := writeOutputJSON(w)
+ require.NoError(t, err)
+
+ // Verify file was created
+ data, err := os.ReadFile(outputFile)
+ require.NoError(t, err)
+
+ // Verify JSON content
+ var result map[string]any
+ err = json.Unmarshal(data, &result)
+ require.NoError(t, err)
+
+ require.Equal(t, "projects/test/releases/123", result["release"])
+ require.Equal(t, "projects/test/plans/456", result["plan"])
+ require.Equal(t, "projects/test/rollouts/789", result["rollout"])
+ require.NotContains(t, result, "checkResults")
+}
+
+func TestWriteOutputJSON_CheckResultsWithProtojson(t *testing.T) {
+ tmpDir := t.TempDir()
+ outputFile := filepath.Join(tmpDir, "output.json")
+
+ w := world.NewWorld()
+ w.Output = outputFile
+ w.OutputMap.CheckResults = &v1pb.CheckReleaseResponse{
+ Results: []*v1pb.CheckReleaseResponse_CheckResult{
+ {
+ File: "migration.sql",
+ Target: "instances/prod/databases/mydb",
+ Advices: []*v1pb.Advice{
+ {
+ Status: v1pb.Advice_WARNING,
+ Code: 1001,
+ Title: "Schema drift detected",
+ Content: "Schema differs from expected",
+ },
+ },
+ AffectedRows: 100,
+ RiskLevel: v1pb.RiskLevel_MODERATE,
+ },
+ },
+ AffectedRows: 100,
+ RiskLevel: v1pb.RiskLevel_MODERATE,
+ }
+
+ err := writeOutputJSON(w)
+ require.NoError(t, err)
+
+ // Verify file was created
+ data, err := os.ReadFile(outputFile)
+ require.NoError(t, err)
+
+ // Verify JSON content
+ var result map[string]any
+ err = json.Unmarshal(data, &result)
+ require.NoError(t, err)
+
+ // Verify CheckResults is present
+ require.Contains(t, result, "checkResults")
+ checkResults, ok := result["checkResults"].(map[string]any)
+ require.True(t, ok, "checkResults should be a map")
+
+ // Verify protojson uses camelCase (not snake_case)
+ require.Contains(t, checkResults, "results")
+ require.Contains(t, checkResults, "affectedRows")
+ require.Contains(t, checkResults, "riskLevel")
+
+ // Verify data integrity
+ // Note: protojson serializes int64 as strings to avoid JavaScript precision loss
+ require.Equal(t, "100", checkResults["affectedRows"])
+ require.Equal(t, "MODERATE", checkResults["riskLevel"])
+
+ results, ok := checkResults["results"].([]any)
+ require.True(t, ok, "results should be an array")
+ require.Len(t, results, 1)
+
+ firstResult, ok := results[0].(map[string]any)
+ require.True(t, ok, "first result should be a map")
+ require.Equal(t, "migration.sql", firstResult["file"])
+ require.Equal(t, "instances/prod/databases/mydb", firstResult["target"])
+ require.Equal(t, "100", firstResult["affectedRows"])
+
+ advices, ok := firstResult["advices"].([]any)
+ require.True(t, ok, "advices should be an array")
+ require.Len(t, advices, 1)
+ advice, ok := advices[0].(map[string]any)
+ require.True(t, ok, "advice should be a map")
+ require.Equal(t, "WARNING", advice["status"])
+ require.Equal(t, float64(1001), advice["code"])
+ require.Equal(t, "Schema drift detected", advice["title"])
+}
+
+func TestWriteOutputJSON_CreatesParentDirectory(t *testing.T) {
+ tmpDir := t.TempDir()
+ outputFile := filepath.Join(tmpDir, "nested", "dir", "output.json")
+
+ w := world.NewWorld()
+ w.Output = outputFile
+ w.OutputMap.Release = "projects/test/releases/123"
+
+ err := writeOutputJSON(w)
+ require.NoError(t, err)
+
+ // Verify file was created
+ _, err = os.Stat(outputFile)
+ require.NoError(t, err)
+
+ // Verify parent directories were created
+ _, err = os.Stat(filepath.Join(tmpDir, "nested", "dir"))
+ require.NoError(t, err)
+}
+
+func TestWriteOutputJSON_AllFieldsPopulated(t *testing.T) {
+ tmpDir := t.TempDir()
+ outputFile := filepath.Join(tmpDir, "output.json")
+
+ w := world.NewWorld()
+ w.Output = outputFile
+ w.OutputMap.Release = "projects/test/releases/123"
+ w.OutputMap.Plan = "projects/test/plans/456"
+ w.OutputMap.Rollout = "projects/test/rollouts/789"
+ w.OutputMap.CheckResults = &v1pb.CheckReleaseResponse{
+ Results: []*v1pb.CheckReleaseResponse_CheckResult{
+ {
+ File: "test.sql",
+ Target: "instances/test/databases/db",
+ AffectedRows: 42,
+ },
+ },
+ AffectedRows: 42,
+ }
+
+ err := writeOutputJSON(w)
+ require.NoError(t, err)
+
+ // Verify file was created
+ data, err := os.ReadFile(outputFile)
+ require.NoError(t, err)
+
+ // Verify JSON content
+ var result map[string]any
+ err = json.Unmarshal(data, &result)
+ require.NoError(t, err)
+
+ // Verify all fields are present
+ require.Equal(t, "projects/test/releases/123", result["release"])
+ require.Equal(t, "projects/test/plans/456", result["plan"])
+ require.Equal(t, "projects/test/rollouts/789", result["rollout"])
+ require.Contains(t, result, "checkResults")
+
+ // Verify CheckResults structure
+ checkResults, ok := result["checkResults"].(map[string]any)
+ require.True(t, ok, "checkResults should be a map")
+ require.Equal(t, "42", checkResults["affectedRows"])
+ results, ok := checkResults["results"].([]any)
+ require.True(t, ok, "results should be an array")
+ require.Len(t, results, 1)
+}
diff --git a/action/world/world.go b/action/world/world.go
index 8472a72c70..f0088f7d00 100644
--- a/action/world/world.go
+++ b/action/world/world.go
@@ -44,9 +44,10 @@ type World struct {
// Outputs
OutputMap struct {
- Release string `json:"release,omitempty"`
- Plan string `json:"plan,omitempty"`
- Rollout string `json:"rollout,omitempty"`
+ Release string `json:"release,omitempty"`
+ Plan string `json:"plan,omitempty"`
+ Rollout string `json:"rollout,omitempty"`
+ CheckResults *v1pb.CheckReleaseResponse `json:"checkResults,omitempty"`
}
PendingStages []string
Rollout *v1pb.Rollout