1
# Copyright (C) 2010, 2011 Canonical Ltd
1
# Copyright (C) 2009, 2010 Canonical Ltd
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
17
20
from bzrlib import (
21
from bzrlib.lazy_import import lazy_import
22
lazy_import(globals(), """
24
from bzrlib.hooks import HookPoint, Hooks
28
25
from bzrlib.plugins.launchpad import (
35
class ProposeMergeHooks(hooks.Hooks):
30
from lazr.restfulclient import errors as restful_errors
33
class ProposeMergeHooks(Hooks):
36
34
"""Hooks for proposing a merge on Launchpad."""
38
36
def __init__(self):
39
hooks.Hooks.__init__(self, "bzrlib.plugins.launchpad.lp_propose",
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))
41
"Return the prerequisite branch for proposing as merge.",
46
'merge_proposal_body',
47
"Return an initial body for the merge proposal message.",
47
52
class Proposer(object):
49
54
hooks = ProposeMergeHooks()
51
56
def __init__(self, tree, source_branch, target_branch, message, reviews,
52
staging=False, approve=False):
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
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
69
70
lp_instance = 'staging'
71
lp_instance = 'production'
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()
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.
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, '')]
86
89
self.reviews = [(self.launchpad.people[reviewer], review_type)
87
90
for reviewer, review_type in
89
self.approve = approve
91
93
def get_comment(self, prerequisite_branch):
92
94
"""Determine the initial comment for the merge proposal."""
144
146
if mp.target_branch.self_link == self.target_branch.lp.self_link:
145
147
raise errors.BzrCommandError(
146
148
'There is already a branch merge proposal: %s' %
147
lp_api.canonical_url(mp))
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
160
def call_webservice(self, call, *args, **kwargs):
161
"""Make a call to the webservice, wrapping failures.
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).
168
from lazr.restfulclient import errors as restful_errors
170
return call(*args, **kwargs)
171
except restful_errors.HTTPError, e:
173
for line in e.content.splitlines():
174
if line.startswith('Traceback (most recent call last):'):
176
error_lines.append(line)
177
raise Exception(''.join(error_lines))
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)
202
self.call_webservice(mp.setStatus, status='Approved')
203
webbrowser.open(lp_api.canonical_url(mp))
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:
186
for line in e.content.splitlines():
187
if line.startswith('Traceback (most recent call last):'):
189
error_lines.append(line)
190
raise Exception(''.join(error_lines))
192
webbrowser.open(canonical_url(mp))
206
195
def modified_files(old_tree, new_tree):
210
199
if c and k == 'file':
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/', '/')