github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/opts/quickstarts.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     8  	"github.com/jenkins-x/jx-logging/pkg/log"
     9  	"github.com/jenkins-x/jx/v2/pkg/auth"
    10  	"github.com/jenkins-x/jx/v2/pkg/gits"
    11  	"github.com/jenkins-x/jx/v2/pkg/kube"
    12  	"github.com/jenkins-x/jx/v2/pkg/quickstarts"
    13  	"github.com/jenkins-x/jx/v2/pkg/versionstream"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // LoadQuickStartsModel Load all quickstarts
    18  func (o *CommonOptions) LoadQuickStartsModel(gitHubOrganisations []string, ignoreTeam bool) (*quickstarts.QuickstartModel, error) {
    19  	authConfigSvc, err := o.GitLocalAuthConfigService()
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	resolver, err := o.GetVersionResolver()
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	config := authConfigSvc.Config()
    29  
    30  	locations, err := o.loadQuickStartLocations(gitHubOrganisations, ignoreTeam)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	model, err := o.LoadQuickStartsFromLocations(locations, config)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("failed to load quickstarts: %s", err)
    38  	}
    39  	quickstarts, err := versionstream.GetQuickStarts(resolver.VersionsDir)
    40  	if err != nil {
    41  		return nil, errors.Wrapf(err, "loading quickstarts from version stream in dir %s", resolver.VersionsDir)
    42  	}
    43  	quickstarts.DefaultMissingValues()
    44  	err = model.LoadQuickStarts(quickstarts)
    45  	if err != nil {
    46  		return nil, errors.Wrapf(err, "loading quickstarts: %v", quickstarts)
    47  	}
    48  	return model, nil
    49  }
    50  
    51  // LoadQuickStartsFromLocations Load all quickstarts from the given locatiotns
    52  func (o *CommonOptions) LoadQuickStartsFromLocations(locations []v1.QuickStartLocation, config *auth.AuthConfig) (*quickstarts.QuickstartModel, error) {
    53  	gitMap := map[string]map[string]v1.QuickStartLocation{}
    54  	for _, loc := range locations {
    55  		m := gitMap[loc.GitURL]
    56  		if m == nil {
    57  			m = map[string]v1.QuickStartLocation{}
    58  			gitMap[loc.GitURL] = m
    59  		}
    60  		m[loc.Owner] = loc
    61  	}
    62  	model := quickstarts.NewQuickstartModel()
    63  
    64  	for gitURL, m := range gitMap {
    65  		for _, location := range m {
    66  			kind := location.GitKind
    67  			if kind == "" {
    68  				kind = gits.KindGitHub
    69  			}
    70  
    71  			// If this is a default quickstart location but there's no github.com credentials, skip and rely on the version stream alone.
    72  			server := config.GetOrCreateServer(gitURL)
    73  			userAuth := config.CurrentUser(server, o.InCluster())
    74  			if kube.IsDefaultQuickstartLocation(location) && (userAuth == nil || userAuth.IsInvalid()) {
    75  				continue
    76  			}
    77  			gitProvider, err := o.GitProviderForGitServerURL(gitURL, kind, "")
    78  			if err != nil {
    79  				return model, err
    80  			}
    81  			log.Logger().Debugf("Searching for repositories in Git server %s owner %s includes %s excludes %s as user %s ", gitProvider.ServerURL(), location.Owner, strings.Join(location.Includes, ", "), strings.Join(location.Excludes, ", "), gitProvider.CurrentUsername())
    82  			err = model.LoadGithubQuickstarts(gitProvider, location.Owner, location.Includes, location.Excludes)
    83  			if err != nil {
    84  				log.Logger().Debugf("Quickstart load error: %s", err.Error())
    85  			}
    86  		}
    87  	}
    88  	return model, nil
    89  }
    90  
    91  // loadQuickStartLocations loads the quickstarts
    92  func (o *CommonOptions) loadQuickStartLocations(gitHubOrganisations []string, ignoreTeam bool) ([]v1.QuickStartLocation, error) {
    93  	var locations []v1.QuickStartLocation
    94  	if !ignoreTeam {
    95  		jxClient, ns, err := o.JXClientAndDevNamespace()
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  
   100  		locations, err = kube.GetQuickstartLocations(jxClient, ns)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  	}
   105  	// lets add any extra github organisations if they are not already configured
   106  	for _, org := range gitHubOrganisations {
   107  		found := false
   108  		for _, loc := range locations {
   109  			if loc.GitURL == gits.GitHubURL && loc.Owner == org {
   110  				found = true
   111  				break
   112  			}
   113  		}
   114  		if !found {
   115  			locations = append(locations, v1.QuickStartLocation{
   116  				GitURL:   gits.GitHubURL,
   117  				GitKind:  gits.KindGitHub,
   118  				Owner:    org,
   119  				Includes: []string{"*"},
   120  				Excludes: []string{"WIP-*"},
   121  			})
   122  		}
   123  	}
   124  	return locations, nil
   125  }