~tarmac-control-tower/tarmac/main

« back to all changes in this revision

Viewing changes to tarmac/plugins/commitmessage.py

  • Committer: Paride Legovini
  • Date: 2019-06-26 18:55:59 UTC
  • Revision ID: paride.legovini@canonical.com-20190626185559-4s54947lxwoq5m7i
Repository converted to git

https://code.launchpad.net/tarmac/+git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Paul Hummer
2
 
# This file is part of Tarmac.
3
 
#
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
6
 
# published by
7
 
# the Free Software Foundation.
8
 
#
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.
13
 
#
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/>.
16
 
 
17
 
'''Tarmac plugin for enforcing a commit message format.'''
18
 
from tarmac.hooks import tarmac_hooks
19
 
from tarmac.plugins import TarmacPlugin
20
 
 
21
 
 
22
 
class CommitMessageTemplate(TarmacPlugin):
23
 
    '''Tarmac plugin for modifying the commit message to adhere to a template.
24
 
 
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
27
 
    template.
28
 
    '''
29
 
 
30
 
    def run(self, command, target, source, proposal):
31
 
    # pylint: disable-msg=W0613
32
 
 
33
 
        try:
34
 
            template = target.config.commit_message_template
35
 
            template = template.replace('<', '%(').replace('>', ')s')
36
 
        except AttributeError:
37
 
            return
38
 
 
39
 
        proposal.commit_message = self.render(
40
 
            template, CommitMessageTemplateInfo(proposal))
41
 
 
42
 
    def render(self, template, info):
43
 
        """Render a template using the given information."""
44
 
        return template % info
45
 
 
46
 
 
47
 
class CommitMessageTemplateInfo(object):
48
 
 
49
 
    def __init__(self, proposal):
50
 
        self._proposal = proposal
51
 
 
52
 
    def __getitem__(self, name):
53
 
        """Return the value of the attribute with the given name.
54
 
 
55
 
        Never returns None; the empty string is substituted.
56
 
        """
57
 
        if name.startswith('_'):
58
 
            value = None
59
 
        else:
60
 
            value = getattr(self, name, None)
61
 
        return ("" if value is None else value)
62
 
 
63
 
    @property
64
 
    def author(self):
65
 
        """The display name of the source branch author."""
66
 
        return self._proposal.source_branch.owner.display_name
67
 
 
68
 
    @property
69
 
    def author_nick(self):
70
 
        """The short name of the source branch author."""
71
 
        return self._proposal.source_branch.owner.name
72
 
 
73
 
    @property
74
 
    def commit_message(self):
75
 
        """The commit message set in the merge proposal."""
76
 
        return self._proposal.commit_message
77
 
 
78
 
    @property
79
 
    def reviewer(self):
80
 
        """The display name of the merge proposal reviewer.
81
 
 
82
 
        This is the person that marked the *whole* proposal as
83
 
        approved, not necessarily an individual voter.
84
 
        """
85
 
        return self._proposal.reviewer.display_name
86
 
 
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':
91
 
                yield vote.reviewer
92
 
 
93
 
    @property
94
 
    def approved_by(self):
95
 
        """Display name of reviewers who approved the review."""
96
 
        return ", ".join(
97
 
            reviewer.display_name for reviewer in self._get_approvers())
98
 
 
99
 
    @property
100
 
    def approved_by_nicks(self):
101
 
        """Short names of reviewers who approved the review."""
102
 
        return ",".join(
103
 
            reviewer.name for reviewer in self._get_approvers())
104
 
 
105
 
    @property
106
 
    def bugs_fixed(self):
107
 
        return ",".join(
108
 
            str(bug.id) for bug in self._proposal.source_branch.linked_bugs)
109
 
 
110
 
    @property
111
 
    def review_url(self):
112
 
        return self._proposal.web_link
113
 
 
114
 
 
115
 
tarmac_hooks['tarmac_pre_commit'].hook(CommitMessageTemplate(),
116
 
    'Commit messsage template editor.')