@@ -302,59 +302,16 @@ func prView(cmd *cobra.Command, args []string) error {
302302 return err
303303 }
304304
305- var baseRepo ghrepo.Interface
306- var prArg string
307- if len (args ) > 0 {
308- prArg = args [0 ]
309- if prNum , repo := prFromURL (prArg ); repo != nil {
310- prArg = prNum
311- baseRepo = repo
312- }
313- }
314-
315- if baseRepo == nil {
316- baseRepo , err = determineBaseRepo (apiClient , cmd , ctx )
317- if err != nil {
318- return err
319- }
320- }
321-
322305 web , err := cmd .Flags ().GetBool ("web" )
323306 if err != nil {
324307 return err
325308 }
326309
327- var openURL string
328- var pr * api.PullRequest
329- if len (args ) > 0 {
330- pr , err = prFromArg (apiClient , baseRepo , prArg )
331- if err != nil {
332- return err
333- }
334- openURL = pr .URL
335- } else {
336- prNumber , branchWithOwner , err := prSelectorForCurrentBranch (ctx , baseRepo )
337- if err != nil {
338- return err
339- }
340-
341- if prNumber > 0 {
342- openURL = fmt .Sprintf ("https://github.com/%s/pull/%d" , ghrepo .FullName (baseRepo ), prNumber )
343- if ! web {
344- pr , err = api .PullRequestByNumber (apiClient , baseRepo , prNumber )
345- if err != nil {
346- return err
347- }
348- }
349- } else {
350- pr , err = api .PullRequestForBranch (apiClient , baseRepo , "" , branchWithOwner )
351- if err != nil {
352- return err
353- }
354-
355- openURL = pr .URL
356- }
310+ pr , _ , err := prFromArgs (ctx , apiClient , cmd , args )
311+ if err != nil {
312+ return err
357313 }
314+ openURL := pr .URL
358315
359316 if web {
360317 fmt .Fprintf (cmd .ErrOrStderr (), "Opening %s in your browser.\n " , openURL )
@@ -372,12 +329,7 @@ func prClose(cmd *cobra.Command, args []string) error {
372329 return err
373330 }
374331
375- baseRepo , err := determineBaseRepo (apiClient , cmd , ctx )
376- if err != nil {
377- return err
378- }
379-
380- pr , err := prFromArg (apiClient , baseRepo , args [0 ])
332+ pr , baseRepo , err := prFromArgs (ctx , apiClient , cmd , args )
381333 if err != nil {
382334 return err
383335 }
@@ -407,12 +359,7 @@ func prReopen(cmd *cobra.Command, args []string) error {
407359 return err
408360 }
409361
410- baseRepo , err := determineBaseRepo (apiClient , cmd , ctx )
411- if err != nil {
412- return err
413- }
414-
415- pr , err := prFromArg (apiClient , baseRepo , args [0 ])
362+ pr , baseRepo , err := prFromArgs (ctx , apiClient , cmd , args )
416363 if err != nil {
417364 return err
418365 }
@@ -444,41 +391,11 @@ func prMerge(cmd *cobra.Command, args []string) error {
444391 return err
445392 }
446393
447- baseRepo , err := determineBaseRepo ( apiClient , cmd , ctx )
394+ pr , baseRepo , err := prFromArgs ( ctx , apiClient , cmd , args )
448395 if err != nil {
449396 return err
450397 }
451398
452- var pr * api.PullRequest
453- if len (args ) > 0 {
454- var prNumber string
455- n , _ := prFromURL (args [0 ])
456- if n != "" {
457- prNumber = n
458- } else {
459- prNumber = args [0 ]
460- }
461-
462- pr , err = prFromArg (apiClient , baseRepo , prNumber )
463- if err != nil {
464- return err
465- }
466- } else {
467- prNumber , branchWithOwner , err := prSelectorForCurrentBranch (ctx , baseRepo )
468- if err != nil {
469- return err
470- }
471-
472- if prNumber != 0 {
473- pr , err = api .PullRequestByNumber (apiClient , baseRepo , prNumber )
474- } else {
475- pr , err = api .PullRequestForBranch (apiClient , baseRepo , "" , branchWithOwner )
476- }
477- if err != nil {
478- return err
479- }
480- }
481-
482399 if pr .Mergeable == "CONFLICTING" {
483400 err := fmt .Errorf ("%s Pull request #%d has conflicts and isn't mergeable " , utils .Red ("!" ), pr .Number )
484401 return err
@@ -712,41 +629,11 @@ func prReady(cmd *cobra.Command, args []string) error {
712629 return err
713630 }
714631
715- baseRepo , err := determineBaseRepo ( apiClient , cmd , ctx )
632+ pr , baseRepo , err := prFromArgs ( ctx , apiClient , cmd , args )
716633 if err != nil {
717634 return err
718635 }
719636
720- var pr * api.PullRequest
721- if len (args ) > 0 {
722- var prNumber string
723- n , _ := prFromURL (args [0 ])
724- if n != "" {
725- prNumber = n
726- } else {
727- prNumber = args [0 ]
728- }
729-
730- pr , err = prFromArg (apiClient , baseRepo , prNumber )
731- if err != nil {
732- return err
733- }
734- } else {
735- prNumber , branchWithOwner , err := prSelectorForCurrentBranch (ctx , baseRepo )
736- if err != nil {
737- return err
738- }
739-
740- if prNumber != 0 {
741- pr , err = api .PullRequestByNumber (apiClient , baseRepo , prNumber )
742- } else {
743- pr , err = api .PullRequestForBranch (apiClient , baseRepo , "" , branchWithOwner )
744- }
745- if err != nil {
746- return err
747- }
748- }
749-
750637 if pr .Closed {
751638 err := fmt .Errorf ("%s Pull request #%d is closed. Only draft pull requests can be marked as \" ready for review\" " , utils .Red ("!" ), pr .Number )
752639 return err
@@ -941,23 +828,6 @@ func prProjectList(pr api.PullRequest) string {
941828 return list
942829}
943830
944- var prURLRE = regexp .MustCompile (`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)` )
945-
946- func prFromURL (arg string ) (string , ghrepo.Interface ) {
947- if m := prURLRE .FindStringSubmatch (arg ); m != nil {
948- return m [3 ], ghrepo .New (m [1 ], m [2 ])
949- }
950- return "" , nil
951- }
952-
953- func prFromArg (apiClient * api.Client , baseRepo ghrepo.Interface , arg string ) (* api.PullRequest , error ) {
954- if prNumber , err := strconv .Atoi (strings .TrimPrefix (arg , "#" )); err == nil {
955- return api .PullRequestByNumber (apiClient , baseRepo , prNumber )
956- }
957-
958- return api .PullRequestForBranch (apiClient , baseRepo , "" , arg )
959- }
960-
961831func prSelectorForCurrentBranch (ctx context.Context , baseRepo ghrepo.Interface ) (prNumber int , prHeadRef string , err error ) {
962832 prHeadRef , err = ctx .Branch ()
963833 if err != nil {
0 commit comments