Skip to content

Commit 3c99ed3

Browse files
committed
Fixed interface of the request create command
- added explicit choice of both local and remote branches - added better guessing of branches when not given
1 parent 7e70459 commit 3c99ed3

File tree

5 files changed

+86
-29
lines changed

5 files changed

+86
-29
lines changed

git_repo/repo.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
{self} [--path=<path>] [-v...] <target> request (list|ls)
1616
{self} [--path=<path>] [-v...] <target> request fetch <request>
1717
{self} [--path=<path>] [-v...] <target> request create <title> [--message=<message>]
18-
{self} [--path=<path>] [-v...] <target> request create <branch> <title> [--message=<message>]
18+
{self} [--path=<path>] [-v...] <target> request create <local_branch> <title> [--message=<message>]
19+
{self} [--path=<path>] [-v...] <target> request create <remote_branch> <local_branch> <title> [--message=<message>]
1920
{self} [--path=<path>] [-v...] <target> request <user>/<repo> (list|ls)
2021
{self} [--path=<path>] [-v...] <target> request <user>/<repo> fetch <request>
21-
{self} [--path=<path>] [-v...] <target> request <user>/<repo> create <title> [--message=<message>]
22-
{self} [--path=<path>] [-v...] <target> request <user>/<repo> create <branch> <title> [--message=<message>]
22+
{self} [--path=<path>] [-v...] <target> request <user>/<repo> create <title> [--branch=<remote>] [--message=<message>]
23+
{self} [--path=<path>] [-v...] <target> request <user>/<repo> create <local_branch> <title> [--branch=<remote>] [--message=<message>]
24+
{self} [--path=<path>] [-v...] <target> request <user>/<repo> create <remote_branch> <local_branch> <title> [--branch=<remote>] [--message=<message>]
2325
{self} [--path=<path>] [-v...] <target> gist (list|ls) [<gist>]
2426
{self} [--path=<path>] [-v...] <target> gist clone <gist>
2527
{self} [--path=<path>] [-v...] <target> gist fetch <gist> [<gist_file>]
@@ -348,13 +350,13 @@ def do_request_create(self):
348350
service = self.get_service()
349351
new_request = service.request_create(self.user_name,
350352
self.repo_name,
351-
self.branch,
353+
self.local_branch,
354+
self.remote_branch,
352355
self.title,
353356
self.message)
354-
log.info('Successfully created request of `{}` onto `{}`, with id `{}`!'.format(
355-
self.branch,
357+
log.info('Successfully created request of `{local}` onto `{}:{remote}`, with id `{ref}`!'.format(
356358
'/'.join([self.user_name, self.repo_name]),
357-
new_request)
359+
**new_request)
358360
)
359361
return 0
360362

git_repo/services/ext/github.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,18 @@ def gist_delete(self, gist_id):
151151
raise ResourceNotFoundError('Could not find gist')
152152
gist.delete()
153153

154-
def request_create(self, user, repo, branch, title, description=None):
154+
def request_create(self, user, repo, local_branch, remote_branch, title, description=None):
155155
repository = self.gh.repository(user, repo)
156156
if not repository:
157157
raise ResourceNotFoundError('Could not find repository `{}/{}`!'.format(user, repo))
158-
if not branch:
159-
branch = self.repository.active_branch.name
160-
base = repository.master_branch if repository.master_branch else 'master'
158+
if not local_branch:
159+
remote_branch = self.repository.active_branch.name or self.repository.active_branch.name
160+
if not remote_branch:
161+
local_branch = repository.master_branch or 'master'
161162
try:
162163
request = repository.create_pull(title,
163-
base=base,
164-
head=':'.join([user, branch]),
164+
base=local_branch,
165+
head=':'.join([user, remote_branch]),
165166
body=description)
166167
except github3.models.GitHubError as err:
167168
if err.code == 422:
@@ -175,7 +176,7 @@ def request_create(self, user, repo, branch, title, description=None):
175176
raise ResourceError("Unhandled formatting error: {}".format(err.errors))
176177

177178

178-
return request.number
179+
return {'local': local_branch, 'remote': remote_branch, 'ref': request.number}
179180

180181
def request_list(self, user, repo):
181182
repository = self.gh.repository(user, repo)

tests/helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ def request_fetch(self, *args, **kwarg):
115115

116116
def request_create(self, *args, **kwarg):
117117
self._did_request_create = (args, kwarg)
118-
if args[2] == 'bad':
118+
if args[2] == 'bad' or args[3] == 'bad':
119119
raise Exception('bad branch to request!')
120-
return 42
120+
local = args[2] or 'pr-test'
121+
remote = args[3] or 'base-test'
122+
return {'local': local, 'remote': remote, 'ref': 42}
121123

122124
@classmethod
123125
def get_auth_token(cls, login, password):
@@ -184,6 +186,8 @@ def setup_args(self, d, args={}):
184186
'<gist_path>': [],
185187
'request': False,
186188
'<request>': None,
189+
'<local_branch>': None,
190+
'<remote_branch>': None,
187191
'<user>/<repo>': None,
188192
}
189193
cli_args.update(d)

tests/integration/test_github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_32_request_create(self):
331331
branch='pr-test',
332332
title='PR test',
333333
description='PR description')
334-
assert r == 1
334+
assert r == {'local': 'pr-test', 'ref': 1, 'remote': 'PR test'}
335335

336336
def test_32_request_create__bad_branch(self):
337337
with pytest.raises(ResourceError):

tests/integration/test_main.py

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,45 +361,94 @@ def test_request_create(self, capsys, caplog):
361361
call(['git', 'init', '-q', self.tempdir.name])
362362
seen_args, extra_args = self.main_request_create('guyzmo/test', 0,
363363
args={
364-
'<branch>': 'pr-test',
364+
'<local_branch>': 'pr-test',
365+
'<remote_branch>': 'base-test',
365366
'<title>': 'This is a test',
366367
'--message': 'This is a test'
367368
})
368369
out, err = capsys.readouterr()
369-
assert ('guyzmo', 'test', 'pr-test', 'This is a test', 'This is a test') == seen_args
370+
assert ('guyzmo', 'test', 'pr-test', 'base-test', 'This is a test', 'This is a test') == seen_args
370371
assert {} == extra_args
371372
assert out == ''
372-
assert 'Successfully created request of `pr-test` onto `guyzmo/test`, with id `42`!' in caplog.text
373+
assert 'Successfully created request of `pr-test` onto `guyzmo/test:base-test`, with id `42`!' in caplog.text
373374

374375
def test_request_create__no_description(self, capsys, caplog):
375376
from subprocess import call
376377
call(['git', 'init', '-q', self.tempdir.name])
377378
seen_args, extra_args = self.main_request_create('guyzmo/test', 0,
378379
args={
379-
'<branch>': 'pr-test',
380+
'<local_branch>': 'pr-test',
381+
'<remote_branch>': 'base-test',
380382
'<title>': 'This is a test',
381383
})
382384
out, err = capsys.readouterr()
383-
assert ('guyzmo', 'test', 'pr-test', 'This is a test', None) == seen_args
385+
assert ('guyzmo', 'test', 'pr-test', 'base-test', 'This is a test', None) == seen_args
384386
assert {} == extra_args
385387
assert out == ''
386-
assert 'Successfully created request of `pr-test` onto `guyzmo/test`, with id `42`!' in caplog.text
388+
assert 'Successfully created request of `pr-test` onto `guyzmo/test:base-test`, with id `42`!' in caplog.text
387389

388-
def test_request_create__bad_branch(self, capsys, caplog):
390+
def test_request_create__bad_local_branch(self, capsys, caplog):
389391
from subprocess import call
390392
call(['git', 'init', '-q', self.tempdir.name])
391393
seen_args, extra_args = self.main_request_create('guyzmo/test', 2,
392394
args={
393-
'<branch>': 'bad',
395+
'<local_branch>': 'bad',
396+
'<remote_branch>': 'base-test',
394397
'<title>': 'This is a test',
395398
'--message': 'This is a test'
396399
})
397400
out, err = capsys.readouterr()
398-
assert ('guyzmo', 'test', 'bad', 'This is a test', 'This is a test') == seen_args
401+
assert ('guyzmo', 'test', 'bad', 'base-test', 'This is a test', 'This is a test') == seen_args
399402
assert {} == extra_args
400403
assert out == ''
401404
assert 'Fatal error: bad branch to request!' in caplog.text
402405

406+
def test_request_create__bad_remote_branch(self, capsys, caplog):
407+
from subprocess import call
408+
call(['git', 'init', '-q', self.tempdir.name])
409+
seen_args, extra_args = self.main_request_create('guyzmo/test', 2,
410+
args={
411+
'<local_branch>': 'pr-test',
412+
'<remote_branch>': 'bad',
413+
'<title>': 'This is a test',
414+
'--message': 'This is a test'
415+
})
416+
out, err = capsys.readouterr()
417+
assert ('guyzmo', 'test', 'pr-test', 'bad', 'This is a test', 'This is a test') == seen_args
418+
assert {} == extra_args
419+
assert out == ''
420+
assert 'Fatal error: bad branch to request!' in caplog.text
421+
422+
def test_request_create__no_local_branch(self, capsys, caplog):
423+
from subprocess import call
424+
call(['git', 'init', '-q', self.tempdir.name])
425+
seen_args, extra_args = self.main_request_create('guyzmo/test', 0,
426+
args={
427+
'<remote_branch>': 'base-test',
428+
'<title>': 'This is a test',
429+
'--message': 'This is a test'
430+
})
431+
out, err = capsys.readouterr()
432+
assert ('guyzmo', 'test', None, 'base-test', 'This is a test', 'This is a test') == seen_args
433+
assert {} == extra_args
434+
assert out == ''
435+
assert 'Successfully created request of `pr-test` onto `guyzmo/test:base-test`, with id `42`!' in caplog.text
436+
437+
def test_request_create__no_remote_branch(self, capsys, caplog):
438+
from subprocess import call
439+
call(['git', 'init', '-q', self.tempdir.name])
440+
seen_args, extra_args = self.main_request_create('guyzmo/test', 0,
441+
args={
442+
'<local_branch>': 'pr-test',
443+
'<title>': 'This is a test',
444+
'--message': 'This is a test'
445+
})
446+
out, err = capsys.readouterr()
447+
assert ('guyzmo', 'test', 'pr-test', None, 'This is a test', 'This is a test') == seen_args
448+
assert {} == extra_args
449+
assert out == ''
450+
assert 'Successfully created request of `pr-test` onto `guyzmo/test:base-test`, with id `42`!' in caplog.text
451+
403452
def test_open(self):
404453
repo_slug, seen_args = self.main_open('guyzmo/git-repo', 0)
405454
assert ('guyzmo', 'git-repo') == repo_slug
@@ -464,15 +513,16 @@ def test_request_create__no_repo_slug(self, capsys, caplog):
464513
self._create_repository()
465514
seen_args, extra_args = self.main_request_create(rc=0,
466515
args={
467-
'<branch>': 'pr-test',
516+
'<local_branch>': 'pr-test',
517+
'<remote_branch>': 'base-test',
468518
'<title>': 'This is a test',
469519
'--message': 'This is a test'
470520
})
471521
out, err = capsys.readouterr()
472-
assert ('guyzmo', 'git-repo', 'pr-test', 'This is a test', 'This is a test') == seen_args
522+
assert ('guyzmo', 'git-repo', 'pr-test', 'base-test', 'This is a test', 'This is a test') == seen_args
473523
assert {} == extra_args
474524
assert out == ''
475-
assert 'Successfully created request of `pr-test` onto `guyzmo/git-repo`, with id `42`!' in caplog.text
525+
assert 'Successfully created request of `pr-test` onto `guyzmo/git-repo:base-test`, with id `42`!' in caplog.text
476526

477527
def test_config(self, capsys, caplog):
478528
import sys, io, getpass

0 commit comments

Comments
 (0)