-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathcreate.go
More file actions
116 lines (104 loc) · 3.94 KB
/
create.go
File metadata and controls
116 lines (104 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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 webhook
import (
"fmt"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/webhook/create"
"github.com/spf13/cobra"
)
func CreateWebhookCmd() *cobra.Command {
var opts create.CreateView
cmd := &cobra.Command{
Use: "create [name]",
Short: "Create a new webhook for a Harbor project",
Long: `This command creates a new webhook policy for a specified Harbor project.
You can either provide all required flags (project name, notify type, endpoint, etc.) directly to create the webhook non-interactively,
or leave them out and be guided through an interactive prompt to input each field. The webhook name is required as an argument.`,
Example: ` # Create a webhook using flags
harbor-cli webhook create my-webhook \
--project my-project \
--notify-type http \
--event-type PUSH_ARTIFACT,DELETE_ARTIFACT \
--endpoint-url https://example.com/webhook \
--description "Webhook for artifact events" \
--payload-format Default \
--auth-header "Bearer mytoken"
# Create a webhook using the interactive prompt
harbor-cli webhook create my-webhook`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if len(args) > 0 {
opts.Name = args[0]
}
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 != "" {
formattedURL := utils.FormatUrl(opts.EndpointURL)
err = utils.ValidateURL(formattedURL)
if err != nil {
return err
}
opts.EndpointURL = formattedURL
err = api.CreateWebhook(&opts)
} else {
err = createWebhookView(createView)
}
if err != nil {
return fmt.Errorf("failed to create webhook: %v", err)
}
return nil
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name")
flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description")
flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)")
flags.StringSliceVarP(&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 {
var err error
if view.ProjectName == "" {
view.ProjectName, err = prompt.GetProjectNameFromUser()
if err != nil {
return err
}
}
err = create.WebhookCreateView(view)
if err != nil {
return err
}
return api.CreateWebhook(view)
}