Example #1
0
// Submit the current code review request.
//
// The "args" parameter contains all of the command line arguments that followed the subcommand.
func submitReview(repo repository.Repo, args []string) error {
	submitFlagSet.Parse(args)
	args = submitFlagSet.Args()

	if *submitMerge && *submitRebase {
		return errors.New("Only one of --merge or --rebase is allowed.")
	}

	var r *review.Review
	var err error
	if len(args) > 1 {
		return errors.New("Only accepting a single review is supported.")
	}
	if len(args) == 1 {
		r, err = review.Get(repo, args[0])
	} else {
		r, err = review.GetCurrent(repo)
	}

	if err != nil {
		return fmt.Errorf("Failed to load the review: %v\n", err)
	}
	if r == nil {
		return errors.New("There is no matching review.")
	}

	if r.Submitted {
		return errors.New("The review has already been submitted.")
	}

	if !*submitTBR && (r.Resolved == nil || !*r.Resolved) {
		return errors.New("Not submitting as the review has not yet been accepted.")
	}

	target := r.Request.TargetRef
	if err := repo.VerifyGitRef(target); err != nil {
		return err
	}
	source, err := r.GetHeadCommit()
	if err != nil {
		return err
	}

	isAncestor, err := repo.IsAncestor(target, source)
	if err != nil {
		return err
	}
	if !isAncestor {
		return errors.New("Refusing to submit a non-fast-forward review. First merge the target ref.")
	}

	if !(*submitRebase || *submitMerge || *submitFastForward) {
		submitStrategy, err := repo.GetSubmitStrategy()
		if err != nil {
			return err
		}
		if submitStrategy == "merge" && !*submitRebase && !*submitFastForward {
			*submitMerge = true
		}
		if submitStrategy == "rebase" && !*submitMerge && !*submitFastForward {
			*submitRebase = true
		}
		if submitStrategy == "fast-forward" && !*submitRebase && !*submitMerge {
			*submitFastForward = true
		}
	}

	if *submitRebase {
		if err := r.Rebase(*submitArchive); err != nil {
			return err
		}
		source, err = r.GetHeadCommit()
		if err != nil {
			return err
		}
	}

	if err := repo.SwitchToRef(target); err != nil {
		return err
	}
	if *submitMerge {
		submitMessage := fmt.Sprintf("Submitting review %.12s", r.Revision)
		return repo.MergeRef(source, false, submitMessage, r.Request.Description)
	} else {
		return repo.MergeRef(source, true)
	}
}