@@ -2,15 +2,11 @@ package command
22
33import (
44 "fmt"
5- "net/url"
6- "os"
75 "os/exec"
86 "strconv"
9- "strings"
107
118 "github.com/github/gh-cli/api"
12- "github.com/github/gh-cli/git"
13- "github.com/github/gh-cli/github"
9+ "github.com/github/gh-cli/context"
1410 "github.com/github/gh-cli/utils"
1511 "github.com/spf13/cobra"
1612)
@@ -52,7 +48,11 @@ func prList(cmd *cobra.Command, args []string) error {
5248 if prPayload .CurrentPR != nil {
5349 printPrs (* prPayload .CurrentPR )
5450 } else {
55- message := fmt .Sprintf (" There is no pull request associated with %s" , utils .Cyan ("[" + currentBranch ()+ "]" ))
51+ currentBranch , err := context .Current ().Branch ()
52+ if err != nil {
53+ return err
54+ }
55+ message := fmt .Sprintf (" There is no pull request associated with %s" , utils .Cyan ("[" + currentBranch + "]" ))
5656 printMessage (message )
5757 }
5858 fmt .Println ()
@@ -77,19 +77,26 @@ func prList(cmd *cobra.Command, args []string) error {
7777}
7878
7979func prView (cmd * cobra.Command , args []string ) error {
80- project := project ()
80+ baseRepo , err := context .Current ().BaseRepo ()
81+ if err != nil {
82+ return err
83+ }
8184
8285 var openURL string
8386 if len (args ) > 0 {
8487 if prNumber , err := strconv .Atoi (args [0 ]); err == nil {
85- openURL = project .WebURL ("" , "" , fmt .Sprintf ("pull/%d" , prNumber ))
88+ // TODO: move URL generation into GitHubRepository
89+ openURL = fmt .Sprintf ("https://github.com/%s/%s/pull/%d" , baseRepo .Owner , baseRepo .Name , prNumber )
8690 } else {
8791 return fmt .Errorf ("invalid pull request number: '%s'" , args [0 ])
8892 }
8993 } else {
9094 prPayload , err := api .PullRequests ()
9195 if err != nil || prPayload .CurrentPR == nil {
92- branch := currentBranch ()
96+ branch , err := context .Current ().Branch ()
97+ if err != nil {
98+ return err
99+ }
93100 return fmt .Errorf ("The [%s] branch has no open PRs" , branch )
94101 }
95102 openURL = prPayload .CurrentPR .URL
@@ -130,41 +137,3 @@ func openInBrowser(url string) error {
130137 endingArgs := append (launcher [1 :], url )
131138 return exec .Command (launcher [0 ], endingArgs ... ).Run ()
132139}
133-
134- // The functions below should be replaced at some point by the context package
135- // 🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨
136- func currentBranch () string {
137- currentBranch , err := git .Head ()
138- if err != nil {
139- panic (err )
140- }
141-
142- return strings .Replace (currentBranch , "refs/heads/" , "" , 1 )
143- }
144-
145- func project () github.Project {
146- if repoFromEnv := os .Getenv ("GH_REPO" ); repoFromEnv != "" {
147- repoURL , err := url .Parse (fmt .Sprintf ("https://github.com/%s.git" , repoFromEnv ))
148- if err != nil {
149- panic (err )
150- }
151- project , err := github .NewProjectFromURL (repoURL )
152- if err != nil {
153- panic (err )
154- }
155- return * project
156- }
157-
158- remotes , err := github .Remotes ()
159- if err != nil {
160- panic (err )
161- }
162-
163- for _ , remote := range remotes {
164- if project , err := remote .Project (); err == nil {
165- return * project
166- }
167- }
168-
169- panic ("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?" )
170- }
0 commit comments