X Tutup
Skip to content

Commit f653dbb

Browse files
committed
wrap and reuse the resolveToRemotes code from PR commands
1 parent 22be13d commit f653dbb

File tree

2 files changed

+53
-94
lines changed

2 files changed

+53
-94
lines changed

command/pr.go

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,52 @@ branch is opened.`,
6767
RunE: prView,
6868
}
6969

70-
func prStatus(cmd *cobra.Command, args []string) error {
71-
ctx := contextForCommand(cmd)
70+
func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (*ghrepo.Interface, error) {
7271
apiClient, err := apiClientForContext(ctx)
7372
if err != nil {
74-
return err
73+
return nil, err
7574
}
7675

77-
referSelf, _ := cmd.Flags().GetBool("self")
78-
if referSelf == false {
79-
ctx = context.ExpandOnline(ctx, apiClient)
76+
baseOverride, err := cmd.Flags().GetString("repo")
77+
if err != nil {
78+
return nil, err
8079
}
8180

82-
baseRepo, err := context.DetermineRepo(ctx, referSelf)
81+
baseRepo, err := ctx.BaseRepo()
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
preferSelf, err := cmd.Flags().GetBool("self")
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
if preferSelf == false {
92+
remotes, err := ctx.Remotes()
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, baseOverride)
98+
if err != nil {
99+
return nil, err
100+
}
101+
baseRepo, err = repoContext.BaseRepo()
102+
}
103+
if err != nil {
104+
return nil, err
105+
}
106+
return &baseRepo, nil
107+
}
108+
109+
func prStatus(cmd *cobra.Command, args []string) error {
110+
ctx := contextForCommand(cmd)
111+
apiClient, err := apiClientForContext(ctx)
83112
if err != nil {
84113
return err
85114
}
115+
86116
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx)
87117
if err != nil {
88118
return err
@@ -92,15 +122,20 @@ func prStatus(cmd *cobra.Command, args []string) error {
92122
return err
93123
}
94124

95-
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)
125+
baseRepo, err := determineBaseRepo(cmd, ctx)
126+
if err != nil {
127+
return err
128+
}
129+
130+
prPayload, err := api.PullRequests(apiClient, *baseRepo, currentPRNumber, currentPRHeadRef, currentUser)
96131
if err != nil {
97132
return err
98133
}
99134

100135
out := colorableOut(cmd)
101136

102137
fmt.Fprintln(out, "")
103-
fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo))
138+
fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(*baseRepo))
104139
fmt.Fprintln(out, "")
105140

106141
printHeader(out, "Current branch")
@@ -138,17 +173,12 @@ func prList(cmd *cobra.Command, args []string) error {
138173
return err
139174
}
140175

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)
176+
baseRepo, err := determineBaseRepo(cmd, ctx)
147177
if err != nil {
148178
return err
149179
}
150180

151-
fmt.Fprintf(colorableErr(cmd), "\nPull requests for %s\n\n", ghrepo.FullName(baseRepo))
181+
fmt.Fprintf(colorableErr(cmd), "\nPull requests for %s\n\n", ghrepo.FullName(*baseRepo))
152182

153183
limit, err := cmd.Flags().GetInt("limit")
154184
if err != nil {
@@ -186,8 +216,8 @@ func prList(cmd *cobra.Command, args []string) error {
186216
}
187217

188218
params := map[string]interface{}{
189-
"owner": baseRepo.RepoOwner(),
190-
"repo": baseRepo.RepoName(),
219+
"owner": (*baseRepo).RepoOwner(),
220+
"repo": (*baseRepo).RepoName(),
191221
"state": graphqlState,
192222
}
193223
if len(labels) > 0 {
@@ -260,12 +290,7 @@ func prView(cmd *cobra.Command, args []string) error {
260290
return err
261291
}
262292

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)
293+
baseRepo, err := determineBaseRepo(cmd, ctx)
269294
if err != nil {
270295
return err
271296
}
@@ -278,7 +303,7 @@ func prView(cmd *cobra.Command, args []string) error {
278303
var openURL string
279304
var pr *api.PullRequest
280305
if len(args) > 0 {
281-
pr, err = prFromArg(apiClient, baseRepo, args[0])
306+
pr, err = prFromArg(apiClient, *baseRepo, args[0])
282307
if err != nil {
283308
return err
284309
}
@@ -290,15 +315,15 @@ func prView(cmd *cobra.Command, args []string) error {
290315
}
291316

292317
if prNumber > 0 {
293-
openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(baseRepo), prNumber)
318+
openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(*baseRepo), prNumber)
294319
if preview {
295-
pr, err = api.PullRequestByNumber(apiClient, baseRepo, prNumber)
320+
pr, err = api.PullRequestByNumber(apiClient, *baseRepo, prNumber)
296321
if err != nil {
297322
return err
298323
}
299324
}
300325
} else {
301-
pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner)
326+
pr, err = api.PullRequestForBranch(apiClient, *baseRepo, branchWithOwner)
302327
if err != nil {
303328
return err
304329
}

context/context.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -118,45 +118,11 @@ func (r ResolvedRemotes) RemoteForRepo(repo ghrepo.Interface) (*Remote, error) {
118118
return nil, errors.New("not found")
119119
}
120120

121-
type OnlineContext interface {
122-
Context
123-
ParentRepos() ([]ghrepo.Interface, error)
124-
}
125-
126121
// New initializes a Context that reads from the filesystem
127122
func New() Context {
128123
return &fsContext{}
129124
}
130125

131-
func ExpandOnline(ctx Context, apiClient *api.Client) OnlineContext {
132-
return &apiContext{
133-
Context: ctx,
134-
apiClient: *apiClient,
135-
}
136-
}
137-
138-
func DetermineRepo(ctx Context, self bool) (ghrepo.Interface, error) {
139-
if self == true {
140-
return ctx.BaseRepo()
141-
}
142-
143-
onlineCtx, isOnline := ctx.(OnlineContext)
144-
if !isOnline {
145-
return nil, errors.New("context not online")
146-
}
147-
148-
repos, err := onlineCtx.ParentRepos()
149-
if err != nil {
150-
return nil, err
151-
}
152-
153-
if len(repos) < 1 {
154-
return ctx.BaseRepo()
155-
}
156-
157-
return repos[0], nil
158-
}
159-
160126
// A Context implementation that queries the filesystem
161127
type fsContext struct {
162128
config *configEntry
@@ -262,35 +228,3 @@ func (c *fsContext) BaseRepo() (ghrepo.Interface, error) {
262228
func (c *fsContext) SetBaseRepo(nwo string) {
263229
c.baseRepo = ghrepo.FromFullName(nwo)
264230
}
265-
266-
type apiContext struct {
267-
Context
268-
apiClient api.Client
269-
}
270-
271-
func (c *apiContext) ParentRepos() ([]ghrepo.Interface, error) {
272-
baseRepo, err := c.BaseRepo()
273-
if err != nil {
274-
return nil, err
275-
}
276-
277-
result, err := api.RepoNetwork(&c.apiClient, []ghrepo.Interface{baseRepo})
278-
if err != nil {
279-
return nil, err
280-
}
281-
282-
if len(result.Repositories) < 1 {
283-
return nil, errors.New("network request returned 0 repositories")
284-
}
285-
286-
var repos []ghrepo.Interface
287-
288-
var repo api.Repository = *result.Repositories[0]
289-
290-
for repo.IsFork() {
291-
repo = *repo.Parent
292-
repos = append(repos, repo)
293-
}
294-
295-
return repos, nil
296-
}

0 commit comments

Comments
 (0)
X Tutup