|
| 1 | +// Copyright Project Harbor Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package webhook |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/goharbor/harbor-cli/pkg/api" |
| 20 | + "github.com/goharbor/harbor-cli/pkg/prompt" |
| 21 | + "github.com/goharbor/harbor-cli/pkg/utils" |
| 22 | + "github.com/goharbor/harbor-cli/pkg/views/webhook/create" |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +func CreateWebhookCmd() *cobra.Command { |
| 27 | + var opts create.CreateView |
| 28 | + |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "create [name]", |
| 31 | + Short: "Create a new webhook for a Harbor project", |
| 32 | + Long: `This command creates a new webhook policy for a specified Harbor project. |
| 33 | +
|
| 34 | +You can either provide all required flags (project name, notify type, endpoint, etc.) directly to create the webhook non-interactively, |
| 35 | +or leave them out and be guided through an interactive prompt to input each field. The webhook name is required as an argument.`, |
| 36 | + Example: ` # Create a webhook using flags |
| 37 | + harbor-cli webhook create my-webhook \ |
| 38 | + --project my-project \ |
| 39 | + --notify-type http \ |
| 40 | + --event-type PUSH_ARTIFACT,DELETE_ARTIFACT \ |
| 41 | + --endpoint-url https://example.com/webhook \ |
| 42 | + --description "Webhook for artifact events" \ |
| 43 | + --payload-format Default \ |
| 44 | + --auth-header "Bearer mytoken" |
| 45 | +
|
| 46 | + # Create a webhook using the interactive prompt |
| 47 | + harbor-cli webhook create my-webhook`, |
| 48 | + Args: cobra.MaximumNArgs(1), |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + var err error |
| 51 | + if len(args) > 0 { |
| 52 | + opts.Name = args[0] |
| 53 | + } |
| 54 | + |
| 55 | + createView := &create.CreateView{ |
| 56 | + ProjectName: opts.ProjectName, |
| 57 | + Name: opts.Name, |
| 58 | + Description: opts.Description, |
| 59 | + NotifyType: opts.NotifyType, |
| 60 | + PayloadFormat: opts.PayloadFormat, |
| 61 | + EventType: opts.EventType, |
| 62 | + EndpointURL: opts.EndpointURL, |
| 63 | + AuthHeader: opts.AuthHeader, |
| 64 | + VerifyRemoteCertificate: opts.VerifyRemoteCertificate, |
| 65 | + } |
| 66 | + |
| 67 | + if opts.ProjectName != "" && |
| 68 | + opts.Name != "" && |
| 69 | + opts.NotifyType != "" && |
| 70 | + len(opts.EventType) != 0 && |
| 71 | + opts.EndpointURL != "" { |
| 72 | + err = utils.ValidateURL(opts.EndpointURL) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + err = api.CreateWebhook(&opts) |
| 77 | + } else { |
| 78 | + err = createWebhookView(createView) |
| 79 | + } |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to create webhook: %v", err) |
| 82 | + } |
| 83 | + return nil |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + flags := cmd.Flags() |
| 88 | + |
| 89 | + flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name") |
| 90 | + flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description") |
| 91 | + flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)") |
| 92 | + flags.StringSliceVarP(&opts.EventType, "event-type", "", []string{}, "Event Types (comma separated)") |
| 93 | + flags.StringVarP(&opts.EndpointURL, "endpoint-url", "", "", "Webhook Endpoint URL") |
| 94 | + flags.StringVarP(&opts.PayloadFormat, "payload-format", "", "", "Payload Format (Default, CloudEvents)") |
| 95 | + flags.StringVarP(&opts.AuthHeader, "auth-header", "", "", "Authentication Header") |
| 96 | + flags.BoolVarP(&opts.VerifyRemoteCertificate, "verify-remote-certificate", "", true, "Verify Remote Certificate") |
| 97 | + |
| 98 | + return cmd |
| 99 | +} |
| 100 | + |
| 101 | +func createWebhookView(view *create.CreateView) error { |
| 102 | + var err error |
| 103 | + if view.ProjectName == "" { |
| 104 | + view.ProjectName, err = prompt.GetProjectNameFromUser() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + } |
| 109 | + err = create.WebhookCreateView(view) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + return api.CreateWebhook(view) |
| 114 | +} |
0 commit comments