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

Add a parameter to update webhooks for a single repo in an org #2336

Merged
merged 7 commits into from Nov 28, 2018
80 changes: 46 additions & 34 deletions pkg/jx/cmd/update_webhooks.go
@@ -1,24 +1,27 @@
package cmd

import (
"io"
"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"
"io"
"strings"

"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// UpdateWebhooks the flags for running create cluster
type UpdateWebhooksOptions struct {
CommonOptions
Org string
Repo string
ExactHookMatch bool
PreviousHookUrl string
DryRun bool
Expand Down Expand Up @@ -55,6 +58,7 @@ func NewCmdUpdateWebhooks(f Factory, in terminal.FileReader, out terminal.FileWr
}

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")

Expand Down Expand Up @@ -112,51 +116,59 @@ func (options *UpdateWebhooksOptions) Run() error {
return errors.Wrap(err, "unable to determine git provider")
}

repositories, err := git.ListRepositories(options.Org)
if err != nil {
return errors.Wrap(err, "unable to list repositories")
}
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)))
log.Infof("Found %v repos\n", util.ColorInfo(len(repositories)))

for _, repo := range repositories {
repoName := repo.Name
webhooks, err := git.ListWebHooks(options.Org, repoName)
if err != nil {
return errors.Wrap(err, "unable to list webhooks")
for _, repo := range repositories {
options.updateRepoHook(git, repo.Name, webhookUrl, isProwEnabled, hmacToken)
}
}

log.Infof("Checking hooks for repository %s\n", util.ColorInfo(repo.Name))
return nil
}

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))
func (options *UpdateWebhooksOptions) updateRepoHook(git gits.GitProvider, repoName string, webhookUrl string, isProwEnabled bool, hmacToken *corev1.Secret) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method parameter webhookUrl should be webhookURL

webhooks, err := git.ListWebHooks(options.Org, repoName)
if err != nil {
return errors.Wrap(err, "unable to list webhooks")
}

// update
webHookArgs := &gits.GitWebHookArguments{
Owner: options.Org,
Repo: &gits.GitRepositoryInfo{
Name: repo.Name,
},
URL: webhookUrl,
}
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{
Owner: options.Org,
Repo: &gits.GitRepositoryInfo{
Name: repoName,
},
URL: webhookUrl,
}

if isProwEnabled {
webHookArgs.Secret = string(hmacToken.Data["hmac"])
}
if isProwEnabled {
webHookArgs.Secret = string(hmacToken.Data["hmac"])
}

log.Infof("Updating WebHook with new args\n")
log.Infof("Updating WebHook with new args\n")

if !options.DryRun {
git.UpdateWebHook(webHookArgs)
}
if !options.DryRun {
git.UpdateWebHook(webHookArgs)
}
}
}
}

return nil
}

Expand Down