@@ -2,14 +2,10 @@ package command
22
33import (
44 "fmt"
5- "net/url"
6- "os"
75 "strconv"
8- "strings"
96
107 "github.com/github/gh-cli/api"
11- "github.com/github/gh-cli/git"
12- "github.com/github/gh-cli/github"
8+ "github.com/github/gh-cli/context"
139 "github.com/github/gh-cli/utils"
1410 "github.com/spf13/cobra"
1511)
@@ -51,7 +47,11 @@ func prList(cmd *cobra.Command, args []string) error {
5147 if prPayload .CurrentPR != nil {
5248 printPrs (* prPayload .CurrentPR )
5349 } else {
54- message := fmt .Sprintf (" There is no pull request associated with %s" , utils .Cyan ("[" + currentBranch ()+ "]" ))
50+ currentBranch , err := context .Current ().Branch ()
51+ if err != nil {
52+ return err
53+ }
54+ message := fmt .Sprintf (" There is no pull request associated with %s" , utils .Cyan ("[" + currentBranch + "]" ))
5555 printMessage (message )
5656 }
5757 fmt .Println ()
@@ -76,12 +76,16 @@ func prList(cmd *cobra.Command, args []string) error {
7676}
7777
7878func prView (cmd * cobra.Command , args []string ) error {
79- project := project ()
79+ baseRepo , err := context .Current ().BaseRepo ()
80+ if err != nil {
81+ return err
82+ }
8083
8184 var openURL string
8285 if len (args ) > 0 {
8386 if prNumber , err := strconv .Atoi (args [0 ]); err == nil {
84- openURL = project .WebURL ("" , "" , fmt .Sprintf ("pull/%d" , prNumber ))
87+ // TODO: move URL generation into GitHubRepository
88+ openURL = fmt .Sprintf ("https://github.com/%s/%s/pull/%d" , baseRepo .Owner , baseRepo .Name , prNumber )
8589 } else {
8690 return fmt .Errorf ("invalid pull request number: '%s'" , args [0 ])
8791 }
@@ -90,7 +94,10 @@ func prView(cmd *cobra.Command, args []string) error {
9094 if err != nil {
9195 return err
9296 } else if prPayload .CurrentPR == nil {
93- branch := currentBranch ()
97+ branch , err := context .Current ().Branch ()
98+ if err != nil {
99+ return err
100+ }
94101 fmt .Printf ("The [%s] branch has no open PRs" , branch )
95102 return nil
96103 }
@@ -123,41 +130,3 @@ func truncateTitle(title string) string {
123130 }
124131 return title
125132}
126-
127- // The functions below should be replaced at some point by the context package
128- // 🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨
129- func currentBranch () string {
130- currentBranch , err := git .Head ()
131- if err != nil {
132- panic (err )
133- }
134-
135- return strings .Replace (currentBranch , "refs/heads/" , "" , 1 )
136- }
137-
138- func project () github.Project {
139- if repoFromEnv := os .Getenv ("GH_REPO" ); repoFromEnv != "" {
140- repoURL , err := url .Parse (fmt .Sprintf ("https://github.com/%s.git" , repoFromEnv ))
141- if err != nil {
142- panic (err )
143- }
144- project , err := github .NewProjectFromURL (repoURL )
145- if err != nil {
146- panic (err )
147- }
148- return * project
149- }
150-
151- remotes , err := github .Remotes ()
152- if err != nil {
153- panic (err )
154- }
155-
156- for _ , remote := range remotes {
157- if project , err := remote .Project (); err == nil {
158- return * project
159- }
160- }
161-
162- panic ("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?" )
163- }
0 commit comments