/
issues.go
74 lines (68 loc) · 2.36 KB
/
issues.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
package opts
import (
"fmt"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/issues"
)
// CreateIssueTrackerAuthConfigService creates auth config service for issue tracker
func (o *CommonOptions) CreateIssueTrackerAuthConfigService(kind string) (auth.ConfigService, error) {
_, namespace, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, errors.Wrapf(err, "failed to find development namespace")
}
return o.factory.CreateIssueTrackerAuthConfigService(namespace, "")
}
// CreateIssueProvider creates a issues provider
func (o *CommonOptions) CreateIssueProvider(dir string) (issues.IssueProvider, error) {
gitDir, gitConfDir, err := o.Git().FindGitConfigDir(dir)
if err != nil {
return nil, fmt.Errorf("No issue tracker configured for this project and cannot find the .git directory: %s", err)
}
pc, _, err := config.LoadProjectConfig(dir)
if err != nil {
return nil, err
}
if pc != nil && pc.IssueTracker == nil {
pc, _, err = config.LoadProjectConfig(gitDir)
if err != nil {
return nil, err
}
}
if pc != nil {
it := pc.IssueTracker
if it != nil {
if it.URL != "" && it.Kind != "" {
authConfigSvc, err := o.CreateIssueTrackerAuthConfigService(it.Kind)
if err != nil {
return nil, err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(it.URL)
userAuth, err := config.PickServerUserAuth(server, "user to access the issue tracker", o.BatchMode, "", o.GetIOFileHandles())
if err != nil {
return nil, err
}
return issues.CreateIssueProvider(it.Kind, server, userAuth, it.Project, o.BatchMode, o.Git())
}
}
}
if gitConfDir == "" {
return nil, fmt.Errorf("No issue tracker configured and no git directory could be found from dir %s\n", dir)
}
gitUrl, err := o.Git().DiscoverUpstreamGitURL(gitConfDir)
if err != nil {
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)
}
gitInfo, err := gits.ParseGitURL(gitUrl)
if err != nil {
return nil, err
}
gitProvider, err := o.GitProviderForURL(gitUrl, "user name to use for authenticating with git issues")
if err != nil {
return nil, err
}
return issues.CreateGitIssueProvider(gitProvider, gitInfo.Organisation, gitInfo.Name)
}