~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-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010 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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
import urlparse
19
18
import webbrowser
20
19
 
21
20
from bzrlib import (
22
21
    errors,
23
 
    hooks,
24
22
    msgeditor,
25
23
)
 
24
from bzrlib.hooks import HookPoint, Hooks
26
25
from bzrlib.plugins.launchpad import (
27
26
    lp_api,
28
27
    lp_registration,
31
30
from lazr.restfulclient import errors as restful_errors
32
31
 
33
32
 
34
 
class ProposeMergeHooks(hooks.Hooks):
 
33
class ProposeMergeHooks(Hooks):
35
34
    """Hooks for proposing a merge on Launchpad."""
36
35
 
37
36
    def __init__(self):
38
 
        hooks.Hooks.__init__(self)
 
37
        Hooks.__init__(self)
39
38
        self.create_hook(
40
 
            hooks.HookPoint(
 
39
            HookPoint(
41
40
                'get_prerequisite',
42
41
                "Return the prerequisite branch for proposing as merge.",
43
42
                (2, 1), None),
44
43
        )
45
44
        self.create_hook(
46
 
            hooks.HookPoint(
 
45
            HookPoint(
47
46
                'merge_proposal_body',
48
47
                "Return an initial body for the merge proposal message.",
49
48
                (2, 1), None),
55
54
    hooks = ProposeMergeHooks()
56
55
 
57
56
    def __init__(self, tree, source_branch, target_branch, message, reviews,
58
 
                 staging=False, approve=False):
 
57
                 staging=False):
59
58
        """Constructor.
60
59
 
61
60
        :param tree: The working tree for the source branch.
65
64
        :param reviews: A list of tuples of reviewer, review type.
66
65
        :param staging: If True, propose the merge against staging instead of
67
66
            production.
68
 
        :param approve: If True, mark the new proposal as approved immediately.
69
 
            This is useful when a project permits some things to be approved
70
 
            by the submitter (e.g. merges between release and deployment
71
 
            branches).
72
67
        """
73
68
        self.tree = tree
74
69
        if staging:
85
80
            self.target_branch = lp_api.LaunchpadBranch.from_bzr(
86
81
                self.launchpad, target_branch)
87
82
        self.commit_message = message
88
 
        # XXX: this is where bug lp:583638 could be tackled.
89
83
        if reviews == []:
90
84
            target_reviewer = self.target_branch.lp.reviewer
91
85
            if target_reviewer is None:
95
89
            self.reviews = [(self.launchpad.people[reviewer], review_type)
96
90
                            for reviewer, review_type in
97
91
                            reviews]
98
 
        self.approve = approve
99
92
 
100
93
    def get_comment(self, prerequisite_branch):
101
94
        """Determine the initial comment for the merge proposal."""
166
159
                 'prerequisite_branch': prerequisite_branch})
167
160
        return prerequisite_branch
168
161
 
169
 
    def call_webservice(self, call, *args, **kwargs):
170
 
        """Make a call to the webservice, wrapping failures.
171
 
        
172
 
        :param call: The call to make.
173
 
        :param *args: *args for the call.
174
 
        :param **kwargs: **kwargs for the call.
175
 
        :return: The result of calling call(*args, *kwargs).
176
 
        """
177
 
        try:
178
 
            return call(*args, **kwargs)
179
 
        except restful_errors.HTTPError, e:
180
 
            error_lines = []
181
 
            for line in e.content.splitlines():
182
 
                if line.startswith('Traceback (most recent call last):'):
183
 
                    break
184
 
                error_lines.append(line)
185
 
            raise Exception(''.join(error_lines))
186
 
 
187
162
    def create_proposal(self):
188
163
        """Perform the submission."""
189
164
        prerequisite_branch = self._get_prerequisite_branch()
199
174
            review_types.append(review_type)
200
175
            reviewers.append(reviewer.self_link)
201
176
        initial_comment = self.get_comment(prerequisite_branch)
202
 
        mp = self.call_webservice(
203
 
            self.source_branch.lp.createMergeProposal,
204
 
            target_branch=self.target_branch.lp,
205
 
            prerequisite_branch=prereq,
206
 
            initial_comment=initial_comment,
207
 
            commit_message=self.commit_message, reviewers=reviewers,
208
 
            review_types=review_types)
209
 
        if self.approve:
210
 
            self.call_webservice(mp.setStatus, status='Approved')
211
 
        webbrowser.open(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))
212
193
 
213
194
 
214
195
def modified_files(old_tree, new_tree):
221
202
 
222
203
def canonical_url(object):
223
204
    """Return the canonical URL for a branch."""
224
 
    scheme, netloc, path, params, query, fragment = urlparse.urlparse(
225
 
        str(object.self_link))
226
 
    path = '/'.join(path.split('/')[2:])
227
 
    netloc = netloc.replace('api.', 'code.')
228
 
    return urlparse.urlunparse((scheme, netloc, path, params, query,
229
 
                                fragment))
 
205
    url = object.self_link.replace('https://api.', 'https://code.')
 
206
    return url.replace('/beta/', '/')