forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
96 lines (90 loc) · 3.16 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
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"
"strings"
)
// ImportProject imports a MultiBranchProject into Jeknins for the given git URL
func ImportProject(out io.Writer, jenk *gojenkins.Jenkins, gitURL string, 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 credentials == "" {
credentials = strings.TrimSuffix(DefaultJenkinsCredentialsPrefix+gitInfo.Host, ".com")
}
_, 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")
if err != nil {
return err
}
err = jenk.CreateCredential(credentials, user.Username, user.ApiToken)
if err != nil {
return fmt.Errorf("error creating jenkins credential %s %v", "bdd-test", 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, credentials)
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)
}