Skip to content

Commit 6e8513c

Browse files
committed
👍 Merge branch devel ; 🔖 Version bump to 1.8.1
🚒 Bugfixes * Fixed race condition for gitlab initialisation (cf #97) * Fixed request create behaviour with github (cf #94) Signed-off-by: Guyzmo <[email protected]>
2 parents ee6384d + 6a7e688 commit 6e8513c

30 files changed

+131
-52
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.0
1+
1.8.1

git_repo/repo.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,29 @@ def init(self): # pragma: no cover
169169
if 'GIT_WORK_TREE' in os.environ.keys() or 'GIT_DIR' in os.environ.keys():
170170
del os.environ['GIT_WORK_TREE']
171171

172-
def _guess_repo_slug(self, repository, service):
172+
def _guess_repo_slug(self, repository, service, resolve_targets=None):
173173
config = repository.config_reader()
174-
target = service.name
174+
if resolve_targets:
175+
targets = [target.format(service=service.name) for target in resolve_targets]
176+
else:
177+
targets = (service.name, 'upstream', 'origin')
175178
for remote in repository.remotes:
176-
if remote.name in (target, 'upstream', 'origin'):
179+
if remote.name in targets:
177180
for url in remote.urls:
178181
if url.startswith('https'):
179182
if url.endswith('.git'):
180183
url = url[:-4]
181184
*_, user, name = url.split('/')
182185
self.set_repo_slug('/'.join([user, name]))
183-
break
186+
return
184187
elif url.startswith('git@'):
185188
if url.endswith('.git'):
186189
url = url[:-4]
187190
_, repo_slug = url.split(':')
188191
self.set_repo_slug(repo_slug)
189-
break
192+
return
190193

191-
def get_service(self, lookup_repository=True):
194+
def get_service(self, lookup_repository=True, resolve_targets=None):
192195
if not lookup_repository:
193196
service = RepositoryService.get_service(None, self.target)
194197
service.connect()
@@ -203,7 +206,7 @@ def get_service(self, lookup_repository=True):
203206
raise FileNotFoundError('Cannot find path to the repository.')
204207
service = RepositoryService.get_service(repository, self.target)
205208
if not self.repo_name:
206-
self._guess_repo_slug(repository, service)
209+
self._guess_repo_slug(repository, service, resolve_targets)
207210
return service
208211

209212
'''Argument storage'''
@@ -408,13 +411,14 @@ def do_request_list(self):
408411

409412
@register_action('request', 'create')
410413
def do_request_create(self):
411-
service = self.get_service()
414+
service = self.get_service(resolve_targets=('upstream', '{service}', 'origin'))
412415
new_request = service.request_create(self.user_name,
413416
self.repo_name,
414417
self.local_branch,
415418
self.remote_branch,
416419
self.title,
417-
self.message)
420+
self.message,
421+
self.repo_slug != None)
418422
log.info('Successfully created request of `{local}` onto `{}:{remote}`, with id `{ref}`!'.format(
419423
'/'.join([self.user_name, self.repo_name]),
420424
**new_request)

git_repo/services/ext/github.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,29 +231,50 @@ def gist_delete(self, gist_id):
231231
raise ResourceNotFoundError('Could not find gist')
232232
gist.delete()
233233

234-
def request_create(self, user, repo, local_branch, remote_branch, title, description=None):
234+
def request_create(self, user, repo, from_branch, onto_branch, title, description=None, auto_slug=False):
235235
repository = self.gh.repository(user, repo)
236236
if not repository:
237237
raise ResourceNotFoundError('Could not find repository `{}/{}`!'.format(user, repo))
238-
if not remote_branch:
239-
remote_branch = self.repository.active_branch.name
240-
if not local_branch:
241-
local_branch = repository.master_branch or 'master'
238+
# when no repo slug has been given to `git-repo X request create`
239+
if auto_slug:
240+
# then chances are current repository is a fork of the target
241+
# repository we want to push to
242+
if repository.fork:
243+
user = repository.parent.owner.login
244+
repo = repository.parent.name
245+
from_branch = from_branch or repository.parent.default_branch
246+
# if no onto branch has been defined, take the default one
247+
# with a fallback on master
248+
if not from_branch:
249+
from_branch = self.repository.active_branch.name
250+
# if no from branch has been defined, chances are we want to push
251+
# the branch we're currently working on
252+
if not onto_branch:
253+
onto_branch = repository.default_branch or 'master'
254+
if self.username != repository.owner.login:
255+
from_branch = ':'.join([self.username, from_branch])
242256
try:
243257
request = repository.create_pull(title,
244-
base=local_branch,
245-
head=':'.join([user, remote_branch]),
258+
base=onto_branch,
259+
head=from_branch,
246260
body=description)
247261
except github3.models.GitHubError as err:
248262
if err.code == 422:
249263
if err.message == 'Validation Failed':
250264
for error in err.errors:
251265
if 'message' in error:
252266
raise ResourceError(error['message'])
267+
if error.get('code', '') == 'invalid':
268+
if error.get('field', '') == 'head':
269+
raise ResourceError(
270+
'Invalid source branch. ' \
271+
'Check it has been pushed first.')
272+
if error.get('field', '') == 'base':
273+
raise ResourceError( 'Invalid target branch.')
253274
raise ResourceError("Unhandled formatting error: {}".format(err.errors))
254275
raise ResourceError(err.message)
255276

256-
return {'local': local_branch, 'remote': remote_branch, 'ref': request.number}
277+
return {'local': from_branch, 'remote': onto_branch, 'ref': request.number}
257278

258279
def request_list(self, user, repo):
259280
repository = self.gh.repository(user, repo)

git_repo/services/ext/gitlab.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class GitlabService(RepositoryService):
1919
fqdn = 'gitlab.com'
2020

2121
def __init__(self, *args, **kwarg):
22+
self.gl = gitlab.Gitlab(self.url_ro)
2223
super().__init__(*args, **kwarg)
23-
self.gl = gitlab.Gitlab(self.url_ro, ssl_verify=not self.insecure)
2424

2525
def connect(self):
26+
self.gl.ssl_verify = not self.insecure
2627
self.gl.set_url(self.url_ro)
2728
self.gl.set_token(self._privatekey)
2829
self.gl.token_auth()
@@ -266,7 +267,7 @@ def gist_delete(self, snippet):
266267

267268
return snippet.delete()
268269

269-
def request_create(self, user, repo, local_branch, remote_branch, title, description=None):
270+
def request_create(self, user, repo, local_branch, remote_branch, title, description=None, auto_slug=False):
270271
try:
271272
repository = self.gl.projects.get('/'.join([user, repo]))
272273
if not repository:

git_repo/services/service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def store_config(cls, config, **kwarg):
7878
section = 'gitrepo "{}"'.format(cls.name)
7979
for option, value in kwarg.items():
8080
if option not in cls.config_options:
81-
raise ArgumentError('Option {} is invalid and cannot be setup.')
81+
raise ArgumentError('Option {} is invalid and cannot be setup.'.format(option))
8282
config.set_value(section, option, value)
8383

8484
@classmethod
@@ -120,7 +120,7 @@ def get_service(cls, repository, command):
120120
if 'type' not in config:
121121
raise ValueError('Missing service type for custom service.')
122122
if config['type'] not in cls.service_map:
123-
raise ValueError('Service type {} does not exists.')
123+
raise ValueError('Service type {} does not exists.'.format(config['type']))
124124
service = cls.service_map.get(config['type'], cls)
125125

126126
cls._current = service(repository, config)
@@ -429,6 +429,9 @@ def request_fetch(self, user, repo, request, pull=False): #pragma: no cover
429429
'''
430430
raise NotImplementedError
431431

432+
def request_create(self, user, repo, from_branch, onto_branch, title, description=None, auto_slug=False):
433+
raise NotImplementedError
434+
432435
@property
433436
def user(self): #pragma: no cover
434437
raise NotImplementedError

tests/integration/cassettes/test_bitbucket_test_00_fork.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@
9494
}
9595
],
9696
"recorded_with": "betamax/0.5.1"
97-
}
97+
}

tests/integration/cassettes/test_bitbucket_test_01_create.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@
9393
}
9494
],
9595
"recorded_with": "betamax/0.5.1"
96-
}
96+
}

tests/integration/cassettes/test_bitbucket_test_02_delete.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@
9292
}
9393
],
9494
"recorded_with": "betamax/0.5.1"
95-
}
95+
}

tests/integration/cassettes/test_bitbucket_test_03_delete_nouser.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@
9292
}
9393
],
9494
"recorded_with": "betamax/0.5.1"
95-
}
95+
}

tests/integration/cassettes/test_bitbucket_test_04_clone.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
}
4848
],
4949
"recorded_with": "betamax/0.5.1"
50-
}
50+
}

0 commit comments

Comments
 (0)