1
# Copyright 2009 Paul Hummer
2
# This file is part of Tarmac.
4
# Tarmac is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License version 3 as
7
# the Free Software Foundation.
9
# Tarmac is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with Tarmac. If not, see <http://www.gnu.org/licenses/>.
17
'''Tarmac plugin for enforcing a commit message format.'''
18
from tarmac.hooks import tarmac_hooks
19
from tarmac.plugins import TarmacPlugin
22
class CommitMessageTemplate(TarmacPlugin):
23
'''Tarmac plugin for modifying the commit message to adhere to a template.
25
This plugin checks for a commit_message_template specific to the project.
26
If to finds one, it will locally change the commit message to use the
30
def run(self, command, target, source, proposal):
31
# pylint: disable-msg=W0613
34
template = target.config.commit_message_template
35
template = template.replace('<', '%(').replace('>', ')s')
36
except AttributeError:
39
proposal.commit_message = self.render(
40
template, CommitMessageTemplateInfo(proposal))
42
def render(self, template, info):
43
"""Render a template using the given information."""
44
return template % info
47
class CommitMessageTemplateInfo(object):
49
def __init__(self, proposal):
50
self._proposal = proposal
52
def __getitem__(self, name):
53
"""Return the value of the attribute with the given name.
55
Never returns None; the empty string is substituted.
57
if name.startswith('_'):
60
value = getattr(self, name, None)
61
return ("" if value is None else value)
65
"""The display name of the source branch author."""
66
return self._proposal.source_branch.owner.display_name
69
def author_nick(self):
70
"""The short name of the source branch author."""
71
return self._proposal.source_branch.owner.name
74
def commit_message(self):
75
"""The commit message set in the merge proposal."""
76
return self._proposal.commit_message
80
"""The display name of the merge proposal reviewer.
82
This is the person that marked the *whole* proposal as
83
approved, not necessarily an individual voter.
85
return self._proposal.reviewer.display_name
87
def _get_approvers(self):
88
for vote in self._proposal.votes:
89
comment = vote.comment
90
if comment is not None and comment.vote == u'Approve':
94
def approved_by(self):
95
"""Display name of reviewers who approved the review."""
97
reviewer.display_name for reviewer in self._get_approvers())
100
def approved_by_nicks(self):
101
"""Short names of reviewers who approved the review."""
103
reviewer.name for reviewer in self._get_approvers())
106
def bugs_fixed(self):
108
str(bug.id) for bug in self._proposal.source_branch.linked_bugs)
111
def review_url(self):
112
return self._proposal.web_link
115
tarmac_hooks['tarmac_pre_commit'].hook(CommitMessageTemplate(),
116
'Commit messsage template editor.')