~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/subscribers/karma.py

  • Committer: Curtis Hovey
  • Date: 2011-12-09 22:10:24 UTC
  • mto: This revision was merged to the branch mainline in revision 14488.
  • Revision ID: curtis.hovey@canonical.com-20111209221024-rw2m8oimugj897my
Move code karma subscribers to lp.code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Assign karma for code domain activity."""
 
5
 
 
6
from canonical.database.sqlbase import block_implicit_flushes
 
7
from lp.code.enums import BranchMergeProposalStatus
 
8
from lp.registry.interfaces.person import IPerson
 
9
 
 
10
 
 
11
@block_implicit_flushes
 
12
def branch_created(branch, event):
 
13
    """Assign karma to the user who registered the branch."""
 
14
    branch.target.assignKarma(branch.registrant, 'branchcreated')
 
15
 
 
16
 
 
17
@block_implicit_flushes
 
18
def branch_merge_proposed(proposal, event):
 
19
    """Assign karma to the user who proposed the merge."""
 
20
    proposal.source_branch.target.assignKarma(
 
21
        proposal.registrant, 'branchmergeproposed')
 
22
 
 
23
 
 
24
@block_implicit_flushes
 
25
def code_review_comment_added(code_review_comment, event):
 
26
    """Assign karma to the user who commented on the review."""
 
27
    proposal = code_review_comment.branch_merge_proposal
 
28
    target = proposal.source_branch.target
 
29
    # If the user is commenting on their own proposal, then they don't
 
30
    # count as a reviewer for that proposal.
 
31
    user = code_review_comment.message.owner
 
32
    reviewer = user.inTeam(proposal.target_branch.code_reviewer)
 
33
    if reviewer and user != proposal.registrant:
 
34
        target.assignKarma(user, 'codereviewreviewercomment')
 
35
    else:
 
36
        target.assignKarma(user, 'codereviewcomment')
 
37
 
 
38
 
 
39
@block_implicit_flushes
 
40
def branch_merge_status_changed(proposal, event):
 
41
    """Assign karma to the user who approved the merge."""
 
42
    target = proposal.source_branch.target
 
43
    user = IPerson(event.user)
 
44
 
 
45
    in_progress_states = (
 
46
        BranchMergeProposalStatus.WORK_IN_PROGRESS,
 
47
        BranchMergeProposalStatus.NEEDS_REVIEW)
 
48
 
 
49
    if ((event.to_state == BranchMergeProposalStatus.CODE_APPROVED) and
 
50
        (event.from_state in (in_progress_states))):
 
51
        if user == proposal.registrant:
 
52
            target.assignKarma(user, 'branchmergeapprovedown')
 
53
        else:
 
54
            target.assignKarma(user, 'branchmergeapproved')
 
55
    elif ((event.to_state == BranchMergeProposalStatus.REJECTED) and
 
56
          (event.from_state in (in_progress_states))):
 
57
        if user == proposal.registrant:
 
58
            target.assignKarma(user, 'branchmergerejectedown')
 
59
        else:
 
60
            target.assignKarma(user, 'branchmergerejected')
 
61
    else:
 
62
        # Only care about approved and rejected right now.
 
63
        pass