Ejemplo n.º 1
0
    def as_html(self):
        commit_id = self.commit_data.extra_data.get(COMMIT_ID_KEY)

        if is_parent(self.review_request_details, self.commit_data):
            user = self.request.user
            parent = get_parent_rr(
                self.review_request_details.get_review_request(),
                self.commit_data)
            parent_details = parent.get_draft() or parent
            children = [
                child for child in gen_child_rrs(parent_details, user=user)
                if child.is_accessible_by(user)]

            commit_data = fetch_commit_data(children[-1])
            commit_id = commit_data.extra_data.get(COMMIT_ID_KEY)

        review_request = self.review_request_details.get_review_request()
        repo_path = review_request.repository.path

        if not commit_id:
            logger.error('No commit_id for review request: %d' % (
                review_request.id))
            return ''

        return get_template('mozreview/hg-pull.html').render(Context({
                'commit_id': commit_id,
                'repo_path': repo_path,
        }))
    def _sort_families(self, request, rrs, families=None):
        """Sort ReviewRequest objects into families.

        'families' is a dict with parent ReviewRequest ids as keys.  Each
        value is another dict, with 'parent' mapped to the parent
        ReviewRequest and 'children' mapped to a list of child ReviewRequests
        of that parent.  If 'families' is not None, it is updated in place;
        if 'families' is not given, it is first initialized.  In both cases
        'families' is also returned.

        For each ReviewRequest in rrs, 'families' is updated appropriately
        to assemble a full set of families.
        """
        if families is None:
            families = defaultdict(lambda: dict(parent=None, children={}))

        for rr in rrs:
            if rr.status == ReviewRequest.DISCARDED:
                continue

            if not self.has_access_permissions(request, rr):
                continue

            if is_parent(rr):
                families[rr.id]['parent'] = rr
            else:
                # Some early review requests were orphaned; ignore them.
                try:
                    parent_rr = get_parent_rr(rr)
                except ReviewRequest.DoesNotExist:
                    continue

                families[parent_rr.id]['children'][rr.id] = rr

        return families
Ejemplo n.º 3
0
    def as_html(self):
        user = self.request.user
        parent = get_parent_rr(self.review_request_details.get_review_request())
        parent_details = parent.get_draft(user) or parent

        # If a user can view the parent draft they should also have
        # permission to view every child. We check if the child is
        # accessible anyways in case it has been restricted for other
        # reasons.
        children_details = [
            child for child in gen_child_rrs(parent_details, user=user)
            if child.is_accessible_by(user)]

        autoland_requests = AutolandRequest.objects.filter(
            review_request_id=parent.id).order_by('-autoland_id')

        repo_urls = set()
        latest_autoland_requests = []


        # We would like to fetch the latest AutolandRequest for each
        # different repository.
        for request in autoland_requests:
            if request.repository_url in repo_urls:
                continue

            repo_urls.add(request.repository_url)
            latest_autoland_requests.append(request)

        return get_template('mozreview/commits.html').render(Context({
            'review_request_details': self.review_request_details,
            'parent_details': parent_details,
            'children_details': children_details,
            'latest_autoland_requests': latest_autoland_requests,
        }))
Ejemplo n.º 4
0
    def _sort_families(self, request, rrs, families=None):
        """Sort ReviewRequest objects into families.

        'families' is a dict with parent ReviewRequest ids as keys.  Each
        value is another dict, with 'parent' mapped to the parent
        ReviewRequest and 'children' mapped to a list of child ReviewRequests
        of that parent.  If 'families' is not None, it is updated in place;
        if 'families' is not given, it is first initialized.  In both cases
        'families' is also returned.

        For each ReviewRequest in rrs, 'families' is updated appropriately
        to assemble a full set of families.
        """
        if families is None:
            families = defaultdict(lambda: dict(parent=None, children={}))

        for rr in rrs:
            if rr.status == ReviewRequest.DISCARDED:
                continue

            if not self.has_access_permissions(request, rr):
                continue

            if is_parent(rr):
                families[rr.id]['parent'] = rr
            else:
                # Some early review requests were orphaned; ignore them.
                try:
                    parent_rr = get_parent_rr(rr)
                except ReviewRequest.DoesNotExist:
                    continue

                families[parent_rr.id]['children'][rr.id] = rr

        return families
Ejemplo n.º 5
0
    def as_html(self):
        commit_id = self.commit_data.extra_data.get(COMMIT_ID_KEY)

        if is_parent(self.review_request_details, self.commit_data):
            user = self.request.user
            parent = get_parent_rr(
                self.review_request_details.get_review_request(),
                self.commit_data)
            parent_details = parent.get_draft() or parent
            children = [
                child for child in gen_child_rrs(parent_details, user=user)
                if child.is_accessible_by(user)]

            commit_data = fetch_commit_data(children[-1])
            commit_id = commit_data.extra_data.get(COMMIT_ID_KEY)

        review_request = self.review_request_details.get_review_request()
        repo_path = review_request.repository.path

        if not commit_id:
            logger.error('No commit_id for review request: %d' % (
                review_request.id))
            return ''

        return get_template('mozreview/hg-pull.html').render(Context({
                'commit_id': commit_id,
                'repo_path': repo_path,
        }))
def get_commit_table_context(request, review_request_details):
    """Get the data needed to display the commits table.

    Information provided includes the parent and child review requests,
    as well as autoland information.
    """
    commit_data = fetch_commit_data(review_request_details)

    user = request.user
    parent = get_parent_rr(review_request_details.get_review_request(), commit_data=commit_data)
    parent_details = parent.get_draft(user) or parent

    # If a user can view the parent draft they should also have
    # permission to view every child. We check if the child is
    # accessible anyways in case it has been restricted for other
    # reasons.
    children_details = [
        child for child in gen_child_rrs(parent_details, user=user)
        if child.is_accessible_by(user)]
    n_children = len(children_details)
    current_child_num = prev_child = next_child = None

    if not is_parent(review_request_details, commit_data=commit_data):
        cur_index = children_details.index(review_request_details)
        current_child_num = cur_index + 1
        next_child = (children_details[cur_index + 1]
                      if cur_index + 1 < n_children else None)
        prev_child = (children_details[cur_index - 1]
                      if cur_index - 1 >= 0 else None)

    latest_autoland_requests = []
    try_syntax = ''
    repo_urls = set()
    autoland_requests = AutolandRequest.objects.filter(
        review_request_id=parent.id).order_by('-autoland_id')

    # We would like to fetch the latest AutolandRequest for each
    # different repository.
    for land_request in autoland_requests:
        if land_request.repository_url in repo_urls:
            continue

        repo_urls.add(land_request.repository_url)
        latest_autoland_requests.append(land_request)
        try_syntax = try_syntax or land_request.extra_data.get('try_syntax', '')

    return {
        'review_request_details': review_request_details,
        'parent_details': parent_details,
        'children_details': children_details,
        'num_children': n_children,
        'current_child_num': current_child_num,
        'next_child': next_child,
        'prev_child': prev_child,
        'latest_autoland_requests': latest_autoland_requests,
        'user': user,
        'try_syntax': try_syntax,
    }
    def get(self, request, *args, **kwargs):
        try:
            parent_request = get_parent_rr(ReviewRequest.objects.get(
                id=kwargs[self.uri_object_key]))
        except ReviewRequest.DoesNotExist:
            return DOES_NOT_EXIST
        if parent_request is None:
            return DOES_NOT_EXIST
        if not is_parent(parent_request):
            return NOT_PARENT
        if not parent_request.is_accessible_by(request.user):
            return PERMISSION_DENIED
        if COMMITS_KEY not in parent_request.extra_data:
            logging.error('Parent review request %s missing COMMIT_KEY'
                          % parent_request.id)
            return NOT_PARENT

        result = []
        children = json.loads(parent_request.extra_data[COMMITS_KEY])
        for child in children:
            try:
                child_request = ReviewRequest.objects.get(id=child[1])
            except ReviewRequest.DoesNotExist:
                return DOES_NOT_EXIST
            if not child_request.approved:
                return AUTOLAND_REVIEW_NOT_APPROVED

            reviewers = [
                r.user.username for r in gen_latest_reviews(child_request) if
                r.ship_it and
                r.user != child_request.submitter
            ]

            if not reviewers and child_request.approved:
                # This review request is approved (the repeated check is
                # to ensure this is guaranteed if other parts of the code
                # change) but we have an empty list of reviewers. We'll
                # assume the author has just approved this themself and
                # set r=me
                reviewers.append('me')

            result.append({
                'commit': child[0],
                'id': child[1],
                'reviewers': reviewers,
                'summary': replace_reviewers(child_request.description,
                                             reviewers)
            })

        return 200, {
            'commits': result,
            'total_results': len(result),
            'links': self.get_links(request=request),
        }
Ejemplo n.º 8
0
    def on_draft_changed(self, sender, **kwargs):
        instance = kwargs["instance"]
        rr = instance.get_review_request()

        if is_pushed(instance) and not is_parent(rr):
            parent_rr = get_parent_rr(rr)
            parent_rr_draft = parent_rr.get_draft()

            if parent_rr_draft is None:
                parent_rr_draft = ReviewRequestDraft.create(parent_rr)

            update_parent_rr_reviewers(parent_rr_draft)
Ejemplo n.º 9
0
    def as_html(self):
        user = self.request.user

        commit_data = fetch_commit_data(self.review_request_details)
        commit_id = commit_data.get_for(self.review_request_details,
                                        COMMIT_ID_KEY)

        review_request = self.review_request_details.get_review_request()
        parent = get_parent_rr(review_request)
        parent_details = parent.get_draft(user) or parent

        author = commit_data.extra_data.get(AUTHOR_KEY, None)

        # If a user can view the parent draft they should also have
        # permission to view every child. We check if the child is
        # accessible anyways in case it has been restricted for other
        # reasons.
        children_details = [
            child for child in gen_child_rrs(parent_details, user=user)
            if child.is_accessible_by(user)
        ]

        # Generate the import and pull input field contents
        import_text = pull_text = ""
        repo_path = review_request.repository.path

        if commit_id:
            import_text = "hg import %s/rev/%s" % (repo_path, commit_id)

        last_child_commit_id = commit_id
        if is_parent(self.review_request_details, commit_data=commit_data):
            last_child_commit_data = fetch_commit_data(children_details[-1])
            last_child_commit_id = (
                last_child_commit_data.extra_data.get(COMMIT_ID_KEY))

        pull_text = "hg pull -r %s %s" % (last_child_commit_id, repo_path)

        # Get just the extended commit message details for display
        commit_message_detail = "\n".join(
            self.review_request_details.description.splitlines()[1:]).strip()

        return get_template('mozreview/commit-main.html').render(
            Context({
                'review_request_details': self.review_request_details,
                'parent_details': parent_details,
                'user': user,
                'author': author,
                'pull_text': pull_text,
                'import_text': import_text,
                'commit_message_detail': commit_message_detail,
            }))
Ejemplo n.º 10
0
    def as_html(self):
        user = self.request.user
        parent = get_parent_rr(self.review_request_details.get_review_request())
        parent_details = parent.get_draft() or parent

        # If a user can view the parent draft they should also have
        # permission to view every child. We check if the child is
        # accessible anyways in case it has been restricted for other
        # reasons.
        children_details = [
            child for child in gen_child_rrs(parent_details, user)
            if child.is_accessible_by(user)]

        return get_template('mozreview/commits.html').render(Context({
            'review_request_details': self.review_request_details,
            'parent_details': parent_details,
            'children_details': children_details,
        }))
Ejemplo n.º 11
0
    def get(self, request, *args, **kwargs):
        try:
            parent_request = get_parent_rr(ReviewRequest.objects.get(
                id=kwargs[self.uri_object_key]))
        except ReviewRequest.DoesNotExist:
            return DOES_NOT_EXIST
        if parent_request is None:
            return DOES_NOT_EXIST
        if not is_parent(parent_request):
            return NOT_PARENT
        if not parent_request.is_accessible_by(request.user):
            return PERMISSION_DENIED
        if COMMITS_KEY not in parent_request.extra_data:
            logging.error('Parent review request %s missing COMMIT_KEY'
                          % parent_request.id)
            return NOT_PARENT

        result = []
        children = json.loads(parent_request.extra_data[COMMITS_KEY])
        for child in children:
            try:
                child_request = ReviewRequest.objects.get(id=child[1])
            except ReviewRequest.DoesNotExist:
                return DOES_NOT_EXIST
            if not child_request.approved:
                return AUTOLAND_REVIEW_NOT_APPROVED

            reviewers = map(lambda review: review.user.username,
                            gen_latest_reviews(child_request))
            result.append({
                'commit': child[0],
                'id': child[1],
                'reviewers': reviewers,
                'summary': replace_reviewers(child_request.description,
                                             reviewers)
            })

        return 200, {
            'commits': result,
            'total_results': len(result),
            'links': self.get_links(request=request),
        }
Ejemplo n.º 12
0
    def as_html(self):
        user = self.request.user
        parent = get_parent_rr(self.review_request_details.get_review_request())
        parent_details = parent.get_draft(user) or parent

        # If a user can view the parent draft they should also have
        # permission to view every child. We check if the child is
        # accessible anyways in case it has been restricted for other
        # reasons.
        children_details = [
            child for child in gen_child_rrs(parent_details, user=user)
            if child.is_accessible_by(user)]

        autoland_requests = AutolandRequest.objects.filter(
            review_request_id=parent.id).order_by('-autoland_id')

        repo_urls = set()
        latest_autoland_requests = []


        # We would like to fetch the latest AutolandRequest for each
        # different repository.
        for request in autoland_requests:
            if request.repository_url in repo_urls:
                continue

            repo_urls.add(request.repository_url)
            latest_autoland_requests.append(request)

        return get_template('mozreview/commits.html').render(Context({
            'review_request_details': self.review_request_details,
            'parent_details': parent_details,
            'children_details': children_details,
            'latest_autoland_requests': latest_autoland_requests,
            'user': user
        }))
Ejemplo n.º 13
0
def ensure_parent_draft(draft):
    """Ensure parent draft exists when child has a draft.

    This is intended to be called in the post_save signal for the
    ReviewRequestDraft model and ensure the parent review request
    has a draft if a child draft is saved. We need to do this so
    that the parent may always be published when a child requires
    publishing.

    Particularly we update our own reviewer information in the
    parent to make sure that a reviewer change on a child request
    will create a parent draft - even if the reviewer change does
    not alter the overall set of reviewers for the series.
    """
    rr = draft.get_review_request()

    if is_pushed(draft) and not is_parent(rr):
        parent_rr = get_parent_rr(rr)
        parent_rr_draft = parent_rr.get_draft()

        if parent_rr_draft is None:
            parent_rr_draft = ReviewRequestDraft.create(parent_rr)

        update_parent_rr_reviewers(parent_rr_draft)
def ensure_parent_draft(draft):
    """Ensure parent draft exists when child has a draft.

    This is intended to be called in the post_save signal for the
    ReviewRequestDraft model and ensure the parent review request
    has a draft if a child draft is saved. We need to do this so
    that the parent may always be published when a child requires
    publishing.

    Particularly we update our own reviewer information in the
    parent to make sure that a reviewer change on a child request
    will create a parent draft - even if the reviewer change does
    not alter the overall set of reviewers for the series.
    """
    rr = draft.get_review_request()

    if is_pushed(draft) and not is_parent(rr):
        parent_rr = get_parent_rr(rr)
        parent_rr_draft = parent_rr.get_draft()

        if parent_rr_draft is None:
            parent_rr_draft = ReviewRequestDraft.create(parent_rr)

        update_parent_rr_reviewers(parent_rr_draft)
Ejemplo n.º 15
0
    def get(self, request, *args, **kwargs):
        try:
            parent_request = get_parent_rr(
                ReviewRequest.objects.get(id=kwargs[self.uri_object_key]))
        except ReviewRequest.DoesNotExist:
            return DOES_NOT_EXIST
        if parent_request is None:
            return DOES_NOT_EXIST

        commit_data = fetch_commit_data(parent_request)

        if not is_parent(parent_request, commit_data):
            return NOT_PARENT
        if not parent_request.is_accessible_by(request.user):
            return PERMISSION_DENIED
        if COMMITS_KEY not in commit_data.extra_data:
            logging.error('Parent review request %s missing COMMIT_KEY' %
                          parent_request.id)
            return NOT_PARENT

        result = []
        children = json.loads(commit_data.extra_data[COMMITS_KEY])
        for child in children:
            try:
                child_request = ReviewRequest.objects.get(id=child[1])
            except ReviewRequest.DoesNotExist:
                return DOES_NOT_EXIST
            if not child_request.approved:
                return AUTOLAND_REVIEW_NOT_APPROVED

            reviewers = [
                r.user.username for r in gen_latest_reviews(child_request)
                if r.ship_it and r.user != child_request.submitter
            ]

            if not reviewers and child_request.approved:
                # This review request is approved (the repeated check is
                # to ensure this is guaranteed if other parts of the code
                # change) but we have an empty list of reviewers. We'll
                # assume the author has just approved this themself.
                reviewers.append(child_request.submitter.username)

            # Detect if the commit has been changed since the last review.
            shipit_carryforward = has_shipit_carryforward(child_request)

            result.append({
                'commit':
                child[0],
                'id':
                child[1],
                'reviewers':
                reviewers,
                'shipit_carryforward':
                shipit_carryforward,
                'summary':
                replace_reviewers(child_request.description, reviewers)
            })

        return 200, {
            'commits': result,
            'total_results': len(result),
            'links': self.get_links(request=request),
        }