@@ -221,6 +221,28 @@ def gist_delete(self, gist_id):
221
221
raise ResourceNotFoundError ('Could not find gist' )
222
222
gist .delete ()
223
223
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
+
224
246
def request_create (self , user , repo , from_branch , onto_branch , title = None , description = None , auto_slug = False , edit = None ):
225
247
repository = self .gh .repository (user , repo )
226
248
if not repository :
@@ -241,10 +263,13 @@ def request_create(self, user, repo, from_branch, onto_branch, title=None, descr
241
263
# the branch we're currently working on
242
264
if not onto_branch :
243
265
onto_branch = repository .default_branch or 'master'
244
- if self .username != repository .owner .login :
245
- from_branch = ':' .join ([self .username , from_branch ])
266
+ from_ref = self ._convert_username_into_ref (user , from_branch )
267
+ if user != repository .owner .login :
268
+ from_branch = ':' .join ([user , from_branch ])
269
+
270
+ # translate from github username to git remote name
246
271
if not title and not description and edit :
247
- title , description = edit (self .repository , from_branch )
272
+ title , description = edit (self .repository , from_ref )
248
273
if not title and not description :
249
274
raise ArgumentError ('Missing message for request creation' )
250
275
try :
0 commit comments