Пример #1
0
// ProcessQueue processes the next job.
func ProcessQueue() {
	next, ok := <-Queue
	if !ok { // channel closed
		return
	}

	err := next()
	if err != nil {
		klog.Criticalf("job: unable to process job: %v", err)
	}
}
Пример #2
0
func (a *ABF) handleResponse(resp *http.Response, testing bool) error {

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	var result map[string]interface{}

	err = json.Unmarshal(body, &result)
	if err != nil {
		return err
	}

	var lists []interface{}

	err = dig.Get(&result, &lists, "build_lists")
	if err != nil {
		return err
	}

	for _, v := range lists {
		asserted := v.(map[string]interface{})
		id := dig.Uint64(&asserted, "id")
		strID := "[" + to.String(id) + "]"

		var num int
		err = models.DB.Model(&models.List{}).Where("integration_name = ? AND integration_one LIKE ?", "abf", "%"+strID+"%").Count(&num).Error
		if err != nil && err != gorm.ErrRecordNotFound {
			klog.Criticalf("abf: couldn't check db for existing (list %v): %v", id, err)
			continue
		}

		if num != 0 {
			klog.Infof("abf: ignoring list, already processed (list %v)", id)
			continue
		}

		json, err := a.getJSONList(id)
		if err != nil {
			klog.Criticalf("abf: couldn't retrieve the build list JSON (list %v): %v", id, err)
			continue
		}

		// check if arch is on whitelist
		if abfArchWhitelistSet != nil && !abfArchWhitelistSet.Contains(dig.String(&json, "arch", "name")) {
			klog.Infof("abf: ignoring list, arch not on whitelist (list %v)", id)
			continue
		}

		// check if platform is on whitelist
		if !abfPlatformsSet.Contains(dig.String(&json, "save_to_repository", "platform", "name")) {
			klog.Infof("abf: ignoring list, platform not on whitelist (list %v)", id)
			continue
		}

		// *check for duplicates before continuing
		// *we only check for duplicates in the same platform; different platforms have different conditions

		// check for duplicates
		var duplicate models.List
		err = models.DB.Where("platform = ? AND integration_two = ? AND stage_current <> ?",
			dig.String(&json, "save_to_repository", "platform", "name"),
			dig.String(&json, "commit_hash"),
			models.ListStageFinished).First(&duplicate).Error

		if err != nil && err != gorm.ErrRecordNotFound {
			klog.Criticalf("abf: couldn't check db for duplicates (list %v): %v", id, err)
			continue
		}

		if err == nil { // we had no problem finding a duplicate, so handle and continue
			duplicate.IntegrationOne += ";" + strID
			duplicate.Variants += ";" + dig.String(&json, "arch", "name")

			if testing { // we got this from the build completed (not in testing) list
				// send id to testing
				go a.sendToTesting(id)
			}

			err = models.DB.Save(&duplicate).Error
			if err != nil {
				klog.Criticalf("abf: couldn't save duplicate modification to %v (list %v): %v", duplicate.ID, id, err)
			}

			pkgs := a.makePkgList(json)
			for _, listpkg := range pkgs {
				listpkg.ListID = duplicate.ID

				err = models.DB.Create(&listpkg).Error
				if err != nil {
					klog.Criticalf("abf: couldn't save new list package to %v (list %v): %v", duplicate.ID, id, err)
				}
			}

			// add a link to it
			newLink := models.ListLink{
				ListID: duplicate.ID,
				Name:   fmt.Sprintf("Build List for %v", dig.String(&json, "arch", "name")),
				URL:    fmt.Sprintf("%s/build_lists/%v", abfURL, id),
			}
			if err := models.DB.Create(&newLink).Error; err != nil {
				klog.Criticalf("abf: couldn't save new list link to %v (list %v): %v", duplicate.ID, id, err)
			}

			// ok, we're done here
			continue
		}

		list, err := a.makeBuildList(json)
		if err != nil {
			klog.Criticalf("abf: couldn't make new list (list %v): %v\n", id, err)
			continue
		}

		if testing {
			// Now send it to testing
			go a.sendToTesting(id)
		}

		if err := models.DB.Create(list).Error; err != nil {
			klog.Criticalf("abf: couldn't create new list in db (list %v): %v", id, err)
			continue
		}
	}

	return nil
}
Пример #3
0
// makeDiff shells out to the cmd line git to get a patch. Unfortunately, this
// may need tweaking given the modifications that OpenMandriva have done to
// their ABF.
func (a *ABF) makeDiff(gitURL, fromHash, toHash string) string {
	// make sure it's not disabled
	if !abfGitDiff {
		return "Diff creation disabled."
	}

	// ugh, looks like we'll have to do this the sadly hard way
	tmpdir, err := ioutil.TempDir("", "kahinah_")
	if err != nil {
		return "Error creating directory for diff creation: " + err.Error()
	}

	defer os.RemoveAll(tmpdir)

	if strings.Contains(gitURL, "@") {
		gitURL = gitURL[:strings.Index(gitURL, "//")+2] + gitURL[strings.Index(gitURL, "@")+1:]
	}

	urlToUse := gitURL
	if abfGitDiffSSH {
		urlToUse = strings.Replace("git@"+gitURL[strings.Index(gitURL, "//")+2:], "/", ":", 1)
	}

	// reusable bytes output
	var b bytes.Buffer

	gitclonecmd := exec.Command("git", "clone", urlToUse, tmpdir)
	gitclonecmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") // git 2.3+
	gitclonecmd.Stderr = &b
	gitclonecmd.Stdout = &b
	gitclonecmd.Start()

	gitresult := make(chan error, 1)
	go func() {
		gitresult <- gitclonecmd.Wait()
	}()
	select {
	case <-time.After(10 * time.Minute):
		if err := gitclonecmd.Process.Kill(); err != nil {
			klog.Criticalf("abf: calling git to kill failed: %v", err)
		}
		<-gitresult // allow goroutine to exit
		klog.Critical("abf: git process called to kill")
	case err := <-gitresult:
		if err != nil { // git exited with non-zero status
			klog.Criticalf("abf: calling git failed: %v", err)
			return "Repository could not be cloned for diff: " + err.Error()
		}
	}

	if fromHash == "" || fromHash == toHash {
		gitdiffcmd := exec.Command("git", "show", "--format=fuller", "--patch-with-stat", "--summary", toHash)
		gitdiffcmd.Dir = tmpdir

		gitdiff, err := gitdiffcmd.CombinedOutput()
		if err != nil {
			klog.Criticalf("abf: calling git diff failed: %v", err)
			return "No diff available: " + err.Error()
		}

		return fmt.Sprintf("$ git show --format=fuller --patch-with-stat --summary %s\n\n%s", toHash, string(gitdiff))
	}

	gitdiffcmd := exec.Command("git", "diff", "--patch-with-stat", "--summary", fromHash+".."+toHash)
	gitdiffcmd.Dir = tmpdir

	gitdiff, err := gitdiffcmd.CombinedOutput()
	if err != nil {
		fmt.Printf("%s", gitdiff)
		return "No diff available: " + err.Error()
	}

	return fmt.Sprintf("$ git diff --patch-with-stat --summary %s\n\n%s", fromHash+".."+toHash, string(gitdiff))
}