Skip to content

Commit f0d8c65

Browse files
bbutkovicvilmibm
authored andcommitted
pr view, status, list parent repo instead of fork
1 parent c23b9d1 commit f0d8c65

2 files changed

Lines changed: 92 additions & 4 deletions

File tree

command/pr.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ func init() {
2929
prListCmd.Flags().StringP("base", "B", "", "Filter by base branch")
3030
prListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label")
3131
prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
32+
prListCmd.Flags().BoolP("self", "S", false, "Query current repository instead of forked parent")
3233

3334
prViewCmd.Flags().BoolP("preview", "p", false, "Display preview of pull request content")
35+
prViewCmd.Flags().BoolP("self", "S", false, "Query current repository instead of forked parent")
36+
37+
prStatusCmd.Flags().BoolP("self", "S", false, "Query current repository instead of forked parent")
3438
}
3539

3640
var prCmd = &cobra.Command{
@@ -70,7 +74,12 @@ func prStatus(cmd *cobra.Command, args []string) error {
7074
return err
7175
}
7276

73-
baseRepo, err := ctx.BaseRepo()
77+
referSelf, _ := cmd.Flags().GetBool("self")
78+
if referSelf == false {
79+
ctx = context.ExpandOnline(ctx, apiClient)
80+
}
81+
82+
baseRepo, err := context.DetermineRepo(ctx, referSelf)
7483
if err != nil {
7584
return err
7685
}
@@ -129,7 +138,12 @@ func prList(cmd *cobra.Command, args []string) error {
129138
return err
130139
}
131140

132-
baseRepo, err := ctx.BaseRepo()
141+
referSelf, _ := cmd.Flags().GetBool("self")
142+
if referSelf == false {
143+
ctx = context.ExpandOnline(ctx, apiClient)
144+
}
145+
146+
baseRepo, err := context.DetermineRepo(ctx, referSelf)
133147
if err != nil {
134148
return err
135149
}
@@ -240,12 +254,18 @@ func colorFuncForState(state string) func(string) string {
240254

241255
func prView(cmd *cobra.Command, args []string) error {
242256
ctx := contextForCommand(cmd)
243-
baseRepo, err := ctx.BaseRepo()
257+
258+
apiClient, err := apiClientForContext(ctx)
244259
if err != nil {
245260
return err
246261
}
247262

248-
apiClient, err := apiClientForContext(ctx)
263+
referSelf, _ := cmd.Flags().GetBool("self")
264+
if referSelf == false {
265+
ctx = context.ExpandOnline(ctx, apiClient)
266+
}
267+
268+
baseRepo, err := context.DetermineRepo(ctx, referSelf)
249269
if err != nil {
250270
return err
251271
}

context/context.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package context
22

33
import (
4+
"errors"
45
"path"
56

7+
"github.com/cli/cli/api"
68
"github.com/cli/cli/git"
79
"github.com/cli/cli/internal/ghrepo"
810
"github.com/mitchellh/go-homedir"
@@ -20,11 +22,45 @@ type Context interface {
2022
SetBaseRepo(string)
2123
}
2224

25+
type OnlineContext interface {
26+
Context
27+
ParentRepos() ([]ghrepo.Interface, error)
28+
}
29+
2330
// New initializes a Context that reads from the filesystem
2431
func New() Context {
2532
return &fsContext{}
2633
}
2734

35+
func ExpandOnline(ctx Context, apiClient *api.Client) OnlineContext {
36+
return &apiContext{
37+
Context: ctx,
38+
apiClient: *apiClient,
39+
}
40+
}
41+
42+
func DetermineRepo(ctx Context, self bool) (ghrepo.Interface, error) {
43+
if self == true {
44+
return ctx.BaseRepo()
45+
}
46+
47+
onlineCtx, isOnline := ctx.(OnlineContext)
48+
if !isOnline {
49+
return nil, errors.New("context not online")
50+
}
51+
52+
repos, err := onlineCtx.ParentRepos()
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
if len(repos) < 1 {
58+
return ctx.BaseRepo()
59+
}
60+
61+
return repos[0], nil
62+
}
63+
2864
// A Context implementation that queries the filesystem
2965
type fsContext struct {
3066
config *configEntry
@@ -130,3 +166,35 @@ func (c *fsContext) BaseRepo() (ghrepo.Interface, error) {
130166
func (c *fsContext) SetBaseRepo(nwo string) {
131167
c.baseRepo = ghrepo.FromFullName(nwo)
132168
}
169+
170+
type apiContext struct {
171+
Context
172+
apiClient api.Client
173+
}
174+
175+
func (c *apiContext) ParentRepos() ([]ghrepo.Interface, error) {
176+
baseRepo, err := c.BaseRepo()
177+
if err != nil {
178+
return nil, err
179+
}
180+
181+
result, err := api.RepoNetwork(&c.apiClient, []ghrepo.Interface{baseRepo})
182+
if err != nil {
183+
return nil, err
184+
}
185+
186+
if len(result.Repositories) < 1 {
187+
return nil, errors.New("network request returned 0 repositories")
188+
}
189+
190+
var repos []ghrepo.Interface
191+
192+
var repo api.Repository = *result.Repositories[0]
193+
194+
for repo.IsFork() {
195+
repo = *repo.Parent
196+
repos = append(repos, repo)
197+
}
198+
199+
return repos, nil
200+
}

0 commit comments

Comments
 (0)