Skip to content

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

refactor: action pkgs (#17366)

p0nyfc26ec3e39e6
diff --git a/action/command/azure_devops.go b/action/azure/azure.go
similarity index 94%
rename from action/command/azure_devops.go
rename to action/azure/azure.go
index 8973b32cef..43bab1bd30 100644
--- a/action/command/azure_devops.go
+++ b/action/azure/azure.go
@@ -1,4 +1,4 @@
-package command
+package azure
import (
"fmt"
@@ -9,7 +9,7 @@ import (
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
-func loggingReleaseChecks(resp *v1pb.CheckReleaseResponse) error {
+func LoggingReleaseChecks(resp *v1pb.CheckReleaseResponse) error {
if resp == nil || len(resp.Results) == 0 {
fmt.Println("No check results found.")
return nil
diff --git a/action/command/bitbucket.go b/action/bitbucket/bitbucket.go
similarity index 98%
rename from action/command/bitbucket.go
rename to action/bitbucket/bitbucket.go
index 02402b46ab..03c4569c5e 100644
--- a/action/command/bitbucket.go
+++ b/action/bitbucket/bitbucket.go
@@ -1,4 +1,4 @@
-package command
+package bitbucket
import (
"bytes"
@@ -37,7 +37,7 @@ type Annotation struct {
Line int `json:"line,omitempty"`
}
-func createBitbucketReport(checkResponse *v1pb.CheckReleaseResponse) error {
+func CreateBitbucketReport(checkResponse *v1pb.CheckReleaseResponse) error {
repoOwner := os.Getenv("BITBUCKET_REPO_OWNER")
repoSlug := os.Getenv("BITBUCKET_REPO_SLUG")
commit := os.Getenv("BITBUCKET_COMMIT")
diff --git a/action/command/check.go b/action/command/check.go
index 8dd8fff9cd..52e8dff7b9 100644
--- a/action/command/check.go
+++ b/action/command/check.go
@@ -5,7 +5,10 @@ import (
"github.com/spf13/cobra"
"github.com/bytebase/bytebase/action/args"
+ "github.com/bytebase/bytebase/action/azure"
+ "github.com/bytebase/bytebase/action/bitbucket"
"github.com/bytebase/bytebase/action/github"
+ "github.com/bytebase/bytebase/action/gitlab"
"github.com/bytebase/bytebase/action/world"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
@@ -51,7 +54,7 @@ func runCheck(w *world.World) func(*cobra.Command, []string) error {
}
// Check version compatibility
- checkVersionCompatibility(w, client, args.Version)
+ CheckVersionCompatibility(w, client, args.Version)
releaseFiles, _, err := getReleaseFiles(w)
if err != nil {
@@ -75,15 +78,15 @@ func runCheck(w *world.World) func(*cobra.Command, []string) error {
return err
}
case world.GitLab:
- if err := writeReleaseCheckToCodeQualityJSON(checkReleaseResponse); err != nil {
+ if err := gitlab.WriteReleaseCheckToCodeQualityJSON(checkReleaseResponse); err != nil {
return err
}
case world.AzureDevOps:
- if err := loggingReleaseChecks(checkReleaseResponse); err != nil {
+ if err := azure.LoggingReleaseChecks(checkReleaseResponse); err != nil {
return err
}
case world.Bitbucket:
- if err := createBitbucketReport(checkReleaseResponse); err != nil {
+ if err := bitbucket.CreateBitbucketReport(checkReleaseResponse); err != nil {
return err
}
default:
diff --git a/action/command/cloud/cloud.go b/action/command/cloud/cloud.go
new file mode 100644
index 0000000000..ffc7a45090
--- /dev/null
+++ b/action/command/cloud/cloud.go
@@ -0,0 +1,124 @@
+package cloud
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "log/slog"
+ "net/http"
+ "net/url"
+ "time"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/action/command/validation"
+ "github.com/bytebase/bytebase/action/world"
+)
+
+// EnsureWorkspaceAwake checks if a Bytebase cloud workspace is healthy and wakes it up if needed.
+// Special handling for Bytebase cloud URLs (*.us-central1.bytebase.com)
+func EnsureWorkspaceAwake(w *world.World) error {
+ u, _ := url.Parse(w.URL) // Already validated in ValidateFlags
+ if !validation.IsCloudURL(u) {
+ return nil
+ }
+ host := u.Host
+ healthzURL := fmt.Sprintf("https://%s/healthz", host)
+
+ // Check if the workspace is already healthy
+ if isHealthy(healthzURL) {
+ w.Logger.Info("Workspace is already healthy", "host", host)
+ return nil
+ }
+
+ // Wake up the workspace
+ w.Logger.Info("Workspace needs to be awakened", "host", host)
+ if err := wakeUpWorkspace(w.Logger, host); err != nil {
+ return errors.Wrapf(err, "failed to wake up workspace")
+ }
+
+ // Wait 15 seconds before checking healthz.
+ time.Sleep(15 * time.Second)
+
+ // Wait for the workspace to become healthy (3 consecutive successful health checks)
+ w.Logger.Info("Waiting for workspace to become healthy...")
+ consecutiveSuccess := 0
+ maxAttempts := 60 // Maximum 5 minutes (60 * 5 seconds)
+
+ for attempt := range maxAttempts {
+ if isHealthy(healthzURL) {
+ consecutiveSuccess++
+ w.Logger.Info("Health check succeeded", "consecutive", consecutiveSuccess)
+ if consecutiveSuccess >= 3 {
+ w.Logger.Info("Workspace is now healthy", "host", host)
+ return nil
+ }
+ } else {
+ consecutiveSuccess = 0
+ w.Logger.Info("Health check failed, retrying...", "attempt", attempt+1)
+ time.Sleep(5 * time.Second)
+ }
+ }
+
+ return errors.Errorf("workspace did not become healthy after %d attempts", maxAttempts)
+}
+
+// isHealthy checks if the workspace health endpoint returns OK.
+func isHealthy(healthzURL string) bool {
+ // Add cache-busting parameter to ensure fresh response
+ urlWithCacheBust := fmt.Sprintf("%s?_=%d", healthzURL, time.Now().UnixNano())
+
+ client := &http.Client{Timeout: 10 * time.Second}
+ req, err := http.NewRequest("GET", urlWithCacheBust, nil)
+ if err != nil {
+ return false
+ }
+
+ // Add no-cache headers
+ req.Header.Set("Cache-Control", "no-cache")
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return false
+ }
+ defer resp.Body.Close()
+ return resp.StatusCode == http.StatusOK
+}
+
+// wakeUpWorkspace calls the API to wake up a Bytebase cloud workspace.
+func wakeUpWorkspace(logger *slog.Logger, domain string) error {
+ wakeUpURL := "https://hub.bytebase.com/v1/workspaces:wakeUp"
+
+ payload := map[string]string{
+ "domain": domain,
+ }
+
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return errors.Wrapf(err, "failed to marshal wake up request")
+ }
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ req, err := http.NewRequest("POST", wakeUpURL, bytes.NewBuffer(jsonData))
+ if err != nil {
+ return errors.Wrapf(err, "failed to create wake up request")
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return errors.Wrapf(err, "failed to send wake up request")
+ }
+ defer resp.Body.Close()
+
+ // Read response body for logging
+ bodyBytes, _ := io.ReadAll(resp.Body)
+ bodyString := string(bodyBytes)
+
+ // Log response status and body
+ logger.Info("Wake up workspace response", "status", resp.StatusCode, "body", bodyString)
+
+ return nil
+}
diff --git a/action/command/output/output.go b/action/command/output/output.go
new file mode 100644
index 0000000000..19ed4e4f59
--- /dev/null
+++ b/action/command/output/output.go
@@ -0,0 +1,74 @@
+package output
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/action/github"
+ "github.com/bytebase/bytebase/action/world"
+)
+
+// WriteOutput writes the output JSON and GitHub step summary if applicable.
+func WriteOutput(w *world.World) {
+ if err := writeOutputJSON(w); err != nil {
+ w.Logger.Error("failed to write output JSON", "error", err)
+ }
+ if err := writeGitHubStepSummary(w); err != nil {
+ w.Logger.Error("failed to write GitHub step summary", "error", err)
+ }
+}
+
+// writeOutputJSON writes the output map to the specified JSON file.
+func writeOutputJSON(w *world.World) error {
+ if w.Output == "" {
+ return nil
+ }
+
+ w.Logger.Info("writing output to file", "file", w.Output)
+
+ // Create parent directory if not exists
+ if dir := filepath.Dir(w.Output); dir != "" {
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ return errors.Wrapf(err, "failed to create output directory: %s", dir)
+ }
+ }
+
+ f, err := os.Create(w.Output)
+ if err != nil {
+ return errors.Wrapf(err, "failed to create output file: %s", w.Output)
+ }
+ defer f.Close()
+
+ j, err := json.Marshal(w.OutputMap)
+ if err != nil {
+ return errors.Wrapf(err, "failed to marshal output map")
+ }
+
+ if _, err := f.Write(j); err != nil {
+ return errors.Wrapf(err, "failed to write output file: %s", w.Output)
+ }
+ return nil
+}
+
+// writeGitHubStepSummary writes the GitHub step summary for rollout operations.
+func writeGitHubStepSummary(w *world.World) error {
+ if w.Platform != world.GitHub || !w.IsRollout {
+ return nil
+ }
+
+ filename := os.Getenv("GITHUB_STEP_SUMMARY")
+ f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ return errors.Wrapf(err, "failed to open GitHub step summary file: %s", filename)
+ }
+ defer f.Close()
+
+ summary := github.BuildSummaryMarkdown(w)
+ if _, err := f.WriteString(summary); err != nil {
+ return errors.Wrapf(err, "failed to write GitHub step summary: %s", filename)
+ }
+ return nil
+}
diff --git a/action/command/rollout.go b/action/command/rollout.go
index 0401a33dae..034e23b77f 100644
--- a/action/command/rollout.go
+++ b/action/command/rollout.go
@@ -14,6 +14,7 @@ import (
"go.uber.org/multierr"
"github.com/bytebase/bytebase/action/args"
+ "github.com/bytebase/bytebase/action/command/output"
"github.com/bytebase/bytebase/action/world"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
@@ -58,7 +59,7 @@ func rolloutPreRun(w *world.World) func(*cobra.Command, []string) error {
func runRollout(w *world.World) func(command *cobra.Command, _ []string) error {
return func(command *cobra.Command, _ []string) error {
defer func() {
- postRun(w)
+ output.WriteOutput(w)
}()
w.IsRollout = true
ctx := command.Context()
@@ -68,7 +69,7 @@ func runRollout(w *world.World) func(command *cobra.Command, _ []string) error {
}
// Check version compatibility
- checkVersionCompatibility(w, client, args.Version)
+ CheckVersionCompatibility(w, client, args.Version)
var plan *v1pb.Plan
if w.Plan != "" {
diff --git a/action/command/root.go b/action/command/root.go
index 47186036f8..2e860dcf54 100644
--- a/action/command/root.go
+++ b/action/command/root.go
@@ -1,26 +1,15 @@
package command
import (
- "bytes"
"context"
- "encoding/json"
- "fmt"
- "io"
"log/slog"
- "net/http"
- "net/url"
- "os"
- "path/filepath"
- "regexp"
- "strings"
- "time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
- "github.com/bytebase/bytebase/action/common"
+ "github.com/bytebase/bytebase/action/command/cloud"
+ "github.com/bytebase/bytebase/action/command/validation"
"github.com/bytebase/bytebase/action/world"
- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
func NewRootCommand(w *world.World) *cobra.Command {
@@ -49,286 +38,22 @@ func NewRootCommand(w *world.World) *cobra.Command {
func rootPreRun(w *world.World) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, _ []string) error {
w.Logger = slog.New(slog.NewTextHandler(cmd.ErrOrStderr(), nil))
- if w.ServiceAccountSecret == "" {
- w.ServiceAccountSecret = os.Getenv("BYTEBASE_SERVICE_ACCOUNT_SECRET")
- }
-
- if w.Platform == world.UnspecifiedPlatform {
- w.Platform = world.GetJobPlatform()
- }
-
- if w.ServiceAccount == "" {
- return errors.Errorf("service-account is required and cannot be empty")
- }
- if w.ServiceAccountSecret == "" {
- return errors.Errorf("service-account-secret is required and cannot be empty")
- }
- // Validate URL format
- u, err := url.Parse(w.URL)
- if err != nil {
- return errors.Wrapf(err, "invalid URL format: %s", w.URL)
+ // Validate all flags and environment variables
+ if err := validation.ValidateFlags(w); err != nil {
+ return errors.Wrapf(err, "failed to validate flags")
}
- w.URL = strings.TrimSuffix(u.String(), "/") // update the URL to the canonical form
// Special handling for Bytebase cloud URLs (*.us-central1.bytebase.com)
- cloudURLPattern := regexp.MustCompile(`^[a-z0-9]+\.us-central1\.bytebase\.com$`)
- if cloudURLPattern.MatchString(u.Host) {
- if err := ensureWorkspaceAwake(w, u.Host); err != nil {
- return errors.Wrapf(err, "failed to wake up workspace")
- }
- }
-
- // Validate project format
- if !strings.HasPrefix(w.Project, "projects/") {
- return errors.Errorf("invalid project format, must be projects/{project}")
- }
-
- // Validate targets format
- var databaseTarget, databaseGroupTarget int
- for _, target := range w.Targets {
- if _, _, err := common.GetInstanceDatabaseID(target); err == nil {
- databaseTarget++
- } else if _, _, err := common.GetProjectIDDatabaseGroupID(target); err == nil {
- databaseGroupTarget++
- } else {
- return errors.Errorf("invalid target format, must be instances/{instance}/databases/{database} or projects/{project}/databaseGroups/{databaseGroup}")
- }
- }
- if databaseTarget > 0 && databaseGroupTarget > 0 {
- return errors.Errorf("targets must be either databases or databaseGroups")
- }
- if databaseGroupTarget > 1 {
- return errors.Errorf("targets must be a single databaseGroup")
+ if err := cloud.EnsureWorkspaceAwake(w); err != nil {
+ return errors.Wrapf(err, "failed to ensure workspace awake")
}
return nil
}
}
-func postRun(w *world.World) {
- if err := func() error {
- if w.Output == "" {
- return nil
- }
-
- w.Logger.Info("writing output to file", "file", w.Output)
-
- // Create parent directory if not exists
- if dir := filepath.Dir(w.Output); dir != "" {
- if err := os.MkdirAll(dir, 0755); err != nil {
- return errors.Wrapf(err, "failed to create output directory: %s", dir)
- }
- }
-
- f, err := os.Create(w.Output)
- if err != nil {
- return errors.Wrapf(err, "failed to create output file: %s", w.Output)
- }
- defer f.Close()
-
- j, err := json.Marshal(w.OutputMap)
- if err != nil {
- return errors.Wrapf(err, "failed to marshal output map")
- }
-
- if _, err := f.Write(j); err != nil {
- return errors.Wrapf(err, "failed to write output file: %s", w.Output)
- }
- return nil
- }(); err != nil {
- w.Logger.Error("failed to write output JSON", "error", err)
- }
- if err := func() error {
- if w.Platform == world.GitHub && w.IsRollout {
- filename := os.Getenv("GITHUB_STEP_SUMMARY")
- f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
- if err != nil {
- return errors.Wrapf(err, "failed to open GitHub step summary file: %s", filename)
- }
- defer f.Close()
-
- summary := buildSummaryMarkdown(w)
- if _, err := f.WriteString(summary); err != nil {
- return errors.Wrapf(err, "failed to write GitHub step summary: %s", filename)
- }
- return nil
- }
- return nil
- }(); err != nil {
- w.Logger.Error("failed to write GitHub step summary", "error", err)
- }
-}
-
-func buildSummaryMarkdown(w *world.World) string {
- var sb strings.Builder
- if w.OutputMap.Release != "" {
- _, _ = sb.WriteString("Release: " + w.URL + "/" + w.OutputMap.Release + "\n")
- }
- if w.OutputMap.Plan != "" {
- _, _ = sb.WriteString("Plan: " + w.URL + "/" + w.OutputMap.Plan + "\n")
- }
- if w.OutputMap.Rollout != "" {
- _, _ = sb.WriteString("Rollout: " + w.URL + "/" + w.OutputMap.Rollout + "\n")
- }
-
- // Add stage status table
- buildOutputStagesMarkdown(w, &sb)
-
- return sb.String()
-}
-
-func buildOutputStagesMarkdown(w *world.World, sb *strings.Builder) {
- if w.IsRollout && w.Rollout != nil {
- pendingStages := w.PendingStages
- rollout := w.Rollout
- // build a table of stage and status in GitHub flavored markdown.
- // The stages come from the aggregated result of pendingStages and rollout.Stages.
- // pendingStages are the stages to be rolled out at the beginning of the execution,
- // they may or may not have been executed (depending on the targetStage input).
- // rollout.Stages are the stages that have been executed, and the stage status is
- // an aggregated status from stage tasks.
-
- // Combine stages from rollout.Stages and pendingStages, deduplicating
- allStages := []string{}
- seenStages := make(map[string]bool)
-
- // First add all stages from rollout.Stages
- for _, stage := range rollout.Stages {
- if !seenStages[stage.Environment] {
- allStages = append(allStages, stage.Environment)
- seenStages[stage.Environment] = true
- }
- }
-
- // Then append pendingStages that aren't already in the list
- for _, stage := range pendingStages {
- if !seenStages[stage] {
- allStages = append(allStages, stage)
- seenStages[stage] = true
- }
- }
-
- // Check if there are any stages to display
- if len(allStages) == 0 {
- sb.WriteString("\n### Rollout Stages\n\n")
- sb.WriteString("_No stages to display. The rollout may not have any stages._\n\n")
- return
- }
-
- // Create a map of stage environment to Stage object for quick lookup
- stageMap := make(map[string]*v1pb.Stage)
- for _, stage := range rollout.Stages {
- stageMap[stage.Environment] = stage
- }
-
- // Build the markdown table
- sb.WriteString("\n### Rollout Stages\n\n")
- sb.WriteString("| Stage | Status |\n")
- sb.WriteString("|-------|--------|\n")
-
- // Output all stages with their status
- for _, stageEnv := range allStages {
- stageName := extractStageName(stageEnv)
-
- // Determine status
- var status string
- if stage, exists := stageMap[stageEnv]; exists {
- // Stage exists in rollout, get its aggregated status
- status = getAggregatedStageStatus(stage)
- } else {
- // Stage is only in pendingStages, mark as pending
- status = "⏳ Pending"
- }
-
- sb.WriteString("| ")
- sb.WriteString(stageName)
- sb.WriteString(" | ")
- sb.WriteString(status)
- sb.WriteString(" |\n")
- }
-
- sb.WriteString("\n")
- }
-}
-
-// getAggregatedStageStatus determines the overall status of a stage based on its tasks
-func getAggregatedStageStatus(stage *v1pb.Stage) string {
- if stage == nil || len(stage.Tasks) == 0 {
- return "⏳ Pending"
- }
-
- hasRunning := false
- hasFailed := false
- hasCanceled := false
- hasSkipped := false
- hasPending := false
- hasNotStarted := false
- allDone := true
-
- for _, task := range stage.Tasks {
- switch task.Status {
- case v1pb.Task_RUNNING:
- hasRunning = true
- allDone = false
- case v1pb.Task_FAILED:
- hasFailed = true
- allDone = false
- case v1pb.Task_CANCELED:
- hasCanceled = true
- allDone = false
- case v1pb.Task_SKIPPED:
- hasSkipped = true
- case v1pb.Task_PENDING:
- hasPending = true
- allDone = false
- case v1pb.Task_NOT_STARTED, v1pb.Task_STATUS_UNSPECIFIED:
- hasNotStarted = true
- allDone = false
- case v1pb.Task_DONE:
- // Task is done
- default:
- allDone = false
- }
- }
-
- // Determine overall stage status based on task statuses
- if hasFailed {
- return "❌ Failed"
- }
- if hasCanceled {
- return "⛔ Canceled"
- }
- if hasRunning {
- return "🔄 Running"
- }
- if hasPending {
- return "⏳ Pending"
- }
- if hasNotStarted {
- return "⏸️ Not Started"
- }
- if allDone && !hasSkipped {
- return "✅ Done"
- }
- if allDone && hasSkipped {
- return "⏭️ Skipped"
- }
-
- return "⏳ Pending"
-}
-
-// extractStageName extracts the stage name from the environment path
-func extractStageName(environment string) string {
- // Format: environments/{environment}
- parts := strings.Split(environment, "/")
- if len(parts) >= 2 {
- return parts[len(parts)-1]
- }
- return environment
-}
-
-func checkVersionCompatibility(w *world.World, client *Client, cliVersion string) {
+func CheckVersionCompatibility(w *world.World, client *Client, cliVersion string) {
if cliVersion == "unknown" {
w.Logger.Warn("CLI version unknown, unable to check compatibility")
return
@@ -357,104 +82,3 @@ func checkVersionCompatibility(w *world.World, client *Client, cliVersion string
w.Logger.Info("CLI version matches server version", "version", cliVersion)
}
}
-
-// ensureWorkspaceAwake checks if a Bytebase cloud workspace is healthy and wakes it up if needed.
-func ensureWorkspaceAwake(w *world.World, host string) error {
- healthzURL := fmt.Sprintf("https://%s/healthz", host)
-
- // Check if the workspace is already healthy
- if isHealthy(healthzURL) {
- w.Logger.Info("Workspace is already healthy", "host", host)
- return nil
- }
-
- // Wake up the workspace
- w.Logger.Info("Workspace needs to be awakened", "host", host)
- if err := wakeUpWorkspace(w.Logger, host); err != nil {
- return errors.Wrapf(err, "failed to wake up workspace")
- }
-
- // Wait 15 seconds before checking healthz.
- time.Sleep(15 * time.Second)
-
- // Wait for the workspace to become healthy (3 consecutive successful health checks)
- w.Logger.Info("Waiting for workspace to become healthy...")
- consecutiveSuccess := 0
- maxAttempts := 60 // Maximum 5 minutes (60 * 5 seconds)
-
- for attempt := range maxAttempts {
- if isHealthy(healthzURL) {
- consecutiveSuccess++
- w.Logger.Info("Health check succeeded", "consecutive", consecutiveSuccess)
- if consecutiveSuccess >= 3 {
- w.Logger.Info("Workspace is now healthy", "host", host)
- return nil
- }
- } else {
- consecutiveSuccess = 0
- w.Logger.Info("Health check failed, retrying...", "attempt", attempt+1)
- time.Sleep(5 * time.Second)
- }
- }
-
- return errors.Errorf("workspace did not become healthy after %d attempts", maxAttempts)
-}
-
-// isHealthy checks if the workspace health endpoint returns OK.
-func isHealthy(healthzURL string) bool {
- // Add cache-busting parameter to ensure fresh response
- urlWithCacheBust := fmt.Sprintf("%s?_=%d", healthzURL, time.Now().UnixNano())
-
- client := &http.Client{Timeout: 10 * time.Second}
- req, err := http.NewRequest("GET", urlWithCacheBust, nil)
- if err != nil {
- return false
- }
-
- // Add no-cache headers
- req.Header.Set("Cache-Control", "no-cache")
-
- resp, err := client.Do(req)
- if err != nil {
- return false
- }
- defer resp.Body.Close()
- return resp.StatusCode == http.StatusOK
-}
-
-// wakeUpWorkspace calls the API to wake up a Bytebase cloud workspace.
-func wakeUpWorkspace(logger *slog.Logger, domain string) error {
- wakeUpURL := "https://hub.bytebase.com/v1/workspaces:wakeUp"
-
- payload := map[string]string{
- "domain": domain,
- }
-
- jsonData, err := json.Marshal(payload)
- if err != nil {
- return errors.Wrapf(err, "failed to marshal wake up request")
- }
-
- client := &http.Client{Timeout: 30 * time.Second}
- req, err := http.NewRequest("POST", wakeUpURL, bytes.NewBuffer(jsonData))
- if err != nil {
- return errors.Wrapf(err, "failed to create wake up request")
- }
-
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := client.Do(req)
- if err != nil {
- return errors.Wrapf(err, "failed to send wake up request")
- }
- defer resp.Body.Close()
-
- // Read response body for logging
- bodyBytes, _ := io.ReadAll(resp.Body)
- bodyString := string(bodyBytes)
-
- // Log response status and body
- logger.Info("Wake up workspace response", "status", resp.StatusCode, "body", bodyString)
-
- return nil
-}
diff --git a/action/command/validation/flags.go b/action/command/validation/flags.go
new file mode 100644
index 0000000000..d549f1264b
--- /dev/null
+++ b/action/command/validation/flags.go
@@ -0,0 +1,76 @@
+package validation
+
+import (
+ "net/url"
+ "os"
+ "regexp"
+ "strings"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/action/common"
+ "github.com/bytebase/bytebase/action/world"
+)
+
+// ValidateFlags validates all the command line flags and environment variables.
+func ValidateFlags(w *world.World) error {
+ // Set service account secret from environment if not provided
+ if w.ServiceAccountSecret == "" {
+ w.ServiceAccountSecret = os.Getenv("BYTEBASE_SERVICE_ACCOUNT_SECRET")
+ }
+
+ // Set platform if not specified
+ if w.Platform == world.UnspecifiedPlatform {
+ w.Platform = world.GetJobPlatform()
+ }
+
+ // Validate service account
+ if w.ServiceAccount == "" {
+ return errors.Errorf("service-account is required and cannot be empty")
+ }
+ if w.ServiceAccountSecret == "" {
+ return errors.Errorf("service-account-secret is required and cannot be empty")
+ }
+
+ // Validate URL format
+ u, err := url.Parse(w.URL)
+ if err != nil {
+ return errors.Wrapf(err, "invalid URL format: %s", w.URL)
+ }
+ w.URL = strings.TrimSuffix(u.String(), "/") // update the URL to the canonical form
+
+ // Validate project format
+ if !strings.HasPrefix(w.Project, "projects/") {
+ return errors.Errorf("invalid project format, must be projects/{project}")
+ }
+
+ // Validate targets format
+ return validateTargets(w.Targets)
+}
+
+// validateTargets validates the targets format and ensures consistency.
+func validateTargets(targets []string) error {
+ var databaseTarget, databaseGroupTarget int
+ for _, target := range targets {
+ if _, _, err := common.GetInstanceDatabaseID(target); err == nil {
+ databaseTarget++
+ } else if _, _, err := common.GetProjectIDDatabaseGroupID(target); err == nil {
+ databaseGroupTarget++
+ } else {
+ return errors.Errorf("invalid target format, must be instances/{instance}/databases/{database} or projects/{project}/databaseGroups/{databaseGroup}")
+ }
+ }
+ if databaseTarget > 0 && databaseGroupTarget > 0 {
+ return errors.Errorf("targets must be either databases or databaseGroups")
+ }
+ if databaseGroupTarget > 1 {
+ return errors.Errorf("targets must be a single databaseGroup")
+ }
+ return nil
+}
+
+// IsCloudURL checks if the given URL is a Bytebase cloud URL.
+func IsCloudURL(u *url.URL) bool {
+ cloudURLPattern := regexp.MustCompile(`^[a-z0-9]+\.us-central1\.bytebase\.com$`)
+ return cloudURLPattern.MatchString(u.Host)
+}
diff --git a/action/github/summary.go b/action/github/summary.go
new file mode 100644
index 0000000000..5510dbc748
--- /dev/null
+++ b/action/github/summary.go
@@ -0,0 +1,176 @@
+package github
+
+import (
+ "strings"
+
+ "github.com/bytebase/bytebase/action/world"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+)
+
+func BuildSummaryMarkdown(w *world.World) string {
+ var sb strings.Builder
+ if w.OutputMap.Release != "" {
+ _, _ = sb.WriteString("Release: " + w.URL + "/" + w.OutputMap.Release + "\n")
+ }
+ if w.OutputMap.Plan != "" {
+ _, _ = sb.WriteString("Plan: " + w.URL + "/" + w.OutputMap.Plan + "\n")
+ }
+ if w.OutputMap.Rollout != "" {
+ _, _ = sb.WriteString("Rollout: " + w.URL + "/" + w.OutputMap.Rollout + "\n")
+ }
+
+ // Add stage status table
+ buildOutputStagesMarkdown(w, &sb)
+
+ return sb.String()
+}
+
+func buildOutputStagesMarkdown(w *world.World, sb *strings.Builder) {
+ if w.IsRollout && w.Rollout != nil {
+ pendingStages := w.PendingStages
+ rollout := w.Rollout
+ // build a table of stage and status in GitHub flavored markdown.
+ // The stages come from the aggregated result of pendingStages and rollout.Stages.
+ // pendingStages are the stages to be rolled out at the beginning of the execution,
+ // they may or may not have been executed (depending on the targetStage input).
+ // rollout.Stages are the stages that have been executed, and the stage status is
+ // an aggregated status from stage tasks.
+
+ // Combine stages from rollout.Stages and pendingStages, deduplicating
+ allStages := []string{}
+ seenStages := make(map[string]bool)
+
+ // First add all stages from rollout.Stages
+ for _, stage := range rollout.Stages {
+ if !seenStages[stage.Environment] {
+ allStages = append(allStages, stage.Environment)
+ seenStages[stage.Environment] = true
+ }
+ }
+
+ // Then append pendingStages that aren't already in the list
+ for _, stage := range pendingStages {
+ if !seenStages[stage] {
+ allStages = append(allStages, stage)
+ seenStages[stage] = true
+ }
+ }
+
+ // Check if there are any stages to display
+ if len(allStages) == 0 {
+ sb.WriteString("\n### Rollout Stages\n\n")
+ sb.WriteString("_No stages to display. The rollout may not have any stages._\n\n")
+ return
+ }
+
+ // Create a map of stage environment to Stage object for quick lookup
+ stageMap := make(map[string]*v1pb.Stage)
+ for _, stage := range rollout.Stages {
+ stageMap[stage.Environment] = stage
+ }
+
+ // Build the markdown table
+ sb.WriteString("\n### Rollout Stages\n\n")
+ sb.WriteString("| Stage | Status |\n")
+ sb.WriteString("|-------|--------|\n")
+
+ // Output all stages with their status
+ for _, stageEnv := range allStages {
+ stageName := extractStageName(stageEnv)
+
+ // Determine status
+ var status string
+ if stage, exists := stageMap[stageEnv]; exists {
+ // Stage exists in rollout, get its aggregated status
+ status = getAggregatedStageStatus(stage)
+ } else {
+ // Stage is only in pendingStages, mark as pending
+ status = "⏳ Pending"
+ }
+
+ sb.WriteString("| ")
+ sb.WriteString(stageName)
+ sb.WriteString(" | ")
+ sb.WriteString(status)
+ sb.WriteString(" |\n")
+ }
+
+ sb.WriteString("\n")
+ }
+}
+
+// getAggregatedStageStatus determines the overall status of a stage based on its tasks
+func getAggregatedStageStatus(stage *v1pb.Stage) string {
+ if stage == nil || len(stage.Tasks) == 0 {
+ return "⏳ Pending"
+ }
+
+ hasRunning := false
+ hasFailed := false
+ hasCanceled := false
+ hasSkipped := false
+ hasPending := false
+ hasNotStarted := false
+ allDone := true
+
+ for _, task := range stage.Tasks {
+ switch task.Status {
+ case v1pb.Task_RUNNING:
+ hasRunning = true
+ allDone = false
+ case v1pb.Task_FAILED:
+ hasFailed = true
+ allDone = false
+ case v1pb.Task_CANCELED:
+ hasCanceled = true
+ allDone = false
+ case v1pb.Task_SKIPPED:
+ hasSkipped = true
+ case v1pb.Task_PENDING:
+ hasPending = true
+ allDone = false
+ case v1pb.Task_NOT_STARTED, v1pb.Task_STATUS_UNSPECIFIED:
+ hasNotStarted = true
+ allDone = false
+ case v1pb.Task_DONE:
+ // Task is done
+ default:
+ allDone = false
+ }
+ }
+
+ // Determine overall stage status based on task statuses
+ if hasFailed {
+ return "❌ Failed"
+ }
+ if hasCanceled {
+ return "⛔ Canceled"
+ }
+ if hasRunning {
+ return "🔄 Running"
+ }
+ if hasPending {
+ return "⏳ Pending"
+ }
+ if hasNotStarted {
+ return "⏸️ Not Started"
+ }
+ if allDone && !hasSkipped {
+ return "✅ Done"
+ }
+ if allDone && hasSkipped {
+ return "⏭️ Skipped"
+ }
+
+ return "⏳ Pending"
+}
+
+// extractStageName extracts the stage name from the environment path
+func extractStageName(environment string) string {
+ // Format: environments/{environment}
+ parts := strings.Split(environment, "/")
+ if len(parts) >= 2 {
+ return parts[len(parts)-1]
+ }
+ return environment
+}
diff --git a/action/command/gitlab.go b/action/gitlab/gitlab.go
similarity index 96%
rename from action/command/gitlab.go
rename to action/gitlab/gitlab.go
index 2ad0b4c580..36601e59a4 100644
--- a/action/command/gitlab.go
+++ b/action/gitlab/gitlab.go
@@ -1,4 +1,4 @@
-package command
+package gitlab
import (
"encoding/json"
@@ -31,7 +31,7 @@ type Finding struct {
Location Location `json:"location"`
}
-func writeReleaseCheckToCodeQualityJSON(resp *v1pb.CheckReleaseResponse) error {
+func WriteReleaseCheckToCodeQualityJSON(resp *v1pb.CheckReleaseResponse) error {
var data []Finding
var warningCount, errorCount int
for _, result := range resp.Results {