~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2011-01-06 06:26:23 UTC
  • mto: This revision was merged to the branch mainline in revision 5612.
  • Revision ID: andrew.bennetts@canonical.com-20110106062623-43tda58mqroybjui
Start of a developer doc describing how fetch works.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 (
27
26
    lp_api,
28
27
    lp_registration,
29
28
)
 
29
from bzrlib.plugins.launchpad.lp_api import canonical_url
30
30
 
31
31
from lazr.restfulclient import errors as restful_errors
32
32
 
55
55
    hooks = ProposeMergeHooks()
56
56
 
57
57
    def __init__(self, tree, source_branch, target_branch, message, reviews,
58
 
                 staging=False):
 
58
                 staging=False, approve=False):
59
59
        """Constructor.
60
60
 
61
61
        :param tree: The working tree for the source branch.
65
65
        :param reviews: A list of tuples of reviewer, review type.
66
66
        :param staging: If True, propose the merge against staging instead of
67
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).
68
72
        """
69
73
        self.tree = tree
70
74
        if staging:
71
75
            lp_instance = 'staging'
72
76
        else:
73
 
            lp_instance = 'edge'
 
77
            lp_instance = 'production'
74
78
        service = lp_registration.LaunchpadService(lp_instance=lp_instance)
75
79
        self.launchpad = lp_api.login(service)
76
80
        self.source_branch = lp_api.LaunchpadBranch.from_bzr(
81
85
            self.target_branch = lp_api.LaunchpadBranch.from_bzr(
82
86
                self.launchpad, target_branch)
83
87
        self.commit_message = message
 
88
        # XXX: this is where bug lp:583638 could be tackled.
84
89
        if reviews == []:
85
90
            target_reviewer = self.target_branch.lp.reviewer
86
91
            if target_reviewer is None:
90
95
            self.reviews = [(self.launchpad.people[reviewer], review_type)
91
96
                            for reviewer, review_type in
92
97
                            reviews]
 
98
        self.approve = approve
93
99
 
94
100
    def get_comment(self, prerequisite_branch):
95
101
        """Determine the initial comment for the merge proposal."""
160
166
                 'prerequisite_branch': prerequisite_branch})
161
167
        return prerequisite_branch
162
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
 
163
187
    def create_proposal(self):
164
188
        """Perform the submission."""
165
189
        prerequisite_branch = self._get_prerequisite_branch()
175
199
            review_types.append(review_type)
176
200
            reviewers.append(reviewer.self_link)
177
201
        initial_comment = self.get_comment(prerequisite_branch)
178
 
        try:
179
 
            mp = self.source_branch.lp.createMergeProposal(
180
 
                target_branch=self.target_branch.lp,
181
 
                prerequisite_branch=prereq,
182
 
                initial_comment=initial_comment,
183
 
                commit_message=self.commit_message, reviewers=reviewers,
184
 
                review_types=review_types)
185
 
        except restful_errors.HTTPError, e:
186
 
            error_lines = []
187
 
            for line in e.content.splitlines():
188
 
                if line.startswith('Traceback (most recent call last):'):
189
 
                    break
190
 
                error_lines.append(line)
191
 
            raise Exception(''.join(error_lines))
192
 
        else:
193
 
            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))
194
212
 
195
213
 
196
214
def modified_files(old_tree, new_tree):
199
217
        old_tree):
200
218
        if c and k == 'file':
201
219
            yield str(path)
202
 
 
203
 
 
204
 
def canonical_url(object):
205
 
    """Return the canonical URL for a branch."""
206
 
    scheme, netloc, path, params, query, fragment = urlparse.urlparse(
207
 
        str(object.self_link))
208
 
    path = '/'.join(path.split('/')[2:])
209
 
    netloc = netloc.replace('api.', 'code.')
210
 
    return urlparse.urlunparse((scheme, netloc, path, params, query,
211
 
                                fragment))