Skip to content
Open
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
53 changes: 40 additions & 13 deletions pkg/views/webhook/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,61 @@ var columns = []table.Column{
{Title: "Creation Time", Width: 20},
}

// placeholder is shown whenever a webhook policy does not carry the target
// details Harbor optionally omits from the response.
const placeholder = "--"

func ListWebhooks(webhooks []*models.WebhookPolicy) {
rows := webhookRows(webhooks)
m := tablelist.NewModel(columns, rows, len(rows))

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}

// webhookRows renders the table rows for the given policies. Targets are
// optional in the Harbor API model, so a policy without any target still
// yields a row with placeholders instead of panicking.
func webhookRows(webhooks []*models.WebhookPolicy) []table.Row {
var rows []table.Row
for _, webhook := range webhooks {
var webhookEnabled string
if webhook == nil {
continue
}

webhookEnabled := "False"
if webhook.Enabled {
webhookEnabled = "True"
} else {
webhookEnabled = "False"
}
payloadFormat := "--"
if len(webhook.Targets[0].PayloadFormat) != 0 {
payloadFormat = string(webhook.Targets[0].PayloadFormat)

endpointURL, notifyType := placeholder, placeholder
payloadFormat := placeholder
if len(webhook.Targets) > 0 && webhook.Targets[0] != nil {
target := webhook.Targets[0]
if target.Address != "" {
endpointURL = target.Address
}
if target.Type != "" {
notifyType = target.Type
}
if target.PayloadFormat != "" {
payloadFormat = string(target.PayloadFormat)
}
}

creationTime, _ := utils.FormatCreatedTime(webhook.CreationTime.String())
rows = append(rows, table.Row{
strconv.FormatInt(webhook.ID, 10),
webhook.Name,
webhookEnabled,
webhook.Targets[0].Address,
webhook.Targets[0].Type,
endpointURL,
notifyType,
payloadFormat,
creationTime,
})
}
m := tablelist.NewModel(columns, rows, len(rows))

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
return rows
}
103 changes: 103 additions & 0 deletions pkg/views/webhook/list/view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package list

import (
"testing"

"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWebhookRows(t *testing.T) {
t.Run("policy with a target", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{
{
ID: 1,
Name: "hook",
Enabled: true,
Targets: []*models.WebhookTargetObject{
{
Address: "https://example.com/hook",
Type: "http",
PayloadFormat: models.PayloadFormatType("CloudEvents"),
},
},
},
})

require.Len(t, rows, 1)
assert.Equal(t, "1", rows[0][0])
assert.Equal(t, "hook", rows[0][1])
assert.Equal(t, "True", rows[0][2])
assert.Equal(t, "https://example.com/hook", rows[0][3])
assert.Equal(t, "http", rows[0][4])
assert.Equal(t, "CloudEvents", rows[0][5])
})

t.Run("policy without targets falls back to placeholders", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{
{ID: 2, Name: "no-targets"},
})

require.Len(t, rows, 1)
assert.Equal(t, "2", rows[0][0])
assert.Equal(t, "no-targets", rows[0][1])
assert.Equal(t, "False", rows[0][2])
assert.Equal(t, placeholder, rows[0][3])
assert.Equal(t, placeholder, rows[0][4])
assert.Equal(t, placeholder, rows[0][5])
})

t.Run("policy with a nil target falls back to placeholders", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{
{ID: 3, Name: "nil-target", Targets: []*models.WebhookTargetObject{nil}},
})

require.Len(t, rows, 1)
assert.Equal(t, placeholder, rows[0][3])
assert.Equal(t, placeholder, rows[0][4])
assert.Equal(t, placeholder, rows[0][5])
})

t.Run("target with empty fields falls back to placeholders", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{
{ID: 4, Name: "empty-target", Targets: []*models.WebhookTargetObject{{}}},
})

require.Len(t, rows, 1)
assert.Equal(t, placeholder, rows[0][3])
assert.Equal(t, placeholder, rows[0][4])
assert.Equal(t, placeholder, rows[0][5])
})

t.Run("nil policies are skipped", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{nil, {ID: 5, Name: "kept"}})

require.Len(t, rows, 1)
assert.Equal(t, "kept", rows[0][1])
})

t.Run("row has one cell per column", func(t *testing.T) {
rows := webhookRows([]*models.WebhookPolicy{{ID: 6}})

require.Len(t, rows, 1)
assert.Len(t, rows[0], len(columns))
})

t.Run("no policies yields no rows", func(t *testing.T) {
assert.Empty(t, webhookRows(nil))
})
}