func jenkinsPoll(jenkins *gojenkins.Jenkins, infobox *ui.Par, ls *ui.List, redbox *ui.Par, yellowbox *ui.Par, greenbox *ui.Par) {
	defer func() {
		if r := recover(); r != nil {
			infobox.Border.FgColor = ui.ColorRed
			//err := fmt.Errorf("%v", r)
			infobox.Text += " : /!\\ Jenkins is currently unreachable"
		}
	}()
	const layout = "Mon Jan 2 15:04:05"
	infobox.Border.FgColor = ui.ColorWhite
	infobox.Text = "Refresh at " + time.Now().Format(layout)
	jenkins.Poll()
	jobs, err := jenkins.GetAllJobs()
	if err != nil {
		infobox.Text = "Error with getAllJobs " + fmt.Sprintf("%s", err)
	}
	for _, k := range jobs {
		addJob(ls, k, redbox, yellowbox, greenbox)
	}
}
Exemple #2
0
func getLatestBuildID(jc *gojenkins.Jenkins, jobname string) int64 {
	job, err := jc.GetJob(jobname)
	if err != nil {
		log.Errorf(err.Error())
		return -1
	}

	ids, err := job.GetAllBuildIds()
	if err != nil {
		log.Errorf(err.Error())
		return -1
	}
	/*
		for _, id := range ids {
			fmt.Println(id.Number)
		}
	*/
	if len(ids) != 0 {
		log.Debugf("%s latest id:%d", jobname, ids[0].Number)
		return ids[0].Number
	} else {
		return -1
	}
}
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
}