Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add webhook cmd #166

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/cmd/harbor/root/webhook"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -109,6 +110,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
webhook.Webhook(),
)

return root
Expand Down
19 changes: 19 additions & 0 deletions cmd/harbor/root/webhook/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package webhook

import "github.com/spf13/cobra"

func Webhook() *cobra.Command {
cmd := &cobra.Command{
Use: "webhook",
Short: "Manage webhooks",
Long: `Manage webhooks in Harbor Repository`,
Example: ` harbor webhook list`,
}
cmd.AddCommand(
CreateWebhookCmd(),
ListWebhookCommand(),
DeleteWebhookCmd(),
EditWebhookCmd(),
)
return cmd
}
67 changes: 67 additions & 0 deletions cmd/harbor/root/webhook/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package webhook

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/webhook/create"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func CreateWebhookCmd() *cobra.Command {
var opts create.CreateView

cmd := &cobra.Command{
Use: "create",
Short: "webhook create",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
createView := &create.CreateView{
ProjectName: opts.ProjectName,
Name: opts.Name,
Description: opts.Description,
NotifyType: opts.NotifyType,
PayloadFormat: opts.PayloadFormat,
EventType: opts.EventType,
EndpointURL: opts.EndpointURL,
AuthHeader: opts.AuthHeader,
VerifyRemoteCertificate: opts.VerifyRemoteCertificate,
}

if opts.ProjectName != "" &&
opts.Name != "" &&
opts.NotifyType != "" &&
len(opts.EventType) != 0 &&
opts.EndpointURL != "" {
err = api.CreateWebhook(&opts)
} else {
err = createWebhookView(createView)
}

if err != nil {
log.Errorf("failed to create webhook: %v", err)
}
},
}
flags := cmd.Flags()

flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name")
flags.StringVarP(&opts.Name, "name", "", "", "Webhook Name")
flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description")
flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)")
flags.StringArrayVarP(&opts.EventType, "event-type", "", []string{}, "Event Types (comma separated)")
flags.StringVarP(&opts.EndpointURL, "endpoint-url", "", "", "Webhook Endpoint URL")
flags.StringVarP(&opts.PayloadFormat, "payload-format", "", "", "Payload Format (Default, CloudEvents)")
flags.StringVarP(&opts.AuthHeader, "auth-header", "", "", "Authentication Header")
flags.BoolVarP(&opts.VerifyRemoteCertificate, "verify-remote-certificate", "", true, "Verify Remote Certificate")

return cmd
}

func createWebhookView(view *create.CreateView) error {

view.ProjectName = prompt.GetProjectNameFromUser()
create.WebhookCreateView(view)
return api.CreateWebhook(view)
}
47 changes: 47 additions & 0 deletions cmd/harbor/root/webhook/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package webhook

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strconv"
)

func DeleteWebhookCmd() *cobra.Command {

var projectName string
var webhookId string
var webhookIdInt int64
var selectedWebhook models.WebhookPolicy

cmd := &cobra.Command{
Use: "delete",
Short: "webhook delete",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error

if projectName != "" && webhookId != "" {
webhookIdInt, err = strconv.ParseInt(webhookId, 10, 64)
err = api.DeleteWebhook(projectName, webhookIdInt)
} else {
projectName = prompt.GetProjectNameFromUser()
selectedWebhook = prompt.GetWebhookFromUser(projectName)
err = api.DeleteWebhook(projectName, selectedWebhook.ID)

}
if err != nil {
log.Errorf("failed to delete webhook: %v", err)
}

},
}

flags := cmd.Flags()
flags.StringVarP(&projectName, "project", "", "", "Project Name")
flags.StringVarP(&webhookId, "webhook", "", "", "Webhook Id")

return cmd
}
90 changes: 90 additions & 0 deletions cmd/harbor/root/webhook/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package webhook

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/webhook/edit"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strconv"
)

func EditWebhookCmd() *cobra.Command {
var opts edit.EditView
var webhookId string
var webhookIdInt int64
cmd := &cobra.Command{
Use: "edit",
Short: "webhook edit",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
editView := &edit.EditView{
WebhookId: opts.WebhookId,
ProjectName: opts.ProjectName,
Name: opts.Name,
Description: opts.Description,
NotifyType: opts.NotifyType,
PayloadFormat: opts.PayloadFormat,
EventType: opts.EventType,
EndpointURL: opts.EndpointURL,
AuthHeader: opts.AuthHeader,
VerifyRemoteCertificate: opts.VerifyRemoteCertificate,
}
if opts.ProjectName != "" &&
webhookId != "" &&
opts.Name != "" &&
opts.NotifyType != "" &&
len(opts.EventType) != 0 &&
opts.EndpointURL != "" {
webhookIdInt, err = strconv.ParseInt(webhookId, 10, 64)
opts.WebhookId = webhookIdInt
err = api.UpdateWebhook(&opts)
} else {
err = editWebhookView(editView)
}

if err != nil {
log.Errorf("failed to create webhook: %v", err)
}

},
}
flags := cmd.Flags()

flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name")
flags.StringVarP(&webhookId, "webhook-id", "", "", "Webhook ID")
flags.StringVarP(&opts.Name, "name", "", "", "Webhook Name")
flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description")
flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)")
flags.StringArrayVarP(&opts.EventType, "event-type", "", []string{}, "Event Types (comma separated)")
flags.StringVarP(&opts.EndpointURL, "endpoint-url", "", "", "Webhook Endpoint URL")
flags.StringVarP(&opts.PayloadFormat, "payload-format", "", "", "Payload Format (Default, CloudEvents)")
flags.StringVarP(&opts.AuthHeader, "auth-header", "", "", "Authentication Header")
flags.BoolVarP(&opts.VerifyRemoteCertificate, "verify-remote-certificate", "", true, "Verify Remote Certificate")
flags.BoolVarP(&opts.Enabled, "enabled", "", true, "Webhook Enabled")

return cmd
}

func editWebhookView(view *edit.EditView) error {
var selectedWebhook models.WebhookPolicy
view.ProjectName = prompt.GetProjectNameFromUser()
selectedWebhook = prompt.GetWebhookFromUser(view.ProjectName)

view.WebhookId = selectedWebhook.ID
view.Description = selectedWebhook.Description
view.Enabled = selectedWebhook.Enabled
view.EventType = selectedWebhook.EventTypes
view.Name = selectedWebhook.Name

view.EndpointURL = selectedWebhook.Targets[0].Address
view.AuthHeader = selectedWebhook.Targets[0].AuthHeader
view.PayloadFormat = string(selectedWebhook.Targets[0].PayloadFormat)
view.VerifyRemoteCertificate = !selectedWebhook.Targets[0].SkipCertVerify
view.NotifyType = selectedWebhook.Targets[0].Type

edit.WebhookEditView(view)
return api.UpdateWebhook(view)
}
45 changes: 45 additions & 0 deletions cmd/harbor/root/webhook/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package webhook

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/webhook"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
webhookViews "github.com/goharbor/harbor-cli/pkg/views/webhook/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListWebhookCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list webhook",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var resp webhook.ListWebhookPoliciesOfProjectOK
if len(args) > 0 {
projectName := args[0]
resp, err = api.ListWebhooks(projectName)
} else {
projectName := prompt.GetProjectNameFromUser()
resp, err = api.ListWebhooks(projectName)
}

if err != nil {
log.Errorf("failed to list webhooks: %v", err)
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(resp)
return
}

webhookViews.ListWebhooks(resp.Payload)

},
}
return cmd
}
Loading
Loading