Skip to content

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

fix: webhook issues (#20964)

ecmadaod0eb2229be70
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index c5f8ce1ea8..ea5c1d98cf 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -891,9 +891,7 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMess
if err != nil {
return nil, errors.Wrap(err, "failed to get environments")
}
- for i, env := range environmentSetting.GetEnvironments() {
- environmentOrderMap[env.Id] = i
- }
+ environmentOrderMap = common.EnvironmentOrderMap(environmentSetting.GetEnvironments())
taskStatusCounts, err := s.ListTaskStatusCountByPlanIDs(ctx, projectIDs, rolloutPlanUIDs)
if err != nil {
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 034a167108..189587d7d6 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -113,10 +113,7 @@ func (s *RolloutService) GetRollout(ctx context.Context, req *connect.Request[v1
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get environments"))
}
- environmentOrderMap := make(map[string]int)
- for i, env := range environments.GetEnvironments() {
- environmentOrderMap[env.Id] = i
- }
+ environmentOrderMap := common.EnvironmentOrderMap(environments.GetEnvironments())
rolloutV1, err := convertToRollout(project, plan, tasks, environmentOrderMap)
if err != nil {
@@ -203,10 +200,7 @@ func (s *RolloutService) ListRollouts(ctx context.Context, req *connect.Request[
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get environments"))
}
- environmentOrderMap := make(map[string]int)
- for i, env := range environments.GetEnvironments() {
- environmentOrderMap[env.Id] = i
- }
+ environmentOrderMap := common.EnvironmentOrderMap(environments.GetEnvironments())
// Convert plans and their tasks to rollouts
rollouts := []*v1pb.Rollout{}
@@ -335,10 +329,7 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get environments"))
}
- environmentOrderMap := make(map[string]int)
- for i, env := range environments.GetEnvironments() {
- environmentOrderMap[env.Id] = i
- }
+ environmentOrderMap := common.EnvironmentOrderMap(environments.GetEnvironments())
rolloutV1, err := convertToRollout(project, plan, tasks, environmentOrderMap)
if err != nil {
diff --git a/backend/common/environment.go b/backend/common/environment.go
new file mode 100644
index 0000000000..896222863b
--- /dev/null
+++ b/backend/common/environment.go
@@ -0,0 +1,12 @@
+package common
+
+import storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+
+// EnvironmentOrderMap returns the workspace display order keyed by environment ID.
+func EnvironmentOrderMap(environments []*storepb.EnvironmentSetting_Environment) map[string]int {
+ orderMap := make(map[string]int)
+ for i, environment := range environments {
+ orderMap[environment.Id] = i
+ }
+ return orderMap
+}
diff --git a/backend/plugin/webhook/discord/discord.go b/backend/plugin/webhook/discord/discord.go
index e651220b90..ca390a151a 100644
--- a/backend/plugin/webhook/discord/discord.go
+++ b/backend/plugin/webhook/discord/discord.go
@@ -119,6 +119,9 @@ func (*Receiver) Post(context webhook.Context) error {
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
}
+ if resp.StatusCode == http.StatusNoContent || len(b) == 0 {
+ return nil
+ }
webhookResponse := &WebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
diff --git a/backend/plugin/webhook/discord/discord_test.go b/backend/plugin/webhook/discord/discord_test.go
new file mode 100644
index 0000000000..9ed7a467a6
--- /dev/null
+++ b/backend/plugin/webhook/discord/discord_test.go
@@ -0,0 +1,40 @@
+package discord
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/webhook"
+)
+
+func TestPostNoContentResponse(t *testing.T) {
+ a := require.New(t)
+
+ requestCount := 0
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requestCount++
+ a.Equal(http.MethodPost, r.Method)
+ a.Equal("application/json", r.Header.Get("Content-Type"))
+
+ var payload Webhook
+ a.NoError(json.NewDecoder(r.Body).Decode(&payload))
+ a.Len(payload.EmbedList, 1)
+ a.Equal(":white_check_mark: Issue approved", payload.EmbedList[0].Title)
+
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer server.Close()
+
+ err := (&Receiver{}).Post(webhook.Context{
+ URL: server.URL,
+ Level: webhook.WebhookSuccess,
+ Title: "Issue approved",
+ })
+
+ a.NoError(err)
+ a.Equal(1, requestCount)
+}
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 3fba303876..dbb39198f5 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -225,6 +225,17 @@ func (s *Scheduler) checkPlanCompletion(ctx context.Context, ref bus.PlanRef) {
}
}
+ project, err := s.store.GetProjectByResourceID(ctx, plan.ProjectID)
+ if err != nil || project == nil {
+ slog.Error("failed to get project for completion webhook", log.BBError(err))
+ return
+ }
+ environmentSetting, err := s.store.GetEnvironment(ctx, project.Workspace)
+ if err != nil {
+ slog.Error("failed to get environments for completion webhook", log.BBError(err))
+ return
+ }
+
// All tasks complete and successful - try to claim completion notification
claimed, err := s.store.ClaimPipelineCompletionNotification(ctx, ref.ProjectID, planID)
if err != nil {
@@ -235,25 +246,13 @@ func (s *Scheduler) checkPlanCompletion(ctx context.Context, ref bus.PlanRef) {
return // Already sent
}
- project, err := s.store.GetProjectByResourceID(ctx, plan.ProjectID)
- if err != nil || project == nil {
- slog.Error("failed to get project for completion webhook", log.BBError(err))
- return
- }
-
- // Use environment from the first task (all tasks should be in the same environment for a rollout)
- environment := ""
- if len(tasks) > 0 {
- environment = tasks[0].Environment
- }
-
// Send PIPELINE_COMPLETED webhook
s.webhookManager.CreateEvent(ctx, &webhook.Event{
Type: storepb.Activity_PIPELINE_COMPLETED,
Project: webhook.NewProject(project),
RolloutCompleted: &webhook.EventRolloutCompleted{
Rollout: webhook.NewRollout(plan),
- Environment: environment,
+ Environment: completionWebhookEnvironment(tasks, common.EnvironmentOrderMap(environmentSetting.GetEnvironments())),
},
})
@@ -300,3 +299,19 @@ func (s *Scheduler) autoResolveIssue(ctx context.Context, projectID string, plan
}
slog.Info("auto-resolved deferred rollout issue", slog.String("project", projectID), slog.Int64("issueUID", issue.UID), slog.Int64("planID", planID))
}
+
+func completionWebhookEnvironment(tasks []*store.TaskMessage, environmentOrderMap map[string]int) string {
+ lastEnvironment := ""
+ lastOrder := -1
+ for _, task := range tasks {
+ order, ok := environmentOrderMap[task.Environment]
+ if !ok {
+ continue
+ }
+ if lastEnvironment == "" || order > lastOrder {
+ lastEnvironment = task.Environment
+ lastOrder = order
+ }
+ }
+ return lastEnvironment
+}
diff --git a/backend/runner/taskrun/scheduler_test.go b/backend/runner/taskrun/scheduler_test.go
new file mode 100644
index 0000000000..2aedddde94
--- /dev/null
+++ b/backend/runner/taskrun/scheduler_test.go
@@ -0,0 +1,26 @@
+package taskrun
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/store"
+)
+
+func TestCompletionWebhookEnvironmentUsesLastEnvironmentOrder(t *testing.T) {
+ tasks := []*store.TaskMessage{
+ {ID: 1, Environment: "dev"},
+ {ID: 2, Environment: "prod"},
+ {ID: 3, Environment: "test"},
+ }
+ environmentOrderMap := common.EnvironmentOrderMap([]*storepb.EnvironmentSetting_Environment{
+ {Id: "dev"},
+ {Id: "test"},
+ {Id: "prod"},
+ })
+
+ require.Equal(t, "prod", completionWebhookEnvironment(tasks, environmentOrderMap))
+}