Skip to content

Commit 3a65cca

Browse files
committed
🚧 Improves request fetch function and refactor request create
Make it so the look up remote code is also used for fetching refs. so now the mechanism is: 1. convert_user_into_remote() 2. extracts_ref (that returns a ref given an user and remote Updated the request create code ; and the request fetch code Signed-off-by: Guyzmo <[email protected]>
1 parent 21be27e commit 3a65cca

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

git_repo/services/ext/github.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,6 @@ def gist_delete(self, gist_id):
221221
raise ResourceNotFoundError('Could not find gist')
222222
gist.delete()
223223

224-
def _convert_username_into_ref(self, username, from_branch):
225-
# builds a ref with an username and a branch
226-
# this method parses the repository's remotes to find the url matching username
227-
# and containing the given branch and returns the corresponding ref
228-
def exists_ref(ref_name):
229-
# this function takes a given ref and returns true if it actually exists
230-
for ref in self.repository.refs:
231-
if ref.name.endswith(ref_name):
232-
return True
233-
return False
234-
235-
remotes = {remote.name: list(remote.urls) for remote in self.repository.remotes}
236-
for name in ('upstream', self.name) + tuple(remotes.keys()):
237-
if name in remotes and name != 'all':
238-
for url in remotes[name]:
239-
if self.fqdn in url and username == url.split(':')[1].split('/')[0]:
240-
ref = '{}/{}'.format(name, from_branch)
241-
if exists_ref(ref):
242-
return ref
243-
244-
raise ArgumentError('Could not find a remote for user {} containing branch {}'.format(username, from_branch))
245-
246224
def request_create(self, user, repo, from_branch, onto_branch, title=None, description=None, auto_slug=False, edit=None):
247225
repository = self.gh.repository(user, repo)
248226
if not repository:
@@ -263,7 +241,8 @@ def request_create(self, user, repo, from_branch, onto_branch, title=None, descr
263241
# the branch we're currently working on
264242
if not onto_branch:
265243
onto_branch = repository.default_branch or 'master'
266-
from_ref = self._convert_username_into_ref(user, from_branch)
244+
245+
from_ref = self._extracts_ref(user, from_branch)
267246
if user != repository.owner.login:
268247
from_branch = ':'.join([user, from_branch])
269248

@@ -307,8 +286,9 @@ def request_fetch(self, user, repo, request, pull=False, force=False):
307286
if pull:
308287
raise NotImplementedError('Pull operation on requests for merge are not yet supported')
309288
try:
289+
remote_names = list(self._convert_user_into_remote(user))
310290
for remote in self.repository.remotes:
311-
if remote.name == self.name:
291+
if remote.name in remote_names:
312292
local_branch_name = 'requests/github/{}'.format(request)
313293
self.fetch(
314294
remote,

git_repo/services/service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,27 @@ def url_rw(self):
226226
url = self.ssh_url
227227
return url if '@' in url else '@'.join([self.git_user, url])
228228

229+
def _convert_user_into_remote(self, username, exclude=['all']):
230+
# builds a ref with an username and a branch
231+
# this method parses the repository's remotes to find the url matching username
232+
# and containing the given branch and returns the corresponding ref
233+
234+
remotes = {remote.name: list(remote.urls) for remote in self.repository.remotes}
235+
for name in ('upstream', self.name) + tuple(remotes.keys()):
236+
if name in remotes and name not in exclude:
237+
for url in remotes[name]:
238+
if self.fqdn in url and username == url.split(':')[1].split('/')[0]:
239+
yield name
240+
241+
def _extracts_ref(self, user, from_branch):
242+
for name in self._convert_user_into_remote(user):
243+
ref_name = '{}/{}'.format(name, from_branch)
244+
for ref in self.repository.refs:
245+
if ref.name.endswith(ref_name):
246+
return ref
247+
248+
raise ArgumentError('Could not find a remote for user {} containing branch {}'.format(username))
249+
229250
def format_path(self, repository, namespace=None, rw=False):
230251
'''format the repository's URL
231252

tests/helpers.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ def action_request_create(self,
689689
namespace, repository, branch,
690690
title, description, service,
691691
create_repository='test_create_requests',
692-
create_branch='pr-test'):
692+
create_branch='pr-test',
693+
auto_slug=False):
693694
'''
694695
Here we are testing the subcommand 'request create'.
695696
@@ -739,6 +740,16 @@ def prepare_project_for_test():
739740
self.repository.git.add('second_file')
740741
self.repository.git.commit(message='Second commit')
741742
self.repository.git.push(service, create_branch)
743+
existing_remotes = [r.name for r in self.repository.remotes]
744+
if 'all' in existing_remotes:
745+
r_all = self.repository.remote('all')
746+
else:
747+
r_all = self.repository.create_remote('all', '')
748+
for name in ('github', 'gitlab', 'bitbucket', 'gogs', 'upstream'):
749+
if name not in existing_remotes:
750+
kw = dict(user=namespace, project=repository, host=name)
751+
self.repository.create_remote(name, 'git@{host}.com:{user}/{project}'.format(**kw))
752+
r_all.add_url('git@{host}.com:{user}/{project}'.format(**kw))
742753
yield
743754
if will_record:
744755
self.service.delete(create_repository)
@@ -747,13 +758,17 @@ def prepare_project_for_test():
747758
with prepare_project_for_test():
748759
with self.recorder.use_cassette(cassette_name):
749760
self.service.connect()
761+
def test_edit(repository, from_branch):
762+
return "PR title", "PR body"
750763
request = self.service.request_create(
751-
namespace,
752-
repository,
753-
branch,
754-
title,
755-
description
756-
)
764+
namespace,
765+
repository,
766+
branch,
767+
create_branch,
768+
title=title,
769+
description=description,
770+
auto_slug=auto_slug,
771+
edit=test_edit)
757772
return request
758773

759774
def action_gist_list(self, gist=None, gist_list_data=[]):

0 commit comments

Comments
 (0)