forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_issues.go
94 lines (87 loc) · 3.11 KB
/
common_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/config"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/issues"
"github.com/jenkins-x/jx/pkg/kube"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (o *CommonOptions) CreateIssueTrackerAuthConfigService(dir string) (auth.AuthConfigService, error) {
var secrets *corev1.SecretList
kubeClient, curNs, err := o.KubeClient()
if err != nil {
return o.errorCreateIssueTrackerAuthConfigService(fmt.Errorf("Failed to create a kuberntees client %s", err))
}
ns, _, err := kube.GetDevNamespace(kubeClient, curNs)
if err != nil {
return o.errorCreateIssueTrackerAuthConfigService(fmt.Errorf("Failed to get the development environment %s", err))
}
secrets, err = kubeClient.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
o.warnf("The current user cannot query secrets in the namespace %s: %s", ns, err)
}
return o.Factory.CreateIssueTrackerAuthConfigService(secrets)
}
func (o *CommonOptions) errorCreateIssueTrackerAuthConfigService(parentError error) (auth.AuthConfigService, error) {
answer, err := o.Factory.CreateIssueTrackerAuthConfigService(nil)
if err == nil {
return answer, parentError
}
return answer, err
}
func (o *CommonOptions) CreateIssueTrackerAuthConfigServiceFromConfig(pc *config.ProjectConfig) (auth.AuthConfigService, error) {
return o.CreateIssueTrackerAuthConfigService("")
}
func (o *CommonOptions) createIssueProvider(dir string) (issues.IssueProvider, error) {
gitDir, gitConfDir, err := gits.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("")
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)
if err != nil {
return nil, err
}
return issues.CreateIssueProvider(it.Kind, server, userAuth, it.Project, o.BatchMode)
}
}
}
if gitConfDir == "" {
return nil, fmt.Errorf("No issue tracker configured and no git directory could be found from dir %s\n", dir)
}
gitUrl, err := gits.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)
}