Skip to content

Modern Docker Management, Designed for Everyone

fix: resolve webhook target names on remote environments (#4299)

Kyle Mendella222a007c3db
diff --git a/backend/internal/project/handler.go b/backend/internal/project/handler.go
index d20ddf972..8b47f38f2 100644
--- a/backend/internal/project/handler.go
+++ b/backend/internal/project/handler.go
@@ -64,6 +64,11 @@ type ListProjectTagsInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
}
+// ListProjectReferencesInput identifies the environment whose project references are requested.
+type ListProjectReferencesInput struct {
+ EnvironmentID string `path:"id" doc:"Environment ID"`
+}
+
// UpdateProjectTagInput identifies a project and the UI tag mutation to apply.
type UpdateProjectTagInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
@@ -217,6 +222,15 @@ func (h *ProjectHandler) ListProjectTags(ctx context.Context, _ *ListProjectTags
return &handlerutil.Out[[]project.TagOption]{Body: base.ApiResponse[[]project.TagOption]{Success: true, Data: options}}, nil
}
+// ListProjectReferences returns the ID and name of every project in an environment.
+func (h *ProjectHandler) ListProjectReferences(ctx context.Context, _ *ListProjectReferencesInput) (*handlerutil.Out[[]project.Reference], error) {
+ references, err := h.projectService.ListProjectReferences(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list project references: " + err.Error())
+ }
+ return &handlerutil.Out[[]project.Reference]{Body: base.ApiResponse[[]project.Reference]{Success: true, Data: references}}, nil
+}
+
// UpdateProjectTag applies one UI-managed project tag association change.
func (h *ProjectHandler) UpdateProjectTag(ctx context.Context, input *UpdateProjectTagInput) (*handlerutil.Out[project.UpdateTagResponse], error) {
user, err := handlerutil.RequireUser(ctx)
diff --git a/backend/internal/project/module.go b/backend/internal/project/module.go
index 3c7347e48..4a1aa58d8 100644
--- a/backend/internal/project/module.go
+++ b/backend/internal/project/module.go
@@ -76,6 +76,16 @@ func RegisterProjects(api huma.API, projectService *ProjectService, activityServ
Security: handlerutil.DefaultOperationSecurity(),
}, authz.PermProjectsList, h.ListProjectTags)
+ middleware.RegisterWithPermission(api, huma.Operation{
+ OperationID: "list-project-references",
+ Method: http.MethodGet,
+ Path: "/environments/{id}/projects/references",
+ Summary: "List project references",
+ Description: "Get the ID and name of every project, including archived projects, without runtime state",
+ Tags: []string{"Projects"},
+ Security: handlerutil.DefaultOperationSecurity(),
+ }, authz.PermProjectsList, h.ListProjectReferences)
+
middleware.RegisterWithPermission(api, huma.Operation{
OperationID: "update-project-tag",
Method: http.MethodPatch,
diff --git a/backend/internal/project/service.go b/backend/internal/project/service.go
index e4f441097..fdfafb22f 100644
--- a/backend/internal/project/service.go
+++ b/backend/internal/project/service.go
@@ -3996,6 +3996,15 @@ func (s *ProjectService) ListProjectTagOptions(ctx context.Context) ([]projectty
return tags.Options(tagAssignmentsInternal(rows)), nil
}
+// ListProjectReferences returns the ID and name of every project, including archived ones, without runtime state.
+func (s *ProjectService) ListProjectReferences(ctx context.Context) ([]projecttypes.Reference, error) {
+ references := []projecttypes.Reference{}
+ if err := s.db.WithContext(ctx).Model(&Project{}).Select("id", "name").Order("name").Find(&references).Error; err != nil {
+ return nil, fmt.Errorf("list project references: %w", err)
+ }
+ return references, nil
+}
+
// UpdateProjectTag attaches or detaches a UI-managed tag and rejects Compose-owned names.
func (s *ProjectService) UpdateProjectTag(ctx context.Context, projectID, name string, color projecttypes.TagColor, attached bool, user usertypes.Actor) ([]projecttypes.Tag, error) {
normalized, normalizedColor, err := tags.NormalizeUpdate(name, color, attached)
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.go
index 5a113acfe..79ddc02f3 100644
--- a/backend/internal/project/service_test.go
+++ b/backend/internal/project/service_test.go
@@ -8703,3 +8703,17 @@ func discoveryListingForTestInternal(imageService *image.ImageService) *listing.
service.initChildrenInternal(nil, nil)
return service.listing
}
+
+func TestListProjectReferencesIncludesArchivedProjects(t *testing.T) {
+ db := setupProjectTestDB(t)
+ service := &ProjectService{db: db}
+ require.NoError(t, db.Create(&Project{ID: "project-active", Name: "active", Path: "/projects/active", Status: ProjectStatusStopped}).Error)
+ require.NoError(t, db.Create(&Project{ID: "project-archived", Name: "archived", Path: "/projects/archived", Status: ProjectStatusStopped, IsArchived: true}).Error)
+
+ references, err := service.ListProjectReferences(t.Context())
+ require.NoError(t, err)
+ require.Equal(t, []projecttypes.Reference{
+ {ID: "project-active", Name: "active"},
+ {ID: "project-archived", Name: "archived"},
+ }, references)
+}
diff --git a/backend/internal/webhook/service.go b/backend/internal/webhook/service.go
index 0925b36b9..133bf950e 100644
--- a/backend/internal/webhook/service.go
+++ b/backend/internal/webhook/service.go
@@ -11,6 +11,7 @@ import (
"log/slog"
"net/http"
"net/url"
+ "slices"
"strings"
"sync"
"time"
@@ -50,8 +51,19 @@ var (
ErrWebhookInvalidType = errors.New("invalid webhook target type")
ErrWebhookInvalidAction = errors.New("invalid webhook action type")
ErrWebhookMissingTarget = errors.New("target ID is required for container, project, and gitops webhook types")
+
+ // remoteWebhookTargetListPaths lists agent endpoints for target types whose records live in the agent's database.
+ remoteWebhookTargetListPaths = map[string]string{
+ WebhookTargetTypeProject: "/api/environments/" + types.LocalDockerEnvironmentID + "/projects/references",
+ WebhookTargetTypeGitOps: "/api/environments/" + types.LocalDockerEnvironmentID + "/gitops-syncs?limit=-1",
+ }
)
+type remoteWebhookTarget struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+}
+
type WebhookService struct {
tokenWriteMu sync.Mutex
tokenMu sync.RWMutex
@@ -315,9 +327,14 @@ func (s *WebhookService) ListWebhookSummaries(ctx context.Context, environmentID
return nil, err
}
+ remoteNames := s.remoteWebhookTargetNamesInternal(ctx, environmentID, webhooks)
summaries := make([]webhook.Summary, len(webhooks))
for i := range webhooks {
wh := webhooks[i]
+ targetName, ok := remoteNames[wh.TargetType][wh.TargetID]
+ if !ok {
+ targetName = s.resolveWebhookTargetNameInternal(ctx, &wh)
+ }
summaries[i] = webhook.Summary{
ID: wh.ID,
Name: wh.Name,
@@ -325,7 +342,7 @@ func (s *WebhookService) ListWebhookSummaries(ctx context.Context, environmentID
TargetType: wh.TargetType,
ActionType: resolvedWebhookActionTypeInternal(wh.TargetType, wh.ActionType),
TargetID: wh.TargetID,
- TargetName: s.resolveWebhookTargetNameInternal(ctx, &wh),
+ TargetName: targetName,
EnvironmentID: wh.EnvironmentID,
Enabled: wh.Enabled,
LastTriggeredAt: wh.LastTriggeredAt,
@@ -336,6 +353,30 @@ func (s *WebhookService) ListWebhookSummaries(ctx context.Context, environmentID
return summaries, nil
}
+// remoteWebhookTargetNamesInternal fetches target names keyed by target type and ID from a remote environment.
+func (s *WebhookService) remoteWebhookTargetNamesInternal(ctx context.Context, environmentID string, webhooks []Webhook) map[string]map[string]string {
+ names := map[string]map[string]string{}
+ if !isRemoteWebhookEnvironmentInternal(environmentID) {
+ return names
+ }
+
+ for targetType, path := range remoteWebhookTargetListPaths {
+ if !slices.ContainsFunc(webhooks, func(wh Webhook) bool { return wh.TargetType == targetType }) {
+ continue
+ }
+ var out base.ApiResponse[[]remoteWebhookTarget]
+ if err := s.environmentService.ProxyJSONRequest(ctx, environmentID, http.MethodGet, path, nil, &out); err != nil {
+ slog.WarnContext(ctx, "failed to resolve remote webhook target names", "environmentId", environmentID, "targetType", targetType, "error", err)
+ continue
+ }
+ names[targetType] = make(map[string]string, len(out.Data))
+ for _, target := range out.Data {
+ names[targetType][target.ID] = target.Name
+ }
+ }
+ return names
+}
+
func (s *WebhookService) resolveWebhookTargetNameInternal(ctx context.Context, wh *Webhook) string {
switch wh.TargetType {
case WebhookTargetTypeContainer:
diff --git a/backend/internal/webhook/service_test.go b/backend/internal/webhook/service_test.go
index 3f8e1f356..cbda24b4c 100644
--- a/backend/internal/webhook/service_test.go
+++ b/backend/internal/webhook/service_test.go
@@ -426,16 +426,16 @@ func TestListWebhookSummaries_ResolvesTargetNames(t *testing.T) {
require.NoError(t, db.WithContext(ctx).Exec(`CREATE TABLE projects (id TEXT PRIMARY KEY, name TEXT NOT NULL)`).Error)
require.NoError(t, db.WithContext(ctx).Exec(`CREATE TABLE gitops_syncs (id TEXT PRIMARY KEY, environment_id TEXT NOT NULL, name TEXT NOT NULL)`).Error)
require.NoError(t, db.WithContext(ctx).Exec(`INSERT INTO projects (id, name) VALUES (?, ?)`, "project-1", "Main Project").Error)
- require.NoError(t, db.WithContext(ctx).Exec(`INSERT INTO gitops_syncs (id, environment_id, name) VALUES (?, ?, ?)`, "sync-1", "env-1", "Deploy Sync").Error)
+ require.NoError(t, db.WithContext(ctx).Exec(`INSERT INTO gitops_syncs (id, environment_id, name) VALUES (?, ?, ?)`, "sync-1", types.LocalDockerEnvironmentID, "Deploy Sync").Error)
- _, _, err := svc.CreateWebhook(ctx, "project-hook", WebhookTargetTypeProject, WebhookActionTypeUpdate, "project-1", "env-1", user.Actor{})
+ _, _, err := svc.CreateWebhook(ctx, "project-hook", WebhookTargetTypeProject, WebhookActionTypeUpdate, "project-1", types.LocalDockerEnvironmentID, user.Actor{})
require.NoError(t, err)
- _, _, err = svc.CreateWebhook(ctx, "updater-hook", WebhookTargetTypeUpdater, WebhookActionTypeRun, "", "env-1", user.Actor{})
+ _, _, err = svc.CreateWebhook(ctx, "updater-hook", WebhookTargetTypeUpdater, WebhookActionTypeRun, "", types.LocalDockerEnvironmentID, user.Actor{})
require.NoError(t, err)
- _, _, err = svc.CreateWebhook(ctx, "gitops-hook", WebhookTargetTypeGitOps, WebhookActionTypeSync, "sync-1", "env-1", user.Actor{})
+ _, _, err = svc.CreateWebhook(ctx, "gitops-hook", WebhookTargetTypeGitOps, WebhookActionTypeSync, "sync-1", types.LocalDockerEnvironmentID, user.Actor{})
require.NoError(t, err)
- summaries, err := svc.ListWebhookSummaries(ctx, "env-1")
+ summaries, err := svc.ListWebhookSummaries(ctx, types.LocalDockerEnvironmentID)
require.NoError(t, err)
require.Len(t, summaries, 3)
@@ -462,12 +462,12 @@ func TestListWebhookSummaries_DefaultsLegacyActionType(t *testing.T) {
TargetType: WebhookTargetTypeProject,
ActionType: "",
TargetID: "project-1",
- EnvironmentID: "env-1",
+ EnvironmentID: types.LocalDockerEnvironmentID,
Enabled: true,
}
require.NoError(t, db.WithContext(ctx).Create(legacyWebhook).Error)
- summaries, err := svc.ListWebhookSummaries(ctx, "env-1")
+ summaries, err := svc.ListWebhookSummaries(ctx, types.LocalDockerEnvironmentID)
require.NoError(t, err)
require.Len(t, summaries, 1)
assert.Equal(t, WebhookActionTypeUpdate, summaries[0].ActionType)
diff --git a/types/project/project.go b/types/project/project.go
index 60949bc67..fec7c4ae3 100644
--- a/types/project/project.go
+++ b/types/project/project.go
@@ -98,6 +98,12 @@ type TagOption struct {
Color TagColor `json:"color"`
}
+// Reference identifies a project by ID and name.
+type Reference struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+}
+
// UpdateTag mutates a UI-managed tag association.
type UpdateTag struct {
Name string `json:"name"`