@@ -231,29 +231,50 @@ def gist_delete(self, gist_id):
231
231
raise ResourceNotFoundError ('Could not find gist' )
232
232
gist .delete ()
233
233
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 ):
235
235
repository = self .gh .repository (user , repo )
236
236
if not repository :
237
237
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 ])
242
256
try :
243
257
request = repository .create_pull (title ,
244
- base = local_branch ,
245
- head = ':' . join ([ user , remote_branch ]) ,
258
+ base = onto_branch ,
259
+ head = from_branch ,
246
260
body = description )
247
261
except github3 .models .GitHubError as err :
248
262
if err .code == 422 :
249
263
if err .message == 'Validation Failed' :
250
264
for error in err .errors :
251
265
if 'message' in error :
252
266
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.' )
253
274
raise ResourceError ("Unhandled formatting error: {}" .format (err .errors ))
254
275
raise ResourceError (err .message )
255
276
256
- return {'local' : local_branch , 'remote' : remote_branch , 'ref' : request .number }
277
+ return {'local' : from_branch , 'remote' : onto_branch , 'ref' : request .number }
257
278
258
279
def request_list (self , user , repo ):
259
280
repository = self .gh .repository (user , repo )
0 commit comments