/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/propose/github.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-12-22 23:23:00 UTC
  • mfrom: (7414.2.1 github-follow-link)
  • Revision ID: breezy.the.bot@gmail.com-20191222232300-5e1nuniiuq1u4p6a
Support following redirects for github repositories.

Merged from https://code.launchpad.net/~jelmer/brz/github-follow-link/+merge/376287

Show diffs side-by-side

added added

removed removed

Lines of Context:
211
211
    return git_url_to_bzr_url(url, branch_name)
212
212
 
213
213
 
 
214
def strip_optional(url):
 
215
    return url.split('{')[0]
 
216
 
 
217
 
214
218
class GitHub(Hoster):
215
219
 
216
220
    name = 'github'
233
237
            raise GitHubLoginRequired(self)
234
238
        return response
235
239
 
236
 
    def _get_repo(self, path):
237
 
        path = 'repos/' + path
 
240
    def _get_repo(self, owner, repo):
 
241
        path = 'repos/%s/%s' % (owner, repo)
238
242
        response = self._api_request('GET', path)
239
243
        if response.status == 404:
240
244
            raise NoSuchProject(path)
243
247
        raise InvalidHttpResponse(path, response.text)
244
248
 
245
249
    def _get_repo_pulls(self, path, head=None, state=None):
246
 
        path = 'repos/' + path + '/pulls?'
 
250
        path = path + '?'
247
251
        params = {}
248
252
        if head is not None:
249
253
            params['head'] = head
259
263
        raise InvalidHttpResponse(path, response.text)
260
264
 
261
265
    def _create_pull(self, path, title, head, base, body=None):
262
 
        path = 'repos/' + path + '/pulls'
263
266
        data = {
264
267
            'title': title,
265
268
            'head': head,
332
335
        path = 'search/issues'
333
336
        return self._list_paged(path, {'q': query}, per_page=DEFAULT_PER_PAGE)
334
337
 
335
 
    def _create_fork(self, repo, owner=None):
336
 
        path = '/repos/%s/forks' % (repo, )
 
338
    def _create_fork(self, path, owner=None):
337
339
        if owner and owner != self._current_user['login']:
338
340
            path += '?organization=%s' % owner
339
341
        response = self._api_request('POST', path)
354
356
                        owner=None, revision_id=None, overwrite=False,
355
357
                        allow_lossy=True):
356
358
        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
357
 
        base_repo = self._get_repo('%s/%s' % (base_owner, base_project))
 
359
        base_repo = self._get_repo(base_owner, base_project)
358
360
        if owner is None:
359
361
            owner = self._current_user['login']
360
362
        if project is None:
361
363
            project = base_repo['name']
362
364
        try:
363
 
            remote_repo = self._get_repo('%s/%s' % (owner, project))
 
365
            remote_repo = self._get_repo(owner, project)
364
366
        except NoSuchProject:
365
 
            base_repo_path = '%s/%s' % (base_owner, base_project)
366
 
            base_repo = self._get_repo(base_repo_path)
367
 
            remote_repo = self._create_fork(base_repo_path, owner)
 
367
            base_repo = self._get_repo(base_owner, base_project)
 
368
            remote_repo = self._create_fork(base_repo['forks_url'], owner)
368
369
            note(gettext('Forking new repository %s from %s') %
369
370
                 (remote_repo['html_url'], base_repo['html_url']))
370
371
        else:
385
386
 
386
387
    def get_push_url(self, branch):
387
388
        owner, project, branch_name = parse_github_branch_url(branch)
388
 
        repo = self._get_repo('%s/%s' % (owner, project))
 
389
        repo = self._get_repo(owner, project)
389
390
        return github_url_to_bzr_url(repo['ssh_url'], branch_name)
390
391
 
391
392
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
392
393
        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
393
 
        base_repo = self._get_repo('%s/%s' % (base_owner, base_project))
 
394
        base_repo = self._get_repo(base_owner, base_project)
394
395
        if owner is None:
395
396
            owner = self._current_user['login']
396
397
        if project is None:
397
398
            project = base_repo['name']
398
399
        try:
399
 
            remote_repo = self._get_repo('%s/%s' % (owner, project))
 
400
            remote_repo = self._get_repo(owner, project)
400
401
            full_url = github_url_to_bzr_url(remote_repo['ssh_url'], name)
401
402
            return _mod_branch.Branch.open(full_url)
402
403
        except NoSuchProject:
410
411
            parse_github_branch_url(source_branch))
411
412
        (target_owner, target_repo_name, target_branch_name) = (
412
413
            parse_github_branch_url(target_branch))
413
 
        target_repo_path = "%s/%s" % (target_owner, target_repo_name)
414
 
        target_repo = self._get_repo(target_repo_path)
 
414
        target_repo = self._get_repo(target_owner, target_repo_name)
415
415
        state = {
416
416
            'open': 'open',
417
417
            'merged': 'closed',
418
418
            'closed': 'closed',
419
419
            'all': 'all'}
420
420
        pulls = self._get_repo_pulls(
421
 
            target_repo_path,
 
421
            strip_optional(target_repo['pulls_url']),
422
422
            head=target_branch_name,
423
423
            state=state[status])
424
424
        for pull in pulls:
520
520
        # TODO(jelmer): Allow setting title explicitly?
521
521
        title = determine_title(description)
522
522
        # TODO(jelmer): Set maintainers_can_modify?
 
523
        target_repo = self.gh._get_repo(
 
524
            self.target_owner, self.target_repo_name)
523
525
        try:
524
526
            pull_request = self.gh._create_pull(
525
 
                "%s/%s" % (self.target_owner, self.target_repo_name),
 
527
                strip_optional(target_repo['pulls_url']),
526
528
                title=title, body=description,
527
529
                head="%s:%s" % (self.source_owner, self.source_branch_name),
528
530
                base=self.target_branch_name)