~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_propose.py

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
 
 
18
import webbrowser
 
19
 
17
20
from bzrlib import (
18
21
    errors,
19
 
    hooks,
20
 
    )
21
 
from bzrlib.lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
 
import webbrowser
24
 
 
25
 
from bzrlib import (
26
22
    msgeditor,
27
 
    )
 
23
)
 
24
from bzrlib.hooks import HookPoint, Hooks
28
25
from bzrlib.plugins.launchpad import (
29
26
    lp_api,
30
27
    lp_registration,
31
 
    )
32
 
""")
33
 
 
34
 
 
35
 
class ProposeMergeHooks(hooks.Hooks):
 
28
)
 
29
 
 
30
from lazr.restfulclient import errors as restful_errors
 
31
 
 
32
 
 
33
class ProposeMergeHooks(Hooks):
36
34
    """Hooks for proposing a merge on Launchpad."""
37
35
 
38
36
    def __init__(self):
39
 
        hooks.Hooks.__init__(self, "bzrlib.plugins.launchpad.lp_propose",
40
 
            "Proposer.hooks")
41
 
        self.add_hook('get_prerequisite',
42
 
            "Return the prerequisite branch for proposing as merge.", (2, 1))
43
 
        self.add_hook('merge_proposal_body',
44
 
            "Return an initial body for the merge proposal message.", (2, 1))
 
37
        Hooks.__init__(self)
 
38
        self.create_hook(
 
39
            HookPoint(
 
40
                'get_prerequisite',
 
41
                "Return the prerequisite branch for proposing as merge.",
 
42
                (2, 1), None),
 
43
        )
 
44
        self.create_hook(
 
45
            HookPoint(
 
46
                'merge_proposal_body',
 
47
                "Return an initial body for the merge proposal message.",
 
48
                (2, 1), None),
 
49
        )
45
50
 
46
51
 
47
52
class Proposer(object):
49
54
    hooks = ProposeMergeHooks()
50
55
 
51
56
    def __init__(self, tree, source_branch, target_branch, message, reviews,
52
 
                 staging=False, approve=False):
 
57
                 staging=False):
53
58
        """Constructor.
54
59
 
55
60
        :param tree: The working tree for the source branch.
59
64
        :param reviews: A list of tuples of reviewer, review type.
60
65
        :param staging: If True, propose the merge against staging instead of
61
66
            production.
62
 
        :param approve: If True, mark the new proposal as approved immediately.
63
 
            This is useful when a project permits some things to be approved
64
 
            by the submitter (e.g. merges between release and deployment
65
 
            branches).
66
67
        """
67
68
        self.tree = tree
68
69
        if staging:
69
70
            lp_instance = 'staging'
70
71
        else:
71
 
            lp_instance = 'production'
 
72
            lp_instance = 'edge'
72
73
        service = lp_registration.LaunchpadService(lp_instance=lp_instance)
73
74
        self.launchpad = lp_api.login(service)
74
75
        self.source_branch = lp_api.LaunchpadBranch.from_bzr(
75
76
            self.launchpad, source_branch)
76
77
        if target_branch is None:
77
 
            self.target_branch = self.source_branch.get_target()
 
78
            self.target_branch = self.source_branch.get_dev_focus()
78
79
        else:
79
80
            self.target_branch = lp_api.LaunchpadBranch.from_bzr(
80
81
                self.launchpad, target_branch)
81
82
        self.commit_message = message
82
 
        # XXX: this is where bug lp:583638 could be tackled.
83
83
        if reviews == []:
84
 
            self.reviews = []
 
84
            target_reviewer = self.target_branch.lp.reviewer
 
85
            if target_reviewer is None:
 
86
                raise errors.BzrCommandError('No reviewer specified')
 
87
            self.reviews = [(target_reviewer, '')]
85
88
        else:
86
89
            self.reviews = [(self.launchpad.people[reviewer], review_type)
87
90
                            for reviewer, review_type in
88
91
                            reviews]
89
 
        self.approve = approve
90
92
 
91
93
    def get_comment(self, prerequisite_branch):
92
94
        """Determine the initial comment for the merge proposal."""
142
144
            if mp.queue_status in ('Merged', 'Rejected'):
143
145
                continue
144
146
            if mp.target_branch.self_link == self.target_branch.lp.self_link:
145
 
                raise errors.BzrCommandError(gettext(
146
 
                    'There is already a branch merge proposal: %s') %
147
 
                    lp_api.canonical_url(mp))
 
147
                raise errors.BzrCommandError(
 
148
                    'There is already a branch merge proposal: %s' %
 
149
                    canonical_url(mp))
148
150
 
149
151
    def _get_prerequisite_branch(self):
150
152
        hooks = self.hooks['get_prerequisite']
157
159
                 'prerequisite_branch': prerequisite_branch})
158
160
        return prerequisite_branch
159
161
 
160
 
    def call_webservice(self, call, *args, **kwargs):
161
 
        """Make a call to the webservice, wrapping failures.
162
 
        
163
 
        :param call: The call to make.
164
 
        :param *args: *args for the call.
165
 
        :param **kwargs: **kwargs for the call.
166
 
        :return: The result of calling call(*args, *kwargs).
167
 
        """
168
 
        from lazr.restfulclient import errors as restful_errors
169
 
        try:
170
 
            return call(*args, **kwargs)
171
 
        except restful_errors.HTTPError, e:
172
 
            error_lines = []
173
 
            for line in e.content.splitlines():
174
 
                if line.startswith('Traceback (most recent call last):'):
175
 
                    break
176
 
                error_lines.append(line)
177
 
            raise Exception(''.join(error_lines))
178
 
 
179
162
    def create_proposal(self):
180
163
        """Perform the submission."""
181
164
        prerequisite_branch = self._get_prerequisite_branch()
191
174
            review_types.append(review_type)
192
175
            reviewers.append(reviewer.self_link)
193
176
        initial_comment = self.get_comment(prerequisite_branch)
194
 
        mp = self.call_webservice(
195
 
            self.source_branch.lp.createMergeProposal,
196
 
            target_branch=self.target_branch.lp,
197
 
            prerequisite_branch=prereq,
198
 
            initial_comment=initial_comment,
199
 
            commit_message=self.commit_message, reviewers=reviewers,
200
 
            review_types=review_types)
201
 
        if self.approve:
202
 
            self.call_webservice(mp.setStatus, status='Approved')
203
 
        webbrowser.open(lp_api.canonical_url(mp))
 
177
        try:
 
178
            mp = self.source_branch.lp.createMergeProposal(
 
179
                target_branch=self.target_branch.lp,
 
180
                prerequisite_branch=prereq,
 
181
                initial_comment=initial_comment,
 
182
                commit_message=self.commit_message, reviewers=reviewers,
 
183
                review_types=review_types)
 
184
        except restful_errors.HTTPError, e:
 
185
            error_lines = []
 
186
            for line in e.content.splitlines():
 
187
                if line.startswith('Traceback (most recent call last):'):
 
188
                    break
 
189
                error_lines.append(line)
 
190
            raise Exception(''.join(error_lines))
 
191
        else:
 
192
            webbrowser.open(canonical_url(mp))
204
193
 
205
194
 
206
195
def modified_files(old_tree, new_tree):
209
198
        old_tree):
210
199
        if c and k == 'file':
211
200
            yield str(path)
 
201
 
 
202
 
 
203
def canonical_url(object):
 
204
    """Return the canonical URL for a branch."""
 
205
    url = object.self_link.replace('https://api.', 'https://code.')
 
206
    return url.replace('/beta/', '/')