예제 #1
0
파일: index.py 프로젝트: Tigge/critic
def updateBranch(user_name, repository_name, name, old, new, multiple):
    repository = gitutils.Repository.fromName(db, repository_name)

    processCommits(repository_name, new)

    try:
        branch = dbutils.Branch.fromName(db, repository, name)
        base_branch_id = branch.base.id if branch.base else None
    except:
        raise IndexException, "The branch '%s' is not in the database!  (This should never happen.)" % name

    if branch.head.sha1 != old:
        if new == branch.head.sha1:
            # This is what we think the ref ought to be already.  Do nothing,
            # and let the repository "catch up."
            return
        else:
            data = { "name": name,
                     "old": old[:8],
                     "new": new[:8],
                     "current": branch.head.sha1[:8] }

            message = """CONFUSED!  Git thinks %(name)s points to %(old)s, but Critic thinks it points to %(current)s.  Rejecting push since it would only makes matters worse.  To resolve this problem, use

  git push critic %(current)s:%(name)s

to resynchronize the Git repository with Critic's database.""" % data

            raise IndexException, textutils.reflow(message, line_length=80 - len("remote: "))

    cursor = db.cursor()
    cursor.execute("SELECT remote, remote_name, forced FROM trackedbranches WHERE repository=%s AND local_name=%s AND NOT disabled", (repository.id, name))
    row = cursor.fetchone()

    if row:
        remote, remote_name, forced = row
        tracked_branch = "%s in %s" % (remote_name, remote)

        assert not forced or not name.startswith("r/")

        if user_name != configuration.base.SYSTEM_USER_NAME:
            raise IndexException, """\
The branch '%s' is set up to track '%s' in
  %s
Please don't push it manually to this repository.""" % (name, remote_name, remote)
        elif not name.startswith("r/"):
            conflicting = repository.revlist([branch.head.sha1], [new])
            added = repository.revlist([new], [branch.head.sha1])

            if conflicting:
                if forced:
                    if branch.base is None:
                        cursor.executemany("""DELETE FROM reachable
                                                    USING commits
                                                    WHERE reachable.branch=%s
                                                      AND reachable.commit=commits.id
                                                      AND commits.sha1=%s""",
                                           [(branch.id, sha1) for sha1 in conflicting])
                    else:
                        output = "Non-fast-forward update detected; deleting and recreating branch."

                        deleteBranch(repository.name, branch.name)
                        createBranch(None, repository, branch.name, new)

                        return output
                else:
                    raise IndexException, """\
Rejecting non-fast-forward update of branch.  To perform the update, you
can delete the branch using
  git push critic :%s
first, and then repeat this push.""" % name

            cursor.executemany("""INSERT INTO reachable (branch, commit)
                                       SELECT %s, commits.id
                                         FROM commits
                                        WHERE sha1=%s""",
                               [(branch.id, sha1) for sha1 in added])

            new_head = gitutils.Commit.fromSHA1(db, repository, new)

            cursor.execute("UPDATE branches SET head=%s WHERE id=%s",
                           (new_head.getId(db), branch.id))

            output = ""

            if conflicting: output += "Pruned %d conflicting commits." % len(conflicting)
            if added: output += "\nAdded %d new commits." % len(added)

            return output.strip() if output else None
    else:
        tracked_branch = False

    user = getUser(db, user_name)

    if isinstance(user, str):
        if not tracked_branch:
            commit = gitutils.Commit.fromSHA1(db, repository, new)
            user = dbutils.User.fromId(db, commit.committer.getUserId(db))
        else:
            user = dbutils.User(0, configuration.base.SYSTEM_USER_NAME, configuration.base.SYSTEM_USER_EMAIL, "Critic System", "current")

    cursor.execute("SELECT id FROM reviews WHERE branch=%s", (branch.id,))
    row = cursor.fetchone()

    is_review = bool(row)

    if is_review:
        if multiple:
            raise IndexException, """\
Refusing to update review in push of multiple refs.  Please push one
review branch at a time."""

        review_id = row[0]

        cursor.execute("""SELECT id, old_head, old_upstream, new_upstream, uid, branch
                            FROM reviewrebases
                           WHERE review=%s AND new_head IS NULL""",
                       (review_id,))
        row = cursor.fetchone()

        if row:
            if tracked_branch:
                raise IndexException, "Refusing to perform a review rebase via an automatic update."

            rebase_id, old_head_id, old_upstream_id, new_upstream_id, rebaser_id, onto_branch = row

            review = dbutils.Review.fromId(db, review_id)
            rebaser = dbutils.User.fromId(db, rebaser_id)

            if isinstance(user, dbutils.User):
                if rebaser.id != user.id:
                    if user_name == configuration.base.SYSTEM_USER_NAME:
                        user = rebaser
                    else:
                        raise IndexException, """\
This review is currently being rebased by
  %s <%s>
and can't be otherwise updated right now.""" % (rebaser.fullname, rebaser.email)
            else:
                assert user == configuration.base.SYSTEM_USER_NAME
                user = rebaser

            old_head = gitutils.Commit.fromId(db, repository, old_head_id)
            old_commitset = log.commitset.CommitSet(review.branch.commits)

            if old_head.sha1 != old:
                raise IndexException, """\
Unexpected error.  The branch appears to have been updated since your
rebase was prepared.  You need to cancel the rebase via the review
front-page and then try again, and/or report a bug about this error."""

            if old_upstream_id is not None:
                new_head = gitutils.Commit.fromSHA1(db, repository, new)

                old_upstream = gitutils.Commit.fromId(db, repository, old_upstream_id)

                if new_upstream_id is not None:
                    new_upstream = gitutils.Commit.fromId(db, repository, new_upstream_id)
                else:
                    if len(new_head.parents) != 1:
                        raise IndexException, "Invalid rebase: New head can't be a merge commit."

                    new_upstream = gitutils.Commit.fromSHA1(db, repository, new_head.parents[0])

                    if new_upstream in old_commitset.getTails():
                        old_upstream = new_upstream = None
            else:
                old_upstream = None

            if old_upstream:
                if old_upstream.sha1 != repository.mergebase([old_upstream.sha1, new_upstream.sha1]):
                    raise IndexException, """\
Invalid rebase: The new upstream commit is not a descendant of the old
upstream commit.  You may want to cancel the rebase via the review
front-page, and prepare another one specifying the correct new
upstream commit; or rebase the branch onto the new upstream specified
and then push that instead."""
                if new_upstream.sha1 != repository.mergebase([new_upstream.sha1, new]):
                    raise IndexException, """\
Invalid rebase: The new upstream commit you specified when the rebase
was prepared is not an ancestor of the commit now pushed.  You may want
to cancel the rebase via the review front-page, and prepare another one
specifying the correct new upstream commit; or rebase the branch onto
the new upstream specified and then push that instead."""

                old_upstream_name = repository.findInterestingTag(db, old_upstream.sha1) or old_upstream.sha1
                new_upstream_name = repository.findInterestingTag(db, new_upstream.sha1) or new_upstream.sha1

                if onto_branch:
                    merged_thing = "branch '%s'" % onto_branch
                else:
                    merged_thing = "commit '%s'" % new_upstream_name

                merge_sha1 = repository.run('commit-tree', new_head.tree, '-p', old_head.sha1, '-p', new_upstream.sha1,
                                            input="""\
Merge %s into %s

This commit was generated automatically by Critic as an equivalent merge
to the rebase of the commits

  %s..%s

onto the %s.""" % (merged_thing, review.branch.name, old_upstream_name, old_head.sha1, merged_thing),
                                            env={ 'GIT_AUTHOR_NAME': user.fullname,
                                                  'GIT_AUTHOR_EMAIL': user.email,
                                                  'GIT_COMMITTER_NAME': user.fullname,
                                                  'GIT_COMMITTER_EMAIL': user.email }).strip()
                merge = gitutils.Commit.fromSHA1(db, repository, merge_sha1)

                gituser_id = merge.author.getGitUserId(db)

                cursor.execute("""INSERT INTO commits (sha1, author_gituser, commit_gituser, author_time, commit_time)
                                       VALUES (%s, %s, %s, %s, %s)
                                    RETURNING id""",
                               (merge_sha1, gituser_id, gituser_id, timestamp(merge.author.time), timestamp(merge.committer.time)))
                merge.id = cursor.fetchone()[0]

                cursor.executemany("INSERT INTO edges (parent, child) VALUES (%s, %s)",
                                   [(old_head.getId(db), merge.id),
                                    (new_upstream.getId(db), merge.id)])

                # Have to commit to make the new commit available to other DB
                # sessions right away, specifically so that the changeset
                # creation server can see it.
                db.commit()

                cursor.execute("""UPDATE reviewrebases
                                     SET old_head=%s, new_head=%s, new_upstream=%s
                                   WHERE review=%s AND new_head IS NULL""",
                               (merge.id, new_head.getId(db), new_upstream.getId(db), review.id))

                new_sha1s = repository.revlist([new], [new_upstream.sha1], '--topo-order')
                rebased_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in new_sha1s]
                reachable_values = [(review.branch.id, sha1) for sha1 in new_sha1s]

                cursor.execute("INSERT INTO previousreachable (rebase, commit) SELECT %s, commit FROM reachable WHERE branch=%s", (rebase_id, review.branch.id))
                cursor.execute("DELETE FROM reachable WHERE branch=%s", (review.branch.id,))
                cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, commits.id FROM commits WHERE commits.sha1=%s", reachable_values)
                cursor.execute("UPDATE branches SET head=%s WHERE id=%s", (gitutils.Commit.fromSHA1(db, repository, new).getId(db), review.branch.id))

                pending_mails = []

                recipients = review.getRecipients(db)
                for to_user in recipients:
                    pending_mails.extend(review_mail.sendReviewRebased(db, user, to_user, recipients, review, new_upstream_name, rebased_commits, onto_branch))

                print "Rebase performed."

                review_utils.addCommitsToReview(db, user, review, [merge], pending_mails=pending_mails, silent_if_empty=set([merge]), full_merges=set([merge]))

                repository.keepalive(merge)
            else:
                old_commitset = log.commitset.CommitSet(review.branch.commits)
                new_sha1s = repository.revlist([new], old_commitset.getTails(), '--topo-order')

                if old_head.sha1 in new_sha1s:
                    raise IndexException, """\
Invalid history rewrite: Old head of the branch reachable from the
pushed ref; no history rewrite performed.  (Cancel the rebase via
the review front-page if you've changed your mind.)"""

                for new_sha1 in new_sha1s:
                    new_head = gitutils.Commit.fromSHA1(db, repository, new_sha1)
                    if new_head.tree == old_head.tree: break
                else:
                    raise IndexException, """\
Invalid history rewrite: No commit on the rebased branch references
the same tree as the old head of the branch."""

                cursor.execute("""UPDATE reviewrebases
                                     SET new_head=%s
                                   WHERE review=%s AND new_head IS NULL""",
                               (new_head.getId(db), review.id))

                rebased_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in repository.revlist([new_head], old_commitset.getTails(), '--topo-order')]
                new_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in repository.revlist([new], [new_head], '--topo-order')]
                reachable_values = [(review.branch.id, sha1) for sha1 in new_sha1s]

                cursor.execute("INSERT INTO previousreachable (rebase, commit) SELECT %s, commit FROM reachable WHERE branch=%s", (rebase_id, review.branch.id))
                cursor.execute("DELETE FROM reachable WHERE branch=%s", (review.branch.id,))
                cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, commits.id FROM commits WHERE commits.sha1=%s", reachable_values)
                cursor.execute("UPDATE branches SET head=%s WHERE id=%s", (gitutils.Commit.fromSHA1(db, repository, new).getId(db), review.branch.id))

                pending_mails = []

                recipients = review.getRecipients(db)
                for to_user in recipients:
                    pending_mails.extend(review_mail.sendReviewRebased(db, user, to_user, recipients, review, None, rebased_commits))

                print "History rewrite performed."

                if new_commits:
                    review_utils.addCommitsToReview(db, user, review, new_commits, pending_mails=pending_mails)
                else:
                    review_mail.sendPendingMails(pending_mails)

                repository.run('update-ref', 'refs/keepalive/%s' % old, old)

            return True
        elif old != repository.mergebase([old, new]):
            raise IndexException, "Rejecting non-fast-forward update of review branch."
    elif old != repository.mergebase([old, new]):
        raise IndexException, """\
Rejecting non-fast-forward update of branch.  To perform the update, you
can delete the branch using
  git push critic :%s
first, and then repeat this push.""" % name

    cursor.execute("SELECT id FROM branches WHERE repository=%s AND base IS NULL ORDER BY id ASC LIMIT 1", (repository.id,))
    root_branch_id = cursor.fetchone()[0]

    def isreachable(sha1):
        #if rescan: return False
        #if is_review: cursor.execute("SELECT 1 FROM commits, reachable, branches WHERE commits.sha1=%s AND commits.id=reachable.commit AND reachable.branch=branches.id AND branches.repository=%s", [sha1, repository.id])
        if is_review and sha1 == branch.tail: return True
        if base_branch_id: cursor.execute("SELECT 1 FROM commits, reachable WHERE commits.sha1=%s AND commits.id=reachable.commit AND reachable.branch IN (%s, %s, %s)", [sha1, branch.id, base_branch_id, root_branch_id])
        else: cursor.execute("SELECT 1 FROM commits, reachable WHERE commits.sha1=%s AND commits.id=reachable.commit AND reachable.branch IN (%s, %s)", [sha1, branch.id, root_branch_id])
        return cursor.fetchone() is not None

    stack = [new]
    commits = set()
    commit_list = []
    processed = set()
    count = 0

    while stack:
        sha1 = stack.pop()

        count += 1
        if (count % 1000) == 0:
            stdout.write(".")
            if (count % 10000) == 0:
                stdout.write("\n")
            stdout.flush()

        if sha1 not in commits and not isreachable(sha1):
            commits.add(sha1)
            commit_list.append(sha1)

            #if is_review:
            #    stack.append(gitutils.Commit.fromSHA1(repository, sha1).parents[0])
            #else:
            stack.extend([parent_sha1 for parent_sha1 in gitutils.Commit.fromSHA1(db, repository, sha1).parents if parent_sha1 not in processed])

        processed.add(sha1)

    branch = dbutils.Branch.fromName(db, repository, name)
    review = dbutils.Review.fromBranch(db, branch)

    if review:
        if review.state != "open":
            raise IndexException, """\
The review is closed and can't be extended.  You need to reopen it at
%s
before you can add commits to it.""" % review.getURL(db, user, 2)

        all_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in reversed(commit_list)]

        tails = CommitSet(all_commits).getTails()

        if old not in tails:
            raise IndexException, """\
Push rejected; would break the review.

It looks like some of the pushed commits are reachable from the
repository's main branch, and thus consequently the commits currently
included in the review are too.

Perhaps you should request a new review of the follow-up commits?"""

        review_utils.addCommitsToReview(db, user, review, all_commits, commitset=commits, tracked_branch=tracked_branch)

    reachable_values = [(branch.id, sha1) for sha1 in reversed(commit_list) if sha1 in commits]

    cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, commits.id FROM commits WHERE commits.sha1=%s", reachable_values)
    cursor.execute("UPDATE branches SET head=%s WHERE id=%s", (gitutils.Commit.fromSHA1(db, repository, new).getId(db), branch.id))

    db.commit()

    if configuration.extensions.ENABLED and review:
        extensions.executeProcessCommits(db, user, review, all_commits, gitutils.Commit.fromSHA1(db, repository, old), gitutils.Commit.fromSHA1(db, repository, new), stdout)
예제 #2
0
    def process(self, db, user, review_id, remark=None):
        cursor = db.cursor()
        profiler = profiling.Profiler()

        profiler.check("start")

        review = dbutils.Review.fromId(db, review_id, load_commits=False)

        profiler.check("create review")

        was_accepted = review.state == "open" and review.accepted(db)

        profiler.check("accepted before")

        if remark:
            chain_id = createCommentChain(db, user, review, 'note')
            createComment(db, user, chain_id, remark, first=True)
        else:
            chain_id = None

        # Create a batch that groups all submitted changes together.
        cursor.execute("INSERT INTO batches (review, uid, comment) VALUES (%s, %s, %s) RETURNING id", (review.id, user.id, chain_id))
        batch_id = cursor.fetchone()[0]

        profiler.check("batches++")

        # Reject all draft file approvals where the affected review file isn't in
        # the state it was in when the change was drafted.
        cursor.execute("""UPDATE reviewfilechanges
                             SET state='rejected',
                                 time=now()
                            FROM reviewfiles
                           WHERE reviewfiles.review=%s
                             AND reviewfiles.id=reviewfilechanges.file
                             AND reviewfilechanges.uid=%s
                             AND reviewfilechanges.state='draft'
                             AND reviewfilechanges.from!=reviewfiles.state""",
                       (review.id, user.id))

        profiler.check("reviewfilechanges reject state changes")

        # Then perform the remaining draft file approvals by updating the state of
        # the corresponding review file.
        cursor.execute("""UPDATE reviewfiles
                             SET state='reviewed',
                                 reviewer=reviewfilechanges.uid,
                                 time=now()
                            FROM reviewfilechanges
                           WHERE reviewfiles.review=%s
                             AND reviewfilechanges.uid=%s
                             AND reviewfilechanges.state='draft'
                             AND reviewfilechanges.file=reviewfiles.id
                             AND reviewfilechanges.from=reviewfiles.state
                             AND reviewfilechanges.to='reviewed'""",
                       (review.id, user.id))

        profiler.check("reviewfiles pending=>reviewed")

        # Then perform the remaining draft file disapprovals by updating the state
        # of the corresponding review file.
        cursor.execute("""UPDATE reviewfiles
                             SET state='pending',
                                 reviewer=NULL,
                                 time=now()
                            FROM reviewfilechanges
                           WHERE reviewfiles.review=%s
                             AND reviewfilechanges.uid=%s
                             AND reviewfilechanges.state='draft'
                             AND reviewfilechanges.file=reviewfiles.id
                             AND reviewfilechanges.from=reviewfiles.state
                             AND reviewfilechanges.to='pending'""",
                       (review.id, user.id))

        profiler.check("reviewfiles reviewed=>pending")

        # Finally change the state of just performed approvals from draft to
        # 'performed'.
        cursor.execute("""UPDATE reviewfilechanges
                             SET batch=%s,
                                 state='performed',
                                 time=now()
                            FROM reviewfiles
                           WHERE reviewfiles.review=%s
                             AND reviewfiles.id=reviewfilechanges.file
                             AND reviewfilechanges.uid=%s
                             AND reviewfilechanges.state='draft'
                             AND reviewfilechanges.to=reviewfiles.state""",
                       (batch_id, review.id, user.id))

        profiler.check("reviewfilechanges draft=>performed")

        # Find all chains with draft comments being submitted that the current user
        # isn't associated with via the commentchainusers table, and associate the
        # user with them.
        cursor.execute("""SELECT DISTINCT commentchains.id, commentchainusers.uid IS NULL
                            FROM commentchains
                            JOIN comments ON (comments.chain=commentchains.id)
                 LEFT OUTER JOIN commentchainusers ON (commentchainusers.chain=commentchains.id
                                                   AND commentchainusers.uid=comments.uid)
                           WHERE commentchains.review=%s
                             AND comments.uid=%s
                             AND comments.state='draft'""",
                       (review.id, user.id))

        for chain_id, need_associate in cursor.fetchall():
            if need_associate:
                cursor.execute("INSERT INTO commentchainusers (chain, uid) VALUES (%s, %s)", (chain_id, user.id))

        profiler.check("commentchainusers++")

        # Find all chains with draft comments being submitted and add a record for
        # every user associated with the chain to read the comment.
        cursor.execute("""INSERT
                            INTO commentstoread (uid, comment)
                          SELECT commentchainusers.uid, comments.id
                            FROM commentchains, commentchainusers, comments
                           WHERE commentchains.review=%s
                             AND commentchainusers.chain=commentchains.id
                             AND commentchainusers.uid!=comments.uid
                             AND comments.chain=commentchains.id
                             AND comments.uid=%s
                             AND comments.state='draft'""",
                       (review.id, user.id))

        profiler.check("commentstoread++")

        # Associate all users associated with a draft comment chain to
        # the review (if they weren't already.)
        cursor.execute("""SELECT DISTINCT commentchainusers.uid
                            FROM commentchains
                            JOIN commentchainusers ON (commentchainusers.chain=commentchains.id)
                 LEFT OUTER JOIN reviewusers ON (reviewusers.review=commentchains.review AND reviewusers.uid=commentchainusers.uid)
                           WHERE commentchains.review=%s
                             AND commentchains.uid=%s
                             AND commentchains.state='draft'
                             AND reviewusers.uid IS NULL""",
                       (review.id, user.id))

        for (user_id,) in cursor.fetchall():
            cursor.execute("INSERT INTO reviewusers (review, uid) VALUES (%s, %s)", (review.id, user_id))

        # Change state on all draft commentchains by the user in the review to 'open'.
        cursor.execute("""UPDATE commentchains
                             SET batch=%s,
                                 state='open',
                                 time=now()
                           WHERE commentchains.review=%s
                             AND commentchains.uid=%s
                             AND commentchains.state='draft'""",
                       (batch_id, review.id, user.id))

        profiler.check("commentchains draft=>open")

        # Reject all draft comment chain changes where the affected comment chain
        # isn't in the state it was in when the change was drafted.
        cursor.execute("""UPDATE commentchainchanges
                             SET state='rejected',
                                 time=now()
                            FROM commentchains
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.from_state IS NOT NULL
                             AND (commentchainchanges.from_state!=commentchains.state
                               OR commentchainchanges.from_last_commit!=commentchains.last_commit)""",
                       (review.id, user.id))

        profiler.check("commentchainchanges reject state changes")

        # Reject all draft comment chain changes where the affected comment chain
        # type isn't what it was in when the change was drafted.
        cursor.execute("""UPDATE commentchainchanges
                             SET state='rejected',
                                 time=now()
                            FROM commentchains
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.from_type IS NOT NULL
                             AND commentchainchanges.from_type!=commentchains.type""",
                       (review.id, user.id))

        profiler.check("commentchainchanges reject type changes")

        # Then perform the remaining draft comment chain changes by updating the
        # state of the corresponding comment chain.

        # Perform open->closed changes, including setting 'closed_by'.
        cursor.execute("""UPDATE commentchains
                             SET state='closed',
                                 closed_by=commentchainchanges.uid
                            FROM commentchainchanges
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.to_state='closed'""",
                       (review.id, user.id))

        profiler.check("commentchains closed")

        # Perform (closed|addressed)->open changes, including resetting 'closed_by' and
        # 'addressed_by' to NULL.
        cursor.execute("""UPDATE commentchains
                             SET state='open',
                                 last_commit=commentchainchanges.to_last_commit,
                                 closed_by=NULL,
                                 addressed_by=NULL
                            FROM commentchainchanges
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.to_state='open'""",
                       (review.id, user.id))

        profiler.check("commentchains reopen")

        # Perform type changes.
        cursor.execute("""UPDATE commentchains
                             SET type=commentchainchanges.to_type
                            FROM commentchainchanges
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.from_type=commentchains.type
                             AND commentchainchanges.to_type IS NOT NULL""",
                       (review.id, user.id))

        profiler.check("commentchains type change")

        # Finally change the state of just performed changes from draft to
        # 'performed'.
        cursor.execute("""UPDATE commentchainchanges
                             SET batch=%s,
                                 state='performed',
                                 time=now()
                            FROM commentchains
                           WHERE commentchains.review=%s
                             AND commentchainchanges.chain=commentchains.id
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'""",
                       (batch_id, review.id, user.id))

        profiler.check("commentchainchanges draft=>performed")

        # Change state on all draft commentchainlines by the user in the review to 'current'.
        cursor.execute("""UPDATE commentchainlines
                             SET state='current',
                                 time=now()
                            FROM commentchains
                           WHERE commentchains.review=%s
                             AND commentchainlines.chain=commentchains.id
                             AND commentchainlines.uid=%s
                             AND commentchainlines.state='draft'""",
                       (review.id, user.id))

        profiler.check("commentchainlines draft=>current")

        # Change state on all draft comments by the user in the review to 'current'.
        cursor.execute("""UPDATE comments
                             SET batch=%s,
                                 state='current',
                                 time=now()
                            FROM commentchains
                           WHERE commentchains.review=%s
                             AND comments.chain=commentchains.id
                             AND comments.uid=%s
                             AND comments.state='draft'""",
                       (batch_id, review.id, user.id))

        profiler.check("comments draft=>current")

        # Associate the submitting user with the review if he isn't already.
        cursor.execute("SELECT 1 FROM reviewusers WHERE review=%s AND uid=%s", (review.id, user.id))
        if not cursor.fetchone():
            cursor.execute("INSERT INTO reviewusers (review, uid) VALUES (%s, %s)", (review.id, user.id))

        generate_emails = profiler.start("generate emails")

        is_accepted = review.state == "open" and review.accepted(db)
        pending_mails = generateMailsForBatch(db, batch_id, was_accepted, is_accepted, profiler=profiler)

        generate_emails.stop()

        review.incrementSerial(db)
        db.commit()

        profiler.check("commit transaction")

        sendPendingMails(pending_mails)

        profiler.check("finished")

        if user.getPreference(db, "debug.profiling.submitChanges"):
            return OperationResult(serial=review.serial, profiling=profiler.output())
        else:
            return OperationResult(serial=review.serial)
예제 #3
0
    def process(self, db, user, review_id, remark=None):
        cursor = db.cursor()
        profiler = profiling.Profiler()

        profiler.check("start")

        review = dbutils.Review.fromId(db, review_id)

        profiler.check("create review")

        was_accepted = review.state == "open" and review.accepted(db)

        profiler.check("accepted before")

        if remark and remark.strip():
            chain_id = createCommentChain(db, user, review, 'note')
            createComment(db, user, chain_id, remark, first=True)
        else:
            chain_id = None

        # Create a batch that groups all submitted changes together.
        cursor.execute(
            "INSERT INTO batches (review, uid, comment) VALUES (%s, %s, %s) RETURNING id",
            (review.id, user.id, chain_id))
        batch_id = cursor.fetchone()[0]

        profiler.check("batches++")

        # Reject all draft file approvals where the affected review file isn't in
        # the state it was in when the change was drafted.
        cursor.execute(
            """UPDATE reviewfilechanges
                             SET state='rejected',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND file IN (SELECT id
                                            FROM reviewfiles
                                            JOIN reviewfilechanges ON (reviewfilechanges.file=reviewfiles.id)
                                           WHERE reviewfiles.review=%s
                                             AND reviewfilechanges.uid=%s
                                             AND reviewfilechanges.state='draft'
                                             AND reviewfilechanges.from_state!=reviewfiles.state)""",
            (user.id, review.id, user.id))

        profiler.check("reviewfilechanges reject state changes")

        # Then perform the remaining draft file approvals by updating the state of
        # the corresponding review file.
        cursor.execute(
            """UPDATE reviewfiles
                             SET state='reviewed',
                                 reviewer=%s,
                                 time=now()
                           WHERE review=%s
                             AND id IN (SELECT file
                                          FROM reviewfilechanges
                                         WHERE uid=%s
                                           AND state='draft'
                                           AND to_state='reviewed')""",
            (user.id, review.id, user.id))

        profiler.check("reviewfiles pending=>reviewed")

        # Then perform the remaining draft file disapprovals by updating the state
        # of the corresponding review file.
        cursor.execute(
            """UPDATE reviewfiles
                             SET state='pending',
                                 reviewer=NULL,
                                 time=now()
                           WHERE review=%s
                             AND id IN (SELECT file
                                          FROM reviewfilechanges
                                         WHERE uid=%s
                                           AND state='draft'
                                           AND to_state='pending')""",
            (review.id, user.id))

        profiler.check("reviewfiles reviewed=>pending")

        # Finally change the state of just performed approvals from draft to
        # 'performed'.
        cursor.execute(
            """UPDATE reviewfilechanges
                             SET batch=%s,
                                 state='performed',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND file IN (SELECT id
                                            FROM reviewfiles
                                           WHERE reviewfiles.review=%s)""",
            (batch_id, user.id, review.id))

        profiler.check("reviewfilechanges draft=>performed")

        # Find all chains with draft comments being submitted that the current user
        # isn't associated with via the commentchainusers table, and associate the
        # user with them.
        cursor.execute(
            """SELECT DISTINCT commentchains.id, commentchainusers.uid IS NULL
                            FROM commentchains
                            JOIN comments ON (comments.chain=commentchains.id)
                 LEFT OUTER JOIN commentchainusers ON (commentchainusers.chain=commentchains.id
                                                   AND commentchainusers.uid=comments.uid)
                           WHERE commentchains.review=%s
                             AND comments.uid=%s
                             AND comments.state='draft'""",
            (review.id, user.id))

        for chain_id, need_associate in cursor.fetchall():
            if need_associate:
                cursor.execute(
                    "INSERT INTO commentchainusers (chain, uid) VALUES (%s, %s)",
                    (chain_id, user.id))

        profiler.check("commentchainusers++")

        # Find all chains with draft comments being submitted and add a record for
        # every user associated with the chain to read the comment.
        cursor.execute(
            """INSERT
                            INTO commentstoread (uid, comment)
                          SELECT commentchainusers.uid, comments.id
                            FROM commentchains, commentchainusers, comments
                           WHERE commentchains.review=%s
                             AND commentchainusers.chain=commentchains.id
                             AND commentchainusers.uid!=comments.uid
                             AND comments.chain=commentchains.id
                             AND comments.uid=%s
                             AND comments.state='draft'""",
            (review.id, user.id))

        profiler.check("commentstoread++")

        # Associate all users associated with a draft comment chain to
        # the review (if they weren't already.)
        cursor.execute(
            """SELECT DISTINCT commentchainusers.uid
                            FROM commentchains
                            JOIN commentchainusers ON (commentchainusers.chain=commentchains.id)
                 LEFT OUTER JOIN reviewusers ON (reviewusers.review=commentchains.review AND reviewusers.uid=commentchainusers.uid)
                           WHERE commentchains.review=%s
                             AND commentchains.uid=%s
                             AND commentchains.state='draft'
                             AND reviewusers.uid IS NULL""",
            (review.id, user.id))

        for (user_id, ) in cursor.fetchall():
            cursor.execute(
                "INSERT INTO reviewusers (review, uid) VALUES (%s, %s)",
                (review.id, user_id))

        # Change state on all draft commentchains by the user in the review to 'open'.
        cursor.execute(
            """UPDATE commentchains
                             SET batch=%s,
                                 state='open',
                                 time=now()
                           WHERE commentchains.review=%s
                             AND commentchains.uid=%s
                             AND commentchains.state='draft'""",
            (batch_id, review.id, user.id))

        profiler.check("commentchains draft=>open")

        # Reject all draft comment chain changes where the affected comment
        # chain isn't in the state it was in when the change was drafted, or has
        # been morphed into a note since the change was drafted.
        cursor.execute(
            """UPDATE commentchainchanges
                             SET state='rejected',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND from_state IS NOT NULL
                             AND chain IN (SELECT id
                                             FROM commentchains
                                             JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id
                                                                      AND (commentchainchanges.from_state!=commentchains.state
                                                                        OR commentchainchanges.from_last_commit!=commentchains.last_commit
                                                                        OR commentchains.type!='issue'))
                                            WHERE commentchains.review=%s
                                              AND commentchainchanges.uid=%s
                                              AND commentchainchanges.state='draft')""",
            (user.id, review.id, user.id))

        profiler.check("commentchainchanges reject state changes")

        # Reject all draft comment chain changes where the affected comment chain
        # type isn't what it was in when the change was drafted.
        cursor.execute(
            """UPDATE commentchainchanges
                             SET state='rejected',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND from_type IS NOT NULL
                             AND chain IN (SELECT id
                                             FROM commentchains
                                             JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id
                                                                      AND commentchainchanges.from_type!=commentchains.type)
                                            WHERE commentchains.review=%s
                                              AND commentchainchanges.uid=%s
                                              AND commentchainchanges.state='draft')""",
            (user.id, review.id, user.id))

        profiler.check("commentchainchanges reject type changes")

        # Reject all draft comment chain changes where the affected comment chain
        # addressed_by isn't what it was in when the change was drafted.
        cursor.execute(
            """UPDATE commentchainchanges
                             SET state='rejected',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND from_addressed_by IS NOT NULL
                             AND chain IN (SELECT id
                                             FROM commentchains
                                             JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id
                                                                      AND commentchainchanges.from_addressed_by!=commentchains.addressed_by)
                                            WHERE commentchains.review=%s
                                              AND commentchainchanges.uid=%s
                                              AND commentchainchanges.state='draft')""",
            (user.id, review.id, user.id))

        profiler.check("commentchainchanges reject addressed_by changes")

        # Then perform the remaining draft comment chain changes by updating the
        # state of the corresponding comment chain.

        # Perform open->closed changes, including setting 'closed_by'.
        cursor.execute(
            """UPDATE commentchains
                             SET state='closed',
                                 closed_by=%s
                           WHERE review=%s
                             AND id IN (SELECT chain
                                          FROM commentchainchanges
                                         WHERE uid=%s
                                           AND state='draft'
                                           AND to_state='closed')""",
            (user.id, review.id, user.id))

        profiler.check("commentchains closed")

        # Perform (closed|addressed)->open changes, including resetting 'closed_by' and
        # 'addressed_by' to NULL.
        cursor.execute(
            """SELECT commentchainchanges.to_last_commit, commentchains.id
                            FROM commentchains
                            JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id)
                           WHERE commentchains.review=%s
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.to_state='open'""",
            (review.id, user.id))
        cursor.executemany(
            """UPDATE commentchains
                                 SET state='open',
                                     last_commit=%s,
                                     closed_by=NULL,
                                     addressed_by=NULL
                               WHERE id=%s""", cursor.fetchall())

        profiler.check("commentchains reopen")

        # Perform addressed->addressed changes, i.e. updating 'addressed_by'.
        cursor.execute(
            """SELECT commentchainchanges.to_addressed_by, commentchains.id
                            FROM commentchains
                            JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id)
                           WHERE commentchains.review=%s
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.to_addressed_by IS NOT NULL""",
            (review.id, user.id))
        cursor.executemany(
            """UPDATE commentchains
                                 SET addressed_by=%s
                               WHERE id=%s""", cursor.fetchall())

        profiler.check("commentchains reopen (partial)")

        # Perform type changes.
        cursor.execute(
            """SELECT commentchainchanges.to_type, commentchains.id
                            FROM commentchains
                            JOIN commentchainchanges ON (commentchainchanges.chain=commentchains.id)
                           WHERE commentchains.review=%s
                             AND commentchainchanges.uid=%s
                             AND commentchainchanges.state='draft'
                             AND commentchainchanges.to_type IS NOT NULL""",
            (review.id, user.id))
        cursor.executemany(
            """UPDATE commentchains
                                 SET type=%s
                               WHERE id=%s""", cursor.fetchall())

        profiler.check("commentchains type change")

        # Finally change the state of just performed changes from draft to
        # 'performed'.
        cursor.execute(
            """UPDATE commentchainchanges
                             SET batch=%s,
                                 state='performed',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND chain IN (SELECT id
                                             FROM commentchains
                                            WHERE review=%s)""",
            (batch_id, user.id, review.id))

        profiler.check("commentchainchanges draft=>performed")

        # Change state on all draft commentchainlines by the user in the review to 'current'.
        cursor.execute(
            """UPDATE commentchainlines
                             SET state='current',
                                 time=now()
                           WHERE uid=%s
                             AND state='draft'
                             AND chain IN (SELECT id
                                             FROM commentchains
                                            WHERE review=%s)""",
            (user.id, review.id))

        profiler.check("commentchainlines draft=>current")

        # Change state on all draft comments by the user in the review to 'current'.
        cursor.execute(
            """UPDATE comments
                             SET batch=%s,
                                 state='current',
                                 time=now()
                           WHERE comments.uid=%s
                             AND comments.state='draft'
                             AND chain IN (SELECT id
                                             FROM commentchains
                                            WHERE review=%s)""",
            (batch_id, user.id, review.id))

        profiler.check("comments draft=>current")

        # Associate the submitting user with the review if he isn't already.
        cursor.execute("SELECT 1 FROM reviewusers WHERE review=%s AND uid=%s",
                       (review.id, user.id))
        if not cursor.fetchone():
            cursor.execute(
                "INSERT INTO reviewusers (review, uid) VALUES (%s, %s)",
                (review.id, user.id))

        generate_emails = profiler.start("generate emails")

        is_accepted = review.state == "open" and review.accepted(db)
        pending_mails = generateMailsForBatch(db,
                                              batch_id,
                                              was_accepted,
                                              is_accepted,
                                              profiler=profiler)

        generate_emails.stop()

        review.incrementSerial(db)
        db.commit()

        profiler.check("commit transaction")

        sendPendingMails(pending_mails)

        profiler.check("finished")

        if user.getPreference(db, "debug.profiling.submitChanges"):
            return OperationResult(batch_id=batch_id,
                                   serial=review.serial,
                                   profiling=profiler.output())
        else:
            return OperationResult(batch_id=batch_id, serial=review.serial)