func HasUpdate()

in app/registry/update_branch.go [31:99]


func HasUpdate(ctx context.Context, gerritService gerrit.ServiceInterface, cb *codebase.Codebase, mrTarget string) (bool, []string, *version.Version, error) {
	gerritProject, err := gerritService.GetProject(ctx, cb.Name)
	if service.IsErrNotFound(err) {
		return false, []string{}, nil, nil
	}

	if err != nil {
		return false, nil, nil, errors.Wrap(err, "unable to get gerrit project")
	}

	branches := UpdateBranches(gerritProject.Status.Branches)

	registryVersion := BranchVersion(cb.Spec.DefaultBranch)
	if cb.Spec.BranchToCopyInDefaultBranch != "" {
		registryVersion = BranchVersion(cb.Spec.BranchToCopyInDefaultBranch)
	}

	if registryVersion.Original() == "0" {
		registryVersion = LowestVersion(branches)
	}

	mrs, err := gerritService.GetMergeRequestByProject(ctx, gerritProject.Spec.Name)
	if err != nil {
		return false, branches, nil, errors.Wrap(err, "unable to get merge requests")
	}

	branchesDict := make(map[string]string)
	for _, br := range branches {
		branchesDict[br] = br
	}

	hasOpenMR := false
	for _, mr := range mrs {
		if mr.Labels[MRLabelTarget] != mrTarget {
			continue
		}

		if mr.Status.Value == gerrit.StatusNew {
			hasOpenMR = true
		}

		if mr.Status.Value == gerrit.StatusMerged {
			mergedBranchVersion := BranchVersion(mr.Spec.SourceBranch)
			if registryVersion.LessThan(mergedBranchVersion) {
				registryVersion = mergedBranchVersion
			}

			delete(branchesDict, mr.Spec.SourceBranch)
		}
	}

	if hasOpenMR {
		return false, nil, registryVersion, nil
	}

	branches = []string{}
	for _, br := range branchesDict {
		if registryVersion.LessThan(BranchVersion(br)) {
			branches = append(branches, br)
		}
	}

	if len(branches) == 0 {
		return false, branches, registryVersion, nil
	}

	sort.Sort(SortByVersion(branches))
	return true, branches, registryVersion, nil
}