forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
118 lines (110 loc) · 4.08 KB
/
import.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package jenkins
import (
"fmt"
"io"
"net/url"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/util"
)
// ImportProject imports a MultiBranchProject into Jeknins for the given git URL
func ImportProject(out io.Writer, jenk *gojenkins.Jenkins, gitURL string, dir string, jenkinsfile string, branchPattern, credentials string, failIfExists bool, gitProvider gits.GitProvider, authConfigSvc auth.AuthConfigService) error {
if gitURL == "" {
return fmt.Errorf("No Git repository URL found!")
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return fmt.Errorf("Failed to parse git URL %s due to: %s", gitURL, err)
}
if branchPattern == "" {
fork, err := gits.GitIsFork(gitProvider, gitInfo, dir)
if err != nil {
return fmt.Errorf("No branch pattern specified and could not determine if the git repository is a fork: %s", err)
}
if fork {
// lets figure out which branches to enable for a fork
branch, err := gits.GitGetBranch(dir)
if err != nil {
return fmt.Errorf("Failed to get current branch in dir %s: %s", dir, err)
}
if branch == "" {
return fmt.Errorf("Failed to get current branch in dir %s", dir)
}
// TODO do we need to scape any wacky characters to make it a valid branch pattern?
branchPattern = branch
fmt.Fprintf(out, "No branch pattern specified and this repository appears to be a fork so defaulting the branch patterns to run CI / CD on to: %s\n", branchPattern)
} else {
branchPattern = DefaultBranchPattern
}
}
if credentials == "" {
credentials = DefaultJenkinsCredentialsPrefix + "git"
}
_, err = jenk.GetCredential(credentials)
if err != nil {
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gitInfo.Host)
user, err := config.PickServerUserAuth(server, "user name for the Jenkins Pipeline", false)
if err != nil {
return err
}
err = jenk.CreateCredential(credentials, user.Username, user.ApiToken)
if err != nil {
return fmt.Errorf("error creating jenkins credential %s at %s %v", credentials, jenk.BaseURL(), err)
}
}
org := gitInfo.Organisation
folder, err := jenk.GetJob(org)
if err != nil {
// could not find folder so lets try create it
jobUrl := util.UrlJoin(jenk.BaseURL(), jenk.GetJobURLPath(org))
folderXml := CreateFolderXml(jobUrl, org)
//fmt.Fprintf(out, "XML: %s\n", folderXml)
err = jenk.CreateJobWithXML(folderXml, org)
if err != nil {
return fmt.Errorf("Failed to create the %s folder in jenkins: %s", org, err)
}
//fmt.Fprintf(out, "Created Jenkins folder: %s\n", org)
} else {
c := folder.Class
if c != "com.cloudbees.hudson.plugins.folder.Folder" {
fmt.Fprintf(out, "Warning the folder %s is of class %s", org, c)
}
}
projectXml := CreateMultiBranchProjectXml(gitInfo, gitProvider, credentials, branchPattern, jenkinsfile)
jobName := gitInfo.Name
job, err := jenk.GetJobByPath(org, jobName)
if err == nil {
if failIfExists {
return fmt.Errorf("Job already exists in Jenkins at %s", job.Url)
} else {
fmt.Fprintf(out, "Job already exists in Jenkins at %s\n", job.Url)
}
} else {
//fmt.Fprintf(out, "Creating MultiBranchProject %s from XML: %s\n", jobName, projectXml)
err = jenk.CreateFolderJobWithXML(projectXml, org, jobName)
if err != nil {
return fmt.Errorf("Failed to create MultiBranchProject job %s in folder %s due to: %s", jobName, org, err)
}
job, err = jenk.GetJobByPath(org, jobName)
if err != nil {
return fmt.Errorf("Failed to find the MultiBranchProject job %s in folder %s due to: %s", jobName, org, err)
}
fmt.Fprintf(out, "Created Jenkins Project: %s\n", util.ColorInfo(job.Url))
params := url.Values{}
err = jenk.Build(job, params)
if err != nil {
return fmt.Errorf("Failed to trigger job %s due to %s", job.Url, err)
}
}
// register the webhook
suffix := gitProvider.JenkinsWebHookPath(gitURL, "")
webhookUrl := util.UrlJoin(jenk.BaseURL(), suffix)
webhook := &gits.GitWebHookArguments{
Owner: gitInfo.Organisation,
Repo: gitInfo.Name,
URL: webhookUrl,
}
return gitProvider.CreateWebHook(webhook)
}