##// END OF EJS Templates
api: fixed creation of pull request with custom rules of reviewers + proper diff creation.
marcink -
r2859:2c7ccc1c stable
parent child Browse files
Show More
@@ -573,7 +573,7 b' def comment_pull_request('
573 @jsonrpc_method()
573 @jsonrpc_method()
574 def create_pull_request(
574 def create_pull_request(
575 request, apiuser, source_repo, target_repo, source_ref, target_ref,
575 request, apiuser, source_repo, target_repo, source_ref, target_ref,
576 title, description=Optional(''), reviewers=Optional(None)):
576 title=Optional(''), description=Optional(''), reviewers=Optional(None)):
577 """
577 """
578 Creates a new pull request.
578 Creates a new pull request.
579
579
@@ -594,7 +594,7 b' def create_pull_request('
594 :type source_ref: str
594 :type source_ref: str
595 :param target_ref: Set the target ref name.
595 :param target_ref: Set the target ref name.
596 :type target_ref: str
596 :type target_ref: str
597 :param title: Set the pull request title.
597 :param title: Optionally Set the pull request title, it's generated otherwise
598 :type title: str
598 :type title: str
599 :param description: Set the pull request description.
599 :param description: Set the pull request description.
600 :type description: Optional(str)
600 :type description: Optional(str)
@@ -615,26 +615,32 b' def create_pull_request('
615
615
616 full_source_ref = resolve_ref_or_error(source_ref, source_db_repo)
616 full_source_ref = resolve_ref_or_error(source_ref, source_db_repo)
617 full_target_ref = resolve_ref_or_error(target_ref, target_db_repo)
617 full_target_ref = resolve_ref_or_error(target_ref, target_db_repo)
618
619 source_scm = source_db_repo.scm_instance()
620 target_scm = target_db_repo.scm_instance()
621
618 source_commit = get_commit_or_error(full_source_ref, source_db_repo)
622 source_commit = get_commit_or_error(full_source_ref, source_db_repo)
619 target_commit = get_commit_or_error(full_target_ref, target_db_repo)
623 target_commit = get_commit_or_error(full_target_ref, target_db_repo)
620 source_scm = source_db_repo.scm_instance()
624
621 target_scm = target_db_repo.scm_instance()
625 ancestor = source_scm.get_common_ancestor(
626 source_commit.raw_id, target_commit.raw_id, target_scm)
627 if not ancestor:
628 raise JSONRPCError('no common ancestor found')
629
630 # recalculate target ref based on ancestor
631 target_ref_type, target_ref_name, __ = full_target_ref.split(':')
632 full_target_ref = ':'.join((target_ref_type, target_ref_name, ancestor))
622
633
623 commit_ranges = target_scm.compare(
634 commit_ranges = target_scm.compare(
624 target_commit.raw_id, source_commit.raw_id, source_scm,
635 target_commit.raw_id, source_commit.raw_id, source_scm,
625 merge=True, pre_load=[])
636 merge=True, pre_load=[])
626
637
627 ancestor = target_scm.get_common_ancestor(
628 target_commit.raw_id, source_commit.raw_id, source_scm)
629
630 if not commit_ranges:
638 if not commit_ranges:
631 raise JSONRPCError('no commits found')
639 raise JSONRPCError('no commits found')
632
640
633 if not ancestor:
634 raise JSONRPCError('no common ancestor found')
635
636 reviewer_objects = Optional.extract(reviewers) or []
641 reviewer_objects = Optional.extract(reviewers) or []
637
642
643 # serialize and validate passed in given reviewers
638 if reviewer_objects:
644 if reviewer_objects:
639 schema = ReviewerListSchema()
645 schema = ReviewerListSchema()
640 try:
646 try:
@@ -647,36 +653,44 b' def create_pull_request('
647 user = get_user_or_error(reviewer_object['username'])
653 user = get_user_or_error(reviewer_object['username'])
648 reviewer_object['user_id'] = user.user_id
654 reviewer_object['user_id'] = user.user_id
649
655
650 get_default_reviewers_data, get_validated_reviewers = \
656 get_default_reviewers_data, validate_default_reviewers = \
651 PullRequestModel().get_reviewer_functions()
657 PullRequestModel().get_reviewer_functions()
652
658
659 # recalculate reviewers logic, to make sure we can validate this
653 reviewer_rules = get_default_reviewers_data(
660 reviewer_rules = get_default_reviewers_data(
654 apiuser.get_instance(), source_db_repo,
661 apiuser.get_instance(), source_db_repo,
655 source_commit, target_db_repo, target_commit)
662 source_commit, target_db_repo, target_commit)
656
663
657 # specified rules are later re-validated, thus we can assume users will
664 # now MERGE our given with the calculated
658 # eventually provide those that meet the reviewer criteria.
665 reviewer_objects = reviewer_rules['reviewers'] + reviewer_objects
659 if not reviewer_objects:
660 reviewer_objects = reviewer_rules['reviewers']
661
666
662 try:
667 try:
663 reviewers = get_validated_reviewers(
668 reviewers = validate_default_reviewers(
664 reviewer_objects, reviewer_rules)
669 reviewer_objects, reviewer_rules)
665 except ValueError as e:
670 except ValueError as e:
666 raise JSONRPCError('Reviewers Validation: {}'.format(e))
671 raise JSONRPCError('Reviewers Validation: {}'.format(e))
667
672
668 pull_request_model = PullRequestModel()
673 title = Optional.extract(title)
669 pull_request = pull_request_model.create(
674 if not title:
675 title_source_ref = source_ref.split(':', 2)[1]
676 title = PullRequestModel().generate_pullrequest_title(
677 source=source_repo,
678 source_ref=title_source_ref,
679 target=target_repo
680 )
681 description = Optional.extract(description)
682
683 pull_request = PullRequestModel().create(
670 created_by=apiuser.user_id,
684 created_by=apiuser.user_id,
671 source_repo=source_repo,
685 source_repo=source_repo,
672 source_ref=full_source_ref,
686 source_ref=full_source_ref,
673 target_repo=target_repo,
687 target_repo=target_repo,
674 target_ref=full_target_ref,
688 target_ref=full_target_ref,
675 revisions=reversed(
689 revisions=[commit.raw_id for commit in reversed(commit_ranges)],
676 [commit.raw_id for commit in reversed(commit_ranges)]),
677 reviewers=reviewers,
690 reviewers=reviewers,
678 title=title,
691 title=title,
679 description=Optional.extract(description)
692 description=description,
693 reviewer_data=reviewer_rules,
680 )
694 )
681
695
682 Session().commit()
696 Session().commit()
General Comments 0
You need to be logged in to leave comments. Login now