Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 57 additions & 33 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ type AlertsResult struct {

// Service represents a Rootly service
type Service struct {
ID string
Name string
Slug string
Description string
Color string
CreatedAt time.Time
UpdatedAt time.Time
OwnerTeamName string // Populated from included owner_group relationship
DetailLoaded bool
ID string
Name string
Slug string
Description string
Color string
EscalationPolicyID string
CreatedAt time.Time
UpdatedAt time.Time
OwnerTeamName string // Populated from included owner_group relationship
DetailLoaded bool
// Raw API response body for JSON/YAML passthrough
RawBody []byte
}
Expand Down Expand Up @@ -1958,12 +1959,13 @@ func (c *Client) ListServicesCLI(ctx context.Context, page, pageSize int, sort s
Data []struct {
ID string `json:"id"`
Attributes struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
EscalationPolicyID *string `json:"escalation_policy_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"attributes"`
} `json:"data"`
Meta struct {
Expand Down Expand Up @@ -1993,6 +1995,9 @@ func (c *Client) ListServicesCLI(ctx context.Context, page, pageSize int, sort s
if item.Attributes.Color != nil {
service.Color = *item.Attributes.Color
}
if item.Attributes.EscalationPolicyID != nil {
service.EscalationPolicyID = *item.Attributes.EscalationPolicyID
}
services = append(services, service)
}

Expand Down Expand Up @@ -2054,12 +2059,13 @@ func (c *Client) GetServiceByID(ctx context.Context, id string) (*Service, error
Data struct {
ID string `json:"id"`
Attributes struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
EscalationPolicyID *string `json:"escalation_policy_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"attributes"`
Relationships struct {
OwnerGroup struct {
Expand Down Expand Up @@ -2096,6 +2102,9 @@ func (c *Client) GetServiceByID(ctx context.Context, id string) (*Service, error
if response.Data.Attributes.Color != nil {
service.Color = *response.Data.Attributes.Color
}
if response.Data.Attributes.EscalationPolicyID != nil {
service.EscalationPolicyID = *response.Data.Attributes.EscalationPolicyID
}

// Parse owner_group from included relationships
if response.Data.Relationships.OwnerGroup.Data != nil {
Expand Down Expand Up @@ -2176,12 +2185,13 @@ func (c *Client) CreateService(ctx context.Context, name string, opts map[string
Data struct {
ID string `json:"id"`
Attributes struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
EscalationPolicyID *string `json:"escalation_policy_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"attributes"`
} `json:"data"`
}
Expand All @@ -2203,6 +2213,9 @@ func (c *Client) CreateService(ctx context.Context, name string, opts map[string
if response.Data.Attributes.Color != nil {
service.Color = *response.Data.Attributes.Color
}
if response.Data.Attributes.EscalationPolicyID != nil {
service.EscalationPolicyID = *response.Data.Attributes.EscalationPolicyID
}

service.RawBody = body
return service, nil
Expand All @@ -2221,6 +2234,13 @@ func (c *Client) UpdateService(ctx context.Context, id string, opts map[string]s
if color, ok := opts["color"]; ok {
attributes["color"] = color
}
if epID, ok := opts["escalation_policy_id"]; ok {
if epID == "" {
attributes["escalation_policy_id"] = nil
} else {
attributes["escalation_policy_id"] = epID
}
}

requestBody := map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -2276,12 +2296,13 @@ func (c *Client) UpdateService(ctx context.Context, id string, opts map[string]s
Data struct {
ID string `json:"id"`
Attributes struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
Color *string `json:"color"`
EscalationPolicyID *string `json:"escalation_policy_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"attributes"`
} `json:"data"`
}
Expand All @@ -2303,6 +2324,9 @@ func (c *Client) UpdateService(ctx context.Context, id string, opts map[string]s
if response.Data.Attributes.Color != nil {
service.Color = *response.Data.Attributes.Color
}
if response.Data.Attributes.EscalationPolicyID != nil {
service.EscalationPolicyID = *response.Data.Attributes.EscalationPolicyID
}

service.RawBody = body
return service, nil
Expand Down
56 changes: 56 additions & 0 deletions internal/api/client_mutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,62 @@ func TestUpdateServiceServerError(t *testing.T) {
}
}

func TestUpdateServiceEscalationPolicyID(t *testing.T) {
cases := []struct {
name string
optVal string
respEPID interface{}
wantEPID string
wantReqVal interface{}
}{
{"attach", "ep-123", "ep-123", "ep-123", "ep-123"},
{"detach", "", nil, "", nil},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var receivedBody map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(body, &receivedBody)

w.Header().Set("Content-Type", "application/vnd.api+json")
w.WriteHeader(http.StatusOK)
resp := map[string]interface{}{
"data": map[string]interface{}{
"id": "svc-1",
"attributes": map[string]interface{}{
"name": "My Service",
"slug": "my-service",
"escalation_policy_id": tc.respEPID,
"created_at": "2025-06-15T10:00:00Z",
"updated_at": "2025-06-15T14:00:00Z",
},
},
}
_ = json.NewEncoder(w).Encode(resp)
}))
defer server.Close()

client := newTestClient(t, server.URL)
svc, err := client.UpdateService(context.Background(), "svc-1", map[string]string{
"escalation_policy_id": tc.optVal,
})
if err != nil {
t.Fatalf("UpdateService returned error: %v", err)
}
if svc.EscalationPolicyID != tc.wantEPID {
t.Errorf("EscalationPolicyID = %q, want %q", svc.EscalationPolicyID, tc.wantEPID)
}

data := receivedBody["data"].(map[string]interface{})
attrs := data["attributes"].(map[string]interface{})
if attrs["escalation_policy_id"] != tc.wantReqVal {
t.Errorf("request escalation_policy_id = %v, want %v", attrs["escalation_policy_id"], tc.wantReqVal)
}
})
}
}

// --- CreateTeam ---

func TestCreateTeam(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions internal/cmd/services/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,56 @@ func TestRunUpdateNoFlags(t *testing.T) {
}
}

func TestRunUpdateEscalationPolicyID(t *testing.T) {
cases := []struct {
name string
flagVal string
respEPID string
}{
{"attach", "ep-123", `"ep-123"`},
{"detach", "", "null"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
setupTestServer(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.api+json")
w.Write([]byte(`{
"data": {
"id": "svc-1",
"attributes": {
"name": "API Gateway",
"slug": "api-gateway",
"escalation_policy_id": ` + tc.respEPID + `,
"created_at": "2025-06-15T10:00:00Z",
"updated_at": "2025-06-15T12:00:00Z"
}
}
}`))
})

viper.Set("format", "table")

cmd := newTestCmd()
cmd.Flags().String("name", "", "")
cmd.Flags().String("description", "", "")
cmd.Flags().String("color", "", "")
cmd.Flags().String("escalation-policy-id", "", "")
cmd.Flags().Set("escalation-policy-id", tc.flagVal)

output := captureStdout(t, func() {
err := runUpdate(cmd, []string{"api-gateway"})
if err != nil {
t.Fatalf("runUpdate error: %v", err)
}
})

if output == "" {
t.Error("expected non-empty output")
}
})
}
}

// --- confirmDelete tests ---

func TestConfirmDeleteSkip(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/services/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func serviceDetailRows(svc *api.Service) [][]string {
if svc.Color != "" {
rows = append(rows, []string{"Color", svc.Color})
}
if svc.EscalationPolicyID != "" {
rows = append(rows, []string{"Escalation Policy", svc.EscalationPolicyID})
}
if svc.OwnerTeamName != "" {
rows = append(rows, []string{"Owner Team", svc.OwnerTeamName})
}
Expand Down
20 changes: 12 additions & 8 deletions internal/cmd/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ func TestTruncateString(t *testing.T) {
func TestServiceDetailRows(t *testing.T) {
now := time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC)
svc := &api.Service{
ID: "svc-123",
Name: "api-gateway",
Slug: "api-gateway",
Description: "Main API gateway",
Color: "#FF5733",
OwnerTeamName: "Platform",
CreatedAt: now,
UpdatedAt: now,
ID: "svc-123",
Name: "api-gateway",
Slug: "api-gateway",
Description: "Main API gateway",
Color: "#FF5733",
EscalationPolicyID: "ep-456",
OwnerTeamName: "Platform",
CreatedAt: now,
UpdatedAt: now,
}

rows := serviceDetailRows(svc)
Expand All @@ -59,6 +60,9 @@ func TestServiceDetailRows(t *testing.T) {
if findRow(rows, "Description") != "Main API gateway" {
t.Errorf("Description = %q, want %q", findRow(rows, "Description"), "Main API gateway")
}
if findRow(rows, "Escalation Policy") != "ep-456" {
t.Errorf("Escalation Policy = %q, want %q", findRow(rows, "Escalation Policy"), "ep-456")
}
if findRow(rows, "Owner Team") != "Platform" {
t.Errorf("Owner Team = %q, want %q", findRow(rows, "Owner Team"), "Platform")
}
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/services/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ var updateCmd = &cobra.Command{
--description="Updated: Main API gateway" \
--color="#00FF00"

# Update multiple fields
rootly services update api-gateway --name="New Name" --color="#FF0000"`,
# Attach escalation policy
rootly services update api-gateway --escalation-policy-id="ep-123"

# Detach escalation policy
rootly services update api-gateway --escalation-policy-id=""`,
Args: cobra.ExactArgs(1),
RunE: runUpdate,
}
Expand All @@ -33,6 +36,7 @@ func init() {
updateCmd.Flags().String("name", "", "Updated service name")
updateCmd.Flags().String("description", "", "Updated description")
updateCmd.Flags().String("color", "", "Updated color (hex format, e.g., #FF5733)")
updateCmd.Flags().String("escalation-policy-id", "", `Escalation policy ID (use "" to detach)`)

// Register with parent command
ServicesCmd.AddCommand(updateCmd)
Expand Down Expand Up @@ -65,6 +69,10 @@ func runUpdate(cmd *cobra.Command, args []string) error {
}
opts["color"] = color
}
if cmd.Flags().Changed("escalation-policy-id") {
epID, _ := cmd.Flags().GetString("escalation-policy-id")
opts["escalation_policy_id"] = epID
}

// If opts is empty (no flags changed), return error
if len(opts) == 0 {
Expand Down
Loading