~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2010-06-17 08:53:15 UTC
  • mfrom: (5300 +trunk)
  • mto: (5303.2.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5305.
  • Revision ID: bialix@ukr.net-20100617085315-hr8186zck57zn35s
merge bzr.dev; fix NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 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
18
19
import webbrowser
19
20
 
20
21
from bzrlib import (
21
22
    errors,
 
23
    hooks,
22
24
    msgeditor,
23
25
)
24
 
from bzrlib.hooks import HookPoint, Hooks
25
26
from bzrlib.plugins.launchpad import (
26
27
    lp_api,
27
28
    lp_registration,
30
31
from lazr.restfulclient import errors as restful_errors
31
32
 
32
33
 
33
 
class ProposeMergeHooks(Hooks):
 
34
class ProposeMergeHooks(hooks.Hooks):
34
35
    """Hooks for proposing a merge on Launchpad."""
35
36
 
36
37
    def __init__(self):
37
 
        Hooks.__init__(self)
 
38
        hooks.Hooks.__init__(self)
38
39
        self.create_hook(
39
 
            HookPoint(
 
40
            hooks.HookPoint(
40
41
                'get_prerequisite',
41
42
                "Return the prerequisite branch for proposing as merge.",
42
43
                (2, 1), None),
43
44
        )
44
45
        self.create_hook(
45
 
            HookPoint(
 
46
            hooks.HookPoint(
46
47
                'merge_proposal_body',
47
48
                "Return an initial body for the merge proposal message.",
48
49
                (2, 1), None),
54
55
    hooks = ProposeMergeHooks()
55
56
 
56
57
    def __init__(self, tree, source_branch, target_branch, message, reviews,
57
 
                 staging=False):
 
58
                 staging=False, approve=False):
58
59
        """Constructor.
59
60
 
60
61
        :param tree: The working tree for the source branch.
64
65
        :param reviews: A list of tuples of reviewer, review type.
65
66
        :param staging: If True, propose the merge against staging instead of
66
67
            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).
67
72
        """
68
73
        self.tree = tree
69
74
        if staging:
80
85
            self.target_branch = lp_api.LaunchpadBranch.from_bzr(
81
86
                self.launchpad, target_branch)
82
87
        self.commit_message = message
 
88
        # XXX: this is where bug lp:583638 could be tackled.
83
89
        if reviews == []:
84
90
            target_reviewer = self.target_branch.lp.reviewer
85
91
            if target_reviewer is None:
89
95
            self.reviews = [(self.launchpad.people[reviewer], review_type)
90
96
                            for reviewer, review_type in
91
97
                            reviews]
 
98
        self.approve = approve
92
99
 
93
100
    def get_comment(self, prerequisite_branch):
94
101
        """Determine the initial comment for the merge proposal."""
159
166
                 'prerequisite_branch': prerequisite_branch})
160
167
        return prerequisite_branch
161
168
 
 
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
 
162
187
    def create_proposal(self):
163
188
        """Perform the submission."""
164
189
        prerequisite_branch = self._get_prerequisite_branch()
174
199
            review_types.append(review_type)
175
200
            reviewers.append(reviewer.self_link)
176
201
        initial_comment = self.get_comment(prerequisite_branch)
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))
 
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))
193
212
 
194
213
 
195
214
def modified_files(old_tree, new_tree):
202
221
 
203
222
def canonical_url(object):
204
223
    """Return the canonical URL for a branch."""
205
 
    url = object.self_link.replace('https://api.', 'https://code.')
206
 
    return url.replace('/beta/', '/')
 
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))