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/probe-url |
Merge into: | lp:brz |
Diff against target: |
277 lines (+54/-37) 4 files modified
breezy/plugins/propose/github.py (+18/-13) breezy/plugins/propose/gitlabs.py (+23/-18) breezy/plugins/propose/launchpad.py (+3/-3) breezy/plugins/propose/propose.py (+10/-3) |
To merge this branch: | bzr merge lp:~jelmer/brz/probe-url |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Martin Packman | Approve | ||
Review via email: mp+362951@code.launchpad.net |
Commit message
Split out a 'probe_from_url' method on Hoster.
Description of the change
Split out a 'probe_from_url' method on Hoster.
To post a comment you must log in.
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-02-14 04:35:05 +0000 | |||
3 | +++ breezy/plugins/propose/github.py 2019-02-15 02:29:22 +0000 | |||
4 | @@ -132,8 +132,7 @@ | |||
5 | 132 | self._pr.edit(state='closed') | 132 | self._pr.edit(state='closed') |
6 | 133 | 133 | ||
7 | 134 | 134 | ||
10 | 135 | def parse_github_url(branch): | 135 | def parse_github_url(url): |
9 | 136 | url = urlutils.split_segment_parameters(branch.user_url)[0] | ||
11 | 137 | (scheme, user, password, host, port, path) = urlutils.parse_url( | 136 | (scheme, user, password, host, port, path) = urlutils.parse_url( |
12 | 138 | url) | 137 | url) |
13 | 139 | if host != 'github.com': | 138 | if host != 'github.com': |
14 | @@ -141,6 +140,12 @@ | |||
15 | 141 | (owner, repo_name) = path.strip('/').split('/') | 140 | (owner, repo_name) = path.strip('/').split('/') |
16 | 142 | if repo_name.endswith('.git'): | 141 | if repo_name.endswith('.git'): |
17 | 143 | repo_name = repo_name[:-4] | 142 | repo_name = repo_name[:-4] |
18 | 143 | return owner, repo_name | ||
19 | 144 | |||
20 | 145 | |||
21 | 146 | def parse_github_branch_url(branch): | ||
22 | 147 | url = urlutils.split_segment_parameters(branch.user_url)[0] | ||
23 | 148 | owner, repo_name = parse_github_url(url) | ||
24 | 144 | return owner, repo_name, branch.name | 149 | return owner, repo_name, branch.name |
25 | 145 | 150 | ||
26 | 146 | 151 | ||
27 | @@ -186,7 +191,7 @@ | |||
28 | 186 | owner=None, revision_id=None, overwrite=False, | 191 | owner=None, revision_id=None, overwrite=False, |
29 | 187 | allow_lossy=True): | 192 | allow_lossy=True): |
30 | 188 | import github | 193 | import github |
32 | 189 | base_owner, base_project, base_branch_name = parse_github_url(base_branch) | 194 | base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch) |
33 | 190 | base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project)) | 195 | base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project)) |
34 | 191 | if owner is None: | 196 | if owner is None: |
35 | 192 | owner = self.gh.get_user().login | 197 | owner = self.gh.get_user().login |
36 | @@ -222,14 +227,14 @@ | |||
37 | 222 | 227 | ||
38 | 223 | @convert_github_error | 228 | @convert_github_error |
39 | 224 | def get_push_url(self, branch): | 229 | def get_push_url(self, branch): |
41 | 225 | owner, project, branch_name = parse_github_url(branch) | 230 | owner, project, branch_name = parse_github_branch_url(branch) |
42 | 226 | repo = self.gh.get_repo('%s/%s' % (owner, project)) | 231 | repo = self.gh.get_repo('%s/%s' % (owner, project)) |
43 | 227 | return github_url_to_bzr_url(repo.ssh_url, branch_name) | 232 | return github_url_to_bzr_url(repo.ssh_url, branch_name) |
44 | 228 | 233 | ||
45 | 229 | @convert_github_error | 234 | @convert_github_error |
46 | 230 | def get_derived_branch(self, base_branch, name, project=None, owner=None): | 235 | def get_derived_branch(self, base_branch, name, project=None, owner=None): |
47 | 231 | import github | 236 | import github |
49 | 232 | base_owner, base_project, base_branch_name = parse_github_url(base_branch) | 237 | base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch) |
50 | 233 | base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project)) | 238 | base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project)) |
51 | 234 | if owner is None: | 239 | if owner is None: |
52 | 235 | owner = self.gh.get_user().login | 240 | owner = self.gh.get_user().login |
53 | @@ -249,9 +254,9 @@ | |||
54 | 249 | @convert_github_error | 254 | @convert_github_error |
55 | 250 | def iter_proposals(self, source_branch, target_branch, status='open'): | 255 | def iter_proposals(self, source_branch, target_branch, status='open'): |
56 | 251 | (source_owner, source_repo_name, source_branch_name) = ( | 256 | (source_owner, source_repo_name, source_branch_name) = ( |
58 | 252 | parse_github_url(source_branch)) | 257 | parse_github_branch_url(source_branch)) |
59 | 253 | (target_owner, target_repo_name, target_branch_name) = ( | 258 | (target_owner, target_repo_name, target_branch_name) = ( |
61 | 254 | parse_github_url(target_branch)) | 259 | parse_github_branch_url(target_branch)) |
62 | 255 | target_repo = self.gh.get_repo( | 260 | target_repo = self.gh.get_repo( |
63 | 256 | "%s/%s" % (target_owner, target_repo_name)) | 261 | "%s/%s" % (target_owner, target_repo_name)) |
64 | 257 | state = { | 262 | state = { |
65 | @@ -277,18 +282,18 @@ | |||
66 | 277 | 282 | ||
67 | 278 | def hosts(self, branch): | 283 | def hosts(self, branch): |
68 | 279 | try: | 284 | try: |
70 | 280 | parse_github_url(branch) | 285 | parse_github_branch_url(branch) |
71 | 281 | except NotGitHubUrl: | 286 | except NotGitHubUrl: |
72 | 282 | return False | 287 | return False |
73 | 283 | else: | 288 | else: |
74 | 284 | return True | 289 | return True |
75 | 285 | 290 | ||
76 | 286 | @classmethod | 291 | @classmethod |
78 | 287 | def probe(cls, branch): | 292 | def probe_from_url(cls, url): |
79 | 288 | try: | 293 | try: |
81 | 289 | parse_github_url(branch) | 294 | parse_github_url(url) |
82 | 290 | except NotGitHubUrl: | 295 | except NotGitHubUrl: |
84 | 291 | raise UnsupportedHoster(branch) | 296 | raise UnsupportedHoster(url) |
85 | 292 | return cls() | 297 | return cls() |
86 | 293 | 298 | ||
87 | 294 | @classmethod | 299 | @classmethod |
88 | @@ -319,9 +324,9 @@ | |||
89 | 319 | self.source_branch = source_branch | 324 | self.source_branch = source_branch |
90 | 320 | self.target_branch = target_branch | 325 | self.target_branch = target_branch |
91 | 321 | (self.target_owner, self.target_repo_name, self.target_branch_name) = ( | 326 | (self.target_owner, self.target_repo_name, self.target_branch_name) = ( |
93 | 322 | parse_github_url(self.target_branch)) | 327 | parse_github_branch_url(self.target_branch)) |
94 | 323 | (self.source_owner, self.source_repo_name, self.source_branch_name) = ( | 328 | (self.source_owner, self.source_repo_name, self.source_branch_name) = ( |
96 | 324 | parse_github_url(self.source_branch)) | 329 | parse_github_branch_url(self.source_branch)) |
97 | 325 | 330 | ||
98 | 326 | def get_infotext(self): | 331 | def get_infotext(self): |
99 | 327 | """Determine the initial comment for the merge proposal.""" | 332 | """Determine the initial comment for the merge proposal.""" |
100 | 328 | 333 | ||
101 | === modified file 'breezy/plugins/propose/gitlabs.py' | |||
102 | --- breezy/plugins/propose/gitlabs.py 2019-02-12 23:38:25 +0000 | |||
103 | +++ breezy/plugins/propose/gitlabs.py 2019-02-15 02:29:22 +0000 | |||
104 | @@ -110,17 +110,22 @@ | |||
105 | 110 | raise GitLabLoginMissing() | 110 | raise GitLabLoginMissing() |
106 | 111 | 111 | ||
107 | 112 | 112 | ||
110 | 113 | def parse_gitlab_url(branch): | 113 | def parse_gitlab_url(url): |
109 | 114 | url = urlutils.split_segment_parameters(branch.user_url)[0] | ||
111 | 115 | (scheme, user, password, host, port, path) = urlutils.parse_url( | 114 | (scheme, user, password, host, port, path) = urlutils.parse_url( |
112 | 116 | url) | 115 | url) |
113 | 117 | if scheme not in ('git+ssh', 'https', 'http'): | 116 | if scheme not in ('git+ssh', 'https', 'http'): |
115 | 118 | raise NotGitLabUrl(branch.user_url) | 117 | raise NotGitLabUrl(url) |
116 | 119 | if not host: | 118 | if not host: |
118 | 120 | raise NotGitLabUrl(branch.user_url) | 119 | raise NotGitLabUrl(url) |
119 | 121 | path = path.strip('/') | 120 | path = path.strip('/') |
120 | 122 | if path.endswith('.git'): | 121 | if path.endswith('.git'): |
121 | 123 | path = path[:-4] | 122 | path = path[:-4] |
122 | 123 | return host, path | ||
123 | 124 | |||
124 | 125 | |||
125 | 126 | def parse_gitlab_branch_url(branch): | ||
126 | 127 | url = urlutils.split_segment_parameters(branch.user_url)[0] | ||
127 | 128 | host, path = parse_gitlab_url(url) | ||
128 | 124 | return host, path, branch.name | 129 | return host, path, branch.name |
129 | 125 | 130 | ||
130 | 126 | 131 | ||
131 | @@ -183,7 +188,7 @@ | |||
132 | 183 | self.gl = gl | 188 | self.gl = gl |
133 | 184 | 189 | ||
134 | 185 | def get_push_url(self, branch): | 190 | def get_push_url(self, branch): |
136 | 186 | (host, project_name, branch_name) = parse_gitlab_url(branch) | 191 | (host, project_name, branch_name) = parse_gitlab_branch_url(branch) |
137 | 187 | project = self.gl.projects.get(project_name) | 192 | project = self.gl.projects.get(project_name) |
138 | 188 | return gitlab_url_to_bzr_url( | 193 | return gitlab_url_to_bzr_url( |
139 | 189 | project.ssh_url_to_repo, branch_name) | 194 | project.ssh_url_to_repo, branch_name) |
140 | @@ -192,7 +197,7 @@ | |||
141 | 192 | owner=None, revision_id=None, overwrite=False, | 197 | owner=None, revision_id=None, overwrite=False, |
142 | 193 | allow_lossy=True): | 198 | allow_lossy=True): |
143 | 194 | import gitlab | 199 | import gitlab |
145 | 195 | (host, base_project, base_branch_name) = parse_gitlab_url(base_branch) | 200 | (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch) |
146 | 196 | self.gl.auth() | 201 | self.gl.auth() |
147 | 197 | try: | 202 | try: |
148 | 198 | base_project = self.gl.projects.get(base_project) | 203 | base_project = self.gl.projects.get(base_project) |
149 | @@ -230,7 +235,7 @@ | |||
150 | 230 | 235 | ||
151 | 231 | def get_derived_branch(self, base_branch, name, project=None, owner=None): | 236 | def get_derived_branch(self, base_branch, name, project=None, owner=None): |
152 | 232 | import gitlab | 237 | import gitlab |
154 | 233 | (host, base_project, base_branch_name) = parse_gitlab_url(base_branch) | 238 | (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch) |
155 | 234 | self.gl.auth() | 239 | self.gl.auth() |
156 | 235 | try: | 240 | try: |
157 | 236 | base_project = self.gl.projects.get(base_project) | 241 | base_project = self.gl.projects.get(base_project) |
158 | @@ -258,9 +263,9 @@ | |||
159 | 258 | def iter_proposals(self, source_branch, target_branch, status): | 263 | def iter_proposals(self, source_branch, target_branch, status): |
160 | 259 | import gitlab | 264 | import gitlab |
161 | 260 | (source_host, source_project_name, source_branch_name) = ( | 265 | (source_host, source_project_name, source_branch_name) = ( |
163 | 261 | parse_gitlab_url(source_branch)) | 266 | parse_gitlab_branch_url(source_branch)) |
164 | 262 | (target_host, target_project_name, target_branch_name) = ( | 267 | (target_host, target_project_name, target_branch_name) = ( |
166 | 263 | parse_gitlab_url(target_branch)) | 268 | parse_gitlab_branch_url(target_branch)) |
167 | 264 | if source_host != target_host: | 269 | if source_host != target_host: |
168 | 265 | raise DifferentGitLabInstances(source_host, target_host) | 270 | raise DifferentGitLabInstances(source_host, target_host) |
169 | 266 | self.gl.auth() | 271 | self.gl.auth() |
170 | @@ -281,17 +286,17 @@ | |||
171 | 281 | 286 | ||
172 | 282 | def hosts(self, branch): | 287 | def hosts(self, branch): |
173 | 283 | try: | 288 | try: |
175 | 284 | (host, project, branch_name) = parse_gitlab_url(branch) | 289 | (host, project, branch_name) = parse_gitlab_branch_url(branch) |
176 | 285 | except NotGitLabUrl: | 290 | except NotGitLabUrl: |
177 | 286 | return False | 291 | return False |
178 | 287 | return (self.gl.url == ('https://%s' % host)) | 292 | return (self.gl.url == ('https://%s' % host)) |
179 | 288 | 293 | ||
180 | 289 | @classmethod | 294 | @classmethod |
182 | 290 | def probe(cls, branch): | 295 | def probe_from_url(cls, url): |
183 | 291 | try: | 296 | try: |
185 | 292 | (host, project, branch_name) = parse_gitlab_url(branch) | 297 | (host, project) = parse_gitlab_url(url) |
186 | 293 | except NotGitLabUrl: | 298 | except NotGitLabUrl: |
188 | 294 | raise UnsupportedHoster(branch) | 299 | raise UnsupportedHoster(url) |
189 | 295 | import gitlab | 300 | import gitlab |
190 | 296 | import requests.exceptions | 301 | import requests.exceptions |
191 | 297 | try: | 302 | try: |
192 | @@ -299,12 +304,12 @@ | |||
193 | 299 | gl.auth() | 304 | gl.auth() |
194 | 300 | except requests.exceptions.SSLError: | 305 | except requests.exceptions.SSLError: |
195 | 301 | # Well, I guess it could be.. | 306 | # Well, I guess it could be.. |
197 | 302 | raise UnsupportedHoster(branch) | 307 | raise UnsupportedHoster(url) |
198 | 303 | except gitlab.GitlabGetError: | 308 | except gitlab.GitlabGetError: |
200 | 304 | raise UnsupportedHoster(branch) | 309 | raise UnsupportedHoster(url) |
201 | 305 | except gitlab.GitlabHttpError as e: | 310 | except gitlab.GitlabHttpError as e: |
202 | 306 | if e.response_code in (404, 405, 503): | 311 | if e.response_code in (404, 405, 503): |
204 | 307 | raise UnsupportedHoster(branch) | 312 | raise UnsupportedHoster(url) |
205 | 308 | else: | 313 | else: |
206 | 309 | raise | 314 | raise |
207 | 310 | return cls(gl) | 315 | return cls(gl) |
208 | @@ -332,10 +337,10 @@ | |||
209 | 332 | self.gl = gl | 337 | self.gl = gl |
210 | 333 | self.source_branch = source_branch | 338 | self.source_branch = source_branch |
211 | 334 | (self.source_host, self.source_project_name, self.source_branch_name) = ( | 339 | (self.source_host, self.source_project_name, self.source_branch_name) = ( |
213 | 335 | parse_gitlab_url(source_branch)) | 340 | parse_gitlab_branch_url(source_branch)) |
214 | 336 | self.target_branch = target_branch | 341 | self.target_branch = target_branch |
215 | 337 | (self.target_host, self.target_project_name, self.target_branch_name) = ( | 342 | (self.target_host, self.target_project_name, self.target_branch_name) = ( |
217 | 338 | parse_gitlab_url(target_branch)) | 343 | parse_gitlab_branch_url(target_branch)) |
218 | 339 | if self.source_host != self.target_host: | 344 | if self.source_host != self.target_host: |
219 | 340 | raise DifferentGitLabInstances(self.source_host, self.target_host) | 345 | raise DifferentGitLabInstances(self.source_host, self.target_host) |
220 | 341 | 346 | ||
221 | 342 | 347 | ||
222 | === modified file 'breezy/plugins/propose/launchpad.py' | |||
223 | --- breezy/plugins/propose/launchpad.py 2019-02-02 17:36:19 +0000 | |||
224 | +++ breezy/plugins/propose/launchpad.py 2019-02-15 02:29:22 +0000 | |||
225 | @@ -174,10 +174,10 @@ | |||
226 | 174 | return plausible_launchpad_url(branch.user_url) | 174 | return plausible_launchpad_url(branch.user_url) |
227 | 175 | 175 | ||
228 | 176 | @classmethod | 176 | @classmethod |
231 | 177 | def probe(cls, branch): | 177 | def probe_from_url(cls, url): |
232 | 178 | if plausible_launchpad_url(branch.user_url): | 178 | if plausible_launchpad_url(url): |
233 | 179 | return Launchpad() | 179 | return Launchpad() |
235 | 180 | raise UnsupportedHoster(branch) | 180 | raise UnsupportedHoster(url) |
236 | 181 | 181 | ||
237 | 182 | def _get_lp_git_ref_from_branch(self, branch): | 182 | def _get_lp_git_ref_from_branch(self, branch): |
238 | 183 | url, params = urlutils.split_segment_parameters(branch.user_url) | 183 | url, params = urlutils.split_segment_parameters(branch.user_url) |
239 | 184 | 184 | ||
240 | === modified file 'breezy/plugins/propose/propose.py' | |||
241 | --- breezy/plugins/propose/propose.py 2019-02-10 00:54:14 +0000 | |||
242 | +++ breezy/plugins/propose/propose.py 2019-02-15 02:29:22 +0000 | |||
243 | @@ -22,6 +22,7 @@ | |||
244 | 22 | errors, | 22 | errors, |
245 | 23 | hooks, | 23 | hooks, |
246 | 24 | registry, | 24 | registry, |
247 | 25 | urlutils, | ||
248 | 25 | ) | 26 | ) |
249 | 26 | 27 | ||
250 | 27 | 28 | ||
251 | @@ -225,9 +226,15 @@ | |||
252 | 225 | raise NotImplementedError(self.hosts) | 226 | raise NotImplementedError(self.hosts) |
253 | 226 | 227 | ||
254 | 227 | @classmethod | 228 | @classmethod |
256 | 228 | def probe(cls, branch): | 229 | def probe_from_branch(cls, branch): |
257 | 229 | """Create a Hoster object if this hoster knows about a branch.""" | 230 | """Create a Hoster object if this hoster knows about a branch.""" |
259 | 230 | raise NotImplementedError(cls.probe) | 231 | url = urlutils.split_segment_parameters(branch.user_url)[0] |
260 | 232 | return cls.probe_from_url(url) | ||
261 | 233 | |||
262 | 234 | @classmethod | ||
263 | 235 | def probe_from_url(cls, url): | ||
264 | 236 | """Create a Hoster object if this hoster knows about a URL.""" | ||
265 | 237 | raise NotImplementedError(cls.probe_from_url) | ||
266 | 231 | 238 | ||
267 | 232 | # TODO(jelmer): Some way of cleaning up old branch proposals/branches | 239 | # TODO(jelmer): Some way of cleaning up old branch proposals/branches |
268 | 233 | 240 | ||
269 | @@ -259,7 +266,7 @@ | |||
270 | 259 | return hoster | 266 | return hoster |
271 | 260 | for name, hoster_cls in hosters.items(): | 267 | for name, hoster_cls in hosters.items(): |
272 | 261 | try: | 268 | try: |
274 | 262 | hoster = hoster_cls.probe(branch) | 269 | hoster = hoster_cls.probe_from_branch(branch) |
275 | 263 | except UnsupportedHoster: | 270 | except UnsupportedHoster: |
276 | 264 | pass | 271 | pass |
277 | 265 | else: | 272 | else: |
Thanks!