-
Notifications
You must be signed in to change notification settings - Fork 787
/
update_webhooks.go
177 lines (142 loc) · 4.5 KB
/
update_webhooks.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cmd
import (
"strings"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// UpdateWebhooksOptions the flags for updating webhooks
type UpdateWebhooksOptions struct {
*CommonOptions
Org string
Repo string
ExactHookMatch bool
PreviousHookUrl string
DryRun bool
}
var (
updateWebhooksLong = templates.LongDesc(`
Updates the webhook for one repository, or all repositories in an organization.
`)
updateWebhooksExample = templates.Examples(`
jx update webhooks --org=mycorp
`)
)
func NewCmdUpdateWebhooks(commonOpts *CommonOptions) *cobra.Command {
options := createUpdateWebhooksOptions(commonOpts)
cmd := &cobra.Command{
Use: "webhooks",
Short: "Updates all webhooks for an existing org",
Long: updateWebhooksLong,
Example: updateWebhooksExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Org, "org", "o", "jenkins-x", "The name of the git organisation to query")
cmd.Flags().StringVarP(&options.Repo, "repo", "r", "", "The name of the repository to query")
cmd.Flags().BoolVarP(&options.ExactHookMatch, "exact-hook-url-match", "", true, "Whether to exactly match the hook based on the URL")
cmd.Flags().StringVarP(&options.PreviousHookUrl, "previous-hook-url", "", "", "Whether to match based on an another URL")
return cmd
}
func createUpdateWebhooksOptions(commonOpts *CommonOptions) UpdateWebhooksOptions {
options := UpdateWebhooksOptions{
CommonOptions: commonOpts,
}
return options
}
func (options *UpdateWebhooksOptions) Run() error {
authConfigService, err := options.CreateGitAuthConfigService()
if err != nil {
return errors.Wrap(err, "failed to create git auth service")
}
client, err := options.KubeClient()
if err != nil {
return errors.Wrap(err, "failed to get kube client")
}
ns, _, err := kube.GetDevNamespace(client, options.currentNamespace)
if err != nil {
return err
}
webhookUrl, err := options.GetWebHookEndpoint()
if err != nil {
return err
}
isProwEnabled, err := options.isProw()
if err != nil {
return err
}
hmacToken, err := client.CoreV1().Secrets(ns).Get("hmac-token", metav1.GetOptions{})
if err != nil {
return err
}
gitServer := authConfigService.Config().CurrentServer
git, err := options.gitProviderForGitServerURL(gitServer, "github")
if err != nil {
return errors.Wrap(err, "unable to determine git provider")
}
if options.Repo != "" {
options.updateRepoHook(git, options.Repo, webhookUrl, isProwEnabled, hmacToken)
} else {
repositories, err := git.ListRepositories(options.Org)
if err != nil {
return errors.Wrap(err, "unable to list repositories")
}
log.Infof("Found %v repos\n", util.ColorInfo(len(repositories)))
for _, repo := range repositories {
options.updateRepoHook(git, repo.Name, webhookUrl, isProwEnabled, hmacToken)
}
}
return nil
}
func (options *UpdateWebhooksOptions) updateRepoHook(git gits.GitProvider, repoName string, webhookURL string, isProwEnabled bool, hmacToken *corev1.Secret) error {
webhooks, err := git.ListWebHooks(options.Org, repoName)
if err != nil {
return errors.Wrap(err, "unable to list webhooks")
}
log.Infof("Checking hooks for repository %s\n", util.ColorInfo(repoName))
if len(webhooks) > 0 {
// find matching hook
for _, webHook := range webhooks {
if options.matches(webhookURL, webHook) {
log.Infof("Found matching hook for url %s\n", util.ColorInfo(webHook.URL))
// update
webHookArgs := &gits.GitWebHookArguments{
ID: webHook.ID,
Owner: options.Org,
Repo: &gits.GitRepository{
Name: repoName,
},
URL: webhookURL,
ExistingURL: options.PreviousHookUrl,
}
if isProwEnabled {
webHookArgs.Secret = string(hmacToken.Data["hmac"])
}
if !options.DryRun {
git.UpdateWebHook(webHookArgs)
}
}
}
}
return nil
}
func (options *UpdateWebhooksOptions) matches(webhookURL string, webHookArgs *gits.GitWebHookArguments) bool {
if "" != options.PreviousHookUrl {
return options.PreviousHookUrl == webHookArgs.URL
}
if options.ExactHookMatch {
return webhookURL == webHookArgs.URL
} else {
return strings.Contains(webHookArgs.URL, "hook.jx")
}
}