github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/opts/issues.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/auth"
     9  	"github.com/olli-ai/jx/v2/pkg/config"
    10  	"github.com/olli-ai/jx/v2/pkg/gits"
    11  	"github.com/olli-ai/jx/v2/pkg/issues"
    12  )
    13  
    14  // CreateIssueTrackerAuthConfigService creates auth config service for issue tracker
    15  func (o *CommonOptions) CreateIssueTrackerAuthConfigService(kind string) (auth.ConfigService, error) {
    16  	_, namespace, err := o.KubeClientAndDevNamespace()
    17  	if err != nil {
    18  		return nil, errors.Wrapf(err, "failed to find development namespace")
    19  	}
    20  	return o.factory.CreateIssueTrackerAuthConfigService(namespace, "")
    21  }
    22  
    23  // CreateIssueProvider creates a issues provider
    24  func (o *CommonOptions) CreateIssueProvider(dir string) (issues.IssueProvider, error) {
    25  	gitDir, gitConfDir, err := o.Git().FindGitConfigDir(dir)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("No issue tracker configured for this project and cannot find the .git directory: %s", err)
    28  	}
    29  	pc, _, err := config.LoadProjectConfig(dir)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	if pc != nil && pc.IssueTracker == nil {
    34  		pc, _, err = config.LoadProjectConfig(gitDir)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  	}
    39  	if pc != nil {
    40  		it := pc.IssueTracker
    41  		if it != nil {
    42  			if it.URL != "" && it.Kind != "" {
    43  				authConfigSvc, err := o.CreateIssueTrackerAuthConfigService(it.Kind)
    44  				if err != nil {
    45  					return nil, err
    46  				}
    47  				config := authConfigSvc.Config()
    48  				server := config.GetOrCreateServer(it.URL)
    49  				userAuth, err := config.PickServerUserAuth(server, "user to access the issue tracker", o.BatchMode, "", o.GetIOFileHandles())
    50  				if err != nil {
    51  					return nil, err
    52  				}
    53  				return issues.CreateIssueProvider(it.Kind, server, userAuth, it.Project, o.BatchMode, o.Git())
    54  			}
    55  		}
    56  	}
    57  
    58  	if gitConfDir == "" {
    59  		return nil, fmt.Errorf("No issue tracker configured and no git directory could be found from dir %s\n", dir)
    60  	}
    61  	gitUrl, err := o.Git().DiscoverUpstreamGitURL(gitConfDir)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("No issue tracker configured and could not find the upstream git URL for dir %s, due to: %s\n", dir, err)
    64  	}
    65  	gitInfo, err := gits.ParseGitURL(gitUrl)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	gitProvider, err := o.GitProviderForURL(gitUrl, "user name to use for authenticating with git issues")
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return issues.CreateGitIssueProvider(gitProvider, gitInfo.Organisation, gitInfo.Name)
    74  }