1
# Copyright (C) 2009, 2010 Canonical Ltd
1
# Copyright (C) 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
20
21
from bzrlib import (
24
from bzrlib.hooks import HookPoint, Hooks
25
26
from bzrlib.plugins.launchpad import (
30
31
from lazr.restfulclient import errors as restful_errors
33
class ProposeMergeHooks(Hooks):
34
class ProposeMergeHooks(hooks.Hooks):
34
35
"""Hooks for proposing a merge on Launchpad."""
36
37
def __init__(self):
38
hooks.Hooks.__init__(self)
40
41
'get_prerequisite',
41
42
"Return the prerequisite branch for proposing as merge.",
46
47
'merge_proposal_body',
47
48
"Return an initial body for the merge proposal message.",
54
55
hooks = ProposeMergeHooks()
56
57
def __init__(self, tree, source_branch, target_branch, message, reviews,
58
staging=False, approve=False):
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
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
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.
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
98
self.approve = approve
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
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))
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)
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))
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))
195
214
def modified_files(old_tree, new_tree):
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,