func (o *Options) GetWebHookEndpointFromHook() (string, error) {
	baseURL, err := services.GetServiceURLFromName(o.KubeClient, "hook", o.Namespace)
	if err != nil {
		return "", err
	}
	if !strings.HasSuffix(baseURL, "/hook") {
		baseURL = stringhelpers.UrlJoin(baseURL, "hook")
	}
	return baseURL, nil
}
GetWebHookEndpointFromHook returns the webhook endpoint
package update
import (
	"context"
	"fmt"
	"io/ioutil"
	"strings"
	"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
	"github.com/jenkins-x/go-scm/scm"
	jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
	v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
	jxc "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
	"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
	"github.com/jenkins-x/jx-helpers/v3/pkg/kube"
	"github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxclient"
	"github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxenv"
	"github.com/jenkins-x/jx-helpers/v3/pkg/kube/services"
	"github.com/jenkins-x/jx-helpers/v3/pkg/options"
	"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
	"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
	"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
	"github.com/jenkins-x/jx-logging/v3/pkg/log"
	"github.com/pkg/errors"
	"k8s.io/client-go/kubernetes"
	"github.com/spf13/cobra"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Options struct {
	options.BaseOptions
	ScmClientFactory scmhelpers.Factory
	Org              string
	User             string
	Repo             string
	ExactHookMatch   bool
	PreviousHookUrl  string
	HMAC             string
	Endpoint         string
	DryRun           bool
	WarnOnFail       bool
	Namespace        string
	KubeClient       kubernetes.Interface
	JXClient         jxc.Interface
}
var (
	info = termcolor.ColorInfo
	cmdLong = templates.LongDesc(`
		Updates the webhooks for all the source repositories optionally filtering by owner and/or repository
`)
	cmdExample = templates.Examples(`
		# update all the webhooks for all SourceRepository and Environment resource:
		%s update
		# only update the webhooks for a given owner
		%s update --org=mycorp
		# use a custom hook webhook endpoint (e.g. if you are on premise using node ports or something)
		%s update --endpoint http: 
`)
)
func NewCmdWebHookVerify() (*cobra.Command, *Options) {
	o := &Options{}
	cmd := &cobra.Command{
		Use:     "update",
		Short:   "Updates the webhooks for all the source repositories optionally filtering by owner and/or repository",
		Long:    cmdLong,
		Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName, rootcmd.BinaryName, rootcmd.BinaryName),
		RunE: func(cmd *cobra.Command, args []string) error {
			return o.Run()
		},
	}
	cmd.Flags().StringVarP(&o.Org, "owner", "o", "", "The name of the git organisation or user to filter on")
	cmd.Flags().StringVarP(&o.Repo, "repo", "r", "", "The name of the repository to filter on")
	cmd.Flags().BoolVarP(&o.ExactHookMatch, "exact-hook-url-match", "", true, "Whether to exactly match the hook based on the URL")
	cmd.Flags().StringVarP(&o.PreviousHookUrl, "previous-hook-url", "", "", "Whether to match based on an another URL")
	cmd.Flags().StringVarP(&o.HMAC, "hmac", "", "", "Don't use the HMAC token from the cluster, use the provided token")
	cmd.Flags().StringVarP(&o.Endpoint, "endpoint", "", "", "Don't use the endpoint from the cluster, use the provided endpoint")
	cmd.Flags().BoolVarP(&o.WarnOnFail, "warn-on-fail", "", false, "If enabled lets just log a warning that we could not update the webhook")
	o.ScmClientFactory.AddFlags(cmd)
	o.BaseOptions.AddBaseFlags(cmd)
	return cmd, o
}
func (o *Options) Validate() error {
	var err error
	o.KubeClient, o.Namespace, err = kube.LazyCreateKubeClientAndNamespace(o.KubeClient, o.Namespace)
	if err != nil {
		return errors.Wrapf(err, "failed to create kube client")
	}
	o.JXClient, err = jxclient.LazyCreateJXClient(o.JXClient)
	if err != nil {
		return errors.Wrapf(err, "failed to create jx client")
	}
	ns, _, err := jxenv.GetDevNamespace(o.KubeClient, o.Namespace)
	if err != nil {
		return errors.Wrapf(err, "failed to find dev namespace in %s", o.Namespace)
	}
	if ns != "" {
		o.Namespace = ns
	}
	if o.Endpoint == "" {
		o.Endpoint, err = o.GetWebHookEndpointFromHook()
		if err != nil {
			return errors.Wrapf(err, "failed to find webhook endpoint")
		}
	}
	return nil
}
func (o *Options) Run() error {
	err := o.Validate()
	if err != nil {
		return errors.Wrapf(err, "failed to validate options")
	}
	jxClient := o.JXClient
	ns := o.Namespace
	srList, err := jxClient.JenkinsV1().SourceRepositories(ns).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		return errors.Wrapf(err, "failed to find any SourceRepositories in namespace %s", ns)
	}
	envMap, _, err := jxenv.GetEnvironments(jxClient, ns)
	for _, sr := range srList.Items {
		sourceRepo := sr
		if sr.Spec.ProviderKind != "bitbucketcloud" && o.HMAC == "" {
			o.HMAC, err = o.GetHMACTokenFromSecret()
			if err != nil {
				return errors.Wrapf(err, "failed to find hmac token from secret")
			}
		}
		_, err2 := o.UpdateWebhookForSourceRepository(&sourceRepo, envMap, err, o.Endpoint, o.HMAC)
		if err2 != nil {
			return err2
		}
	}
	return nil
}