Merge lp:~jelmer/brz/github-follow-link into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/github-follow-link
Merge into: lp:brz
Diff against target: 127 lines (+20/-18)
1 file modified
breezy/plugins/propose/github.py (+20/-18)
To merge this branch: bzr merge lp:~jelmer/brz/github-follow-link
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+376287@code.launchpad.net

Commit message

Support following redirects for github repositories.

Description of the change

Support following redirects for github repositories.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/plugins/propose/github.py'
2--- breezy/plugins/propose/github.py 2019-11-01 00:39:55 +0000
3+++ breezy/plugins/propose/github.py 2019-12-03 16:22:57 +0000
4@@ -211,6 +211,10 @@
5 return git_url_to_bzr_url(url, branch_name)
6
7
8+def strip_optional(url):
9+ return url.split('{')[0]
10+
11+
12 class GitHub(Hoster):
13
14 name = 'github'
15@@ -233,8 +237,8 @@
16 raise GitHubLoginRequired(self)
17 return response
18
19- def _get_repo(self, path):
20- path = 'repos/' + path
21+ def _get_repo(self, owner, repo):
22+ path = 'repos/%s/%s' % (owner, repo)
23 response = self._api_request('GET', path)
24 if response.status == 404:
25 raise NoSuchProject(path)
26@@ -243,7 +247,7 @@
27 raise InvalidHttpResponse(path, response.text)
28
29 def _get_repo_pulls(self, path, head=None, state=None):
30- path = 'repos/' + path + '/pulls?'
31+ path = path + '?'
32 params = {}
33 if head is not None:
34 params['head'] = head
35@@ -259,7 +263,6 @@
36 raise InvalidHttpResponse(path, response.text)
37
38 def _create_pull(self, path, title, head, base, body=None):
39- path = 'repos/' + path + '/pulls'
40 data = {
41 'title': title,
42 'head': head,
43@@ -332,8 +335,7 @@
44 path = 'search/issues'
45 return self._list_paged(path, {'q': query}, per_page=DEFAULT_PER_PAGE)
46
47- def _create_fork(self, repo, owner=None):
48- path = '/repos/%s/forks' % (repo, )
49+ def _create_fork(self, path, owner=None):
50 if owner and owner != self._current_user['login']:
51 path += '?organization=%s' % owner
52 response = self._api_request('POST', path)
53@@ -354,17 +356,16 @@
54 owner=None, revision_id=None, overwrite=False,
55 allow_lossy=True):
56 base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
57- base_repo = self._get_repo('%s/%s' % (base_owner, base_project))
58+ base_repo = self._get_repo(base_owner, base_project)
59 if owner is None:
60 owner = self._current_user['login']
61 if project is None:
62 project = base_repo['name']
63 try:
64- remote_repo = self._get_repo('%s/%s' % (owner, project))
65+ remote_repo = self._get_repo(owner, project)
66 except NoSuchProject:
67- base_repo_path = '%s/%s' % (base_owner, base_project)
68- base_repo = self._get_repo(base_repo_path)
69- remote_repo = self._create_fork(base_repo_path, owner)
70+ base_repo = self._get_repo(base_owner, base_project)
71+ remote_repo = self._create_fork(base_repo['forks_url'], owner)
72 note(gettext('Forking new repository %s from %s') %
73 (remote_repo['html_url'], base_repo['html_url']))
74 else:
75@@ -385,18 +386,18 @@
76
77 def get_push_url(self, branch):
78 owner, project, branch_name = parse_github_branch_url(branch)
79- repo = self._get_repo('%s/%s' % (owner, project))
80+ repo = self._get_repo(owner, project)
81 return github_url_to_bzr_url(repo['ssh_url'], branch_name)
82
83 def get_derived_branch(self, base_branch, name, project=None, owner=None):
84 base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
85- base_repo = self._get_repo('%s/%s' % (base_owner, base_project))
86+ base_repo = self._get_repo(base_owner, base_project)
87 if owner is None:
88 owner = self._current_user['login']
89 if project is None:
90 project = base_repo['name']
91 try:
92- remote_repo = self._get_repo('%s/%s' % (owner, project))
93+ remote_repo = self._get_repo(owner, project)
94 full_url = github_url_to_bzr_url(remote_repo['ssh_url'], name)
95 return _mod_branch.Branch.open(full_url)
96 except NoSuchProject:
97@@ -410,15 +411,14 @@
98 parse_github_branch_url(source_branch))
99 (target_owner, target_repo_name, target_branch_name) = (
100 parse_github_branch_url(target_branch))
101- target_repo_path = "%s/%s" % (target_owner, target_repo_name)
102- target_repo = self._get_repo(target_repo_path)
103+ target_repo = self._get_repo(target_owner, target_repo_name)
104 state = {
105 'open': 'open',
106 'merged': 'closed',
107 'closed': 'closed',
108 'all': 'all'}
109 pulls = self._get_repo_pulls(
110- target_repo_path,
111+ strip_optional(target_repo['pulls_url']),
112 head=target_branch_name,
113 state=state[status])
114 for pull in pulls:
115@@ -520,9 +520,11 @@
116 # TODO(jelmer): Allow setting title explicitly?
117 title = determine_title(description)
118 # TODO(jelmer): Set maintainers_can_modify?
119+ target_repo = self.gh._get_repo(
120+ self.target_owner, self.target_repo_name)
121 try:
122 pull_request = self.gh._create_pull(
123- "%s/%s" % (self.target_owner, self.target_repo_name),
124+ strip_optional(target_repo['pulls_url']),
125 title=title, body=description,
126 head="%s:%s" % (self.source_owner, self.source_branch_name),
127 base=self.target_branch_name)

Subscribers

People subscribed via source and target branches