func main() { flag.Parse() jenkins, err := gojenkins.CreateJenkins(*proto+"://"+*host+"/", *user, *token).Init() if err != nil { panic("Something Went Wrong: " + err.Error()) } fmt.Printf("jenkins: %+v\n", jenkins) job, err := jenkins.GetJob(*jobName) if err != nil { panic("Job Does Not Exist: " + err.Error()) } build, err := job.GetLastSuccessfulBuild() if err != nil { panic("Build Does Not Exist: " + err.Error()) } fmt.Printf("LastSuccessfulBuild: %+v\n", build) build.GetBuildNumber() fmt.Printf("GetBuildNumber: %+v\n", build.GetBuildNumber()) }
func newJenkinsProxy(url, username, password string) (BuildServerProxy, error) { fmt.Printf("Connecting to Jenkins instance: %s\n", url) proxy := &JenkinsProxy{} j, err := gojenkins.CreateJenkins(url, username, password).Init() if err != nil { return proxy, err } proxy.client = j return proxy, nil }
func main() { defer glog.Flush() flag.Parse() err := ui.Init() if err != nil { panic(err) } defer ui.Close() jenkins, err := gojenkins.CreateJenkins(*jenkinsUrl).Init() if err != nil { panic(err) } ls, infobox, redbox, yellowbox, greenbox := initWidgets() if *filter != "" { filterBuildName = regexp.MustCompile(*filter) } evt := make(chan tm.Event) go func() { for { evt <- tm.PollEvent() } }() ticker := time.NewTicker(*sampleInterval).C for { select { case e := <-evt: if e.Type == tm.EventKey && e.Ch == 'q' { return } case <-ticker: // alway resize, strange behaviour with tm.EventResize resizeUI(ls) ls.Items = ls.Items[:0] resetBox(infobox, redbox, yellowbox, greenbox) jenkinsPoll(jenkins, infobox, ls, redbox, yellowbox, greenbox) ui.Render(ui.Body) } } }
func periodicallyRecheckConnection(cfg data.GoobernetConfig) { go func() { for { select { case <-time.After(1 * time.Minute): fmt.Fprintf(os.Stderr, "Retrying Jenkins connection\n") j, err := gojenkins.CreateJenkins(cfg.JenkinsUrl, cfg.JenkinsUsername, cfg.JenkinsPassword).Init() if err == nil { fmt.Printf("Connected to Jenkins. Small miracles.\n") jenkinsProxy, ok := Proxy.(*JenkinsProxy) if ok { jenkinsProxy.client = j } return } } } }() }
func addJenkinsClient(user, passwd string) *gojenkins.Jenkins { var jenkinsHost, jenkinsPort string if os.Getenv("ENVIRONMENT") == "production" { jenkinsHost = os.Getenv("JENKINS_HOST") jenkinsPort = os.Getenv("JENKINS_PORT") if jenkinsHost == "" || jenkinsPort == "" { log.Errorf("jenkinsHost:%s, jenkinsPort:%s", jenkinsHost, jenkinsPort) return nil } } else { jenkinsHost = "10.10.11.207" jenkinsPort = "8080" } url := "http://" + jenkinsHost + ":" + jenkinsPort jenkins, err := gojenkins.CreateJenkins(url, user, passwd).Init() if err != nil { log.Errorf("user:%s connecting jenkins server:%s:%s failed with error:%s", user, jenkinsHost, jenkinsPort, err) return nil } log.Infof("user:%s connect jenkins server:%s:%s is OK!", user, jenkinsHost, jenkinsPort) return jenkins }
func jenkinsParser(jenkinsName string, jenkinsURL string, username string, password string) ([]Job, error) { var resultJobs []Job var jenkins *gojenkins.Jenkins var err error if username != "" { jenkins, err = gojenkins.CreateJenkins(jenkinsURL, username, password).Init() } else { jenkins, err = gojenkins.CreateJenkins(jenkinsURL).Init() } if err != nil { return nil, err } fmt.Printf("%s: Connected\n", jenkinsName) status, err := jenkins.Poll() if status != 200 { log.Fatalf("Jenkins replied with status %v ; check your credentials for %v", status, jenkinsURL) return nil, err } fmt.Printf("%s: Polled\n", jenkinsName) jobs, err := jenkins.GetAllJobs() if err != nil { return nil, err } fmt.Printf("%s: GetAllJobs done (%d jobs)\n", jenkinsName, len(jobs)) path := xmlpath.MustCompile("//scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url") for _, job := range jobs { jobConfig, err := job.GetConfig() if err != nil { fmt.Printf("error: %v", err) continue } b := bytes.NewBufferString(jobConfig) root, err := xmlpath.Parse(b) if err != nil { fmt.Printf("error: %v", err) continue } if scmUrl, ok := path.String(root); ok { build, _ := job.GetLastBuild() var lastBuildStatus int if build == nil { lastBuildStatus = 2 } else { if build.IsGood() { lastBuildStatus = 0 } else { lastBuildStatus = 1 } } resultJobs = append(resultJobs, Job{JenkinsName: jenkinsName, Name: job.GetName(), Url: job.GetDetails().URL, ScmUrl: scmUrl, LastBuildStatus: lastBuildStatus}) } } return resultJobs, nil }