-
Notifications
You must be signed in to change notification settings - Fork 83
/
add.go
144 lines (126 loc) · 4.31 KB
/
add.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package webhook
import (
"context"
"fmt"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/webhook"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/completion"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
"github.com/openshift-pipelines/pipelines-as-code/pkg/pipelineascode"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var namespaceFlag = "namespace"
func webhookAdd(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
var pacNamespace string
cmd := &cobra.Command{
Use: "add",
Aliases: []string{""},
Short: "Adds a webhook secret on the git provider settings",
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
repoName string
)
opts := cli.NewCliOptions(cmd)
opts.Namespace, err = cmd.Flags().GetString(namespaceFlag)
if err != nil {
return err
}
ctx := cmd.Context()
if len(args) > 0 {
repoName = args[0]
}
if err := run.Clients.NewClients(ctx, &run.Info); err != nil {
return err
}
return add(ctx, opts, run, ioStreams, repoName, pacNamespace)
},
Annotations: map[string]string{
"commandType": "main",
},
}
cmd.PersistentFlags().StringVarP(&pacNamespace, "pac-namespace",
"", "", "The namespace where pac is installed")
cmd.Flags().StringP(
namespaceFlag, "n", "", "If present, the namespace scope for this CLI request")
_ = cmd.RegisterFlagCompletionFunc(namespaceFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completion.BaseCompletion(namespaceFlag, args)
},
)
return cmd
}
func add(ctx context.Context, opts *cli.PacCliOpts, run *params.Run, ioStreams *cli.IOStreams, repoName, pacNamespace string) error {
var (
err error
repo *v1alpha1.Repository
providerName string
)
if opts.Namespace != "" {
run.Info.Kube.Namespace = opts.Namespace
}
if repoName != "" {
repo, err = run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(run.Info.Kube.Namespace).Get(ctx,
repoName, metav1.GetOptions{})
if err != nil {
return err
}
} else {
repo, err = prompt.SelectRepo(ctx, run, run.Info.Kube.Namespace)
if err != nil {
return err
}
}
if providerName, err = webhook.GetProviderName(repo.Spec.URL); err != nil {
return err
}
if repo.Spec.GitProvider == nil {
config := &webhook.Options{
Run: run,
RepositoryName: repo.Name,
RepositoryNamespace: repo.Namespace,
PACNamespace: pacNamespace,
RepositoryURL: repo.Spec.URL,
IOStreams: ioStreams,
RepositoryCreateORUpdate: true,
}
return config.Install(ctx, providerName)
}
if repo.Spec.GitProvider.Secret == nil {
fmt.Fprintf(ioStreams.Out, "%s Can not configure webhook as git_provider secret is empty",
ioStreams.ColorScheme().WarningIcon())
return nil
}
secretName := repo.Spec.GitProvider.Secret.Name
secretData, err := run.Clients.Kube.CoreV1().Secrets(repo.Namespace).Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return err
}
gitProviderSecretKey := repo.Spec.GitProvider.Secret.Key
if gitProviderSecretKey == "" {
gitProviderSecretKey = pipelineascode.DefaultGitProviderSecretKey
}
tokenData, ok := secretData.Data[gitProviderSecretKey]
if !ok {
fmt.Fprintf(ioStreams.Out, "Token is empty, You can use the command \"%s pac webhook update-token\" to update the provider token in %s secret", settings.TknBinaryName, repoName)
return nil
}
config := &webhook.Options{
Run: run,
RepositoryName: repo.Name,
RepositoryNamespace: repo.Namespace,
PACNamespace: pacNamespace,
RepositoryURL: repo.Spec.URL,
ProviderAPIURL: repo.Spec.GitProvider.URL,
IOStreams: ioStreams,
PersonalAccessToken: string(tokenData),
RepositoryCreateORUpdate: false,
SecretName: secretName,
ProviderSecretKey: gitProviderSecretKey,
}
return config.Install(ctx, providerName)
}