diff --git a/pkg/cmd/connection_upsert.go b/pkg/cmd/connection_upsert.go index a4d10374..34656c28 100644 --- a/pkg/cmd/connection_upsert.go +++ b/pkg/cmd/connection_upsert.go @@ -501,16 +501,14 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection, } req.Source = sourceInput } else if isUpdate && existing != nil && existing.Source != nil { - // Check if any source config fields are being updated - hasSourceConfigUpdate := cu.SourceWebhookSecret != "" || cu.SourceAPIKey != "" || + hasSourceUpdate := cu.sourceDescription != "" || cu.SourceWebhookSecret != "" || cu.SourceAPIKey != "" || cu.SourceBasicAuthUser != "" || cu.SourceBasicAuthPass != "" || cu.SourceHMACSecret != "" || cu.SourceHMACAlgo != "" || cu.SourceAllowedHTTPMethods != "" || cu.SourceCustomResponseType != "" || cu.SourceCustomResponseBody != "" || cu.SourceConfig != "" || cu.SourceConfigFile != "" - if hasSourceConfigUpdate { - // For partial config updates, we need to send the full source object - // with the updated config merged in + if hasSourceUpdate { + // Merge the flags into the existing source and send it inline. sourceInput, err := cu.buildSourceInputForUpdate(existing.Source) if err != nil { return nil, err @@ -551,8 +549,7 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection, } req.Destination = destinationInput } else if isUpdate && existing != nil && existing.Destination != nil { - // Check if any destination config fields are being updated - hasDestinationConfigUpdate := cu.destinationURL != "" || cu.destinationCliPath != "" || + hasDestinationUpdate := cu.destinationDescription != "" || cu.destinationURL != "" || cu.destinationCliPath != "" || cu.destinationPathForwardingDisabled != nil || cu.destinationHTTPMethod != "" || cu.DestinationRateLimit != 0 || cu.DestinationRateLimitPeriod != "" || @@ -560,9 +557,8 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection, cu.DestinationDeliveryGroupRatePeriod != "" || cu.DestinationDeliveryGroupOverrides != "" || cu.DestinationAuthMethod != "" - if hasDestinationConfigUpdate { - // For partial config updates, we need to send the full destination object - // with the updated config merged in + if hasDestinationUpdate { + // Merge the flags into the existing destination and send it inline. destinationInput, err := cu.buildDestinationInputForUpdate(existing.Destination) if err != nil { return nil, err @@ -602,7 +598,7 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection, return req, nil } -// buildSourceInputForUpdate builds a source input for partial config updates +// buildSourceInputForUpdate builds a source input for partial updates // It merges the existing source config with any new flags provided func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdeck.Source) (*hookdeck.SourceCreateInput, error) { // Start with the existing source @@ -611,6 +607,9 @@ func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdec Type: existingSource.Type, Description: existingSource.Description, } + if cu.sourceDescription != "" { + input.Description = &cu.sourceDescription + } // Get existing config or create new one sourceConfig := make(map[string]interface{}) @@ -649,7 +648,7 @@ func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdec return input, nil } -// buildDestinationInputForUpdate builds a destination input for partial config updates +// buildDestinationInputForUpdate builds a destination input for partial updates // It merges the existing destination config with any new flags provided func (cu *connectionUpsertCmd) buildDestinationInputForUpdate(existingDest *hookdeck.Destination) (*hookdeck.DestinationCreateInput, error) { // Start with the existing destination @@ -658,6 +657,9 @@ func (cu *connectionUpsertCmd) buildDestinationInputForUpdate(existingDest *hook Type: existingDest.Type, Description: existingDest.Description, } + if cu.destinationDescription != "" { + input.Description = &cu.destinationDescription + } // Get existing config or create new one destConfig := make(map[string]interface{}) diff --git a/pkg/cmd/connection_upsert_command_test.go b/pkg/cmd/connection_upsert_command_test.go index 73545171..0a6a968c 100644 --- a/pkg/cmd/connection_upsert_command_test.go +++ b/pkg/cmd/connection_upsert_command_test.go @@ -126,3 +126,37 @@ func TestConnectionUpsertInlineDestinationKeepsStoredSource(t *testing.T) { require.True(t, ok, "the destination flags must still be sent inline") assert.Equal(t, "my-api", destination["name"]) } + +func TestConnectionUpsertEndpointDescriptions(t *testing.T) { + stored := storedUpsertConnection() + stored.Source.Description = strPtr("old source description") + stored.Destination.Description = strPtr("old destination description") + + t.Run("updates descriptions", func(t *testing.T) { + body := captureConnectionUpsertBody(t, stored, + "gateway", "connection", "upsert", "my-conn", + "--source-description", "new source description", + "--destination-description", "new destination description", + ) + + source, sourceOK := body["source"].(map[string]interface{}) + destination, destinationOK := body["destination"].(map[string]interface{}) + assert.True(t, sourceOK, "the source must be sent inline") + assert.True(t, destinationOK, "the destination must be sent inline") + assert.Equal(t, "new source description", source["description"]) + assert.Equal(t, "new destination description", destination["description"]) + assert.NotContains(t, body, "source_id") + assert.NotContains(t, body, "destination_id") + }) + + t.Run("preserves IDs when omitted", func(t *testing.T) { + body := captureConnectionUpsertBody(t, stored, + "gateway", "connection", "upsert", "my-conn", + ) + + assert.Equal(t, "src_1", body["source_id"]) + assert.Equal(t, "des_1", body["destination_id"]) + assert.NotContains(t, body, "source") + assert.NotContains(t, body, "destination") + }) +}