1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Assign karma for code domain activity."""
6
from canonical.database.sqlbase import block_implicit_flushes
7
from lp.code.enums import BranchMergeProposalStatus
8
from lp.registry.interfaces.person import IPerson
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')
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')
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')
36
target.assignKarma(user, 'codereviewcomment')
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)
45
in_progress_states = (
46
BranchMergeProposalStatus.WORK_IN_PROGRESS,
47
BranchMergeProposalStatus.NEEDS_REVIEW)
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')
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')
60
target.assignKarma(user, 'branchmergerejected')
62
# Only care about approved and rejected right now.