1
# Copyright (C) 2010 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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
20
from bzrlib import (
24
from bzrlib.hooks import HookPoint, Hooks
26
25
from bzrlib.plugins.launchpad import (
31
30
from lazr.restfulclient import errors as restful_errors
34
class ProposeMergeHooks(hooks.Hooks):
33
class ProposeMergeHooks(Hooks):
35
34
"""Hooks for proposing a merge on Launchpad."""
37
36
def __init__(self):
38
hooks.Hooks.__init__(self)
41
40
'get_prerequisite',
42
41
"Return the prerequisite branch for proposing as merge.",
47
46
'merge_proposal_body',
48
47
"Return an initial body for the merge proposal message.",
55
54
hooks = ProposeMergeHooks()
57
56
def __init__(self, tree, source_branch, target_branch, message, reviews,
58
staging=False, approve=False):
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
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
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.
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
98
self.approve = approve
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
169
def call_webservice(self, call, *args, **kwargs):
170
"""Make a call to the webservice, wrapping failures.
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).
178
return call(*args, **kwargs)
179
except restful_errors.HTTPError, e:
181
for line in e.content.splitlines():
182
if line.startswith('Traceback (most recent call last):'):
184
error_lines.append(line)
185
raise Exception(''.join(error_lines))
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)
210
self.call_webservice(mp.setStatus, status='Approved')
211
webbrowser.open(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))
214
195
def modified_files(old_tree, new_tree):
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,
205
url = object.self_link.replace('https://api.', 'https://code.')
206
return url.replace('/beta/', '/')