From 7d7c240f4b0d1c2ef31fe73fc021a0802aec6454 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 3 Nov 2024 11:36:14 +1100 Subject: [PATCH 001/100] feat: let user select pr to checkout --- pkg/cmd/pr/checkout/checkout.go | 84 +++++++++++++++++++++++++++++--- pkg/cmd/pr/list/http.go | 2 +- pkg/cmd/pr/list/list.go | 2 +- pkg/cmd/pr/shared/commentable.go | 3 ++ pkg/cmd/pr/shared/display.go | 9 ++++ 5 files changed, 90 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index f566cbe1d..5bb0cc113 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -11,6 +11,7 @@ import ( "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/pr/list" "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -25,8 +26,10 @@ type CheckoutOptions struct { Remotes func() (cliContext.Remotes, error) Branch func() (string, error) - Finder shared.PRFinder + Finder shared.PRFinder + Prompter shared.Prompter + BaseRepo func() (ghrepo.Interface, error) SelectorArg string RecurseSubmodules bool Force bool @@ -42,12 +45,14 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr Config: f.Config, Remotes: f.Remotes, Branch: f.Branch, + Prompter: f.Prompter, + BaseRepo: f.BaseRepo, } cmd := &cobra.Command{ - Use: "checkout { | | }", + Use: "checkout [ | | ]", Short: "Check out a pull request in git", - Args: cmdutil.ExactArgs(1, "argument required"), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Finder = shared.NewFinder(f) @@ -71,15 +76,41 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr } func checkoutRun(opts *CheckoutOptions) error { - findOptions := shared.FindOptions{ - Selector: opts.SelectorArg, - Fields: []string{"number", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"}, - } - pr, baseRepo, err := opts.Finder.Find(findOptions) + baseRepo, err := opts.BaseRepo() if err != nil { return err } + var pr *api.PullRequest + + if len(opts.SelectorArg) > 0 { + + findOptions := shared.FindOptions{ + Selector: opts.SelectorArg, + Fields: []string{"number", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"}, + } + pr0, _, err := opts.Finder.Find(findOptions) + if err != nil { + return err + } + + pr = pr0 + + } else { + + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + + pr0, err := selectPR(httpClient, baseRepo, opts.Prompter, opts.IO.ColorScheme()) + if err != nil { + return err + } + + pr = pr0 + + } cfg, err := opts.Config() if err != nil { return err @@ -258,3 +289,40 @@ func executeCmds(client *git.Client, cmdQueue [][]string) error { } return nil } + +func selectPR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, cs *iostreams.ColorScheme) (*api.PullRequest, error) { + listResult, err := list.ListPullRequests(httpClient, baseRepo, shared.FilterOptions{Entity: "pr", State: "open", Fields: []string{ + "number", + "title", + "state", + "url", + "headRefName", + "headRepositoryOwner", + "isCrossRepository", + "isDraft", + "createdAt", + }}, 30) + if err != nil { + return nil, err + } + pr, err := promptForPR(prompter, cs, *listResult) + return pr, err +} + +func promptForPR(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { + candidates := []string{} + for _, pr := range jobs.PullRequests { + candidates = append(candidates, fmt.Sprintf("%s %s", shared.PRNumberTitleWithColor(cs, pr), pr.Title)) + } + + selected, err := prompter.Select("Check out a specific PR?", "", candidates) + if err != nil { + return nil, err + } + + if selected >= 0 { + return &jobs.PullRequests[selected], nil + } + + return nil, nil +} diff --git a/pkg/cmd/pr/list/http.go b/pkg/cmd/pr/list/http.go index 5887551cf..cb79b3b87 100644 --- a/pkg/cmd/pr/list/http.go +++ b/pkg/cmd/pr/list/http.go @@ -13,7 +13,7 @@ func shouldUseSearch(filters prShared.FilterOptions) bool { return filters.Draft != nil || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0 } -func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) { +func ListPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) { if shouldUseSearch(filters) { return searchPullRequests(httpClient, repo, filters, limit) } diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index 93a9b8b4b..401e168e3 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -172,7 +172,7 @@ func listRun(opts *ListOptions) error { return opts.Browser.Browse(openURL) } - listResult, err := listPullRequests(httpClient, baseRepo, filters, opts.LimitResults) + listResult, err := ListPullRequests(httpClient, baseRepo, filters, opts.LimitResults) if err != nil { return err } diff --git a/pkg/cmd/pr/shared/commentable.go b/pkg/cmd/pr/shared/commentable.go index 7a38286d2..46a71a2b4 100644 --- a/pkg/cmd/pr/shared/commentable.go +++ b/pkg/cmd/pr/shared/commentable.go @@ -19,6 +19,9 @@ import ( type InputType int +type Prompter interface { + Select(string, string, []string) (int, error) +} const ( InputTypeEditor InputType = iota InputTypeInline diff --git a/pkg/cmd/pr/shared/display.go b/pkg/cmd/pr/shared/display.go index 7bd306104..7920eac4e 100644 --- a/pkg/cmd/pr/shared/display.go +++ b/pkg/cmd/pr/shared/display.go @@ -17,6 +17,15 @@ func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { return prStateColorFunc(text.Title(pr.State)) } +func PRNumberTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { + prStateColorFunc := cs.ColorFromString((ColorForPRState(pr))) + prNumber := fmt.Sprintf("%d", pr.Number) + if pr.State == "OPEN" && pr.IsDraft { + return prStateColorFunc(text.Title(prNumber)) + } + return prStateColorFunc(text.Title(prNumber)) +} + func ColorForPRState(pr api.PullRequest) string { switch pr.State { case "OPEN": From 4386fcb4e5d984ffbe4c276f63da9e8f39f4b7bc Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 17 Nov 2024 14:51:27 +1100 Subject: [PATCH 002/100] issue #2329: fix the unit tests issue - ensure its backward compatible --- pkg/cmd/pr/checkout/checkout.go | 42 +++++++++++++++++---------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 5bb0cc113..0c3cb2571 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -76,40 +76,42 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr } func checkoutRun(opts *CheckoutOptions) error { - baseRepo, err := opts.BaseRepo() - if err != nil { - return err - } + var ( + baseRepo ghrepo.Interface + pr *api.PullRequest + err error + ) - var pr *api.PullRequest - - if len(opts.SelectorArg) > 0 { - - findOptions := shared.FindOptions{ + switch { + case opts.SelectorArg != "": + pr, baseRepo, err = opts.Finder.Find(shared.FindOptions{ Selector: opts.SelectorArg, - Fields: []string{"number", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"}, - } - pr0, _, err := opts.Finder.Find(findOptions) + Fields: []string{ + "number", + "headRefName", + "headRepository", + "headRepositoryOwner", + "isCrossRepository", + "maintainerCanModify", + }, + }) if err != nil { return err } - pr = pr0 - - } else { - + default: httpClient, err := opts.HttpClient() if err != nil { return err } - pr0, err := selectPR(httpClient, baseRepo, opts.Prompter, opts.IO.ColorScheme()) - if err != nil { + if baseRepo, err = opts.BaseRepo(); err != nil { return err } - pr = pr0 - + if pr, err = selectPR(httpClient, baseRepo, opts.Prompter, opts.IO.ColorScheme()); err != nil { + return err + } } cfg, err := opts.Config() if err != nil { From 1832c1a767bcb7c1bafeca134817ec54e9335d79 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 17 Nov 2024 14:21:21 +1100 Subject: [PATCH 003/100] issue #2329: fix the linting issue --- pkg/cmd/pr/checkout/checkout.go | 2 +- pkg/cmd/pr/list/http_test.go | 6 +++--- pkg/cmd/pr/list/list_test.go | 4 ++-- pkg/cmd/pr/shared/commentable.go | 1 + 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 0c3cb2571..54c0cae93 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -88,7 +88,7 @@ func checkoutRun(opts *CheckoutOptions) error { Selector: opts.SelectorArg, Fields: []string{ "number", - "headRefName", + "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", diff --git a/pkg/cmd/pr/list/http_test.go b/pkg/cmd/pr/list/http_test.go index 95230245b..060e3a179 100644 --- a/pkg/cmd/pr/list/http_test.go +++ b/pkg/cmd/pr/list/http_test.go @@ -10,7 +10,7 @@ import ( "github.com/cli/cli/v2/pkg/httpmock" ) -func Test_listPullRequests(t *testing.T) { +func Test_ListPullRequests(t *testing.T) { type args struct { repo ghrepo.Interface filters prShared.FilterOptions @@ -153,9 +153,9 @@ func Test_listPullRequests(t *testing.T) { } httpClient := &http.Client{Transport: reg} - _, err := listPullRequests(httpClient, tt.args.repo, tt.args.filters, tt.args.limit) + _, err := ListPullRequests(httpClient, tt.args.repo, tt.args.filters, tt.args.limit) if (err != nil) != tt.wantErr { - t.Errorf("listPullRequests() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("ListPullRequests() error = %v, wantErr %v", err, tt.wantErr) return } }) diff --git a/pkg/cmd/pr/list/list_test.go b/pkg/cmd/pr/list/list_test.go index ecd0326b5..7ee111afc 100644 --- a/pkg/cmd/pr/list/list_test.go +++ b/pkg/cmd/pr/list/list_test.go @@ -368,7 +368,7 @@ func TestPRList_withProjectItems(t *testing.T) { })) client := &http.Client{Transport: reg} - prsAndTotalCount, err := listPullRequests( + prsAndTotalCount, err := ListPullRequests( client, ghrepo.New("OWNER", "REPO"), prShared.FilterOptions{ @@ -437,7 +437,7 @@ func TestPRList_Search_withProjectItems(t *testing.T) { })) client := &http.Client{Transport: reg} - prsAndTotalCount, err := listPullRequests( + prsAndTotalCount, err := ListPullRequests( client, ghrepo.New("OWNER", "REPO"), prShared.FilterOptions{ diff --git a/pkg/cmd/pr/shared/commentable.go b/pkg/cmd/pr/shared/commentable.go index 46a71a2b4..342898687 100644 --- a/pkg/cmd/pr/shared/commentable.go +++ b/pkg/cmd/pr/shared/commentable.go @@ -22,6 +22,7 @@ type InputType int type Prompter interface { Select(string, string, []string) (int, error) } + const ( InputTypeEditor InputType = iota InputTypeInline From c6c27113fe4485a916299205aeaafbe16f26a8fd Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 17 Nov 2024 16:36:10 +1100 Subject: [PATCH 004/100] issue #2329: add happy path unit test --- pkg/cmd/pr/checkout/checkout_test.go | 52 ++++++++++++++++-- pkg/cmd/pr/checkout/fixtures/prList.json | 67 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 pkg/cmd/pr/checkout/fixtures/prList.json diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 1eda5dda2..d2962f2d9 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -14,6 +14,7 @@ import ( "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/internal/run" "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" @@ -63,10 +64,12 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) { func Test_checkoutRun(t *testing.T) { tests := []struct { - name string - opts *CheckoutOptions - httpStubs func(*httpmock.Registry) - runStubs func(*run.CommandStubber) + name string + opts *CheckoutOptions + httpStubs func(*httpmock.Registry) + runStubs func(*run.CommandStubber) + promptStubs func(*prompter.MockPrompter) + remotes map[string]string wantStdout string wantStderr string @@ -158,6 +161,41 @@ func Test_checkoutRun(t *testing.T) { cs.Register(`git config branch\.foobar\.merge refs/heads/feature`, 0, "") }, }, + { + name: "with no selected PR args, prompts for choice", + opts: &CheckoutOptions{ + SelectorArg: "", + Finder: func() shared.PRFinder { + baseRepo, pr := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") + finder := shared.NewMockFinder("123", pr, baseRepo) + return finder + }(), + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + Config: func() (gh.Config, error) { + return config.NewBlankConfig(), nil + }, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) + }, + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Check out a specific PR?", + []string{"32 New feature", "29 Fixed bad bug", "28 Improve documentation"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "32 New feature") + }) + }, + runStubs: func(cs *run.CommandStubber) { + cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") + cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "") + cs.Register(`git checkout -b feature --track origin/feature`, 0, "") + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -180,6 +218,12 @@ func Test_checkoutRun(t *testing.T) { tt.runStubs(cmdStubs) } + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) + } + opts.Remotes = func() (context.Remotes, error) { if len(tt.remotes) == 0 { return nil, errors.New("no remotes") diff --git a/pkg/cmd/pr/checkout/fixtures/prList.json b/pkg/cmd/pr/checkout/fixtures/prList.json new file mode 100644 index 000000000..f915df136 --- /dev/null +++ b/pkg/cmd/pr/checkout/fixtures/prList.json @@ -0,0 +1,67 @@ +{ + "data": { + "repository": { + "pullRequests": { + "totalCount": 3, + "nodes": [ + { + "number": 32, + "title": "New feature", + "url": "https://github.com/OWNER/REPO/pull/32", + "createdAt": "2022-08-24T20:01:12Z", + "headRefName": "feature", + "state": "OPEN", + "isDraft": true, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + }, + { + "number": 29, + "title": "Fixed bad bug", + "url": "https://github.com/OWNER/REPO/pull/29", + "createdAt": "2022-07-20T19:01:12Z", + "headRefName": "bug-fix", + "state": "OPEN", + "isDraft": false, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + }, + { + "number": 28, + "state": "MERGED", + "isDraft": false, + "title": "Improve documentation", + "createdAt": "2020-01-26T19:01:12Z", + "url": "https://github.com/OWNER/REPO/pull/28", + "isCrossRepository": false, + "headRefName": "docs", + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" + } + } + } + } + } + \ No newline at end of file From 36eaf14857dc8110c47b3d91b823589771aae305 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 17 Nov 2024 23:47:29 +1100 Subject: [PATCH 005/100] issue #2329: improve UI/UX Improve the interactive PR selection UI by - prefix the PR number with hashcode # - perserve the text formatting (bold) upon an option is hovered - add the PR head label Technical changes: - Replace \033[0m with \033[39m for maintaining text formatting --- pkg/cmd/pr/checkout/checkout.go | 21 ++++++++++++++++++--- pkg/cmd/pr/checkout/checkout_test.go | 4 ++-- pkg/cmd/pr/shared/display.go | 28 ++++++++++++++++++++++------ pkg/iostreams/color.go | 2 +- pkg/iostreams/color_test.go | 4 ++-- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 54c0cae93..cd9da809c 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -6,11 +6,13 @@ import ( "net/http" "strings" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" cliContext "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/cmd/pr/list" "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" @@ -52,7 +54,16 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr cmd := &cobra.Command{ Use: "checkout [ | | ]", Short: "Check out a pull request in git", - Args: cobra.MaximumNArgs(1), + Example: heredoc.Doc(` + # Interactively select a PR to checkout from the 10 most recent + $ gh pr checkout + + # Checkout a specific PR + $ gh pr checkout 32 + $ gh pr checkout https://github.com/OWNER/REPO/pull/32 + $ gh pr checkout feature + `), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Finder = shared.NewFinder(f) @@ -303,7 +314,7 @@ func selectPR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter share "isCrossRepository", "isDraft", "createdAt", - }}, 30) + }}, 10) if err != nil { return nil, err } @@ -314,7 +325,11 @@ func selectPR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter share func promptForPR(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { - candidates = append(candidates, fmt.Sprintf("%s %s", shared.PRNumberTitleWithColor(cs, pr), pr.Title)) + candidates = append(candidates, text.Truncate(120, fmt.Sprintf("%s %s (%s)", + shared.PRNumberWithColor(cs, pr), + text.RemoveExcessiveWhitespace(pr.Title), + cs.Gray(pr.HeadLabel()), + ))) } selected, err := prompter.Select("Check out a specific PR?", "", candidates) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index d2962f2d9..138e1afdf 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -182,9 +182,9 @@ func Test_checkoutRun(t *testing.T) { }, promptStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Check out a specific PR?", - []string{"32 New feature", "29 Fixed bad bug", "28 Improve documentation"}, + []string{"#32 New feature (feature)", "#29 Fixed bad bug (bug-fix)", "#28 Improve documentation (docs)"}, func(_, _ string, opts []string) (int, error) { - return prompter.IndexFor(opts, "32 New feature") + return prompter.IndexFor(opts, "#32 New feature (feature)") }) }, runStubs: func(cs *run.CommandStubber) { diff --git a/pkg/cmd/pr/shared/display.go b/pkg/cmd/pr/shared/display.go index 7920eac4e..ffc4b4dce 100644 --- a/pkg/cmd/pr/shared/display.go +++ b/pkg/cmd/pr/shared/display.go @@ -17,12 +17,12 @@ func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { return prStateColorFunc(text.Title(pr.State)) } -func PRNumberTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { - prStateColorFunc := cs.ColorFromString((ColorForPRState(pr))) - prNumber := fmt.Sprintf("%d", pr.Number) - if pr.State == "OPEN" && pr.IsDraft { - return prStateColorFunc(text.Title(prNumber)) - } +// PRNumberWithColor returns a colored string representation of a pull request number +// based on its state (open, closed, merged, or draft). +// It prefixes the number with a hash symbol (#) to indicate it's a pull request. +func PRNumberWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { + prStateColorFunc := cs.ColorFromRGB(ColorHexCodeForPRState(pr)) + prNumber := fmt.Sprintf("#%d", pr.Number) return prStateColorFunc(text.Title(prNumber)) } @@ -42,6 +42,22 @@ func ColorForPRState(pr api.PullRequest) string { } } +func ColorHexCodeForPRState(pr api.PullRequest) string { + switch pr.State { + case "OPEN": + if pr.IsDraft { + return "808080" + } + return "00FF00" + case "CLOSED": + return "FF0000" + case "MERGED": + return "FFA500" + default: + return "" + } +} + func ColorForIssueState(issue api.Issue) string { switch issue.State { case "OPEN": diff --git a/pkg/iostreams/color.go b/pkg/iostreams/color.go index c8d48168f..33df6bddf 100644 --- a/pkg/iostreams/color.go +++ b/pkg/iostreams/color.go @@ -252,5 +252,5 @@ func (c *ColorScheme) HexToRGB(hex string, x string) string { r, _ := strconv.ParseInt(hex[0:2], 16, 64) g, _ := strconv.ParseInt(hex[2:4], 16, 64) b, _ := strconv.ParseInt(hex[4:6], 16, 64) - return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, x) + return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[39m", r, g, b, x) } diff --git a/pkg/iostreams/color_test.go b/pkg/iostreams/color_test.go index 59fea53ed..f3370da29 100644 --- a/pkg/iostreams/color_test.go +++ b/pkg/iostreams/color_test.go @@ -18,7 +18,7 @@ func TestColorFromRGB(t *testing.T) { name: "truecolor", hex: "fc0303", text: "red", - wants: "\033[38;2;252;3;3mred\033[0m", + wants: "\033[38;2;252;3;3mred\033[39m", cs: NewColorScheme(true, true, true), }, { @@ -62,7 +62,7 @@ func TestHexToRGB(t *testing.T) { name: "truecolor", hex: "fc0303", text: "red", - wants: "\033[38;2;252;3;3mred\033[0m", + wants: "\033[38;2;252;3;3mred\033[39m", cs: NewColorScheme(true, true, true), }, { From e4dced01f781659a78a20c945c36ca284eb1335b Mon Sep 17 00:00:00 2001 From: nilvng Date: Sat, 30 Nov 2024 23:51:00 +1100 Subject: [PATCH 006/100] issue #2329: fix missing stub --- pkg/cmd/pr/checkout/checkout_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 0d6b12bc4..86e7e6d70 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -218,6 +218,7 @@ func Test_checkoutRun(t *testing.T) { }, runStubs: func(cs *run.CommandStubber) { cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") + cs.Register(`git remote get-url origin`, 0, "https://github.com/hubot/REPO.git") cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "") cs.Register(`git checkout -b feature --track origin/feature`, 0, "") }, From d92e529629f2ccc0c87c4a49e0b2f199602bfb3c Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 1 Dec 2024 21:13:54 +1100 Subject: [PATCH 007/100] issue #2329: return error when not running interactively --- pkg/cmd/pr/checkout/checkout.go | 4 ++++ pkg/cmd/pr/checkout/checkout_test.go | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 54ecf17f6..e78eb026b 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -111,6 +111,10 @@ func checkoutRun(opts *CheckoutOptions) error { } default: + if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("must provide a pull request number (or URL or branch) when not running interactively") + } + httpClient, err := opts.HttpClient() if err != nil { return err diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 86e7e6d70..73055e1f8 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -64,8 +64,10 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) { func Test_checkoutRun(t *testing.T) { tests := []struct { - name string - opts *CheckoutOptions + name string + opts *CheckoutOptions + tty bool + httpStubs func(*httpmock.Registry) runStubs func(*run.CommandStubber) promptStubs func(*prompter.MockPrompter) @@ -191,7 +193,15 @@ func Test_checkoutRun(t *testing.T) { }, }, { - name: "with no selected PR args, prompts for choice", + name: "with no selected PR args and not stdin tty, return error", + opts: &CheckoutOptions{ + SelectorArg: "", + }, + tty: false, + wantErr: true, + }, + { + name: "with no selected PR args and stdin tty, prompts for choice", opts: &CheckoutOptions{ SelectorArg: "", Finder: func() shared.PRFinder { @@ -206,6 +216,7 @@ func Test_checkoutRun(t *testing.T) { return config.NewBlankConfig(), nil }, }, + tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) }, @@ -232,6 +243,12 @@ func Test_checkoutRun(t *testing.T) { opts := tt.opts ios, _, stdout, stderr := iostreams.Test() + if tt.tty { + ios.SetStdinTTY(tt.tty) + ios.SetStdoutTTY(tt.tty) + ios.SetStderrTTY(tt.tty) + } + opts.IO = ios httpReg := &httpmock.Registry{} defer httpReg.Verify(t) From 985118e36ac87e22aa8967f451d6bdd6ddf19c51 Mon Sep 17 00:00:00 2001 From: nilvng Date: Tue, 3 Dec 2024 20:13:38 +1100 Subject: [PATCH 008/100] fix: update the copy --- pkg/cmd/pr/checkout/checkout.go | 4 ++-- pkg/cmd/pr/checkout/checkout_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index e78eb026b..b40aa9062 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -55,7 +55,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr Use: "checkout [ | | ]", Short: "Check out a pull request in git", Example: heredoc.Doc(` - # Interactively select a PR to checkout from the 10 most recent + # Interactively select a PR from the 10 most recent to check out $ gh pr checkout # Checkout a specific PR @@ -346,7 +346,7 @@ func promptForPR(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs api.P ))) } - selected, err := prompter.Select("Check out a specific PR?", "", candidates) + selected, err := prompter.Select("Select a pull request", "", candidates) if err != nil { return nil, err } diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 73055e1f8..cf1c504d1 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -221,7 +221,7 @@ func Test_checkoutRun(t *testing.T) { reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) }, promptStubs: func(pm *prompter.MockPrompter) { - pm.RegisterSelect("Check out a specific PR?", + pm.RegisterSelect("Select a pull request", []string{"#32 New feature (feature)", "#29 Fixed bad bug (bug-fix)", "#28 Improve documentation (docs)"}, func(_, _ string, opts []string) (int, error) { return prompter.IndexFor(opts, "#32 New feature (feature)") From 9c82156e8626486791f9f22165f3127498c43752 Mon Sep 17 00:00:00 2001 From: nilvng Date: Tue, 3 Dec 2024 22:22:34 +1100 Subject: [PATCH 009/100] issue #2329: move Interactive check to RunE Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com> --- pkg/cmd/pr/checkout/checkout.go | 7 ++++++- pkg/cmd/pr/checkout/checkout_test.go | 13 +++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index b40aa9062..9516e580a 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -31,6 +31,7 @@ type CheckoutOptions struct { Finder shared.PRFinder Prompter shared.Prompter + Interactive bool BaseRepo func() (ghrepo.Interface, error) SelectorArg string RecurseSubmodules bool @@ -69,6 +70,10 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr if len(args) > 0 { opts.SelectorArg = args[0] + } else if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("pull request number, URL, or branch required when not running interactively") + } else { + opts.Interactive = true } if runF != nil { @@ -111,7 +116,7 @@ func checkoutRun(opts *CheckoutOptions) error { } default: - if !opts.IO.CanPrompt() { + if !opts.Interactive { return cmdutil.FlagErrorf("must provide a pull request number (or URL or branch) when not running interactively") } diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index cf1c504d1..49ac0bb22 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -66,7 +66,6 @@ func Test_checkoutRun(t *testing.T) { tests := []struct { name string opts *CheckoutOptions - tty bool httpStubs func(*httpmock.Registry) runStubs func(*run.CommandStubber) @@ -196,14 +195,18 @@ func Test_checkoutRun(t *testing.T) { name: "with no selected PR args and not stdin tty, return error", opts: &CheckoutOptions{ SelectorArg: "", + Interactive: false, + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", }, - tty: false, wantErr: true, }, { name: "with no selected PR args and stdin tty, prompts for choice", opts: &CheckoutOptions{ SelectorArg: "", + Interactive: true, Finder: func() shared.PRFinder { baseRepo, pr := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") finder := shared.NewMockFinder("123", pr, baseRepo) @@ -216,7 +219,6 @@ func Test_checkoutRun(t *testing.T) { return config.NewBlankConfig(), nil }, }, - tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) }, @@ -243,11 +245,6 @@ func Test_checkoutRun(t *testing.T) { opts := tt.opts ios, _, stdout, stderr := iostreams.Test() - if tt.tty { - ios.SetStdinTTY(tt.tty) - ios.SetStdoutTTY(tt.tty) - ios.SetStderrTTY(tt.tty) - } opts.IO = ios httpReg := &httpmock.Registry{} From 2a1f27ee01c9e9ac1f37917cc2ea9f84283fd3a8 Mon Sep 17 00:00:00 2001 From: nilvng Date: Thu, 5 Dec 2024 01:07:23 +1100 Subject: [PATCH 010/100] issue cli#2329: fix unsufficent properties required for PR checkout Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com> --- pkg/cmd/pr/checkout/checkout.go | 89 ++++++++++++++-------------- pkg/cmd/pr/checkout/checkout_test.go | 48 +++++++++++---- 2 files changed, 80 insertions(+), 57 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 9516e580a..b9ded029b 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -92,47 +92,21 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr } func checkoutRun(opts *CheckoutOptions) error { - var ( - baseRepo ghrepo.Interface - pr *api.PullRequest - err error - ) - - switch { - case opts.SelectorArg != "": - pr, baseRepo, err = opts.Finder.Find(shared.FindOptions{ - Selector: opts.SelectorArg, - Fields: []string{ - "number", - "headRefName", - "headRepository", - "headRepositoryOwner", - "isCrossRepository", - "maintainerCanModify", - }, - }) - if err != nil { - return err - } - - default: - if !opts.Interactive { - return cmdutil.FlagErrorf("must provide a pull request number (or URL or branch) when not running interactively") - } - - httpClient, err := opts.HttpClient() - if err != nil { - return err - } - - if baseRepo, err = opts.BaseRepo(); err != nil { - return err - } - - if pr, err = selectPR(httpClient, baseRepo, opts.Prompter, opts.IO.ColorScheme()); err != nil { - return err - } + baseRepo, err := opts.BaseRepo() + if err != nil { + return err } + + client, err := opts.HttpClient() + if err != nil { + return err + } + + pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder, opts.IO.ColorScheme()) + if err != nil { + return err + } + cfg, err := opts.Config() if err != nil { return err @@ -322,22 +296,49 @@ func executeCmds(client *git.Client, credentialPattern git.CredentialPattern, cm return nil } -func selectPR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, cs *iostreams.ColorScheme) (*api.PullRequest, error) { +func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, cs *iostreams.ColorScheme) (*api.PullRequest, error) { + // When non-interactive + if pullRequestSelector != "" { + pr, _, err := pullRequestFinder.Find(shared.FindOptions{ + Selector: pullRequestSelector, + Fields: []string{ + "number", + "headRefName", + "headRepository", + "headRepositoryOwner", + "isCrossRepository", + "maintainerCanModify", + }, + }) + if err != nil { + return nil, err + } + return pr, nil + } + if !isInteractive { + return nil, cmdutil.FlagErrorf("pull request number, URL, or branch required when not running interactively") + } + // When interactive listResult, err := list.ListPullRequests(httpClient, baseRepo, shared.FilterOptions{Entity: "pr", State: "open", Fields: []string{ "number", "title", "state", "url", - "headRefName", - "headRepositoryOwner", - "isCrossRepository", "isDraft", "createdAt", + + "headRefName", + "headRepository", + "headRepositoryOwner", + "isCrossRepository", + "maintainerCanModify", }}, 10) if err != nil { return nil, err } + pr, err := promptForPR(prompter, cs, *listResult) + return pr, err } diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 49ac0bb22..5c2109f20 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -85,6 +85,10 @@ func Test_checkoutRun(t *testing.T) { finder := shared.NewMockFinder("123", pr, baseRepo) return finder }(), + BaseRepo: func() (ghrepo.Interface, error) { + baseRepo, _ := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") + return baseRepo, nil + }, Config: func() (gh.Config, error) { return config.NewBlankConfig(), nil }, @@ -113,6 +117,10 @@ func Test_checkoutRun(t *testing.T) { finder := shared.NewMockFinder("123", pr, baseRepo) return finder }(), + BaseRepo: func() (ghrepo.Interface, error) { + baseRepo, _ := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") + return baseRepo, nil + }, Config: func() (gh.Config, error) { return config.NewBlankConfig(), nil }, @@ -143,6 +151,10 @@ func Test_checkoutRun(t *testing.T) { finder := shared.NewMockFinder("123", pr, baseRepo) return finder }(), + BaseRepo: func() (ghrepo.Interface, error) { + baseRepo, _ := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") + return baseRepo, nil + }, Config: func() (gh.Config, error) { return config.NewBlankConfig(), nil }, @@ -171,6 +183,10 @@ func Test_checkoutRun(t *testing.T) { finder := shared.NewMockFinder("123", pr, baseRepo) return finder }(), + BaseRepo: func() (ghrepo.Interface, error) { + baseRepo, _ := stubPR("OWNER/REPO:master", "hubot/REPO:feature") + return baseRepo, nil + }, Config: func() (gh.Config, error) { return config.NewBlankConfig(), nil }, @@ -196,6 +212,9 @@ func Test_checkoutRun(t *testing.T) { opts: &CheckoutOptions{ SelectorArg: "", Interactive: false, + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, }, remotes: map[string]string{ "origin": "OWNER/REPO", @@ -303,7 +322,7 @@ func Test_checkoutRun(t *testing.T) { /** LEGACY TESTS **/ -func runCommand(rt http.RoundTripper, remotes context.Remotes, branch string, cli string) (*test.CmdOut, error) { +func runCommand(rt http.RoundTripper, remotes context.Remotes, branch string, cli string, baseRepo ghrepo.Interface) (*test.CmdOut, error) { ios, _, stdout, stderr := iostreams.Test() factory := &cmdutil.Factory{ @@ -332,6 +351,9 @@ func runCommand(rt http.RoundTripper, remotes context.Remotes, branch string, cl GhPath: "some/path/gh", GitPath: "some/path/git", }, + BaseRepo: func() (ghrepo.Interface, error) { + return baseRepo, nil + }, } cmd := NewCmdCheckout(factory, nil) @@ -369,7 +391,7 @@ func TestPRCheckout_sameRepo(t *testing.T) { cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") cs.Register(`git checkout -b feature --track origin/feature`, 0, "") - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -391,7 +413,7 @@ func TestPRCheckout_existingBranch(t *testing.T) { cs.Register(`git checkout feature`, 0, "") cs.Register(`git merge --ff-only refs/remotes/origin/feature`, 0, "") - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -424,7 +446,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) { cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") cs.Register(`git checkout -b feature --track robot-fork/feature`, 0, "") - output, err := runCommand(http, remotes, "master", `123`) + output, err := runCommand(http, remotes, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -449,7 +471,7 @@ func TestPRCheckout_differentRepo(t *testing.T) { cs.Register(`git config branch\.feature\.pushRemote origin`, 0, "") cs.Register(`git config branch\.feature\.merge refs/pull/123/head`, 0, "") - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -470,7 +492,7 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) { cs.Register(`git config branch\.feature\.merge`, 0, "refs/heads/feature\n") cs.Register(`git checkout feature`, 0, "") - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -491,7 +513,7 @@ func TestPRCheckout_detachedHead(t *testing.T) { cs.Register(`git config branch\.feature\.merge`, 0, "refs/heads/feature\n") cs.Register(`git checkout feature`, 0, "") - output, err := runCommand(http, nil, "", `123`) + output, err := runCommand(http, nil, "", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -512,7 +534,7 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) { cs.Register(`git config branch\.feature\.merge`, 0, "refs/heads/feature\n") cs.Register(`git merge --ff-only FETCH_HEAD`, 0, "") - output, err := runCommand(http, nil, "feature", `123`) + output, err := runCommand(http, nil, "feature", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -528,7 +550,7 @@ func TestPRCheckout_differentRepo_invalidBranchName(t *testing.T) { _, cmdTeardown := run.Stub() defer cmdTeardown(t) - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.EqualError(t, err, `invalid branch name: "-foo"`) assert.Equal(t, "", output.Stderr()) assert.Equal(t, "", output.Stderr()) @@ -553,7 +575,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) { cs.Register(`git config branch\.feature\.pushRemote https://github\.com/hubot/REPO\.git`, 0, "") cs.Register(`git config branch\.feature\.merge refs/heads/feature`, 0, "") - output, err := runCommand(http, nil, "master", `123`) + output, err := runCommand(http, nil, "master", `123`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -576,7 +598,7 @@ func TestPRCheckout_recurseSubmodules(t *testing.T) { cs.Register(`git submodule sync --recursive`, 0, "") cs.Register(`git submodule update --init --recursive`, 0, "") - output, err := runCommand(http, nil, "master", `123 --recurse-submodules`) + output, err := runCommand(http, nil, "master", `123 --recurse-submodules`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) @@ -597,7 +619,7 @@ func TestPRCheckout_force(t *testing.T) { cs.Register(`git checkout feature`, 0, "") cs.Register(`git reset --hard refs/remotes/origin/feature`, 0, "") - output, err := runCommand(http, nil, "master", `123 --force`) + output, err := runCommand(http, nil, "master", `123 --force`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) @@ -618,7 +640,7 @@ func TestPRCheckout_detach(t *testing.T) { cs.Register(`git remote get-url origin`, 0, "https://github.com/hubot/REPO.git") cs.Register(`git fetch origin refs/pull/123/head`, 0, "") - output, err := runCommand(http, nil, "", `123 --detach`) + output, err := runCommand(http, nil, "", `123 --detach`, baseRepo) assert.NoError(t, err) assert.Equal(t, "", output.String()) assert.Equal(t, "", output.Stderr()) From 490bfdac0339c16049f8e37ce5a434d0a331ea7a Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Wed, 4 Dec 2024 14:40:28 -0700 Subject: [PATCH 011/100] issue cli#2329: Fix linter --- pkg/cmd/pr/checkout/checkout_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 428125bbb..fe68fe059 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -532,9 +532,9 @@ func TestPRCheckout_differentRepo_invalidBranchName(t *testing.T) { _, cmdTeardown := run.Stub() defer cmdTeardown(t) - + output, err := runCommand(http, nil, "master", `123`, baseRepo) - + assert.EqualError(t, err, `invalid branch name: "-foo"`) assert.Equal(t, "", output.Stderr()) assert.Equal(t, "", output.Stderr()) From 12479e307282fabc9c6d68981073477de6a5c07c Mon Sep 17 00:00:00 2001 From: nilvng Date: Fri, 6 Dec 2024 18:19:07 +1100 Subject: [PATCH 012/100] issue #2329: fix unit tests --- pkg/cmd/pr/checkout/checkout_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index fe68fe059..4bbf23cdb 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -246,7 +246,6 @@ func Test_checkoutRun(t *testing.T) { }, runStubs: func(cs *run.CommandStubber) { cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") - cs.Register(`git remote get-url origin`, 0, "https://github.com/hubot/REPO.git") cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "") cs.Register(`git checkout -b feature --track origin/feature`, 0, "") }, From eced54c9044e78deeb202722c96e2b15d178f12d Mon Sep 17 00:00:00 2001 From: nilvng Date: Fri, 6 Dec 2024 18:49:43 +1100 Subject: [PATCH 013/100] issue #2329: update the prompt's format Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com> --- pkg/cmd/pr/checkout/checkout.go | 2 +- pkg/cmd/pr/checkout/checkout_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 8fb591b58..adaa6abe6 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -340,7 +340,7 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar func promptForPR(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { - candidates = append(candidates, text.Truncate(120, fmt.Sprintf("%s %s (%s)", + candidates = append(candidates, text.Truncate(120, fmt.Sprintf("%s %s [%s]", shared.PRNumberWithColor(cs, pr), text.RemoveExcessiveWhitespace(pr.Title), cs.Gray(pr.HeadLabel()), diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 4bbf23cdb..33173252e 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -239,9 +239,9 @@ func Test_checkoutRun(t *testing.T) { }, promptStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Select a pull request", - []string{"#32 New feature (feature)", "#29 Fixed bad bug (bug-fix)", "#28 Improve documentation (docs)"}, + []string{"#32 New feature [feature]", "#29 Fixed bad bug [bug-fix]", "#28 Improve documentation [docs]"}, func(_, _ string, opts []string) (int, error) { - return prompter.IndexFor(opts, "#32 New feature (feature)") + return prompter.IndexFor(opts, "#32 New feature [feature]") }) }, runStubs: func(cs *run.CommandStubber) { From 409e3ca08caffe21b4e3c6a09915ffc55eea5b49 Mon Sep 17 00:00:00 2001 From: nilvng Date: Fri, 6 Dec 2024 19:16:00 +1100 Subject: [PATCH 014/100] issue #2329: simplify the UI of the prompt This commit reverts the previous color changes in the prompt UI. While color highlighting could potentially improve the visual appealing of the prompt using the existing color library (mguz/ansi) with the prompt library (AlecAivazis/survey) caused unintended side effects. It reset the bold text style for the selected option. We decide to that bold text style would have a higher priority than the color text ,for three reasons: 1. To maintain consistency with other prompts in the UI and prioritize accessibility 2. While color can enhance the user experience, according to Primer Design Guidelines, color should not be relied upon to convey essential information. 3. visual indicator of the selected option, especially crucial when dealing with long PR titles or branch names. As a future improvement, we may consider a separate issue or PR to address the color library issue and explore controlled color usage in prompts. This could potentially allow for more nuanced visual differentiation while avoiding unintended style resets. Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com> --- pkg/cmd/pr/checkout/checkout.go | 16 ++++++++-------- pkg/cmd/pr/shared/display.go | 25 ------------------------- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index adaa6abe6..e60658955 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -102,7 +102,7 @@ func checkoutRun(opts *CheckoutOptions) error { return err } - pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder, opts.IO.ColorScheme()) + pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder) if err != nil { return err } @@ -291,7 +291,7 @@ func executeCmds(client *git.Client, credentialPattern git.CredentialPattern, cm return nil } -func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, cs *iostreams.ColorScheme) (*api.PullRequest, error) { +func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder) (*api.PullRequest, error) { // When non-interactive if pullRequestSelector != "" { pr, _, err := pullRequestFinder.Find(shared.FindOptions{ @@ -332,19 +332,19 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar return nil, err } - pr, err := promptForPR(prompter, cs, *listResult) + pr, err := promptForPR(prompter, *listResult) return pr, err } -func promptForPR(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { +func promptForPR(prompter shared.Prompter, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { - candidates = append(candidates, text.Truncate(120, fmt.Sprintf("%s %s [%s]", - shared.PRNumberWithColor(cs, pr), + candidates = append(candidates, fmt.Sprintf("#%d %s [%s]", + pr.Number, text.RemoveExcessiveWhitespace(pr.Title), - cs.Gray(pr.HeadLabel()), - ))) + pr.HeadLabel(), + )) } selected, err := prompter.Select("Select a pull request", "", candidates) diff --git a/pkg/cmd/pr/shared/display.go b/pkg/cmd/pr/shared/display.go index ffc4b4dce..7bd306104 100644 --- a/pkg/cmd/pr/shared/display.go +++ b/pkg/cmd/pr/shared/display.go @@ -17,15 +17,6 @@ func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { return prStateColorFunc(text.Title(pr.State)) } -// PRNumberWithColor returns a colored string representation of a pull request number -// based on its state (open, closed, merged, or draft). -// It prefixes the number with a hash symbol (#) to indicate it's a pull request. -func PRNumberWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { - prStateColorFunc := cs.ColorFromRGB(ColorHexCodeForPRState(pr)) - prNumber := fmt.Sprintf("#%d", pr.Number) - return prStateColorFunc(text.Title(prNumber)) -} - func ColorForPRState(pr api.PullRequest) string { switch pr.State { case "OPEN": @@ -42,22 +33,6 @@ func ColorForPRState(pr api.PullRequest) string { } } -func ColorHexCodeForPRState(pr api.PullRequest) string { - switch pr.State { - case "OPEN": - if pr.IsDraft { - return "808080" - } - return "00FF00" - case "CLOSED": - return "FF0000" - case "MERGED": - return "FFA500" - default: - return "" - } -} - func ColorForIssueState(issue api.Issue) string { switch issue.State { case "OPEN": From 4bc90101cd11ccf7a4e8a26c014d2f759dfc7108 Mon Sep 17 00:00:00 2001 From: nilvng Date: Mon, 9 Dec 2024 21:20:07 +1100 Subject: [PATCH 015/100] issue #2329: fix prompt header getting duplicated following the approach in gh run view seems to fix this issue --- pkg/cmd/pr/checkout/checkout.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index e60658955..71af45dda 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -102,7 +102,7 @@ func checkoutRun(opts *CheckoutOptions) error { return err } - pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder) + pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder, opts.IO) if err != nil { return err } @@ -291,7 +291,7 @@ func executeCmds(client *git.Client, credentialPattern git.CredentialPattern, cm return nil } -func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder) (*api.PullRequest, error) { +func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, io *iostreams.IOStreams) (*api.PullRequest, error) { // When non-interactive if pullRequestSelector != "" { pr, _, err := pullRequestFinder.Find(shared.FindOptions{ @@ -314,6 +314,7 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar return nil, cmdutil.FlagErrorf("pull request number, URL, or branch required when not running interactively") } // When interactive + io.StartProgressIndicator() listResult, err := list.ListPullRequests(httpClient, baseRepo, shared.FilterOptions{Entity: "pr", State: "open", Fields: []string{ "number", "title", @@ -328,6 +329,7 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar "isCrossRepository", "maintainerCanModify", }}, 10) + io.StopProgressIndicator() if err != nil { return nil, err } From 5109336963c445204b7eeee72ed20dc095a33cf7 Mon Sep 17 00:00:00 2001 From: nilvng Date: Mon, 9 Dec 2024 21:22:00 +1100 Subject: [PATCH 016/100] issue #2329: include PR status in the prompt's options --- pkg/cmd/pr/checkout/checkout.go | 3 +- pkg/cmd/pr/checkout/checkout_test.go | 4 +- pkg/cmd/pr/checkout/fixtures/prList.json | 117 +++++++++++------------ pkg/cmd/pr/list/list.go | 10 +- pkg/cmd/pr/shared/display.go | 8 ++ 5 files changed, 71 insertions(+), 71 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 71af45dda..88850b940 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -342,8 +342,9 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar func promptForPR(prompter shared.Prompter, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { - candidates = append(candidates, fmt.Sprintf("#%d %s [%s]", + candidates = append(candidates, fmt.Sprintf("%d\t%s\t%s [%s]", pr.Number, + shared.PrStateWithDraft(&pr), text.RemoveExcessiveWhitespace(pr.Title), pr.HeadLabel(), )) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 33173252e..f2b379912 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -239,9 +239,9 @@ func Test_checkoutRun(t *testing.T) { }, promptStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Select a pull request", - []string{"#32 New feature [feature]", "#29 Fixed bad bug [bug-fix]", "#28 Improve documentation [docs]"}, + []string{"32\tDRAFT\tNew feature [feature]", "29\tOPEN\tFixed bad bug [bug-fix]", "28\tOPEN\tImprove documentation [docs]"}, func(_, _ string, opts []string) (int, error) { - return prompter.IndexFor(opts, "#32 New feature [feature]") + return prompter.IndexFor(opts, "32\tDRAFT\tNew feature [feature]") }) }, runStubs: func(cs *run.CommandStubber) { diff --git a/pkg/cmd/pr/checkout/fixtures/prList.json b/pkg/cmd/pr/checkout/fixtures/prList.json index f915df136..84928722c 100644 --- a/pkg/cmd/pr/checkout/fixtures/prList.json +++ b/pkg/cmd/pr/checkout/fixtures/prList.json @@ -1,67 +1,66 @@ { - "data": { - "repository": { - "pullRequests": { - "totalCount": 3, - "nodes": [ - { - "number": 32, - "title": "New feature", - "url": "https://github.com/OWNER/REPO/pull/32", - "createdAt": "2022-08-24T20:01:12Z", - "headRefName": "feature", - "state": "OPEN", - "isDraft": true, - "isCrossRepository": false, - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true + "data": { + "repository": { + "pullRequests": { + "totalCount": 3, + "nodes": [ + { + "number": 32, + "title": "New feature", + "url": "https://github.com/OWNER/REPO/pull/32", + "createdAt": "2022-08-24T20:01:12Z", + "headRefName": "feature", + "state": "OPEN", + "isDraft": true, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" }, - { - "number": 29, - "title": "Fixed bad bug", - "url": "https://github.com/OWNER/REPO/pull/29", - "createdAt": "2022-07-20T19:01:12Z", - "headRefName": "bug-fix", - "state": "OPEN", - "isDraft": false, - "isCrossRepository": false, - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true + "headRepositoryOwner": { + "login": "OWNER" }, - { - "number": 28, - "state": "MERGED", - "isDraft": false, - "title": "Improve documentation", - "createdAt": "2020-01-26T19:01:12Z", - "url": "https://github.com/OWNER/REPO/pull/28", - "isCrossRepository": false, - "headRefName": "docs", - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true - } - ], - "pageInfo": { - "hasNextPage": false, - "endCursor": "" + "maintainerCanModify": true + }, + { + "number": 29, + "title": "Fixed bad bug", + "url": "https://github.com/OWNER/REPO/pull/29", + "createdAt": "2022-07-20T19:01:12Z", + "headRefName": "bug-fix", + "state": "OPEN", + "isDraft": false, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + }, + { + "number": 28, + "state": "OPEN", + "isDraft": false, + "title": "Improve documentation", + "createdAt": "2020-01-26T19:01:12Z", + "url": "https://github.com/OWNER/REPO/pull/28", + "isCrossRepository": false, + "headRefName": "docs", + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" } } } } - \ No newline at end of file +} diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index 401e168e3..e5ebe50fb 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -222,7 +222,7 @@ func listRun(opts *ListOptions) error { table.AddField(text.RemoveExcessiveWhitespace(pr.Title)) table.AddField(pr.HeadLabel(), tableprinter.WithColor(cs.Cyan)) if !isTTY { - table.AddField(prStateWithDraft(&pr)) + table.AddField(shared.PrStateWithDraft(&pr)) } table.AddTimeField(opts.Now(), pr.CreatedAt, cs.Gray) table.EndRow() @@ -234,11 +234,3 @@ func listRun(opts *ListOptions) error { return nil } - -func prStateWithDraft(pr *api.PullRequest) string { - if pr.IsDraft && pr.State == "OPEN" { - return "DRAFT" - } - - return pr.State -} diff --git a/pkg/cmd/pr/shared/display.go b/pkg/cmd/pr/shared/display.go index 7bd306104..02482951c 100644 --- a/pkg/cmd/pr/shared/display.go +++ b/pkg/cmd/pr/shared/display.go @@ -17,6 +17,14 @@ func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { return prStateColorFunc(text.Title(pr.State)) } +func PrStateWithDraft(pr *api.PullRequest) string { + if pr.IsDraft && pr.State == "OPEN" { + return "DRAFT" + } + + return pr.State +} + func ColorForPRState(pr api.PullRequest) string { switch pr.State { case "OPEN": From 1f525bf1752cb171150f2c899a2d228943f98138 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 15 Dec 2024 12:34:46 +1100 Subject: [PATCH 017/100] issue #2329: handle empty list of PR --- pkg/cmd/pr/checkout/checkout.go | 4 +++- pkg/cmd/pr/checkout/checkout_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 88850b940..8df4a6faf 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -333,9 +333,11 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar if err != nil { return nil, err } + if len(listResult.PullRequests) == 0 { + return nil, shared.ListNoResults(ghrepo.FullName(baseRepo), "pull request", false) + } pr, err := promptForPR(prompter, *listResult) - return pr, err } diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index f2b379912..b1d70f68b 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -75,6 +75,7 @@ func Test_checkoutRun(t *testing.T) { wantStdout string wantStderr string wantErr bool + errMsg string }{ { name: "checkout with ssh remote URL", @@ -204,7 +205,7 @@ func Test_checkoutRun(t *testing.T) { }, }, { - name: "with no selected PR args and not stdin tty, return error", + name: "with no selected PR args and non tty, return error", opts: &CheckoutOptions{ SelectorArg: "", Interactive: false, @@ -216,6 +217,7 @@ func Test_checkoutRun(t *testing.T) { "origin": "OWNER/REPO", }, wantErr: true, + errMsg: "pull request number, URL, or branch required when not running interactively", }, { name: "with no selected PR args and stdin tty, prompts for choice", @@ -253,6 +255,26 @@ func Test_checkoutRun(t *testing.T) { "origin": "OWNER/REPO", }, }, + { + name: "with no select PR args and no open PR, return error", + opts: &CheckoutOptions{ + SelectorArg: "", + Interactive: true, + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL(`query PullRequestList\b`), + httpmock.StringResponse(`{"data":{"repository":{"pullRequests":{ "totalCount": 0,"nodes":[]}}}}`)) + + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + wantErr: true, + errMsg: "no open pull requests in OWNER/REPO", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -309,6 +331,9 @@ func Test_checkoutRun(t *testing.T) { if (err != nil) != tt.wantErr { t.Errorf("want error: %v, got: %v", tt.wantErr, err) } + if err != nil { + assert.Equal(t, tt.errMsg, err.Error()) + } assert.Equal(t, tt.wantStdout, stdout.String()) assert.Equal(t, tt.wantStderr, stderr.String()) }) From d7cabf18f7b805dec117f641e071c23247f64872 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 15 Dec 2024 13:55:47 +1100 Subject: [PATCH 018/100] fix: tab format may break checkout prompts --- pkg/cmd/pr/checkout/checkout.go | 2 +- pkg/cmd/pr/checkout/checkout_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 8df4a6faf..faf006c7e 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -344,7 +344,7 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar func promptForPR(prompter shared.Prompter, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { - candidates = append(candidates, fmt.Sprintf("%d\t%s\t%s [%s]", + candidates = append(candidates, fmt.Sprintf("%d\t%s %s [%s]", pr.Number, shared.PrStateWithDraft(&pr), text.RemoveExcessiveWhitespace(pr.Title), diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index b1d70f68b..a099a848d 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -241,9 +241,9 @@ func Test_checkoutRun(t *testing.T) { }, promptStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Select a pull request", - []string{"32\tDRAFT\tNew feature [feature]", "29\tOPEN\tFixed bad bug [bug-fix]", "28\tOPEN\tImprove documentation [docs]"}, + []string{"32\tDRAFT New feature [feature]", "29\tOPEN Fixed bad bug [bug-fix]", "28\tOPEN Improve documentation [docs]"}, func(_, _ string, opts []string) (int, error) { - return prompter.IndexFor(opts, "32\tDRAFT\tNew feature [feature]") + return prompter.IndexFor(opts, "32\tDRAFT New feature [feature]") }) }, runStubs: func(cs *run.CommandStubber) { From 91b3b99b767dd0367a49d87668885d83c43515bd Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 15 Dec 2024 14:01:08 +1100 Subject: [PATCH 019/100] issue #2329: create shared PRLister --- pkg/cmd/pr/checkout/checkout.go | 39 +++--- pkg/cmd/pr/checkout/checkout_test.go | 41 ++++-- pkg/cmd/pr/shared/finder.go | 4 +- pkg/cmd/pr/shared/lister.go | 196 +++++++++++++++++++++++++++ 4 files changed, 242 insertions(+), 38 deletions(-) create mode 100644 pkg/cmd/pr/shared/lister.go diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index faf006c7e..b265ea25e 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -13,7 +13,6 @@ import ( "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" - "github.com/cli/cli/v2/pkg/cmd/pr/list" "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -30,6 +29,7 @@ type CheckoutOptions struct { Finder shared.PRFinder Prompter shared.Prompter + Lister shared.PRLister Interactive bool BaseRepo func() (ghrepo.Interface, error) @@ -67,6 +67,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Finder = shared.NewFinder(f) + opts.Lister = shared.NewLister(f) if len(args) > 0 { opts.SelectorArg = args[0] @@ -97,12 +98,7 @@ func checkoutRun(opts *CheckoutOptions) error { return err } - client, err := opts.HttpClient() - if err != nil { - return err - } - - pr, err := resolvePR(client, baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder, opts.IO) + pr, err := resolvePR(baseRepo, opts.Prompter, opts.SelectorArg, opts.Interactive, opts.Finder, opts.Lister, opts.IO) if err != nil { return err } @@ -291,7 +287,7 @@ func executeCmds(client *git.Client, credentialPattern git.CredentialPattern, cm return nil } -func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, io *iostreams.IOStreams) (*api.PullRequest, error) { +func resolvePR(baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, prLister shared.PRLister, io *iostreams.IOStreams) (*api.PullRequest, error) { // When non-interactive if pullRequestSelector != "" { pr, _, err := pullRequestFinder.Find(shared.FindOptions{ @@ -315,20 +311,21 @@ func resolvePR(httpClient *http.Client, baseRepo ghrepo.Interface, prompter shar } // When interactive io.StartProgressIndicator() - listResult, err := list.ListPullRequests(httpClient, baseRepo, shared.FilterOptions{Entity: "pr", State: "open", Fields: []string{ - "number", - "title", - "state", - "url", - "isDraft", - "createdAt", + listResult, err := prLister.List(shared.ListOptions{ + State: "open", + Fields: []string{ + "number", + "title", + "state", + "isDraft", - "headRefName", - "headRepository", - "headRepositoryOwner", - "isCrossRepository", - "maintainerCanModify", - }}, 10) + "headRefName", + "headRepository", + "headRepositoryOwner", + "isCrossRepository", + "maintainerCanModify", + }, + LimitResults: 10}) io.StopProgressIndicator() if err != nil { return nil, err diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index a099a848d..bd8bc3984 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -28,6 +28,10 @@ import ( // repo: either "baseOwner/baseRepo" or "baseOwner/baseRepo:defaultBranch" // prHead: "headOwner/headRepo:headBranch" func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) { + return _stubPR(repo, prHead, 123, "PR title", "OPEN", false) +} + +func _stubPR(repo, prHead string, number int, title string, state string, isDraft bool) (ghrepo.Interface, *api.PullRequest) { defaultBranch := "" if idx := strings.IndexRune(repo, ':'); idx >= 0 { defaultBranch = repo[idx+1:] @@ -53,12 +57,16 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) { } return baseRepo, &api.PullRequest{ - Number: 123, + Number: number, HeadRefName: headRefName, HeadRepositoryOwner: api.Owner{Login: headRepo.RepoOwner()}, HeadRepository: &api.PRRepository{Name: headRepo.RepoName()}, IsCrossRepository: !ghrepo.IsSame(baseRepo, headRepo), MaintainerCanModify: false, + + Title: title, + State: state, + IsDraft: isDraft, } } @@ -224,10 +232,17 @@ func Test_checkoutRun(t *testing.T) { opts: &CheckoutOptions{ SelectorArg: "", Interactive: true, - Finder: func() shared.PRFinder { - baseRepo, pr := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") - finder := shared.NewMockFinder("123", pr, baseRepo) - return finder + Lister: func() shared.PRLister { + _, pr1 := _stubPR("OWNER/REPO:master", "OWNER/REPO:feature", 32, "New feature", "OPEN", false) + _, pr2 := _stubPR("OWNER/REPO:master", "OWNER/REPO:bug-fix", 29, "Fixed bad bug", "OPEN", false) + _, pr3 := _stubPR("OWNER/REPO:master", "OWNER/REPO:docs", 28, "Improve documentation", "OPEN", true) + lister := shared.NewMockLister(&api.PullRequestAndTotalCount{ + TotalCount: 3, + PullRequests: []api.PullRequest{ + *pr1, *pr2, *pr3, + }, SearchCapped: false}, nil) + lister.ExpectFields([]string{"number", "title", "state", "isDraft", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"}) + return lister }(), BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil @@ -236,14 +251,11 @@ func Test_checkoutRun(t *testing.T) { return config.NewBlankConfig(), nil }, }, - httpStubs: func(reg *httpmock.Registry) { - reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) - }, promptStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Select a pull request", - []string{"32\tDRAFT New feature [feature]", "29\tOPEN Fixed bad bug [bug-fix]", "28\tOPEN Improve documentation [docs]"}, + []string{"32\tOPEN New feature [feature]", "29\tOPEN Fixed bad bug [bug-fix]", "28\tDRAFT Improve documentation [docs]"}, func(_, _ string, opts []string) (int, error) { - return prompter.IndexFor(opts, "32\tDRAFT New feature [feature]") + return prompter.IndexFor(opts, "32\tOPEN New feature [feature]") }) }, runStubs: func(cs *run.CommandStubber) { @@ -263,11 +275,10 @@ func Test_checkoutRun(t *testing.T) { BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }, - }, - httpStubs: func(reg *httpmock.Registry) { - reg.Register(httpmock.GraphQL(`query PullRequestList\b`), - httpmock.StringResponse(`{"data":{"repository":{"pullRequests":{ "totalCount": 0,"nodes":[]}}}}`)) - + Lister: shared.NewMockLister(&api.PullRequestAndTotalCount{ + TotalCount: 0, + PullRequests: []api.PullRequest{}, + }, nil), }, remotes: map[string]string{ "origin": "OWNER/REPO", diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index e4a70eb22..b1fec267e 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -548,7 +548,7 @@ func (m *mockFinder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, if m.expectSelector != opts.Selector { return nil, nil, fmt.Errorf("mockFinder: expected selector %q, got %q", m.expectSelector, opts.Selector) } - if len(m.expectFields) > 0 && !isEqualSet(m.expectFields, opts.Fields) { + if len(m.expectFields) > 0 && !IsEqualSet(m.expectFields, opts.Fields) { return nil, nil, fmt.Errorf("mockFinder: expected fields %v, got %v", m.expectFields, opts.Fields) } if m.called { @@ -568,7 +568,7 @@ func (m *mockFinder) ExpectFields(fields []string) { m.expectFields = fields } -func isEqualSet(a, b []string) bool { +func IsEqualSet(a, b []string) bool { if len(a) != len(b) { return false } diff --git a/pkg/cmd/pr/shared/lister.go b/pkg/cmd/pr/shared/lister.go new file mode 100644 index 000000000..004f726d5 --- /dev/null +++ b/pkg/cmd/pr/shared/lister.go @@ -0,0 +1,196 @@ +package shared + +import ( + "fmt" + "net/http" + + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmdutil" + + api "github.com/cli/cli/v2/api" +) + +type PRLister interface { + List(opt ListOptions) (*api.PullRequestAndTotalCount, error) +} + +type ListOptions struct { + LimitResults int + + State string + BaseBranch string + HeadBranch string + + Fields []string +} + +type lister struct { + baseRepoFn func() (ghrepo.Interface, error) + httpClient func() (*http.Client, error) +} + +func NewLister(factory *cmdutil.Factory) PRLister { + return &lister{ + baseRepoFn: factory.BaseRepo, + httpClient: factory.HttpClient, + } +} + +func (l *lister) List(opts ListOptions) (*api.PullRequestAndTotalCount, error) { + repo, err := l.baseRepoFn() + if err != nil { + return nil, err + } + + client, err := l.httpClient() + if err != nil { + return nil, err + } + + return listPullRequests(client, repo, opts) +} + +func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters ListOptions) (*api.PullRequestAndTotalCount, error) { + type response struct { + Repository struct { + PullRequests struct { + Nodes []api.PullRequest + PageInfo struct { + HasNextPage bool + EndCursor string + } + TotalCount int + } + } + } + limit := filters.LimitResults + fragment := fmt.Sprintf("fragment pr on PullRequest{%s}", api.PullRequestGraphQL(filters.Fields)) + query := fragment + ` + query PullRequestList( + $owner: String!, + $repo: String!, + $limit: Int!, + $endCursor: String, + $baseBranch: String, + $headBranch: String, + $state: [PullRequestState!] = OPEN + ) { + repository(owner: $owner, name: $repo) { + pullRequests( + states: $state, + baseRefName: $baseBranch, + headRefName: $headBranch, + first: $limit, + after: $endCursor, + orderBy: {field: CREATED_AT, direction: DESC} + ) { + totalCount + nodes { + ...pr + } + pageInfo { + hasNextPage + endCursor + } + } + } + }` + + pageLimit := min(limit, 100) + variables := map[string]interface{}{ + "owner": repo.RepoOwner(), + "repo": repo.RepoName(), + } + + switch filters.State { + case "open": + variables["state"] = []string{"OPEN"} + case "closed": + variables["state"] = []string{"CLOSED", "MERGED"} + case "merged": + variables["state"] = []string{"MERGED"} + case "all": + variables["state"] = []string{"OPEN", "CLOSED", "MERGED"} + default: + return nil, fmt.Errorf("invalid state: %s", filters.State) + } + + if filters.BaseBranch != "" { + variables["baseBranch"] = filters.BaseBranch + } + if filters.HeadBranch != "" { + variables["headBranch"] = filters.HeadBranch + } + + res := api.PullRequestAndTotalCount{} + var check = make(map[int]struct{}) + client := api.NewClientFromHTTP(httpClient) + +loop: + for { + variables["limit"] = pageLimit + var data response + err := client.GraphQL(repo.RepoHost(), query, variables, &data) + if err != nil { + return nil, err + } + prData := data.Repository.PullRequests + res.TotalCount = prData.TotalCount + + for _, pr := range prData.Nodes { + if _, exists := check[pr.Number]; exists && pr.Number > 0 { + continue + } + check[pr.Number] = struct{}{} + + res.PullRequests = append(res.PullRequests, pr) + if len(res.PullRequests) == limit { + break loop + } + } + + if prData.PageInfo.HasNextPage { + variables["endCursor"] = prData.PageInfo.EndCursor + pageLimit = min(pageLimit, limit-len(res.PullRequests)) + } else { + break + } + } + + return &res, nil +} + +type mockLister struct { + called bool + expectFields []string + + result *api.PullRequestAndTotalCount + err error +} + +func NewMockLister(result *api.PullRequestAndTotalCount, err error) *mockLister { + return &mockLister{ + result: result, + err: err, + } +} + +func (m *mockLister) List(opt ListOptions) (*api.PullRequestAndTotalCount, error) { + m.called = true + if m.err != nil { + return nil, m.err + } + + if m.expectFields != nil { + + if !IsEqualSet(m.expectFields, opt.Fields) { + return nil, fmt.Errorf("unexpected fields: %v", opt.Fields) + } + } + + return m.result, m.err +} + +func (m *mockLister) ExpectFields(fields []string) { + m.expectFields = fields +} From 55598d0a2c7d0b4c4772d3c824b6339d4dd90bc2 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 15 Dec 2024 18:03:06 +1100 Subject: [PATCH 020/100] feat: integrate new PRLister into pr list command --- pkg/cmd/pr/list/http.go | 120 ++++------------------------------- pkg/cmd/pr/list/http_test.go | 2 +- pkg/cmd/pr/list/list.go | 2 +- pkg/cmd/pr/list/list_test.go | 4 +- 4 files changed, 16 insertions(+), 112 deletions(-) diff --git a/pkg/cmd/pr/list/http.go b/pkg/cmd/pr/list/http.go index cb79b3b87..3836b9eb9 100644 --- a/pkg/cmd/pr/list/http.go +++ b/pkg/cmd/pr/list/http.go @@ -7,124 +7,28 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" + "github.com/cli/cli/v2/pkg/cmdutil" ) func shouldUseSearch(filters prShared.FilterOptions) bool { return filters.Draft != nil || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0 } -func ListPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) { +func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) { if shouldUseSearch(filters) { return searchPullRequests(httpClient, repo, filters, limit) } - type response struct { - Repository struct { - PullRequests struct { - Nodes []api.PullRequest - PageInfo struct { - HasNextPage bool - EndCursor string - } - TotalCount int - } - } - } - - fragment := fmt.Sprintf("fragment pr on PullRequest{%s}", api.PullRequestGraphQL(filters.Fields)) - query := fragment + ` - query PullRequestList( - $owner: String!, - $repo: String!, - $limit: Int!, - $endCursor: String, - $baseBranch: String, - $headBranch: String, - $state: [PullRequestState!] = OPEN - ) { - repository(owner: $owner, name: $repo) { - pullRequests( - states: $state, - baseRefName: $baseBranch, - headRefName: $headBranch, - first: $limit, - after: $endCursor, - orderBy: {field: CREATED_AT, direction: DESC} - ) { - totalCount - nodes { - ...pr - } - pageInfo { - hasNextPage - endCursor - } - } - } - }` - - pageLimit := min(limit, 100) - variables := map[string]interface{}{ - "owner": repo.RepoOwner(), - "repo": repo.RepoName(), - } - - switch filters.State { - case "open": - variables["state"] = []string{"OPEN"} - case "closed": - variables["state"] = []string{"CLOSED", "MERGED"} - case "merged": - variables["state"] = []string{"MERGED"} - case "all": - variables["state"] = []string{"OPEN", "CLOSED", "MERGED"} - default: - return nil, fmt.Errorf("invalid state: %s", filters.State) - } - - if filters.BaseBranch != "" { - variables["baseBranch"] = filters.BaseBranch - } - if filters.HeadBranch != "" { - variables["headBranch"] = filters.HeadBranch - } - - res := api.PullRequestAndTotalCount{} - var check = make(map[int]struct{}) - client := api.NewClientFromHTTP(httpClient) - -loop: - for { - variables["limit"] = pageLimit - var data response - err := client.GraphQL(repo.RepoHost(), query, variables, &data) - if err != nil { - return nil, err - } - prData := data.Repository.PullRequests - res.TotalCount = prData.TotalCount - - for _, pr := range prData.Nodes { - if _, exists := check[pr.Number]; exists && pr.Number > 0 { - continue - } - check[pr.Number] = struct{}{} - - res.PullRequests = append(res.PullRequests, pr) - if len(res.PullRequests) == limit { - break loop - } - } - - if prData.PageInfo.HasNextPage { - variables["endCursor"] = prData.PageInfo.EndCursor - pageLimit = min(pageLimit, limit-len(res.PullRequests)) - } else { - break - } - } - - return &res, nil + return prShared.NewLister(&cmdutil.Factory{ + HttpClient: func() (*http.Client, error) { return httpClient, nil }, + BaseRepo: func() (ghrepo.Interface, error) { return repo, nil }, + }).List(prShared.ListOptions{ + LimitResults: limit, + State: filters.State, + BaseBranch: filters.BaseBranch, + HeadBranch: filters.HeadBranch, + Fields: filters.Fields, + }) } func searchPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) { diff --git a/pkg/cmd/pr/list/http_test.go b/pkg/cmd/pr/list/http_test.go index 060e3a179..1aa16ae1d 100644 --- a/pkg/cmd/pr/list/http_test.go +++ b/pkg/cmd/pr/list/http_test.go @@ -153,7 +153,7 @@ func Test_ListPullRequests(t *testing.T) { } httpClient := &http.Client{Transport: reg} - _, err := ListPullRequests(httpClient, tt.args.repo, tt.args.filters, tt.args.limit) + _, err := listPullRequests(httpClient, tt.args.repo, tt.args.filters, tt.args.limit) if (err != nil) != tt.wantErr { t.Errorf("ListPullRequests() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index e5ebe50fb..b160f7ebc 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -172,7 +172,7 @@ func listRun(opts *ListOptions) error { return opts.Browser.Browse(openURL) } - listResult, err := ListPullRequests(httpClient, baseRepo, filters, opts.LimitResults) + listResult, err := listPullRequests(httpClient, baseRepo, filters, opts.LimitResults) if err != nil { return err } diff --git a/pkg/cmd/pr/list/list_test.go b/pkg/cmd/pr/list/list_test.go index 7ee111afc..ecd0326b5 100644 --- a/pkg/cmd/pr/list/list_test.go +++ b/pkg/cmd/pr/list/list_test.go @@ -368,7 +368,7 @@ func TestPRList_withProjectItems(t *testing.T) { })) client := &http.Client{Transport: reg} - prsAndTotalCount, err := ListPullRequests( + prsAndTotalCount, err := listPullRequests( client, ghrepo.New("OWNER", "REPO"), prShared.FilterOptions{ @@ -437,7 +437,7 @@ func TestPRList_Search_withProjectItems(t *testing.T) { })) client := &http.Client{Transport: reg} - prsAndTotalCount, err := ListPullRequests( + prsAndTotalCount, err := listPullRequests( client, ghrepo.New("OWNER", "REPO"), prShared.FilterOptions{ From 3987f4af4a3eb7d4d289ceacfb82a68bb9548da8 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 15 Dec 2024 18:08:45 +1100 Subject: [PATCH 021/100] chore: remove unused file --- pkg/cmd/pr/checkout/fixtures/prList.json | 66 ------------------------ 1 file changed, 66 deletions(-) delete mode 100644 pkg/cmd/pr/checkout/fixtures/prList.json diff --git a/pkg/cmd/pr/checkout/fixtures/prList.json b/pkg/cmd/pr/checkout/fixtures/prList.json deleted file mode 100644 index 84928722c..000000000 --- a/pkg/cmd/pr/checkout/fixtures/prList.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "data": { - "repository": { - "pullRequests": { - "totalCount": 3, - "nodes": [ - { - "number": 32, - "title": "New feature", - "url": "https://github.com/OWNER/REPO/pull/32", - "createdAt": "2022-08-24T20:01:12Z", - "headRefName": "feature", - "state": "OPEN", - "isDraft": true, - "isCrossRepository": false, - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true - }, - { - "number": 29, - "title": "Fixed bad bug", - "url": "https://github.com/OWNER/REPO/pull/29", - "createdAt": "2022-07-20T19:01:12Z", - "headRefName": "bug-fix", - "state": "OPEN", - "isDraft": false, - "isCrossRepository": false, - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true - }, - { - "number": 28, - "state": "OPEN", - "isDraft": false, - "title": "Improve documentation", - "createdAt": "2020-01-26T19:01:12Z", - "url": "https://github.com/OWNER/REPO/pull/28", - "isCrossRepository": false, - "headRefName": "docs", - "headRepository": { - "name": "REPO" - }, - "headRepositoryOwner": { - "login": "OWNER" - }, - "maintainerCanModify": true - } - ], - "pageInfo": { - "hasNextPage": false, - "endCursor": "" - } - } - } - } -} From 0179381efdc45980596aae7a2147ce13440ec483 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Fri, 14 Jun 2024 14:43:38 +1000 Subject: [PATCH 022/100] Find PRs using @{push} When using a push.default = current central workflow [1], we should use @{push} instead to locate the remote branch. In fact, @{push} covers most cases in push.default = upstream too. The branch..merge is probably only needed when using RemoteURL and different remote / local branch names. [1] https://github.com/tpope/vim-fugitive/issues/1172#issuecomment-522301607 --- git/client.go | 3 ++ git/client_test.go | 37 +++++++++++++++++------- git/objects.go | 1 + pkg/cmd/pr/create/create_test.go | 49 ++++++++++++++++++++++++++++++++ pkg/cmd/pr/shared/finder.go | 4 ++- pkg/cmd/pr/shared/finder_test.go | 10 +++++++ pkg/cmd/pr/status/status.go | 4 ++- pkg/cmd/pr/status/status_test.go | 1 + 8 files changed, 96 insertions(+), 13 deletions(-) diff --git a/git/client.go b/git/client.go index 1a6d9ae7f..99acb0ac9 100644 --- a/git/client.go +++ b/git/client.go @@ -412,6 +412,9 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg Branc cfg.MergeBase = parts[1] } } + if out, err = c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}"); err == nil { + cfg.Push = strings.TrimSuffix(string(out), "\n") + } return } diff --git a/git/client_test.go b/git/client_test.go index fff1397f3..c547d0335 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -727,28 +727,43 @@ func TestClientCommitBody(t *testing.T) { func TestClientReadBranchConfig(t *testing.T) { tests := []struct { name string - cmdExitStatus int - cmdStdout string - cmdStderr string - wantCmdArgs string + cmdExitStatus []int + cmdStdout []string + cmdStderr []string + wantCmdArgs []string wantBranchConfig BranchConfig }{ { name: "read branch config", - cmdStdout: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.gh-merge-base trunk", - wantCmdArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|gh-merge-base)$`, - wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/trunk", MergeBase: "trunk"}, + cmdExitStatus: []int{0, 0}, + cmdStdout: []string{"branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.gh-merge-base trunk", "origin/trunk"}, + cmdStderr: []string{"", ""}, + wantCmdArgs: []string{`path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|gh-merge-base)$`, `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`}, + wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/trunk", MergeBase: "trunk", Push: "origin/trunk"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus, tt.cmdStdout, tt.cmdStderr) + var cmds []*exec.Cmd + var cmdCtxs []commandCtx + for i := 0; i < len(tt.cmdExitStatus); i++ { + cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus[i], tt.cmdStdout[i], tt.cmdStderr[i]) + cmds = append(cmds, cmd) + cmdCtxs = append(cmdCtxs, cmdCtx) + } + i := -1 client := Client{ - GitPath: "path/to/git", - commandContext: cmdCtx, + GitPath: "path/to/git", + commandContext: func(ctx context.Context, name string, args ...string) *exec.Cmd { + i++ + cmdCtxs[i](ctx, name, args...) + return cmds[i] + }, } branchConfig := client.ReadBranchConfig(context.Background(), "trunk") - assert.Equal(t, tt.wantCmdArgs, strings.Join(cmd.Args[3:], " ")) + for i := 0; i < len(tt.cmdExitStatus); i++ { + assert.Equal(t, tt.wantCmdArgs[i], strings.Join(cmds[i].Args[3:], " ")) + } assert.Equal(t, tt.wantBranchConfig, branchConfig) }) } diff --git a/git/objects.go b/git/objects.go index c09683042..3b23d20b7 100644 --- a/git/objects.go +++ b/git/objects.go @@ -66,4 +66,5 @@ type BranchConfig struct { // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. MergeBase string MergeRef string + Push string } diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 6c0ff11e2..2446b16c0 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -354,6 +354,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, expectedBrowse: "https://github.com/OWNER/REPO/compare/master...feature?body=&expand=1", @@ -383,6 +384,9 @@ func Test_createRun(t *testing.T) { opts.HeadBranch = "feature" return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", }, { @@ -397,6 +401,9 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, expectedOutputs: []string{ "Would have created a Pull Request with:", `title: my title`, @@ -428,6 +435,9 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -488,6 +498,9 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, expectedOutputs: []string{ `Would have created a Pull Request with:`, `Title: my title`, @@ -525,6 +538,9 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -591,6 +607,9 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, expectedOut: heredoc.Doc(` Would have created a Pull Request with: Title: TITLE @@ -637,6 +656,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -700,6 +720,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -746,6 +767,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -795,6 +817,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register("git remote rename origin upstream", 0, "") cs.Register(`git remote add origin https://github.com/monalisa/REPO.git`, 0, "") @@ -853,6 +876,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/upstream/feature @@ -890,6 +914,7 @@ func Test_createRun(t *testing.T) { branch.feature.remote origin branch.feature.merge refs/heads/my-feat2 `)) // determineTrackingBranch + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/my-feat2") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD deadbeef refs/remotes/origin/my-feat2 @@ -930,6 +955,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "d3476a1\u0000commit 0\u0000\u0000\n7a6ea13\u0000commit 1\u0000\u0000") }, promptStubs: func(pm *prompter.PrompterMock) { @@ -970,6 +996,9 @@ func Test_createRun(t *testing.T) { opts.Milestone = "big one.oh" return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -1057,6 +1086,9 @@ func Test_createRun(t *testing.T) { opts.Finder = shared.NewMockFinder("feature", &api.PullRequest{URL: "https://github.com/OWNER/REPO/pull/123"}, ghrepo.New("OWNER", "REPO")) return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, wantErr: "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123", }, { @@ -1073,6 +1105,7 @@ func Test_createRun(t *testing.T) { httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`)) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -1105,6 +1138,7 @@ func Test_createRun(t *testing.T) { mockRetrieveProjects(t, reg) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -1152,6 +1186,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature`, 0, "") cs.Register(`git rev-parse --show-toplevel`, 0, "") }, @@ -1211,6 +1246,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, promptStubs: func(pm *prompter.PrompterMock) { @@ -1259,6 +1295,7 @@ func Test_createRun(t *testing.T) { { name: "web long URL", cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, setup: func(opts *CreateOptions, t *testing.T) func() { @@ -1285,6 +1322,9 @@ func Test_createRun(t *testing.T) { } return func() {} }, + cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") + }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`mutation PullRequestCreate\b`), @@ -1304,6 +1344,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1379,6 +1420,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1415,6 +1457,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1465,6 +1508,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", @@ -1524,6 +1568,7 @@ func Test_createRun(t *testing.T) { deadbeef HEAD deadb00f refs/remotes/upstream/feature/feat2 deadbeef refs/remotes/origin/task1`)) // determineTrackingBranch + cs.Register(`git rev-parse --verify --quiet --abbrev-ref task1@\{push\}`, 1, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", expectedErrOut: "\nCreating pull request for monalisa:task1 into feature/feat2 in OWNER/REPO\n\n", @@ -1634,6 +1679,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "empty", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD`, 0, "abc HEAD") }, expectedTrackingRef: trackingRef{}, @@ -1643,6 +1689,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "no match", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature", 0, "abc HEAD\nbca refs/remotes/upstream/feature") }, remotes: context.Remotes{ @@ -1662,6 +1709,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "match", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/feature") cs.Register(`git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/upstream/feature @@ -1691,6 +1739,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { branch.feature.remote origin branch.feature.merge refs/heads/great-feat `)) + cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/great-feat") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/great-feat refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/origin/feature diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index e4a70eb22..23aee371c 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -261,7 +261,9 @@ func (f *finder) parseCurrentBranch() (string, int, error) { } if gitRemoteRepo != nil { - if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { + if branchConfig.Push != "" { + prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") + } else if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") } // prepend `OWNER:` if this branch is pushed to a fork diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 258ef01e8..88ee170a0 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -232,8 +232,17 @@ func TestFind(t *testing.T) { return "blueberries", nil }, branchConfig: func(branch string) (c git.BranchConfig) { + c.MergeRef = "refs/heads/blueberries" + c.RemoteName = "origin" + c.Push = "origin/blueberries" return }, + remotesFn: func() (context.Remotes, error) { + return context.Remotes{{ + Remote: &git.Remote{Name: "origin"}, + Repo: ghrepo.New("OWNER", "REPO"), + }}, nil + }, }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -318,6 +327,7 @@ func TestFind(t *testing.T) { branchConfig: func(branch string) (c git.BranchConfig) { c.MergeRef = "refs/heads/blue-upstream-berries" c.RemoteName = "origin" + c.Push = "origin/blue-upstream-berries" return }, remotesFn: func() (context.Remotes, error) { diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 27666461d..ebba3313c 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -209,7 +209,9 @@ func prSelectorForCurrentBranch(gitClient *git.Client, baseRepo ghrepo.Interface } if branchOwner != "" { - if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { + if branchConfig.Push != "" { + selector = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") + } else if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") } // prepend `OWNER:` if this branch is pushed to a fork diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index e8e487559..9fbe484c5 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -383,6 +383,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { branch.Frederick888/main.remote git@github.com:Frederick888/playground.git branch.Frederick888/main.merge refs/heads/main `)) + rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "") repo := ghrepo.NewWithHost("octocat", "playground", "github.com") rem := context.Remotes{ From 7fc35fd47d9482068a5c7666c5c67d4e5e001fce Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Fri, 14 Jun 2024 15:10:53 +1000 Subject: [PATCH 023/100] Only find PRs w/ branch..merge if push.default = upstream/tracking When push.default is not 'upstream' / 'tracking' (or 'nothing'), we can expect local and remote branch names to be the same and solely rely on @{push} or RemoteURL. This fixes the wrong error message 'no pull requests found for branch ""' when the local branch is not pushed in the push.default = simple / current and upstream = setup. --- pkg/cmd/pr/shared/finder.go | 9 ++++-- pkg/cmd/pr/shared/finder_test.go | 51 +++++++++++++++++++++++++++++++- pkg/cmd/pr/status/status.go | 3 +- pkg/cmd/pr/status/status_test.go | 34 ++++++++++++++++++++- 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 23aee371c..6e36b4dd6 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -38,6 +38,7 @@ type finder struct { remotesFn func() (remotes.Remotes, error) httpClient func() (*http.Client, error) branchConfig func(string) git.BranchConfig + pushDefault func() (string, error) progress progressIndicator repo ghrepo.Interface @@ -57,7 +58,10 @@ func NewFinder(factory *cmdutil.Factory) PRFinder { branchFn: factory.Branch, remotesFn: factory.Remotes, httpClient: factory.HttpClient, - progress: factory.IOStreams, + pushDefault: func() (string, error) { + return factory.GitClient.Config(context.Background(), "push.default") + }, + progress: factory.IOStreams, branchConfig: func(s string) git.BranchConfig { return factory.GitClient.ReadBranchConfig(context.Background(), s) }, @@ -263,7 +267,8 @@ func (f *finder) parseCurrentBranch() (string, int, error) { if gitRemoteRepo != nil { if branchConfig.Push != "" { prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") - } else if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { + } else if pushDefault, _ := f.pushDefault(); (pushDefault == "upstream" || pushDefault == "tracking") && + strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") } // prepend `OWNER:` if this branch is pushed to a fork diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 88ee170a0..2c34833c8 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -18,6 +18,7 @@ func TestFind(t *testing.T) { baseRepoFn func() (ghrepo.Interface, error) branchFn func() (string, error) branchConfig func(string) git.BranchConfig + pushDefault func() (string, error) remotesFn func() (context.Remotes, error) selector string fields []string @@ -330,6 +331,7 @@ func TestFind(t *testing.T) { c.Push = "origin/blue-upstream-berries" return }, + pushDefault: func() (string, error) { return "upstream", nil }, remotesFn: func() (context.Remotes, error) { return context.Remotes{{ Remote: &git.Remote{Name: "origin"}, @@ -373,7 +375,52 @@ func TestFind(t *testing.T) { c.RemoteURL = u return }, - remotesFn: nil, + pushDefault: func() (string, error) { return "upstream", nil }, + remotesFn: nil, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequestForBranch\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequests":{"nodes":[ + { + "number": 13, + "state": "OPEN", + "baseRefName": "main", + "headRefName": "blue-upstream-berries", + "isCrossRepository": true, + "headRepositoryOwner": {"login":"UPSTREAMOWNER"} + } + ]} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "current branch with tracking (deprecated synonym of upstream) configuration", + args: args{ + selector: "", + fields: []string{"id", "number"}, + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + }, + branchFn: func() (string, error) { + return "blueberries", nil + }, + branchConfig: func(branch string) (c git.BranchConfig) { + c.MergeRef = "refs/heads/blue-upstream-berries" + c.RemoteName = "origin" + c.Push = "origin/blue-upstream-berries" + return + }, + pushDefault: func() (string, error) { return "tracking", nil }, + remotesFn: func() (context.Remotes, error) { + return context.Remotes{{ + Remote: &git.Remote{Name: "origin"}, + Repo: ghrepo.New("UPSTREAMOWNER", "REPO"), + }}, nil + }, }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -407,6 +454,7 @@ func TestFind(t *testing.T) { }, branchConfig: func(branch string) (c git.BranchConfig) { c.RemoteName = "origin" + c.Push = "origin/blueberries" return }, remotesFn: func() (context.Remotes, error) { @@ -539,6 +587,7 @@ func TestFind(t *testing.T) { baseRepoFn: tt.args.baseRepoFn, branchFn: tt.args.branchFn, branchConfig: tt.args.branchConfig, + pushDefault: tt.args.pushDefault, remotesFn: tt.args.remotesFn, } diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index ebba3313c..1b6da8427 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -211,7 +211,8 @@ func prSelectorForCurrentBranch(gitClient *git.Client, baseRepo ghrepo.Interface if branchOwner != "" { if branchConfig.Push != "" { selector = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") - } else if strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { + } else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); (pushDefault == "upstream" || pushDefault == "tracking") && + strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") } // prepend `OWNER:` if this branch is pushed to a fork diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index 9fbe484c5..d09abf5b9 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -375,7 +375,7 @@ Requesting a code review from you } } -func Test_prSelectorForCurrentBranch(t *testing.T) { +func Test_prSelectorForCurrentBranchPushDefaultUpstream(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) @@ -384,6 +384,38 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { branch.Frederick888/main.merge refs/heads/main `)) rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "") + rs.Register(`git config push\.default`, 0, "upstream") + + repo := ghrepo.NewWithHost("octocat", "playground", "github.com") + rem := context.Remotes{ + &context.Remote{ + Remote: &git.Remote{Name: "origin"}, + Repo: repo, + }, + } + gitClient := &git.Client{GitPath: "some/path/git"} + prNum, headRef, err := prSelectorForCurrentBranch(gitClient, repo, "Frederick888/main", rem) + if err != nil { + t.Fatalf("prSelectorForCurrentBranch error: %v", err) + } + if prNum != 0 { + t.Errorf("expected prNum to be 0, got %q", prNum) + } + if headRef != "Frederick888:main" { + t.Errorf("expected headRef to be \"Frederick888:main\", got %q", headRef) + } +} + +func Test_prSelectorForCurrentBranchPushDefaultTracking(t *testing.T) { + rs, cleanup := run.Stub() + defer cleanup(t) + + rs.Register(`git config --get-regexp \^branch\\.`, 0, heredoc.Doc(` + branch.Frederick888/main.remote git@github.com:Frederick888/playground.git + branch.Frederick888/main.merge refs/heads/main + `)) + rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "") + rs.Register(`git config push\.default`, 0, "tracking") repo := ghrepo.NewWithHost("octocat", "playground", "github.com") rem := context.Remotes{ From 4254818dbdfa59b4f51e524687bd6cd49e7b61c2 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Fri, 14 Jun 2024 19:00:31 +1000 Subject: [PATCH 024/100] Find push remote using branch..pushRemote and remote.pushDefault When using a push.default = current triangular workflow, apart from using @{push} to determine the remote branch name, we should also follow the 1. branch..pushRemote 2. remote.pushDefault 3. branch..remote ...list to determine which remote Git pushes to. --- git/client.go | 31 ++++-- git/client_test.go | 157 ++++++++++++++++++++++++++++--- git/objects.go | 8 +- pkg/cmd/pr/create/create_test.go | 35 ++++++- pkg/cmd/pr/shared/finder.go | 9 +- pkg/cmd/pr/shared/finder_test.go | 4 + pkg/cmd/pr/status/status.go | 6 +- pkg/cmd/pr/status/status_test.go | 2 + 8 files changed, 215 insertions(+), 37 deletions(-) diff --git a/git/client.go b/git/client.go index 99acb0ac9..d82fd7b78 100644 --- a/git/client.go +++ b/git/client.go @@ -379,7 +379,7 @@ func (c *Client) lookupCommit(ctx context.Context, sha, format string) ([]byte, // ReadBranchConfig parses the `branch.BRANCH.(remote|merge|gh-merge-base)` part of git config. func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg BranchConfig) { prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch)) - args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|%s)$", prefix, MergeBaseConfig)} + args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|pushremote|%s)$", prefix, MergeBaseConfig)} cmd, err := c.Command(ctx, args...) if err != nil { return @@ -397,21 +397,22 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg Branc keys := strings.Split(parts[0], ".") switch keys[len(keys)-1] { case "remote": - if strings.Contains(parts[1], ":") { - u, err := ParseURL(parts[1]) - if err != nil { - continue - } - cfg.RemoteURL = u - } else if !isFilesystemPath(parts[1]) { - cfg.RemoteName = parts[1] - } + parseRemoteURLOrName(parts[1], &cfg.RemoteURL, &cfg.RemoteName) + case "pushremote": + parseRemoteURLOrName(parts[1], &cfg.PushRemoteURL, &cfg.PushRemoteName) case "merge": cfg.MergeRef = parts[1] case MergeBaseConfig: cfg.MergeBase = parts[1] } } + if cfg.PushRemoteURL == nil && cfg.PushRemoteName == "" { + if conf, err := c.Config(ctx, "remote.pushDefault"); err == nil && conf != "" { + parseRemoteURLOrName(conf, &cfg.PushRemoteURL, &cfg.PushRemoteName) + } else { + cfg.PushRemoteName = cfg.RemoteName + } + } if out, err = c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}"); err == nil { cfg.Push = strings.TrimSuffix(string(out), "\n") } @@ -776,6 +777,16 @@ func parseRemotes(remotesStr []string) RemoteSet { return remotes } +func parseRemoteURLOrName(value string, remoteURL **url.URL, remoteName *string) { + if strings.Contains(value, ":") { + if u, err := ParseURL(value); err == nil { + *remoteURL = u + } + } else if !isFilesystemPath(value) { + *remoteName = value + } +} + func populateResolvedRemotes(remotes RemoteSet, resolved []string) { for _, l := range resolved { parts := strings.SplitN(l, " ", 2) diff --git a/git/client_test.go b/git/client_test.go index c547d0335..3136363c1 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -725,31 +725,160 @@ func TestClientCommitBody(t *testing.T) { } func TestClientReadBranchConfig(t *testing.T) { + type cmdTest struct { + exitStatus int + stdOut string + stdErr string + wantArgs string + } tests := []struct { name string - cmdExitStatus []int - cmdStdout []string - cmdStderr []string - wantCmdArgs []string + cmds []cmdTest wantBranchConfig BranchConfig }{ { - name: "read branch config", - cmdExitStatus: []int{0, 0}, - cmdStdout: []string{"branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.gh-merge-base trunk", "origin/trunk"}, - cmdStderr: []string{"", ""}, - wantCmdArgs: []string{`path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|gh-merge-base)$`, `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`}, - wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/trunk", MergeBase: "trunk", Push: "origin/trunk"}, + name: "read branch config, central", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.gh-merge-base trunk", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + stdOut: "origin/trunk", + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/trunk", MergeBase: "trunk", PushRemoteName: "origin", Push: "origin/trunk"}, + }, + { + name: "read branch config, central, push.default = upstream", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk-remote", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + stdOut: "origin/trunk-remote", + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/trunk-remote", PushRemoteName: "origin", Push: "origin/trunk-remote"}, + }, + { + name: "read branch config, central, push.default = current", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/main", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + stdOut: "origin/trunk", + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "origin", MergeRef: "refs/heads/main", PushRemoteName: "origin", Push: "origin/trunk"}, + }, + { + name: "read branch config, central, push.default = current, target branch not pushed, no existing remote branch", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote .\nbranch.trunk.merge refs/heads/trunk-middle", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + stdOut: "origin", + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + exitStatus: 1, + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{MergeRef: "refs/heads/trunk-middle", PushRemoteName: "origin"}, + }, + { + name: "read branch config, triangular, push.default = current, has existing remote branch, branch.trunk.pushremote effective", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main\nbranch.trunk.pushremote origin", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + stdOut: "origin/trunk", + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin", Push: "origin/trunk"}, + }, + { + name: "read branch config, triangular, push.default = current, has existing remote branch, remote.pushDefault effective", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + stdOut: "origin", + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + stdOut: "origin/trunk", + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin", Push: "origin/trunk"}, + }, + { + name: "read branch config, triangular, push.default = current, no existing remote branch, branch.trunk.pushremote effective", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main\nbranch.trunk.pushremote origin", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + exitStatus: 1, + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin"}, + }, + { + name: "read branch config, triangular, push.default = current, no existing remote branch, remote.pushDefault effective", + cmds: []cmdTest{ + { + stdOut: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main", + wantArgs: `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`, + }, + { + stdOut: "origin", + wantArgs: `path/to/git config remote.pushDefault`, + }, + { + exitStatus: 1, + wantArgs: `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`, + }, + }, + wantBranchConfig: BranchConfig{RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var cmds []*exec.Cmd var cmdCtxs []commandCtx - for i := 0; i < len(tt.cmdExitStatus); i++ { - cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus[i], tt.cmdStdout[i], tt.cmdStderr[i]) + for _, c := range tt.cmds { + cmd, cmdCtx := createCommandContext(t, c.exitStatus, c.stdOut, c.stdErr) cmds = append(cmds, cmd) cmdCtxs = append(cmdCtxs, cmdCtx) + } i := -1 client := Client{ @@ -761,8 +890,8 @@ func TestClientReadBranchConfig(t *testing.T) { }, } branchConfig := client.ReadBranchConfig(context.Background(), "trunk") - for i := 0; i < len(tt.cmdExitStatus); i++ { - assert.Equal(t, tt.wantCmdArgs[i], strings.Join(cmds[i].Args[3:], " ")) + for i := 0; i < len(tt.cmds); i++ { + assert.Equal(t, tt.cmds[i].wantArgs, strings.Join(cmds[i].Args[3:], " ")) } assert.Equal(t, tt.wantBranchConfig, branchConfig) }) diff --git a/git/objects.go b/git/objects.go index 3b23d20b7..eff209be4 100644 --- a/git/objects.go +++ b/git/objects.go @@ -64,7 +64,9 @@ type BranchConfig struct { RemoteName string RemoteURL *url.URL // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. - MergeBase string - MergeRef string - Push string + MergeBase string + MergeRef string + PushRemoteURL *url.URL + PushRemoteName string + Push string } diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 2446b16c0..073dfb879 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -354,6 +354,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, @@ -385,6 +386,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", @@ -402,6 +404,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, expectedOutputs: []string{ @@ -436,6 +439,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { @@ -499,6 +503,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, expectedOutputs: []string{ @@ -539,6 +544,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { @@ -608,6 +614,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, expectedOut: heredoc.Doc(` @@ -656,6 +663,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -720,6 +728,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -767,6 +776,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -817,6 +827,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register("git remote rename origin upstream", 0, "") @@ -876,6 +887,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD @@ -914,6 +926,7 @@ func Test_createRun(t *testing.T) { branch.feature.remote origin branch.feature.merge refs/heads/my-feat2 `)) // determineTrackingBranch + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/my-feat2") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD @@ -955,6 +968,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "d3476a1\u0000commit 0\u0000\u0000\n7a6ea13\u0000commit 1\u0000\u0000") }, @@ -997,6 +1011,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { @@ -1087,6 +1102,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, wantErr: "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123", @@ -1105,6 +1121,7 @@ func Test_createRun(t *testing.T) { httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`)) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") @@ -1138,6 +1155,7 @@ func Test_createRun(t *testing.T) { mockRetrieveProjects(t, reg) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") @@ -1186,6 +1204,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature`, 0, "") cs.Register(`git rev-parse --show-toplevel`, 0, "") @@ -1246,6 +1265,7 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, @@ -1295,6 +1315,7 @@ func Test_createRun(t *testing.T) { { name: "web long URL", cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, @@ -1323,6 +1344,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { @@ -1344,6 +1366,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", @@ -1420,6 +1443,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", @@ -1457,6 +1481,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", @@ -1508,6 +1533,7 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, "") }, @@ -1560,7 +1586,7 @@ func Test_createRun(t *testing.T) { }, customBranchConfig: true, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config --get-regexp \^branch\\\.task1\\\.\(remote\|merge\|gh-merge-base\)\$`, 0, heredoc.Doc(` + cs.Register(`git config --get-regexp \^branch\\\.task1\\\.\(remote\|merge\|pushremote\|gh-merge-base\)\$`, 0, heredoc.Doc(` branch.task1.remote origin branch.task1.merge refs/heads/task1 branch.task1.gh-merge-base feature/feat2`)) // ReadBranchConfig @@ -1568,6 +1594,7 @@ func Test_createRun(t *testing.T) { deadbeef HEAD deadb00f refs/remotes/upstream/feature/feat2 deadbeef refs/remotes/origin/task1`)) // determineTrackingBranch + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref task1@\{push\}`, 1, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", @@ -1595,7 +1622,7 @@ func Test_createRun(t *testing.T) { defer cmdTeardown(t) cs.Register(`git status --porcelain`, 0, "") if !tt.customBranchConfig { - cs.Register(`git config --get-regexp \^branch\\\..+\\\.\(remote\|merge\|gh-merge-base\)\$`, 0, "") + cs.Register(`git config --get-regexp \^branch\\\..+\\\.\(remote\|merge\|pushremote\|gh-merge-base\)\$`, 0, "") } if tt.cmdStubs != nil { @@ -1679,6 +1706,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "empty", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD`, 0, "abc HEAD") }, @@ -1689,6 +1717,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "no match", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature", 0, "abc HEAD\nbca refs/remotes/upstream/feature") }, @@ -1709,6 +1738,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { name: "match", cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/feature") cs.Register(`git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD @@ -1739,6 +1769,7 @@ func Test_tryDetermineTrackingRef(t *testing.T) { branch.feature.remote origin branch.feature.merge refs/heads/great-feat `)) + cs.Register(`git config remote.pushDefault`, 1, "") cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/great-feat") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/great-feat refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 6e36b4dd6..f2c8f9f92 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -251,22 +251,21 @@ func (f *finder) parseCurrentBranch() (string, int, error) { } var gitRemoteRepo ghrepo.Interface - if branchConfig.RemoteURL != nil { + if branchConfig.PushRemoteURL != nil { // the branch merges from a remote specified by URL if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil { gitRemoteRepo = r } - } else if branchConfig.RemoteName != "" { - // the branch merges from a remote specified by name + } else if branchConfig.PushRemoteName != "" { rem, _ := f.remotesFn() - if r, err := rem.FindByName(branchConfig.RemoteName); err == nil { + if r, err := rem.FindByName(branchConfig.PushRemoteName); err == nil { gitRemoteRepo = r } } if gitRemoteRepo != nil { if branchConfig.Push != "" { - prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") + prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/") } else if pushDefault, _ := f.pushDefault(); (pushDefault == "upstream" || pushDefault == "tracking") && strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 2c34833c8..7c9f1ab57 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -328,6 +328,7 @@ func TestFind(t *testing.T) { branchConfig: func(branch string) (c git.BranchConfig) { c.MergeRef = "refs/heads/blue-upstream-berries" c.RemoteName = "origin" + c.PushRemoteName = "origin" c.Push = "origin/blue-upstream-berries" return }, @@ -373,6 +374,7 @@ func TestFind(t *testing.T) { u, _ := url.Parse("https://github.com/UPSTREAMOWNER/REPO") c.MergeRef = "refs/heads/blue-upstream-berries" c.RemoteURL = u + c.PushRemoteURL = u return }, pushDefault: func() (string, error) { return "upstream", nil }, @@ -411,6 +413,7 @@ func TestFind(t *testing.T) { branchConfig: func(branch string) (c git.BranchConfig) { c.MergeRef = "refs/heads/blue-upstream-berries" c.RemoteName = "origin" + c.PushRemoteName = "origin" c.Push = "origin/blue-upstream-berries" return }, @@ -454,6 +457,7 @@ func TestFind(t *testing.T) { }, branchConfig: func(branch string) (c git.BranchConfig) { c.RemoteName = "origin" + c.PushRemoteName = "origin" c.Push = "origin/blueberries" return }, diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 1b6da8427..33fc096c8 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -201,16 +201,16 @@ func prSelectorForCurrentBranch(gitClient *git.Client, baseRepo ghrepo.Interface if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil { branchOwner = r.RepoOwner() } - } else if branchConfig.RemoteName != "" { + } else if branchConfig.PushRemoteName != "" { // the branch merges from a remote specified by name - if r, err := rem.FindByName(branchConfig.RemoteName); err == nil { + if r, err := rem.FindByName(branchConfig.PushRemoteName); err == nil { branchOwner = r.RepoOwner() } } if branchOwner != "" { if branchConfig.Push != "" { - selector = strings.TrimPrefix(branchConfig.Push, branchConfig.RemoteName+"/") + selector = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/") } else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); (pushDefault == "upstream" || pushDefault == "tracking") && strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index d09abf5b9..e60764c4f 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -383,6 +383,7 @@ func Test_prSelectorForCurrentBranchPushDefaultUpstream(t *testing.T) { branch.Frederick888/main.remote git@github.com:Frederick888/playground.git branch.Frederick888/main.merge refs/heads/main `)) + rs.Register(`git config remote.pushDefault`, 1, "") rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "") rs.Register(`git config push\.default`, 0, "upstream") @@ -414,6 +415,7 @@ func Test_prSelectorForCurrentBranchPushDefaultTracking(t *testing.T) { branch.Frederick888/main.remote git@github.com:Frederick888/playground.git branch.Frederick888/main.merge refs/heads/main `)) + rs.Register(`git config remote.pushDefault`, 1, "") rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "") rs.Register(`git config push\.default`, 0, "tracking") From 2d1e4d625bc2a31c61c70452388188b327032b3c Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 16 Dec 2024 12:02:09 -0500 Subject: [PATCH 025/100] Add base gh pr view acceptance tests for changes These are acceptance tests based on @williammartin work within various issues associated with #9208. These need to be enhanced to account for `gh pr status`, however that should be quicker given these pass based on the updated branch and upstream fix for cross-repo same org changes. --- .../pr-view-respects-branch-pushremote.txtar | 42 +++++++++++++++++++ .../pr-view-respects-push-destination.txtar | 33 +++++++++++++++ .../pr-view-respects-remote-pushdefault.txtar | 42 +++++++++++++++++++ .../pr-view-respects-simple-pushdefault.txtar | 30 +++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar create mode 100644 acceptance/testdata/pr/pr-view-respects-push-destination.txtar create mode 100644 acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar create mode 100644 acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar diff --git a/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar b/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar new file mode 100644 index 000000000..3e79e755d --- /dev/null +++ b/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar @@ -0,0 +1,42 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env FORK=${REPO}-fork + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository to act as upstream with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup of upstream +defer gh repo delete --yes ${ORG}/${REPO} +exec gh repo view ${ORG}/${REPO} --json id --jq '.id' +stdout2env REPO_ID + +# Create a user fork of repository as opposed to private organization fork +exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} + +# Defer repo cleanup of fork +defer gh repo delete --yes ${ORG}/${FORK} +sleep 5 +exec gh repo view ${ORG}/${FORK} --json id --jq '.id' +stdout2env FORK_ID + +# Clone the repo +exec gh repo clone ${ORG}/${FORK} +cd ${FORK} + +# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork +exec git checkout -b feature-branch upstream/main +exec git config branch.feature-branch.pushRemote origin +exec git config --list +exec git commit --allow-empty -m 'Empty Commit' +exec git push + +# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks +exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' + +# View the PR +env GH_DEBUG=api +exec gh pr view +stdout 'Feature Title' diff --git a/acceptance/testdata/pr/pr-view-respects-push-destination.txtar b/acceptance/testdata/pr/pr-view-respects-push-destination.txtar new file mode 100644 index 000000000..6772d6cfd --- /dev/null +++ b/acceptance/testdata/pr/pr-view-respects-push-destination.txtar @@ -0,0 +1,33 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} +cd ${REPO} + +# Configure default push behavior so local and remote branches will be the same +exec git config push.default current + +# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name +exec git checkout -b test-feature +exec git branch --set-upstream-to origin/main +exec git rev-parse --abbrev-ref test-feature@{upstream} +stdout origin/main + +# Create the PR +exec git commit --allow-empty -m 'Empty Commit' +exec git push +exec gh pr create -B main -H test-feature --title 'Feature Title' --body 'Feature Body' + +# View the PR +exec gh pr view +stdout 'Feature Title' diff --git a/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar new file mode 100644 index 000000000..2a8a92826 --- /dev/null +++ b/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar @@ -0,0 +1,42 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env FORK=${REPO}-fork + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository to act as upstream with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup of upstream +defer gh repo delete --yes ${ORG}/${REPO} +exec gh repo view ${ORG}/${REPO} --json id --jq '.id' +stdout2env REPO_ID + +# Create a user fork of repository as opposed to private organization fork +exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} + +# Defer repo cleanup of fork +defer gh repo delete --yes ${ORG}/${FORK} +sleep 5 +exec gh repo view ${ORG}/${FORK} --json id --jq '.id' +stdout2env FORK_ID + +# Clone the repo +exec gh repo clone ${ORG}/${FORK} +cd ${FORK} + +# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork +exec git checkout -b feature-branch upstream/main +exec git config remote.pushDefault origin +exec git config --list +exec git commit --allow-empty -m 'Empty Commit' +exec git push + +# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks +exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' + +# View the PR +env GH_DEBUG=api +exec gh pr view +stdout 'Feature Title' diff --git a/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar new file mode 100644 index 000000000..55da849ac --- /dev/null +++ b/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar @@ -0,0 +1,30 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} +cd ${REPO} + +# Configure default push behavior so local and remote branches have to be the same +exec git config push.default simple + +# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name +exec git checkout -b test-feature origin/main + +# Create the PR +exec git commit --allow-empty -m 'Empty Commit' +exec git push origin test-feature +exec gh pr create -H test-feature --title 'Feature Title' --body 'Feature Body' + +# View the PR +exec gh pr view +stdout 'Feature Title' From eb16a75ab1a02b7a7f309c8de4c03528fe83e1eb Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 16 Dec 2024 14:24:59 -0500 Subject: [PATCH 026/100] Expand with gh pr status --- .../pr/pr-view-respects-branch-pushremote.txtar | 7 +++++-- .../pr/pr-view-respects-push-destination.txtar | 11 ++++++++--- .../pr/pr-view-respects-remote-pushdefault.txtar | 7 +++++-- .../pr/pr-view-respects-simple-pushdefault.txtar | 11 ++++++++--- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar b/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar index 3e79e755d..ef80cd8ba 100644 --- a/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar +++ b/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar @@ -29,7 +29,6 @@ cd ${FORK} # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork exec git checkout -b feature-branch upstream/main exec git config branch.feature-branch.pushRemote origin -exec git config --list exec git commit --allow-empty -m 'Empty Commit' exec git push @@ -37,6 +36,10 @@ exec git push exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' # View the PR -env GH_DEBUG=api exec gh pr view stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-push-destination.txtar b/acceptance/testdata/pr/pr-view-respects-push-destination.txtar index 6772d6cfd..ff9db4037 100644 --- a/acceptance/testdata/pr/pr-view-respects-push-destination.txtar +++ b/acceptance/testdata/pr/pr-view-respects-push-destination.txtar @@ -18,16 +18,21 @@ cd ${REPO} exec git config push.default current # Prepare a branch where changes are pulled from the default branch instead of remote branch of same name -exec git checkout -b test-feature +exec git checkout -b feature-branch exec git branch --set-upstream-to origin/main -exec git rev-parse --abbrev-ref test-feature@{upstream} +exec git rev-parse --abbrev-ref feature-branch@{upstream} stdout origin/main # Create the PR exec git commit --allow-empty -m 'Empty Commit' exec git push -exec gh pr create -B main -H test-feature --title 'Feature Title' --body 'Feature Body' +exec gh pr create -B main -H feature-branch --title 'Feature Title' --body 'Feature Body' # View the PR exec gh pr view stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar index 2a8a92826..8bfac2837 100644 --- a/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar +++ b/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar @@ -29,7 +29,6 @@ cd ${FORK} # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork exec git checkout -b feature-branch upstream/main exec git config remote.pushDefault origin -exec git config --list exec git commit --allow-empty -m 'Empty Commit' exec git push @@ -37,6 +36,10 @@ exec git push exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' # View the PR -env GH_DEBUG=api exec gh pr view stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar index 55da849ac..114f401ec 100644 --- a/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar +++ b/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar @@ -18,13 +18,18 @@ cd ${REPO} exec git config push.default simple # Prepare a branch where changes are pulled from the default branch instead of remote branch of same name -exec git checkout -b test-feature origin/main +exec git checkout -b feature-branch origin/main # Create the PR exec git commit --allow-empty -m 'Empty Commit' -exec git push origin test-feature -exec gh pr create -H test-feature --title 'Feature Title' --body 'Feature Body' +exec git push origin feature-branch +exec gh pr create -H feature-branch --title 'Feature Title' --body 'Feature Body' # View the PR exec gh pr view stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH From 8bb2879b87b9be2d104f6b3e6266d2058e117252 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 16 Dec 2024 14:26:13 -0500 Subject: [PATCH 027/100] Reflect coverage for view and status subcommands --- .../pr-view-respects-branch-pushremote.txtar | 45 ------------------- .../pr-view-respects-push-destination.txtar | 38 ---------------- .../pr-view-respects-remote-pushdefault.txtar | 45 ------------------- .../pr-view-respects-simple-pushdefault.txtar | 35 --------------- 4 files changed, 163 deletions(-) delete mode 100644 acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar delete mode 100644 acceptance/testdata/pr/pr-view-respects-push-destination.txtar delete mode 100644 acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar delete mode 100644 acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar diff --git a/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar b/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar deleted file mode 100644 index ef80cd8ba..000000000 --- a/acceptance/testdata/pr/pr-view-respects-branch-pushremote.txtar +++ /dev/null @@ -1,45 +0,0 @@ -# Setup environment variables used for testscript -env REPO=${SCRIPT_NAME}-${RANDOM_STRING} -env FORK=${REPO}-fork - -# Use gh as a credential helper -exec gh auth setup-git - -# Create a repository to act as upstream with a file so it has a default branch -exec gh repo create ${ORG}/${REPO} --add-readme --private - -# Defer repo cleanup of upstream -defer gh repo delete --yes ${ORG}/${REPO} -exec gh repo view ${ORG}/${REPO} --json id --jq '.id' -stdout2env REPO_ID - -# Create a user fork of repository as opposed to private organization fork -exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} - -# Defer repo cleanup of fork -defer gh repo delete --yes ${ORG}/${FORK} -sleep 5 -exec gh repo view ${ORG}/${FORK} --json id --jq '.id' -stdout2env FORK_ID - -# Clone the repo -exec gh repo clone ${ORG}/${FORK} -cd ${FORK} - -# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork -exec git checkout -b feature-branch upstream/main -exec git config branch.feature-branch.pushRemote origin -exec git commit --allow-empty -m 'Empty Commit' -exec git push - -# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks -exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' - -# View the PR -exec gh pr view -stdout 'Feature Title' - -# Check the PR status -env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] -exec gh pr status -stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-push-destination.txtar b/acceptance/testdata/pr/pr-view-respects-push-destination.txtar deleted file mode 100644 index ff9db4037..000000000 --- a/acceptance/testdata/pr/pr-view-respects-push-destination.txtar +++ /dev/null @@ -1,38 +0,0 @@ -# Setup environment variables used for testscript -env REPO=${SCRIPT_NAME}-${RANDOM_STRING} - -# Use gh as a credential helper -exec gh auth setup-git - -# Create a repository with a file so it has a default branch -exec gh repo create ${ORG}/${REPO} --add-readme --private - -# Defer repo cleanup -defer gh repo delete --yes ${ORG}/${REPO} - -# Clone the repo -exec gh repo clone ${ORG}/${REPO} -cd ${REPO} - -# Configure default push behavior so local and remote branches will be the same -exec git config push.default current - -# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name -exec git checkout -b feature-branch -exec git branch --set-upstream-to origin/main -exec git rev-parse --abbrev-ref feature-branch@{upstream} -stdout origin/main - -# Create the PR -exec git commit --allow-empty -m 'Empty Commit' -exec git push -exec gh pr create -B main -H feature-branch --title 'Feature Title' --body 'Feature Body' - -# View the PR -exec gh pr view -stdout 'Feature Title' - -# Check the PR status -env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] -exec gh pr status -stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar deleted file mode 100644 index 8bfac2837..000000000 --- a/acceptance/testdata/pr/pr-view-respects-remote-pushdefault.txtar +++ /dev/null @@ -1,45 +0,0 @@ -# Setup environment variables used for testscript -env REPO=${SCRIPT_NAME}-${RANDOM_STRING} -env FORK=${REPO}-fork - -# Use gh as a credential helper -exec gh auth setup-git - -# Create a repository to act as upstream with a file so it has a default branch -exec gh repo create ${ORG}/${REPO} --add-readme --private - -# Defer repo cleanup of upstream -defer gh repo delete --yes ${ORG}/${REPO} -exec gh repo view ${ORG}/${REPO} --json id --jq '.id' -stdout2env REPO_ID - -# Create a user fork of repository as opposed to private organization fork -exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} - -# Defer repo cleanup of fork -defer gh repo delete --yes ${ORG}/${FORK} -sleep 5 -exec gh repo view ${ORG}/${FORK} --json id --jq '.id' -stdout2env FORK_ID - -# Clone the repo -exec gh repo clone ${ORG}/${FORK} -cd ${FORK} - -# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork -exec git checkout -b feature-branch upstream/main -exec git config remote.pushDefault origin -exec git commit --allow-empty -m 'Empty Commit' -exec git push - -# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks -exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' - -# View the PR -exec gh pr view -stdout 'Feature Title' - -# Check the PR status -env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] -exec gh pr status -stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar b/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar deleted file mode 100644 index 114f401ec..000000000 --- a/acceptance/testdata/pr/pr-view-respects-simple-pushdefault.txtar +++ /dev/null @@ -1,35 +0,0 @@ -# Setup environment variables used for testscript -env REPO=${SCRIPT_NAME}-${RANDOM_STRING} - -# Use gh as a credential helper -exec gh auth setup-git - -# Create a repository with a file so it has a default branch -exec gh repo create ${ORG}/${REPO} --add-readme --private - -# Defer repo cleanup -defer gh repo delete --yes ${ORG}/${REPO} - -# Clone the repo -exec gh repo clone ${ORG}/${REPO} -cd ${REPO} - -# Configure default push behavior so local and remote branches have to be the same -exec git config push.default simple - -# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name -exec git checkout -b feature-branch origin/main - -# Create the PR -exec git commit --allow-empty -m 'Empty Commit' -exec git push origin feature-branch -exec gh pr create -H feature-branch --title 'Feature Title' --body 'Feature Body' - -# View the PR -exec gh pr view -stdout 'Feature Title' - -# Check the PR status -env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] -exec gh pr status -stdout $PR_STATUS_BRANCH From be250b3d332d42fe16bf2b420df2debfc2482a1c Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 16 Dec 2024 15:36:43 -0500 Subject: [PATCH 028/100] Add renamed acceptance tests --- ...ew-status-respects-branch-pushremote.txtar | 45 +++++++++++++++++++ ...iew-status-respects-push-destination.txtar | 38 ++++++++++++++++ ...w-status-respects-remote-pushdefault.txtar | 45 +++++++++++++++++++ ...w-status-respects-simple-pushdefault.txtar | 35 +++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 acceptance/testdata/pr/pr-view-status-respects-branch-pushremote.txtar create mode 100644 acceptance/testdata/pr/pr-view-status-respects-push-destination.txtar create mode 100644 acceptance/testdata/pr/pr-view-status-respects-remote-pushdefault.txtar create mode 100644 acceptance/testdata/pr/pr-view-status-respects-simple-pushdefault.txtar diff --git a/acceptance/testdata/pr/pr-view-status-respects-branch-pushremote.txtar b/acceptance/testdata/pr/pr-view-status-respects-branch-pushremote.txtar new file mode 100644 index 000000000..ef80cd8ba --- /dev/null +++ b/acceptance/testdata/pr/pr-view-status-respects-branch-pushremote.txtar @@ -0,0 +1,45 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env FORK=${REPO}-fork + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository to act as upstream with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup of upstream +defer gh repo delete --yes ${ORG}/${REPO} +exec gh repo view ${ORG}/${REPO} --json id --jq '.id' +stdout2env REPO_ID + +# Create a user fork of repository as opposed to private organization fork +exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} + +# Defer repo cleanup of fork +defer gh repo delete --yes ${ORG}/${FORK} +sleep 5 +exec gh repo view ${ORG}/${FORK} --json id --jq '.id' +stdout2env FORK_ID + +# Clone the repo +exec gh repo clone ${ORG}/${FORK} +cd ${FORK} + +# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork +exec git checkout -b feature-branch upstream/main +exec git config branch.feature-branch.pushRemote origin +exec git commit --allow-empty -m 'Empty Commit' +exec git push + +# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks +exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' + +# View the PR +exec gh pr view +stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-status-respects-push-destination.txtar b/acceptance/testdata/pr/pr-view-status-respects-push-destination.txtar new file mode 100644 index 000000000..ff9db4037 --- /dev/null +++ b/acceptance/testdata/pr/pr-view-status-respects-push-destination.txtar @@ -0,0 +1,38 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} +cd ${REPO} + +# Configure default push behavior so local and remote branches will be the same +exec git config push.default current + +# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name +exec git checkout -b feature-branch +exec git branch --set-upstream-to origin/main +exec git rev-parse --abbrev-ref feature-branch@{upstream} +stdout origin/main + +# Create the PR +exec git commit --allow-empty -m 'Empty Commit' +exec git push +exec gh pr create -B main -H feature-branch --title 'Feature Title' --body 'Feature Body' + +# View the PR +exec gh pr view +stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-status-respects-remote-pushdefault.txtar b/acceptance/testdata/pr/pr-view-status-respects-remote-pushdefault.txtar new file mode 100644 index 000000000..8bfac2837 --- /dev/null +++ b/acceptance/testdata/pr/pr-view-status-respects-remote-pushdefault.txtar @@ -0,0 +1,45 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env FORK=${REPO}-fork + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository to act as upstream with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup of upstream +defer gh repo delete --yes ${ORG}/${REPO} +exec gh repo view ${ORG}/${REPO} --json id --jq '.id' +stdout2env REPO_ID + +# Create a user fork of repository as opposed to private organization fork +exec gh repo fork ${ORG}/${REPO} --org ${ORG} --fork-name ${FORK} + +# Defer repo cleanup of fork +defer gh repo delete --yes ${ORG}/${FORK} +sleep 5 +exec gh repo view ${ORG}/${FORK} --json id --jq '.id' +stdout2env FORK_ID + +# Clone the repo +exec gh repo clone ${ORG}/${FORK} +cd ${FORK} + +# Prepare a branch where changes are pulled from the upstream default branch but pushed to fork +exec git checkout -b feature-branch upstream/main +exec git config remote.pushDefault origin +exec git commit --allow-empty -m 'Empty Commit' +exec git push + +# Create the PR spanning upstream and fork repositories, gh pr create does not support headRepositoryId needed for private forks +exec gh api graphql -F repositoryId="${REPO_ID}" -F headRepositoryId="${FORK_ID}" -F query='mutation CreatePullRequest($headRepositoryId: ID!, $repositoryId: ID!) { createPullRequest(input:{ baseRefName: "main", body: "Feature Body", draft: false, headRefName: "feature-branch", headRepositoryId: $headRepositoryId, repositoryId: $repositoryId, title:"Feature Title" }){ pullRequest{ id url } } }' + +# View the PR +exec gh pr view +stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [${ORG}:feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH diff --git a/acceptance/testdata/pr/pr-view-status-respects-simple-pushdefault.txtar b/acceptance/testdata/pr/pr-view-status-respects-simple-pushdefault.txtar new file mode 100644 index 000000000..114f401ec --- /dev/null +++ b/acceptance/testdata/pr/pr-view-status-respects-simple-pushdefault.txtar @@ -0,0 +1,35 @@ +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} +cd ${REPO} + +# Configure default push behavior so local and remote branches have to be the same +exec git config push.default simple + +# Prepare a branch where changes are pulled from the default branch instead of remote branch of same name +exec git checkout -b feature-branch origin/main + +# Create the PR +exec git commit --allow-empty -m 'Empty Commit' +exec git push origin feature-branch +exec gh pr create -H feature-branch --title 'Feature Title' --body 'Feature Body' + +# View the PR +exec gh pr view +stdout 'Feature Title' + +# Check the PR status +env PR_STATUS_BRANCH=#1 Feature Title [feature-branch] +exec gh pr status +stdout $PR_STATUS_BRANCH From 0006091d766ce3016ff3f2c84d3dc430bd2349bb Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Wed, 18 Dec 2024 12:26:43 +1100 Subject: [PATCH 029/100] Fix up intra-org fork test setup [1] https://github.com/cli/cli/commit/96ac8d6a2fb268a5825e155ae398e640d3e472fd --- pkg/cmd/pr/shared/finder_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 7c9f1ab57..ffa0615a9 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -456,6 +456,7 @@ func TestFind(t *testing.T) { return "blueberries", nil }, branchConfig: func(branch string) (c git.BranchConfig) { + c.MergeRef = "refs/heads/main" c.RemoteName = "origin" c.PushRemoteName = "origin" c.Push = "origin/blueberries" @@ -465,6 +466,9 @@ func TestFind(t *testing.T) { return context.Remotes{{ Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.New("OWNER", "REPO-FORK"), + }, { + Remote: &git.Remote{Name: "upstream"}, + Repo: ghrepo.New("OWNER", "REPO"), }}, nil }, }, From c7d04c980bd115e23f8c874a1c2b8d70bd15f872 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 08:34:15 -0700 Subject: [PATCH 030/100] update testing Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 59 +++++++++++-------- pkg/cmd/attestation/api/client_test.go | 40 ++++++++++++- .../attestation/api/mock_httpClient_test.go | 32 +++++++--- 3 files changed, 98 insertions(+), 33 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 37579b7bc..2d491e108 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -176,7 +176,7 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ return fmt.Errorf("attestation has no bundle or bundle URL") } - // for now, we fallback to the bundle field if the bundle URL is empty + // for now, we fall back to the bundle field if the bundle URL is empty if a.BundleURL == "" { c.logger.VerbosePrintf("Bundle URL is empty. Falling back to bundle field\n\n") fetched[i] = &Attestation{ @@ -207,35 +207,46 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ func (c *LiveClient) GetBundle(url string) (*bundle.Bundle, error) { c.logger.VerbosePrintf("Fetching attestation bundle with bundle URL\n\n") - resp, err := c.httpClient.Get(url) - if err != nil { - return nil, err - } + var sgBundle *bundle.Bundle + bo := backoff.NewConstantBackOff(getAttestationRetryInterval) + err := backoff.Retry(func() error { + resp, err := c.httpClient.Get(url) + if err != nil { + return backoff.Permanent(err) + } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) - } + if resp.StatusCode >= 500 && resp.StatusCode <= 599 { + return fmt.Errorf("attestation bundle with URL %s returned status code %d", url, resp.StatusCode) + } - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("failed to read blob storage response body: %w", err) - } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read blob storage response body: %w", err) + } - var out []byte - decompressed, err := snappy.Decode(out, body) - if err != nil { - return nil, fmt.Errorf("failed to decompress with snappy: %w", err) - } + var out []byte + decompressed, err := snappy.Decode(out, body) + if err != nil { + fmt.Errorf("failed to decompress with snappy: %w", err) + } - var pbBundle v1.Bundle - if err = protojson.Unmarshal(decompressed, &pbBundle); err != nil { - return nil, fmt.Errorf("failed to unmarshal to bundle: %w", err) - } + var pbBundle v1.Bundle + if err = protojson.Unmarshal(decompressed, &pbBundle); err != nil { + return fmt.Errorf("failed to unmarshal to bundle: %w", err) + } - c.logger.VerbosePrintf("Successfully fetched bundle\n\n") + c.logger.VerbosePrintf("Successfully fetched bundle\n\n") - return bundle.NewBundle(&pbBundle) + sgBundle, err = bundle.NewBundle(&pbBundle) + if err != nil { + return fmt.Errorf("failed to create new bundle: %w", err) + } + + return nil + }, backoff.WithMaxRetries(bo, 3)) + + return sgBundle, err } func shouldRetry(err error) bool { diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 65f8d59ca..96edbf061 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -212,7 +212,10 @@ func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { } func TestFetchBundleFromAttestations_Fail(t *testing.T) { - httpClient := &failAfterOneCallHttpClient{} + httpClient := &failAfterNCallsHttpClient{ + FailOnCallN: 2, + FailOnAllSubsequentCalls: true, + } c := &LiveClient{ httpClient: httpClient, @@ -225,7 +228,7 @@ func TestFetchBundleFromAttestations_Fail(t *testing.T) { fetched, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) require.Nil(t, fetched) - httpClient.AssertNumberOfCalls(t, "OnGetFailAfterOneCall", 2) + httpClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) } func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { @@ -244,6 +247,22 @@ func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) } +func TestFetchBundleFromAttestations_FetchByURL5XX_Resp(t *testing.T) { + mockHTTPClient := &failHttpClient{} + + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + + a := makeTestAttestation() + attestations := []*Attestation{&a} + bundle, err := c.fetchBundleFromAttestations(attestations) + require.Error(t, err) + require.Nil(t, bundle) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 3) +} + func TestFetchBundleByURL_FallbackToBundleField(t *testing.T) { mockHTTPClient := &mockHttpClient{} @@ -260,6 +279,23 @@ func TestFetchBundleByURL_FallbackToBundleField(t *testing.T) { mockHTTPClient.AssertNotCalled(t, "OnGetSuccess") } +func TestGetBundle(t *testing.T) { + mockHTTPClient := &failAfterNCallsHttpClient{ + FailOnCallN: 2, + FailOnAllSubsequentCalls: false, + } + + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + + b, err := c.GetBundle("whatever") + require.NoError(t, err) + require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 3) +} + func TestGetTrustDomain(t *testing.T) { fetcher := mockMetaGenerator{ TrustDomain: "foo", diff --git a/pkg/cmd/attestation/api/mock_httpClient_test.go b/pkg/cmd/attestation/api/mock_httpClient_test.go index 4dc34fbf8..074979c2a 100644 --- a/pkg/cmd/attestation/api/mock_httpClient_test.go +++ b/pkg/cmd/attestation/api/mock_httpClient_test.go @@ -40,21 +40,39 @@ func (m *failHttpClient) Get(url string) (*http.Response, error) { }, fmt.Errorf("failed to fetch with %s", url) } -type failAfterOneCallHttpClient struct { +type failHttpClient5XX struct { mock.Mock } -func (m *failAfterOneCallHttpClient) Get(url string) (*http.Response, error) { - m.On("OnGetFailAfterOneCall").Return() +func (m *failHttpClient5XX) Get(url string) (*http.Response, error) { + m.On("OnGetFail").Return() + m.MethodCalled("OnGetFail") - if len(m.Calls) >= 1 { - m.MethodCalled("OnGetFailAfterOneCall") + return &http.Response{ + StatusCode: 500, + }, nil +} + +type failAfterNCallsHttpClient struct { + mock.Mock + FailOnCallN int + FailOnAllSubsequentCalls bool + NumCalls int +} + +func (m *failAfterNCallsHttpClient) Get(url string) (*http.Response, error) { + m.On("OnGetFailAfterNCalls").Return() + + m.NumCalls++ + + if m.NumCalls == m.FailOnCallN || (m.NumCalls > m.FailOnCallN && m.FailOnAllSubsequentCalls) { + m.MethodCalled("OnGetFailAfterNCalls") return &http.Response{ StatusCode: 500, - }, fmt.Errorf("failed to fetch with %s", url) + }, nil } - m.MethodCalled("OnGetFailAfterOneCall") + m.MethodCalled("OnGetFailAfterNCalls") var compressed []byte compressed = snappy.Encode(compressed, data.SigstoreBundleRaw) return &http.Response{ From 4d99ae920c6b056f9077f5e538dd7600738b1795 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 10:34:06 -0700 Subject: [PATCH 031/100] fix tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client_test.go | 60 ++++++++++++++++++-------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 96edbf061..9728b265c 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -211,27 +211,22 @@ func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { require.Nil(t, fetched, 2) } -func TestFetchBundleFromAttestations_Fail(t *testing.T) { - httpClient := &failAfterNCallsHttpClient{ - FailOnCallN: 2, - FailOnAllSubsequentCalls: true, - } +func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { + mockHTTPClient := &failHttpClient{} c := &LiveClient{ - httpClient: httpClient, + httpClient: mockHTTPClient, logger: io.NewTestHandler(), } - att1 := makeTestAttestation() - att2 := makeTestAttestation() - attestations := []*Attestation{&att1, &att2} - fetched, err := c.fetchBundleFromAttestations(attestations) + a := makeTestAttestation() + attestations := []*Attestation{&a} + bundle, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) - require.Nil(t, fetched) - httpClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) + require.Nil(t, bundle) } -func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { +func TestFetchBundleFromAttestations_FailWithGetErr(t *testing.T) { mockHTTPClient := &failHttpClient{} c := &LiveClient{ @@ -248,7 +243,10 @@ func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { } func TestFetchBundleFromAttestations_FetchByURL5XX_Resp(t *testing.T) { - mockHTTPClient := &failHttpClient{} + mockHTTPClient := &failAfterNCallsHttpClient{ + FailOnCallN: 1, + FailOnAllSubsequentCalls: true, + } c := &LiveClient{ httpClient: mockHTTPClient, @@ -260,10 +258,9 @@ func TestFetchBundleFromAttestations_FetchByURL5XX_Resp(t *testing.T) { bundle, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) require.Nil(t, bundle) - mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 3) } -func TestFetchBundleByURL_FallbackToBundleField(t *testing.T) { +func TestFetchBundleFromAttestations_FallbackToBundleField(t *testing.T) { mockHTTPClient := &mockHttpClient{} c := &LiveClient{ @@ -280,8 +277,22 @@ func TestFetchBundleByURL_FallbackToBundleField(t *testing.T) { } func TestGetBundle(t *testing.T) { + mockHTTPClient := &mockHttpClient{} + + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + + b, err := c.GetBundle("whatever") + require.NoError(t, err) + require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetSuccess", 1) +} + +func TestGetBundle_Retry(t *testing.T) { mockHTTPClient := &failAfterNCallsHttpClient{ - FailOnCallN: 2, + FailOnCallN: 1, FailOnAllSubsequentCalls: false, } @@ -293,7 +304,20 @@ func TestGetBundle(t *testing.T) { b, err := c.GetBundle("whatever") require.NoError(t, err) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) - mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 3) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) +} + +func TestGetBundle_Fail(t *testing.T) { + mockHTTPClient := &failHttpClient{} + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + + b, err := c.GetBundle("whatever") + require.Error(t, err) + require.Nil(t, b) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) } func TestGetTrustDomain(t *testing.T) { From 40e7353b52a1da047c8ff984a79823239d04a670 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 11:02:33 -0700 Subject: [PATCH 032/100] deduplicate get attestation code Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 2d491e108..526916bcd 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -67,18 +67,9 @@ func (c *LiveClient) BuildRepoAndDigestURL(repo, digest string) string { // GetByRepoAndDigest fetches the attestation by repo and digest func (c *LiveClient) GetByRepoAndDigest(repo, digest string, limit int) ([]*Attestation, error) { + c.logger.VerbosePrintf("Fetching attestations for artifact digest %s\n\n", digest) url := c.BuildRepoAndDigestURL(repo, digest) - attestations, err := c.getAttestations(url, repo, digest, limit) - if err != nil { - return nil, err - } - - bundles, err := c.fetchBundleFromAttestations(attestations) - if err != nil { - return nil, fmt.Errorf("failed to fetch bundle with URL: %w", err) - } - - return bundles, nil + return c.getByURL(url, limit) } func (c *LiveClient) BuildOwnerAndDigestURL(owner, digest string) string { @@ -88,16 +79,17 @@ func (c *LiveClient) BuildOwnerAndDigestURL(owner, digest string) string { // GetByOwnerAndDigest fetches attestation by owner and digest func (c *LiveClient) GetByOwnerAndDigest(owner, digest string, limit int) ([]*Attestation, error) { + c.logger.VerbosePrintf("Fetching attestations for artifact digest %s\n\n", digest) url := c.BuildOwnerAndDigestURL(owner, digest) - attestations, err := c.getAttestations(url, owner, digest, limit) + return c.getByURL(url, limit) +} + +func (c *LiveClient) getByURL(url string, limit int) ([]*Attestation, error) { + attestations, err := c.getAttestations(url, limit) if err != nil { return nil, err } - if len(attestations) == 0 { - return nil, newErrNoAttestations(owner, digest) - } - bundles, err := c.fetchBundleFromAttestations(attestations) if err != nil { return nil, fmt.Errorf("failed to fetch bundle with URL: %w", err) @@ -112,9 +104,7 @@ func (c *LiveClient) GetTrustDomain() (string, error) { return c.getTrustDomain(MetaPath) } -func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*Attestation, error) { - c.logger.VerbosePrintf("Fetching attestations for artifact digest %s\n\n", digest) - +func (c *LiveClient) getAttestations(url string, limit int) ([]*Attestation, error) { perPage := limit if perPage <= 0 || perPage > maxLimitForFlag { return nil, fmt.Errorf("limit must be greater than 0 and less than or equal to %d", maxLimitForFlag) @@ -157,7 +147,7 @@ func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*At } if len(attestations) == 0 { - return nil, newErrNoAttestations(name, digest) + return nil, ErrNoAttestations{} } if len(attestations) > limit { From 5462582401fcc68b3e4a7b13004fec2227715704 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 11:05:17 -0700 Subject: [PATCH 033/100] drop unneeded methods Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 14 ++--------- pkg/cmd/attestation/api/client_test.go | 34 +++++++++++++------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 526916bcd..c3670f42d 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -60,27 +60,17 @@ func NewLiveClient(hc *http.Client, host string, l *ioconfig.Handler) *LiveClien } } -func (c *LiveClient) BuildRepoAndDigestURL(repo, digest string) string { - repo = strings.Trim(repo, "/") - return fmt.Sprintf(GetAttestationByRepoAndSubjectDigestPath, repo, digest) -} - // GetByRepoAndDigest fetches the attestation by repo and digest func (c *LiveClient) GetByRepoAndDigest(repo, digest string, limit int) ([]*Attestation, error) { c.logger.VerbosePrintf("Fetching attestations for artifact digest %s\n\n", digest) - url := c.BuildRepoAndDigestURL(repo, digest) + url := fmt.Sprintf(GetAttestationByRepoAndSubjectDigestPath, repo, digest) return c.getByURL(url, limit) } -func (c *LiveClient) BuildOwnerAndDigestURL(owner, digest string) string { - owner = strings.Trim(owner, "/") - return fmt.Sprintf(GetAttestationByOwnerAndSubjectDigestPath, owner, digest) -} - // GetByOwnerAndDigest fetches attestation by owner and digest func (c *LiveClient) GetByOwnerAndDigest(owner, digest string, limit int) ([]*Attestation, error) { c.logger.VerbosePrintf("Fetching attestations for artifact digest %s\n\n", digest) - url := c.BuildOwnerAndDigestURL(owner, digest) + url := fmt.Sprintf(GetAttestationByOwnerAndSubjectDigestPath, owner, digest) return c.getByURL(url, limit) } diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 9728b265c..b93d53b70 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -42,23 +42,23 @@ func NewClientWithMockGHClient(hasNextPage bool) Client { } } -func TestGetURL(t *testing.T) { - c := LiveClient{} - - testData := []struct { - repo string - digest string - expected string - }{ - {repo: "/github/example/", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, - {repo: "/github/example", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, - } - - for _, data := range testData { - s := c.BuildRepoAndDigestURL(data.repo, data.digest) - require.Equal(t, data.expected, s) - } -} +//func TestGetURL(t *testing.T) { +// c := LiveClient{} +// +// testData := []struct { +// repo string +// digest string +// expected string +// }{ +// {repo: "/github/example/", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, +// {repo: "/github/example", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, +// } +// +// for _, data := range testData { +// s := c.BuildRepoAndDigestURL(data.repo, data.digest) +// require.Equal(t, data.expected, s) +// } +//} func TestGetByDigest(t *testing.T) { c := NewClientWithMockGHClient(false) From fc0d0210c0cfcc37d5f45f8f3a92893690c5b587 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 12:21:27 -0700 Subject: [PATCH 034/100] remove old tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client_test.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index b93d53b70..77cd22ee6 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -42,24 +42,6 @@ func NewClientWithMockGHClient(hasNextPage bool) Client { } } -//func TestGetURL(t *testing.T) { -// c := LiveClient{} -// -// testData := []struct { -// repo string -// digest string -// expected string -// }{ -// {repo: "/github/example/", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, -// {repo: "/github/example", digest: "sha256:12313213", expected: "repos/github/example/attestations/sha256:12313213"}, -// } -// -// for _, data := range testData { -// s := c.BuildRepoAndDigestURL(data.repo, data.digest) -// require.Equal(t, data.expected, s) -// } -//} - func TestGetByDigest(t *testing.T) { c := NewClientWithMockGHClient(false) attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) From b7f6af03b59f9a697c879a0fda06e44fb134c07b Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 12:42:10 -0700 Subject: [PATCH 035/100] update no attestations found err Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/attestation.go | 16 ++-------------- pkg/cmd/attestation/api/client.go | 2 +- pkg/cmd/attestation/api/client_test.go | 4 ++-- pkg/cmd/attestation/api/mock_httpClient_test.go | 13 ------------- pkg/cmd/attestation/download/download.go | 2 +- pkg/cmd/attestation/download/download_test.go | 2 +- pkg/cmd/attestation/verify/verify.go | 2 +- 7 files changed, 8 insertions(+), 33 deletions(-) diff --git a/pkg/cmd/attestation/api/attestation.go b/pkg/cmd/attestation/api/attestation.go index 16b062a96..fd6b484a7 100644 --- a/pkg/cmd/attestation/api/attestation.go +++ b/pkg/cmd/attestation/api/attestation.go @@ -1,8 +1,7 @@ package api import ( - "fmt" - + "errors" "github.com/sigstore/sigstore-go/pkg/bundle" ) @@ -11,18 +10,7 @@ const ( GetAttestationByOwnerAndSubjectDigestPath = "orgs/%s/attestations/%s" ) -type ErrNoAttestations struct { - name string - digest string -} - -func (e ErrNoAttestations) Error() string { - return fmt.Sprintf("no attestations found for digest %s in %s", e.name, e.digest) -} - -func newErrNoAttestations(name, digest string) ErrNoAttestations { - return ErrNoAttestations{name, digest} -} +var ErrNoAttestationsFound = errors.New("no attestations found") type Attestation struct { Bundle *bundle.Bundle `json:"bundle"` diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index c3670f42d..faa1609fc 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -137,7 +137,7 @@ func (c *LiveClient) getAttestations(url string, limit int) ([]*Attestation, err } if len(attestations) == 0 { - return nil, ErrNoAttestations{} + return nil, ErrNoAttestationsFound } if len(attestations) > limit { diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 77cd22ee6..5a7c299c7 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -132,12 +132,12 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) { attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit) require.Error(t, err) - require.IsType(t, ErrNoAttestations{}, err) + require.IsType(t, ErrNoAttestationsFound, err) require.Nil(t, attestations) attestations, err = c.GetByOwnerAndDigest(testOwner, testDigest, DefaultLimit) require.Error(t, err) - require.IsType(t, ErrNoAttestations{}, err) + require.IsType(t, ErrNoAttestationsFound, err) require.Nil(t, attestations) } diff --git a/pkg/cmd/attestation/api/mock_httpClient_test.go b/pkg/cmd/attestation/api/mock_httpClient_test.go index 074979c2a..e11266a60 100644 --- a/pkg/cmd/attestation/api/mock_httpClient_test.go +++ b/pkg/cmd/attestation/api/mock_httpClient_test.go @@ -40,19 +40,6 @@ func (m *failHttpClient) Get(url string) (*http.Response, error) { }, fmt.Errorf("failed to fetch with %s", url) } -type failHttpClient5XX struct { - mock.Mock -} - -func (m *failHttpClient5XX) Get(url string) (*http.Response, error) { - m.On("OnGetFail").Return() - m.MethodCalled("OnGetFail") - - return &http.Response{ - StatusCode: 500, - }, nil -} - type failAfterNCallsHttpClient struct { mock.Mock FailOnCallN int diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index 7547e9e68..cdbdc0078 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -135,7 +135,7 @@ func runDownload(opts *Options) error { } attestations, err := verification.GetRemoteAttestations(opts.APIClient, params) if err != nil { - if errors.Is(err, api.ErrNoAttestations{}) { + if errors.Is(err, api.ErrNoAttestationsFound) { fmt.Fprintf(opts.Logger.IO.Out, "No attestations found for %s\n", opts.ArtifactPath) return nil } diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index 6c2986065..ddcd08c92 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -276,7 +276,7 @@ func TestRunDownload(t *testing.T) { opts := baseOpts opts.APIClient = api.MockClient{ OnGetByOwnerAndDigest: func(repo, digest string, limit int) ([]*api.Attestation, error) { - return nil, api.ErrNoAttestations{} + return nil, api.ErrNoAttestationsFound }, } diff --git a/pkg/cmd/attestation/verify/verify.go b/pkg/cmd/attestation/verify/verify.go index 016ec1fa8..a5e78a48e 100644 --- a/pkg/cmd/attestation/verify/verify.go +++ b/pkg/cmd/attestation/verify/verify.go @@ -220,7 +220,7 @@ func runVerify(opts *Options) error { attestations, logMsg, err := getAttestations(opts, *artifact) if err != nil { - if ok := errors.Is(err, api.ErrNoAttestations{}); ok { + if ok := errors.Is(err, api.ErrNoAttestationsFound); ok { opts.Logger.Printf(opts.Logger.ColorScheme.Red("✗ No attestations found for subject %s\n"), artifact.DigestWithAlg()) return err } From 611eb86e68d2bd2dc44cd801af11f2ea514ab8c1 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 12:47:25 -0700 Subject: [PATCH 036/100] method update Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 4 ++-- pkg/cmd/attestation/api/client_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index faa1609fc..f6991261c 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -166,7 +166,7 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ } // otherwise fetch the bundle with the provided URL - b, err := c.GetBundle(a.BundleURL) + b, err := c.getBundle(a.BundleURL) if err != nil { return fmt.Errorf("failed to fetch bundle with URL: %w", err) } @@ -184,7 +184,7 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ return fetched, nil } -func (c *LiveClient) GetBundle(url string) (*bundle.Bundle, error) { +func (c *LiveClient) getBundle(url string) (*bundle.Bundle, error) { c.logger.VerbosePrintf("Fetching attestation bundle with bundle URL\n\n") var sgBundle *bundle.Bundle diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 5a7c299c7..f89dcddf2 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -266,7 +266,7 @@ func TestGetBundle(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.GetBundle("whatever") + b, err := c.getBundle("whatever") require.NoError(t, err) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) mockHTTPClient.AssertNumberOfCalls(t, "OnGetSuccess", 1) @@ -283,7 +283,7 @@ func TestGetBundle_Retry(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.GetBundle("whatever") + b, err := c.getBundle("whatever") require.NoError(t, err) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) @@ -296,7 +296,7 @@ func TestGetBundle_Fail(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.GetBundle("whatever") + b, err := c.getBundle("whatever") require.Error(t, err) require.Nil(t, b) mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) From 1d807c22915282d5d576c6406828ef60ec5eb0db Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 13 Jan 2025 12:50:58 -0700 Subject: [PATCH 037/100] add missing return statement Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index f6991261c..359bdad54 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -208,7 +208,7 @@ func (c *LiveClient) getBundle(url string) (*bundle.Bundle, error) { var out []byte decompressed, err := snappy.Decode(out, body) if err != nil { - fmt.Errorf("failed to decompress with snappy: %w", err) + return fmt.Errorf("failed to decompress with snappy: %w", err) } var pbBundle v1.Bundle From 018438088e111df9cdc1a36ec11141de85a0bbe6 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Tue, 14 Jan 2025 16:02:41 -0800 Subject: [PATCH 038/100] Add missing git stubs to tests --- pkg/cmd/pr/status/status_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index 19a5a4519..97524acba 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -100,6 +100,8 @@ func TestPRStatus(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -130,6 +132,8 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -160,6 +164,8 @@ func TestPRStatus_reviewsAndChecksWithStatesByCount(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommandWithDetector(http, "blueberries", true, "", &fd.EnabledDetectorMock{}) if err != nil { @@ -189,6 +195,8 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -222,6 +230,8 @@ func TestPRStatus_currentBranch_defaultBranch(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -261,6 +271,8 @@ func TestPRStatus_currentBranch_Closed(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -283,6 +295,8 @@ func TestPRStatus_currentBranch_Closed_defaultBranch(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -305,6 +319,8 @@ func TestPRStatus_currentBranch_Merged(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -327,6 +343,8 @@ func TestPRStatus_currentBranch_Merged_defaultBranch(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -349,6 +367,8 @@ func TestPRStatus_blankSlate(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -407,6 +427,8 @@ func TestPRStatus_detachedHead(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") + rs.Register(`git config remote.pushDefault`, 0, "") + rs.Register(`git rev-parse --verify --quiet --abbrev-ref @{push}`, 0, "") output, err := runCommand(http, "", true, "") if err != nil { @@ -434,7 +456,8 @@ Requesting a code review from you func TestPRStatus_error_ReadBranchConfig(t *testing.T) { rs, cleanup := run.Stub() defer cleanup(t) - rs.Register(`git config --get-regexp \^branch\\.`, 1, "") + // We only need the one stub because this fails early + rs.Register(`git config --get-regexp \^branch\\.`, 2, "") _, err := runCommand(initFakeHTTP(), "blueberries", true, "") assert.Error(t, err) } From 4a9fd9508f71abf4dd81d61a3e6b38088a9f775c Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Wed, 15 Jan 2025 16:13:03 -0800 Subject: [PATCH 039/100] Add comments and a bit of code cleanup --- git/client.go | 34 +++++++---- git/client_test.go | 97 +++++++++++++------------------- git/objects.go | 12 ++-- pkg/cmd/pr/status/status.go | 3 +- pkg/cmd/pr/status/status_test.go | 8 +-- 5 files changed, 73 insertions(+), 81 deletions(-) diff --git a/git/client.go b/git/client.go index 82ead58a4..956d13671 100644 --- a/git/client.go +++ b/git/client.go @@ -402,23 +402,25 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon return BranchConfig{}, nil } - pushDefaultOut, err := c.Config(ctx, "remote.pushDefault") + // Check to see if there is a pushDefault ref set for the repo + remotePushDefaultOut, err := c.Config(ctx, "remote.pushDefault") if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 { return BranchConfig{}, err } - revParseOut, err := c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}") - if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 { - return BranchConfig{}, err - } + // Check to see if we can resolve the @{push} revision syntax. This is the easiest way to get + // the name of the push remote. + //We ignore errors resolving simple push.Default settings as these are handled downstream + revParseOut, _ := c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}") - return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(pushDefaultOut, "\n"), firstLine(revParseOut)), nil + return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(remotePushDefaultOut, "\n"), firstLine(revParseOut)), nil } -func parseBranchConfig(configLines []string, pushDefault string, revParse string) BranchConfig { +func parseBranchConfig(branchConfigLines []string, remotePushDefault string, revParse string) BranchConfig { var cfg BranchConfig - for _, line := range configLines { + // Read the config lines for the specific branch + for _, line := range branchConfigLines { parts := strings.SplitN(line, " ", 2) if len(parts) < 2 { continue @@ -429,6 +431,7 @@ func parseBranchConfig(configLines []string, pushDefault string, revParse string remoteURL, remoteName := parseRemoteURLOrName(parts[1]) cfg.RemoteURL = remoteURL cfg.RemoteName = remoteName + // pushremote usually indicates a "triangular" workflow case "pushremote": pushRemoteURL, pushRemoteName := parseRemoteURLOrName(parts[1]) cfg.PushRemoteURL = pushRemoteURL @@ -440,18 +443,27 @@ func parseBranchConfig(configLines []string, pushDefault string, revParse string } } + // PushRemote{URL|Name} takes precedence over remotePushDefault, so we'll only + // use remotePushDefault if we don't have a push remote. if cfg.PushRemoteURL == nil && cfg.PushRemoteName == "" { - if pushDefault != "" { - pushRemoteURL, pushRemoteName := parseRemoteURLOrName(pushDefault) + // remotePushDefault usually indicates a "triangular" workflow + if remotePushDefault != "" { + pushRemoteURL, pushRemoteName := parseRemoteURLOrName(remotePushDefault) cfg.PushRemoteURL = pushRemoteURL cfg.PushRemoteName = pushRemoteName } else { + // Without a PushRemote{URL|Name} or a remotePushDefault, we assume that the + // push remote ref is the same as the remote ref. This is likely + // a "centralized" workflow. cfg.PushRemoteName = cfg.RemoteName } } + // The value returned by revParse is the easiest way to get the name of the push ref cfg.Push = revParse - cfg.PushDefaultName = pushDefault + + // Some `gh pr` workflows don't work if this is set to 'simple' or 'current' + cfg.RemotePushDefault = remotePushDefault return cfg } diff --git a/git/client_test.go b/git/client_test.go index 801d7c025..ba13c889a 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -787,7 +787,7 @@ func TestClientReadBranchConfig(t *testing.T) { Stdout: "branch.trunk.remote origin\n", }, `path/to/git config remote.pushDefault`: { - Stdout: "pushdefault", + Stdout: "remotePushDefault", }, `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { Stdout: "origin/trunk", @@ -795,10 +795,10 @@ func TestClientReadBranchConfig(t *testing.T) { }, branch: "trunk", wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "pushdefault", - Push: "origin/trunk", - PushDefaultName: "pushdefault", + RemoteName: "origin", + PushRemoteName: "remotePushDefault", + Push: "origin/trunk", + RemotePushDefault: "remotePushDefault", }, wantError: nil, }, @@ -809,7 +809,7 @@ func TestClientReadBranchConfig(t *testing.T) { Stdout: "branch.trunk.remote origin\n", }, `path/to/git config remote.pushDefault`: { - Stdout: "pushdefault", + Stdout: "remotePushDefault", }, `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { ExitStatus: 1, @@ -817,49 +817,28 @@ func TestClientReadBranchConfig(t *testing.T) { }, branch: "trunk", wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "pushdefault", - PushDefaultName: "pushdefault", + RemoteName: "origin", + PushRemoteName: "remotePushDefault", + RemotePushDefault: "remotePushDefault", }, wantError: nil, }, { - name: "when git reads the config, pushDefault is set, and rev-parse fails, it should return an empty BranchConfig and the error", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\n", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "pushdefault", - }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - ExitStatus: 2, - Stderr: "rev-parse error", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{}, - wantError: &GitError{ - ExitCode: 2, - Stderr: "rev-parse error", - }, - }, - { - name: "when git reads the config but pushdefault fails, it should return and empty BranchConfig and the error", + name: "when git reads the config but remotePushDefault fails, it should return and empty BranchConfig and the error", cmds: mockedCommands{ `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { Stdout: "branch.trunk.remote origin\n", }, `path/to/git config remote.pushDefault`: { ExitStatus: 2, - Stderr: "pushdefault error", + Stderr: "remotePushDefault error", }, }, branch: "trunk", wantBranchConfig: BranchConfig{}, wantError: &GitError{ ExitCode: 2, - Stderr: "pushdefault error", + Stderr: "remotePushDefault error", }, }, { @@ -963,11 +942,11 @@ func TestClientReadBranchConfig(t *testing.T) { }, branch: "trunk", wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - Push: "origin/trunk", - PushDefaultName: "origin", + RemoteName: "upstream", + MergeRef: "refs/heads/main", + PushRemoteName: "origin", + Push: "origin/trunk", + RemotePushDefault: "origin", }, }, { @@ -985,10 +964,10 @@ func TestClientReadBranchConfig(t *testing.T) { }, branch: "trunk", wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - PushDefaultName: "current", + RemoteName: "upstream", + MergeRef: "refs/heads/main", + PushRemoteName: "origin", + RemotePushDefault: "current", }, }, { @@ -1006,10 +985,10 @@ func TestClientReadBranchConfig(t *testing.T) { }, branch: "trunk", wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - PushDefaultName: "origin", + RemoteName: "upstream", + MergeRef: "refs/heads/main", + PushRemoteName: "origin", + RemotePushDefault: "origin", }, }, } @@ -1021,15 +1000,15 @@ func TestClientReadBranchConfig(t *testing.T) { commandContext: cmdCtx, } branchConfig, err := client.ReadBranchConfig(context.Background(), tt.branch) - assert.Equal(t, tt.wantBranchConfig, branchConfig) if tt.wantError != nil { var gitError *GitError require.ErrorAs(t, err, &gitError) assert.Equal(t, tt.wantError.ExitCode, gitError.ExitCode) assert.Equal(t, tt.wantError.Stderr, gitError.Stderr) } else { - assert.NoError(t, err) + require.NoError(t, err) } + assert.Equal(t, tt.wantBranchConfig, branchConfig) }) } } @@ -1082,10 +1061,10 @@ func Test_parseBranchConfig(t *testing.T) { { name: "push default specified", configLines: []string{}, - pushDefault: "pushdefault", + pushDefault: "remotePushDefault", wantBranchConfig: BranchConfig{ - PushRemoteName: "pushdefault", - PushDefaultName: "pushdefault", + PushRemoteName: "remotePushDefault", + RemotePushDefault: "remotePushDefault", }, }, { @@ -1128,15 +1107,15 @@ func Test_parseBranchConfig(t *testing.T) { "branch.trunk.gh-merge-base gh-merge-base", "branch.trunk.merge refs/heads/trunk", }, - pushDefault: "pushdefault", + pushDefault: "remotePushDefault", revParse: "origin/trunk", wantBranchConfig: BranchConfig{ - RemoteName: "remote", - PushRemoteName: "pushremote", - MergeBase: "gh-merge-base", - MergeRef: "refs/heads/trunk", - Push: "origin/trunk", - PushDefaultName: "pushdefault", + RemoteName: "remote", + PushRemoteName: "pushremote", + MergeBase: "gh-merge-base", + MergeRef: "refs/heads/trunk", + Push: "origin/trunk", + RemotePushDefault: "remotePushDefault", }, }, { @@ -1158,7 +1137,7 @@ func Test_parseBranchConfig(t *testing.T) { assert.Equalf(t, tt.wantBranchConfig.MergeBase, branchConfig.MergeBase, "unexpected MergeBase") assert.Equalf(t, tt.wantBranchConfig.PushRemoteName, branchConfig.PushRemoteName, "unexpected PushRemoteName") assert.Equalf(t, tt.wantBranchConfig.Push, branchConfig.Push, "unexpected Push") - assert.Equalf(t, tt.wantBranchConfig.PushDefaultName, branchConfig.PushDefaultName, "unexpected PushDefaultName") + assert.Equalf(t, tt.wantBranchConfig.RemotePushDefault, branchConfig.RemotePushDefault, "unexpected RemotePushDefault") if tt.wantBranchConfig.RemoteURL != nil { assert.Equalf(t, tt.wantBranchConfig.RemoteURL.String(), branchConfig.RemoteURL.String(), "unexpected RemoteURL") } diff --git a/git/objects.go b/git/objects.go index 8a9d086c6..ff4068173 100644 --- a/git/objects.go +++ b/git/objects.go @@ -64,10 +64,10 @@ type BranchConfig struct { RemoteName string RemoteURL *url.URL // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. - MergeBase string - MergeRef string - PushDefaultName string - PushRemoteURL *url.URL - PushRemoteName string - Push string + MergeBase string + MergeRef string + RemotePushDefault string + PushRemoteURL *url.URL + PushRemoteName string + Push string } diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 593f05e63..fe674ad6e 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -226,8 +226,9 @@ func prSelectorForCurrentBranch(branchConfig git.BranchConfig, baseRepo ghrepo.I if branchOwner != "" { selector := prHeadRef if branchConfig.Push != "" { + // If we have a resolved push revision ref, we defer to that selector = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/") - } else if (branchConfig.PushDefaultName == "upstream" || branchConfig.PushDefaultName == "tracking") && + } else if (branchConfig.RemotePushDefault == "upstream" || branchConfig.RemotePushDefault == "tracking") && strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") } diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index 97524acba..586e22183 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -666,8 +666,8 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { Host: "github.com", Path: "Frederick888/playground.git", }, - MergeRef: "refs/heads/main", - PushDefaultName: "upstream", + MergeRef: "refs/heads/main", + RemotePushDefault: "upstream", }, baseRepo: ghrepo.NewWithHost("octocat", "playground", "github.com"), prHeadRef: "Frederick888/main", @@ -690,8 +690,8 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { Host: "github.com", Path: "Frederick888/playground.git", }, - MergeRef: "refs/heads/main", - PushDefaultName: "tracking", + MergeRef: "refs/heads/main", + RemotePushDefault: "tracking", }, baseRepo: ghrepo.NewWithHost("octocat", "playground", "github.com"), prHeadRef: "Frederick888/main", From d289ddd6178aa177f9c564dfc9b299c642b50cff Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Wed, 15 Jan 2025 16:21:06 -0800 Subject: [PATCH 040/100] Use PushRemoteURL instead of RemoteURL in prSelectorForCurrentBranch --- git/client.go | 1 + pkg/cmd/pr/status/status.go | 5 ++--- pkg/cmd/pr/status/status_test.go | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/git/client.go b/git/client.go index 956d13671..5e47098c2 100644 --- a/git/client.go +++ b/git/client.go @@ -456,6 +456,7 @@ func parseBranchConfig(branchConfigLines []string, remotePushDefault string, rev // push remote ref is the same as the remote ref. This is likely // a "centralized" workflow. cfg.PushRemoteName = cfg.RemoteName + cfg.PushRemoteURL = cfg.RemoteURL } } diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index fe674ad6e..89926bfa2 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -199,11 +199,10 @@ func prSelectorForCurrentBranch(branchConfig git.BranchConfig, baseRepo ghrepo.I } return prNumber, prHeadRef, nil } - var branchOwner string - if branchConfig.RemoteURL != nil { + if branchConfig.PushRemoteURL != nil { // the branch merges from a remote specified by URL - r, err := ghrepo.FromURL(branchConfig.RemoteURL) + r, err := ghrepo.FromURL(branchConfig.PushRemoteURL) if err != nil { // TODO: We aren't returning the error because we discovered that it was shadowed // before refactoring to its current return pattern. Thus, we aren't confident diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index 586e22183..4e9ca19b0 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -494,7 +494,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { { name: "Branch merges from a remote specified by URL", branchConfig: git.BranchConfig{ - RemoteURL: &url.URL{ + PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", @@ -537,7 +537,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { { name: "Branch is a fork and merges from a remote specified by URL", branchConfig: git.BranchConfig{ - RemoteURL: &url.URL{ + PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", @@ -625,7 +625,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { { name: "Remote URL errors", branchConfig: git.BranchConfig{ - RemoteURL: &url.URL{ + PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", @@ -660,7 +660,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { { name: "Current branch pushes to default upstream", branchConfig: git.BranchConfig{ - RemoteURL: &url.URL{ + PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", @@ -684,7 +684,7 @@ func Test_prSelectorForCurrentBranch(t *testing.T) { { name: "Current branch pushes to default tracking", branchConfig: git.BranchConfig{ - RemoteURL: &url.URL{ + PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", From aef2642581348e56b6031087288c40aae268cc8b Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Thu, 16 Jan 2025 15:07:33 +1100 Subject: [PATCH 041/100] fixup! Add comments and a bit of code cleanup --- git/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/client.go b/git/client.go index 5e47098c2..9ef6c152e 100644 --- a/git/client.go +++ b/git/client.go @@ -410,7 +410,7 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon // Check to see if we can resolve the @{push} revision syntax. This is the easiest way to get // the name of the push remote. - //We ignore errors resolving simple push.Default settings as these are handled downstream + // We ignore errors resolving simple push.Default settings as these are handled downstream revParseOut, _ := c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}") return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(remotePushDefaultOut, "\n"), firstLine(revParseOut)), nil From 41729b004def8e1d827f763a0f236afc5fa448b4 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Wed, 22 Jan 2025 15:39:43 -0800 Subject: [PATCH 042/100] Refactor finder.Find and replace parseCurrentBranch with parsePRRefs I've been struggling horribly to reason through all of this code, and after much mental gymnastics I identified the culprit as the overloaded "branch" string returned by parseCurrentBranch. This value was either the name of the branch that the PR we're looking for is associated with, or that name prepended with the owner's name and a : if we're on a branch, so: PR branch: featureBranch branch == "featureBranch" If on Fork belonging to "ForkOwner" branch == "ForkOwner:featureBranch" Since this extra information was bundled up into this single string, it complicated the responsibilities of parseCurrentBranch's "branch" return value. Thus, I've teased out "branch" into the new PRRefs struct: type PRRefs struct{ BranchName string HeadRepo ghrepo.Interface BaseRepo ghrepo.Interface } This allows the new parsePRRefs function to move all the previous "branch" string's information into structured data, and allows for a new method on PRRefs, GetPRLabel(), to create the string that "branch" previously held to pass into its downstream consumer, namely findForBranch. This also allowed for better test coverage, directly connecting the PRRefs fields to the values contained in the git config. Overall, I am now confident that this is doing what its supposed to do with respect to my understanding of the various central and triangular git workflows we are addressing. --- git/objects.go | 6 +- pkg/cmd/pr/shared/finder.go | 199 ++++++---- pkg/cmd/pr/shared/finder_test.go | 621 +++++++++++++++++++++---------- 3 files changed, 540 insertions(+), 286 deletions(-) diff --git a/git/objects.go b/git/objects.go index ff4068173..c6176fb73 100644 --- a/git/objects.go +++ b/git/objects.go @@ -64,8 +64,10 @@ type BranchConfig struct { RemoteName string RemoteURL *url.URL // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. - MergeBase string - MergeRef string + MergeBase string + MergeRef string + // These are used to handle triangular workflows. They can be defined by either + // a remote.pushDefault or a branch..pushremote value set on the git config. RemotePushDefault string PushRemoteURL *url.URL PushRemoteName string diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index a707b034e..06a66e93d 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -41,9 +41,9 @@ type finder struct { branchConfig func(string) (git.BranchConfig, error) progress progressIndicator - repo ghrepo.Interface - prNumber int - branchName string + baseRefRepo ghrepo.Interface + prNumber int + branchName string } func NewFinder(factory *cmdutil.Factory) PRFinder { @@ -89,6 +89,22 @@ type FindOptions struct { States []string } +type PRRefs struct { + BranchName string + HeadRepo ghrepo.Interface + BaseRepo ghrepo.Interface +} + +// GetPRLabel returns the string that the GitHub API uses to identify the PR. This is +// either just the branch name or, if the PR is originating from a fork, the fork owner +// and the branch name, like :. +func (s *PRRefs) GetPRLabel() string { + if s.HeadRepo == s.BaseRepo { + return s.BranchName + } + return fmt.Sprintf("%s:%s", s.HeadRepo.RepoOwner(), s.BranchName) +} + func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, error) { if len(opts.Fields) == 0 { return nil, nil, errors.New("Find error: no fields specified") @@ -96,26 +112,18 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err if repo, prNumber, err := f.parseURL(opts.Selector); err == nil { f.prNumber = prNumber - f.repo = repo + f.baseRefRepo = repo } - if f.repo == nil { + if f.baseRefRepo == nil { repo, err := f.baseRepoFn() if err != nil { return nil, nil, err } - f.repo = repo + f.baseRefRepo = repo } - if opts.Selector == "" { - if branch, prNumber, err := f.parseCurrentBranch(); err != nil { - return nil, nil, err - } else if prNumber > 0 { - f.prNumber = prNumber - } else { - f.branchName = branch - } - } else if f.prNumber == 0 { + if f.prNumber == 0 && opts.Selector != "" { // If opts.Selector is a valid number then assume it is the // PR number unless opts.BaseBranch is specified. This is a // special case for PR create command which will always want @@ -127,8 +135,28 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err } else { f.branchName = opts.Selector } + } else { + currentBranchName, err := f.branchFn() + if err != nil { + return nil, nil, err + } + f.branchName = currentBranchName } + // Get the branch config for the current branchName + branchConfig, err := f.branchConfig(f.branchName) + if err != nil { + return nil, nil, err + } + + // Determine if the branch is configured to merge to a special PR ref + prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) + if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { + prNumber, _ := strconv.Atoi(m[1]) + f.prNumber = prNumber + } + + // Set up HTTP client httpClient, err := f.httpClient() if err != nil { return nil, nil, err @@ -147,7 +175,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err if fields.Contains("isInMergeQueue") || fields.Contains("isMergeQueueEnabled") { cachedClient := api.NewCachedHTTPClient(httpClient, time.Hour*24) - detector := fd.NewDetector(cachedClient, f.repo.RepoHost()) + detector := fd.NewDetector(cachedClient, f.baseRefRepo.RepoHost()) prFeatures, err := detector.PullRequestFeatures() if err != nil { return nil, nil, err @@ -168,36 +196,54 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err if f.prNumber > 0 { if numberFieldOnly { // avoid hitting the API if we already have all the information - return &api.PullRequest{Number: f.prNumber}, f.repo, nil + return &api.PullRequest{Number: f.prNumber}, f.baseRefRepo, nil + } + pr, err = findByNumber(httpClient, f.baseRefRepo, f.prNumber, fields.ToSlice()) + if err != nil { + return pr, f.baseRefRepo, err } - pr, err = findByNumber(httpClient, f.repo, f.prNumber, fields.ToSlice()) } else { - pr, err = findForBranch(httpClient, f.repo, opts.BaseBranch, f.branchName, opts.States, fields.ToSlice()) - } - if err != nil { - return pr, f.repo, err + rems, err := f.remotesFn() + if err != nil { + return nil, nil, err + } + + pushDefault, err := f.pushDefault() + if err != nil { + return nil, nil, err + } + + prRefs, err := parsePRRefs(f.branchName, branchConfig, pushDefault, f.baseRefRepo, rems) + if err != nil { + return nil, nil, err + } + + pr, err = findForBranch(httpClient, f.baseRefRepo, opts.BaseBranch, prRefs.GetPRLabel(), opts.States, fields.ToSlice()) + if err != nil { + return pr, f.baseRefRepo, err + } } g, _ := errgroup.WithContext(context.Background()) if fields.Contains("reviews") { g.Go(func() error { - return preloadPrReviews(httpClient, f.repo, pr) + return preloadPrReviews(httpClient, f.baseRefRepo, pr) }) } if fields.Contains("comments") { g.Go(func() error { - return preloadPrComments(httpClient, f.repo, pr) + return preloadPrComments(httpClient, f.baseRefRepo, pr) }) } if fields.Contains("statusCheckRollup") { g.Go(func() error { - return preloadPrChecks(httpClient, f.repo, pr) + return preloadPrChecks(httpClient, f.baseRefRepo, pr) }) } if getProjectItems { g.Go(func() error { apiClient := api.NewClientFromHTTP(httpClient) - err := api.ProjectsV2ItemsForPullRequest(apiClient, f.repo, pr) + err := api.ProjectsV2ItemsForPullRequest(apiClient, f.baseRefRepo, pr) if err != nil && !api.ProjectsV2IgnorableError(err) { return err } @@ -205,7 +251,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err }) } - return pr, f.repo, g.Wait() + return pr, f.baseRefRepo, g.Wait() } var pullURLRE = regexp.MustCompile(`^/([^/]+)/([^/]+)/pull/(\d+)`) @@ -234,61 +280,53 @@ func (f *finder) parseURL(prURL string) (ghrepo.Interface, int, error) { return repo, prNumber, nil } -var prHeadRE = regexp.MustCompile(`^refs/pull/(\d+)/head$`) - -func (f *finder) parseCurrentBranch() (string, int, error) { - prHeadRef, err := f.branchFn() - if err != nil { - return "", 0, err +func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, pushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { + prRefs := PRRefs{ + BaseRepo: baseRefRepo, } - branchConfig, err := f.branchConfig(prHeadRef) - if err != nil { - return "", 0, err - } - - // the branch is configured to merge a special PR head ref - if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { - prNumber, _ := strconv.Atoi(m[1]) - return "", prNumber, nil - } - - var gitRemoteRepo ghrepo.Interface - if branchConfig.PushRemoteURL != nil { - // the branch merges from a remote specified by URL - if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil { - gitRemoteRepo = r - } - } else if branchConfig.PushRemoteName != "" { - rem, _ := f.remotesFn() - if r, err := rem.FindByName(branchConfig.PushRemoteName); err == nil { - gitRemoteRepo = r + // If @{push} resolves, then we have all the information we need to determine the head repo + // and branch name. It is of the form /. + if branchConfig.Push != "" { + for _, r := range rems { + // Find the remote who's name matches the push prefix + if strings.HasPrefix(branchConfig.Push, r.Name+"/") { + prRefs.BranchName = strings.TrimPrefix(branchConfig.Push, r.Name+"/") + prRefs.HeadRepo = r.Repo + return prRefs, nil + } } } - if gitRemoteRepo != nil { - if branchConfig.Push != "" { - prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/") - } else if pushDefault, _ := f.pushDefault(); (pushDefault == "upstream" || pushDefault == "tracking") && - strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { - prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") + // To get the HeadRepo, we look to the git config. The PushRemote{Name | URL} comes from + // one of the following, in order of precedence: + // 1. branch..pushRemote + // 2. remote.pushDefault + // 3. branch..remote + if branchConfig.PushRemoteName != "" { + if r, err := rems.FindByName(branchConfig.PushRemoteName); err == nil { + prRefs.HeadRepo = r.Repo } - // prepend `OWNER:` if this branch is pushed to a fork - // This is determined by: - // - The repo having a different owner - // - The repo having the same owner but a different name (private org fork) - // I suspect that the implementation of the second case may be broken in the face - // of a repo rename, where the remote hasn't been updated locally. This is a - // frequent issue in commands that use SmartBaseRepoFunc. It's not any worse than not - // supporting this case at all though. - sameOwner := strings.EqualFold(gitRemoteRepo.RepoOwner(), f.repo.RepoOwner()) - sameOwnerDifferentRepoName := sameOwner && !strings.EqualFold(gitRemoteRepo.RepoName(), f.repo.RepoName()) - if !sameOwner || sameOwnerDifferentRepoName { - prHeadRef = fmt.Sprintf("%s:%s", gitRemoteRepo.RepoOwner(), prHeadRef) + } else if branchConfig.PushRemoteURL != nil { + if r, err := ghrepo.FromURL(branchConfig.PushRemoteURL); err == nil { + prRefs.HeadRepo = r } } - return prHeadRef, 0, nil + // We assume the PR's branch name is the same as whatever f.BranchFn() returned earlier. + // unless the user has specified push.default = upstream or tracking, then we use the + // branch name from the merge ref. + prRefs.BranchName = currentBranchName + if pushDefault == "upstream" || pushDefault == "tracking" { + prRefs.BranchName = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") + } + + // The PR merges from a branch in the same repo as the base branch (usually the default branch) + if prRefs.HeadRepo == nil { + prRefs.HeadRepo = baseRefRepo + } + + return prRefs, nil } func findByNumber(httpClient *http.Client, repo ghrepo.Interface, number int, fields []string) (*api.PullRequest, error) { @@ -321,7 +359,7 @@ func findByNumber(httpClient *http.Client, repo ghrepo.Interface, number int, fi return &resp.Repository.PullRequest, nil } -func findForBranch(httpClient *http.Client, repo ghrepo.Interface, baseBranch, headBranch string, stateFilters, fields []string) (*api.PullRequest, error) { +func findForBranch(httpClient *http.Client, repo ghrepo.Interface, baseBranch, headBranchWithOwnerIfFork string, stateFilters, fields []string) (*api.PullRequest, error) { type response struct { Repository struct { PullRequests struct { @@ -348,9 +386,9 @@ func findForBranch(httpClient *http.Client, repo ghrepo.Interface, baseBranch, h } }`, api.PullRequestGraphQL(fieldSet.ToSlice())) - branchWithoutOwner := headBranch - if idx := strings.Index(headBranch, ":"); idx >= 0 { - branchWithoutOwner = headBranch[idx+1:] + branchWithoutOwner := headBranchWithOwnerIfFork + if idx := strings.Index(headBranchWithOwnerIfFork, ":"); idx >= 0 { + branchWithoutOwner = headBranchWithOwnerIfFork[idx+1:] } variables := map[string]interface{}{ @@ -373,18 +411,17 @@ func findForBranch(httpClient *http.Client, repo ghrepo.Interface, baseBranch, h }) for _, pr := range prs { - headBranchMatches := pr.HeadLabel() == headBranch + headBranchMatches := pr.HeadLabel() == headBranchWithOwnerIfFork baseBranchEmptyOrMatches := baseBranch == "" || pr.BaseRefName == baseBranch // When the head is the default branch, it doesn't really make sense to show merged or closed PRs. // https://github.com/cli/cli/issues/4263 - isNotClosedOrMergedWhenHeadIsDefault := pr.State == "OPEN" || resp.Repository.DefaultBranchRef.Name != headBranch - + isNotClosedOrMergedWhenHeadIsDefault := pr.State == "OPEN" || resp.Repository.DefaultBranchRef.Name != headBranchWithOwnerIfFork if headBranchMatches && baseBranchEmptyOrMatches && isNotClosedOrMergedWhenHeadIsDefault { return &pr, nil } } - return nil, &NotFoundError{fmt.Errorf("no pull requests found for branch %q", headBranch)} + return nil, &NotFoundError{fmt.Errorf("no pull requests found for branch %q", headBranchWithOwnerIfFork)} } func preloadPrReviews(httpClient *http.Client, repo ghrepo.Interface, pr *api.PullRequest) error { diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 340f69810..7a0140088 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -18,20 +18,45 @@ type args struct { baseRepoFn func() (ghrepo.Interface, error) branchFn func() (string, error) branchConfig func(string) (git.BranchConfig, error) - remotesFn func() (context.Remotes, error) + pushDefault func() (string, error) + selector string + fields []string + baseBranch string } func TestFind(t *testing.T) { - type args struct { - baseRepoFn func() (ghrepo.Interface, error) - branchFn func() (string, error) - branchConfig func(string) (git.BranchConfig, error) - pushDefault func() (string, error) - remotesFn func() (context.Remotes, error) - selector string - fields []string - baseBranch string + // TODO: Abstract these out meaningfully for reuse in parsePRRefs tests + originOwnerUrl, err := url.Parse("https://github.com/ORIGINOWNER/REPO.git") + if err != nil { + t.Fatal(err) } + remoteOrigin := context.Remote{ + Remote: &git.Remote{ + Name: "origin", + FetchURL: originOwnerUrl, + }, + Repo: ghrepo.New("ORIGINOWNER", "REPO"), + } + remoteOther := context.Remote{ + Remote: &git.Remote{ + Name: "other", + FetchURL: originOwnerUrl, + }, + Repo: ghrepo.New("ORIGINOWNER", "OTHER-REPO"), + } + + upstreamOwnerUrl, err := url.Parse("https://github.com/UPSTREAMOWNER/REPO.git") + if err != nil { + t.Fatal(err) + } + remoteUpstream := context.Remote{ + Remote: &git.Remote{ + Name: "upstream", + FetchURL: upstreamOwnerUrl, + }, + Repo: ghrepo.New("UPSTREAMOWNER", "REPO"), + } + tests := []struct { name string args args @@ -43,11 +68,13 @@ func TestFind(t *testing.T) { { name: "number argument", args: args{ - selector: "13", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") + selector: "13", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -57,7 +84,7 @@ func TestFind(t *testing.T) { }}}`)) }, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "number argument with base branch", @@ -65,9 +92,14 @@ func TestFind(t *testing.T) { selector: "13", baseBranch: "main", fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{ + PushRemoteName: remoteOrigin.Remote.Name, + }, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -80,22 +112,25 @@ func TestFind(t *testing.T) { "baseRefName": "main", "headRefName": "13", "isCrossRepository": false, - "headRepositoryOwner": {"login":"OWNER"} + "headRepositoryOwner": {"login":"ORIGINOWNER"} } ]} }}}`)) }, wantPR: 123, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "baseRepo is error", args: args{ - selector: "13", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return nil, errors.New("baseRepoErr") + selector: "13", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(nil, errors.New("baseRepoErr")), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, wantErr: true, }, @@ -110,24 +145,30 @@ func TestFind(t *testing.T) { { name: "number only", args: args{ - selector: "13", - fields: []string{"number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") + selector: "13", + fields: []string{"number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: nil, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "number with hash argument", args: args{ - selector: "#13", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") + selector: "#13", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -137,14 +178,19 @@ func TestFind(t *testing.T) { }}}`)) }, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { - name: "URL argument", + name: "PR URL argument", args: args{ selector: "https://example.org/OWNER/REPO/pull/13/files", fields: []string{"id", "number"}, baseRepoFn: nil, + branchFn: func() (string, error) { + return "blueberries", nil + }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -157,13 +203,16 @@ func TestFind(t *testing.T) { wantRepo: "https://example.org/OWNER/REPO", }, { - name: "branch argument", + name: "when provided branch argument with an open and closed PR for that branch name, it returns the open PR", args: args{ - selector: "blueberries", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") + selector: "blueberries", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -176,7 +225,7 @@ func TestFind(t *testing.T) { "baseRefName": "main", "headRefName": "blueberries", "isCrossRepository": false, - "headRepositoryOwner": {"login":"OWNER"} + "headRepositoryOwner": {"login":"ORIGINOWNER"} }, { "number": 13, @@ -184,13 +233,13 @@ func TestFind(t *testing.T) { "baseRefName": "main", "headRefName": "blueberries", "isCrossRepository": false, - "headRepositoryOwner": {"login":"OWNER"} + "headRepositoryOwner": {"login":"ORIGINOWNER"} } ]} }}}`)) }, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "branch argument with base branch", @@ -201,6 +250,11 @@ func TestFind(t *testing.T) { baseRepoFn: func() (ghrepo.Interface, error) { return ghrepo.FromFullName("OWNER/REPO") }, + branchFn: func() (string, error) { + return "blueberries", nil + }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -240,17 +294,8 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{ - MergeRef: "refs/heads/blueberries", - RemoteName: "origin", - Push: "origin/blueberries", - }, nil), - remotesFn: func() (context.Remotes, error) { - return context.Remotes{{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.New("OWNER", "REPO"), - }}, nil - }, + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -283,6 +328,7 @@ func TestFind(t *testing.T) { return "blueberries", nil }, branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -320,114 +366,19 @@ func TestFind(t *testing.T) { wantErr: true, }, { - name: "current branch with upstream configuration", + name: "when the current branch is configured to push to and pull from 'upstream' and push.default = upstream but the repo push/pulls from 'origin', it finds the PR associated with the upstream repo and returns origin as the base repo", args: args{ - selector: "", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") - }, - branchFn: func() (string, error) { - return "blueberries", nil - }, - pushDefault: func() (string, error) { return "upstream", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{ - MergeRef: "refs/heads/blue-upstream-berries", - RemoteName: "origin", - PushRemoteName: "origin", - Push: "origin/blue-upstream-berries", - }, nil), - remotesFn: func() (context.Remotes, error) { - return context.Remotes{{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.New("UPSTREAMOWNER", "REPO"), - }}, nil - }, - }, - httpStub: func(r *httpmock.Registry) { - r.Register( - httpmock.GraphQL(`query PullRequestForBranch\b`), - httpmock.StringResponse(`{"data":{"repository":{ - "pullRequests":{"nodes":[ - { - "number": 13, - "state": "OPEN", - "baseRefName": "main", - "headRefName": "blue-upstream-berries", - "isCrossRepository": true, - "headRepositoryOwner": {"login":"UPSTREAMOWNER"} - } - ]} - }}}`)) - }, - wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", - }, - { - name: "current branch with upstream RemoteURL configuration", - args: args{ - selector: "", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") - }, - branchFn: func() (string, error) { - return "blueberries", nil - }, - branchConfig: func(branch string) (git.BranchConfig, error) { - u, _ := url.Parse("https://github.com/UPSTREAMOWNER/REPO") - return stubBranchConfig(git.BranchConfig{ - MergeRef: "refs/heads/blue-upstream-berries", - RemoteURL: u, - PushRemoteURL: u, - }, nil)(branch) - }, - pushDefault: func() (string, error) { return "upstream", nil }, - remotesFn: nil, - }, - httpStub: func(r *httpmock.Registry) { - r.Register( - httpmock.GraphQL(`query PullRequestForBranch\b`), - httpmock.StringResponse(`{"data":{"repository":{ - "pullRequests":{"nodes":[ - { - "number": 13, - "state": "OPEN", - "baseRefName": "main", - "headRefName": "blue-upstream-berries", - "isCrossRepository": true, - "headRepositoryOwner": {"login":"UPSTREAMOWNER"} - } - ]} - }}}`)) - }, - wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", - }, - { - name: "current branch with tracking (deprecated synonym of upstream) configuration", - args: args{ - selector: "", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") - }, + selector: "", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), branchFn: func() (string, error) { return "blueberries", nil }, branchConfig: stubBranchConfig(git.BranchConfig{ MergeRef: "refs/heads/blue-upstream-berries", - RemoteName: "origin", - PushRemoteName: "origin", - Push: "origin/blue-upstream-berries", + PushRemoteName: "upstream", }, nil), - pushDefault: func() (string, error) { return "tracking", nil }, - remotesFn: func() (context.Remotes, error) { - return context.Remotes{{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.New("UPSTREAMOWNER", "REPO"), - }}, nil - }, + pushDefault: stubPushDefault("upstream", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -446,34 +397,55 @@ func TestFind(t *testing.T) { }}}`)) }, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", + }, + { + name: "the current branch is configured to push to and pull from a URL (upstream, in this example) that is different from what the repo is configured to push to and pull from (origin, in this example) and push.default = upstream, it finds the PR associated with the upstream repo and returns origin as the base repo", + args: args{ + selector: "", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + branchFn: func() (string, error) { + return "blueberries", nil + }, + branchConfig: stubBranchConfig(git.BranchConfig{ + MergeRef: "refs/heads/blue-upstream-berries", + PushRemoteURL: remoteUpstream.Remote.FetchURL, + }, nil), + pushDefault: stubPushDefault("upstream", nil), + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequestForBranch\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequests":{"nodes":[ + { + "number": 13, + "state": "OPEN", + "baseRefName": "main", + "headRefName": "blue-upstream-berries", + "isCrossRepository": true, + "headRepositoryOwner": {"login":"UPSTREAMOWNER"} + } + ]} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "current branch with upstream and fork in same org", args: args{ - selector: "", - fields: []string{"id", "number"}, - baseRepoFn: func() (ghrepo.Interface, error) { - return ghrepo.FromFullName("OWNER/REPO") - }, + selector: "", + fields: []string{"id", "number"}, + baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), branchFn: func() (string, error) { return "blueberries", nil }, branchConfig: stubBranchConfig(git.BranchConfig{ - RemoteName: "origin", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - Push: "origin/blueberries", + Push: "other/blueberries", }, nil), - remotesFn: func() (context.Remotes, error) { - return context.Remotes{{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.New("OWNER", "REPO-FORK"), - }, { - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.New("OWNER", "REPO"), - }}, nil - }, + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -486,13 +458,13 @@ func TestFind(t *testing.T) { "baseRefName": "main", "headRefName": "blueberries", "isCrossRepository": true, - "headRepositoryOwner": {"login":"OWNER"} + "headRepositoryOwner": {"login":"ORIGINOWNER"} } ]} }}}`)) }, wantPR: 13, - wantRepo: "https://github.com/OWNER/REPO", + wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { name: "current branch made by pr checkout", @@ -533,6 +505,7 @@ func TestFind(t *testing.T) { branchConfig: stubBranchConfig(git.BranchConfig{ MergeRef: "refs/pull/13/head", }, nil), + pushDefault: stubPushDefault("simple", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -597,7 +570,11 @@ func TestFind(t *testing.T) { branchFn: tt.args.branchFn, branchConfig: tt.args.branchConfig, pushDefault: tt.args.pushDefault, - remotesFn: tt.args.remotesFn, + remotesFn: stubRemotes(context.Remotes{ + &remoteOrigin, + &remoteOther, + &remoteUpstream, + }, nil), } pr, repo, err := f.Find(FindOptions{ @@ -630,42 +607,262 @@ func TestFind(t *testing.T) { } } -func Test_parseCurrentBranch(t *testing.T) { +func Test_parsePRRefs(t *testing.T) { + originOwnerUrl, err := url.Parse("https://github.com/ORIGINOWNER/REPO.git") + if err != nil { + t.Fatal(err) + } + remoteOrigin := context.Remote{ + Remote: &git.Remote{ + Name: "origin", + FetchURL: originOwnerUrl, + }, + Repo: ghrepo.New("ORIGINOWNER", "REPO"), + } + remoteOther := context.Remote{ + Remote: &git.Remote{ + Name: "other", + FetchURL: originOwnerUrl, + }, + Repo: ghrepo.New("ORIGINOWNER", "REPO"), + } + + upstreamOwnerUrl, err := url.Parse("https://github.com/UPSTREAMOWNER/REPO.git") + if err != nil { + t.Fatal(err) + } + remoteUpstream := context.Remote{ + Remote: &git.Remote{ + Name: "upstream", + FetchURL: upstreamOwnerUrl, + }, + Repo: ghrepo.New("UPSTREAMOWNER", "REPO"), + } + tests := []struct { - name string - args args - wantSelector string - wantPR int - wantError error + name string + branchConfig git.BranchConfig + pushDefault string + currentBranchName string + baseRefRepo ghrepo.Interface + rems context.Remotes + wantPRRefs PRRefs + wantErr error }{ { - name: "failed branch config", - args: args{ - branchConfig: stubBranchConfig(git.BranchConfig{}, errors.New("branchConfigErr")), - branchFn: func() (string, error) { - return "blueberries", nil - }, + name: "When the branch is called 'blueberries' with an empty branch config, it returns the correct PRRefs", + branchConfig: git.BranchConfig{}, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteOrigin.Repo, }, - wantSelector: "", - wantPR: 0, - wantError: errors.New("branchConfigErr"), + wantErr: nil, + }, + { + name: "When the branch is called 'otherBranch' with an empty branch config, it returns the correct PRRefs", + branchConfig: git.BranchConfig{}, + currentBranchName: "otherBranch", + baseRefRepo: remoteOrigin.Repo, + wantPRRefs: PRRefs{ + BranchName: "otherBranch", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the branch name doesn't match the branch name in BranchConfig.Push, it returns the BranchConfig.Push branch name", + branchConfig: git.BranchConfig{ + Push: "origin/pushBranch", + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + }, + wantPRRefs: PRRefs{ + BranchName: "pushBranch", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the branch name doesn't match a different branch name in BranchConfig.Push, it returns the BranchConfig.Push branch name", + branchConfig: git.BranchConfig{ + Push: "origin/differentPushBranch", + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + }, + wantPRRefs: PRRefs{ + BranchName: "differentPushBranch", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the branch name doesn't match a different branch name in BranchConfig.Push and the remote isn't 'origin', it returns the BranchConfig.Push branch name", + branchConfig: git.BranchConfig{ + Push: "other/pushBranch", + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOther, + }, + wantPRRefs: PRRefs{ + BranchName: "pushBranch", + HeadRepo: remoteOther.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the push remote is the same as the baseRepo, it returns the baseRepo as the PRRefs HeadRepo", + branchConfig: git.BranchConfig{ + PushRemoteName: remoteOrigin.Remote.Name, + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the push remote is different from the baseRepo, it returns the push remote repo as the PRRefs HeadRepo", + branchConfig: git.BranchConfig{ + PushRemoteName: remoteOrigin.Remote.Name, + }, + currentBranchName: "blueberries", + baseRefRepo: remoteUpstream.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteUpstream.Repo, + }, + wantErr: nil, + }, + { + name: "When the push remote defined by a URL and the baseRepo is different from the push remote, it returns the push remote repo as the PRRefs HeadRepo", + branchConfig: git.BranchConfig{ + PushRemoteURL: remoteOrigin.Remote.FetchURL, + }, + currentBranchName: "blueberries", + baseRefRepo: remoteUpstream.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteOrigin.Repo, + BaseRepo: remoteUpstream.Repo, + }, + wantErr: nil, + }, + { + name: "When the push remote and merge ref are configured to a different repo and push.default = upstream, it should return the branch name from the other repo", + branchConfig: git.BranchConfig{ + PushRemoteName: remoteUpstream.Remote.Name, + MergeRef: "refs/heads/blue-upstream-berries", + }, + pushDefault: "upstream", + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blue-upstream-berries", + HeadRepo: remoteUpstream.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the push remote and merge ref are configured to a different repo and push.default = tracking, it should return the branch name from the other repo", + branchConfig: git.BranchConfig{ + PushRemoteName: remoteUpstream.Remote.Name, + MergeRef: "refs/heads/blue-upstream-berries", + }, + pushDefault: "tracking", + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blue-upstream-berries", + HeadRepo: remoteUpstream.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - f := finder{ - httpClient: func() (*http.Client, error) { - return &http.Client{}, nil - }, - baseRepoFn: tt.args.baseRepoFn, - branchFn: tt.args.branchFn, - branchConfig: tt.args.branchConfig, - remotesFn: tt.args.remotesFn, + prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.pushDefault, tt.baseRefRepo, tt.rems) + if tt.wantErr != nil { + require.Error(t, err) + assert.Equal(t, tt.wantErr, err) + } else { + require.NoError(t, err) } - selector, pr, err := f.parseCurrentBranch() - assert.Equal(t, tt.wantSelector, selector) - assert.Equal(t, tt.wantPR, pr) - assert.Equal(t, tt.wantError, err) + assert.Equal(t, tt.wantPRRefs, prRefs) + }) + } +} + +func TestPRRefs_GetPRLabel(t *testing.T) { + originRepo := ghrepo.New("ORIGINOWNER", "REPO") + upstreamRepo := ghrepo.New("UPSTREAMOWNER", "REPO") + tests := []struct { + name string + prRefs PRRefs + want string + }{ + { + name: "When the HeadRepo and BaseRepo match, it returns the branch name", + prRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: originRepo, + BaseRepo: originRepo, + }, + want: "blueberries", + }, + { + name: "When the HeadRepo and BaseRepo do not match, it returns the prepended HeadRepo owner to the branch name", + prRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: originRepo, + BaseRepo: upstreamRepo, + }, + want: "ORIGINOWNER:blueberries", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.prRefs.GetPRLabel()) }) } } @@ -675,3 +872,21 @@ func stubBranchConfig(branchConfig git.BranchConfig, err error) func(string) (gi return branchConfig, err } } + +func stubRemotes(remotes context.Remotes, err error) func() (context.Remotes, error) { + return func() (context.Remotes, error) { + return remotes, err + } +} + +func stubBaseRepoFn(baseRepo ghrepo.Interface, err error) func() (ghrepo.Interface, error) { + return func() (ghrepo.Interface, error) { + return baseRepo, err + } +} + +func stubPushDefault(pushDefault string, err error) func() (string, error) { + return func() (string, error) { + return pushDefault, err + } +} From a72bef9b42e29667d00f0f2032ee9f4336709d9b Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 24 Jan 2025 17:07:24 +0100 Subject: [PATCH 043/100] Error if push revision doesn't match a remote --- pkg/cmd/pr/shared/finder.go | 8 +++++++- pkg/cmd/pr/shared/finder_test.go | 35 +++++++++++++++----------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 06a66e93d..4aa58af96 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -99,7 +99,7 @@ type PRRefs struct { // either just the branch name or, if the PR is originating from a fork, the fork owner // and the branch name, like :. func (s *PRRefs) GetPRLabel() string { - if s.HeadRepo == s.BaseRepo { + if ghrepo.IsSame(s.HeadRepo, s.BaseRepo) { return s.BranchName } return fmt.Sprintf("%s:%s", s.HeadRepo.RepoOwner(), s.BranchName) @@ -296,6 +296,12 @@ func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, pushDe return prRefs, nil } } + + remoteNames := make([]string, len(rems)) + for i, r := range rems { + remoteNames[i] = r.Name + } + return PRRefs{}, fmt.Errorf("no remote for %q found in %q", branchConfig.Push, strings.Join(remoteNames, ", ")) } // To get the HeadRepo, we look to the git config. The PushRemote{Name | URL} comes from diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 7a0140088..fe01e6b6e 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -2,6 +2,7 @@ package shared import ( "errors" + "fmt" "net/http" "net/url" "testing" @@ -70,7 +71,7 @@ func TestFind(t *testing.T) { args: args{ selector: "13", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -92,7 +93,7 @@ func TestFind(t *testing.T) { selector: "13", baseBranch: "main", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -147,7 +148,7 @@ func TestFind(t *testing.T) { args: args{ selector: "13", fields: []string{"number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -163,7 +164,7 @@ func TestFind(t *testing.T) { args: args{ selector: "#13", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -207,7 +208,7 @@ func TestFind(t *testing.T) { args: args{ selector: "blueberries", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -370,7 +371,7 @@ func TestFind(t *testing.T) { args: args{ selector: "", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -404,7 +405,7 @@ func TestFind(t *testing.T) { args: args{ selector: "", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -438,7 +439,7 @@ func TestFind(t *testing.T) { args: args{ selector: "", fields: []string{"id", "number"}, - baseRepoFn: stubBaseRepoFn(remoteOrigin.Repo, nil), + baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil), branchFn: func() (string, error) { return "blueberries", nil }, @@ -691,21 +692,18 @@ func Test_parsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the branch name doesn't match a different branch name in BranchConfig.Push, it returns the BranchConfig.Push branch name", + name: "When the push revision doesn't match a remote, it returns an error", branchConfig: git.BranchConfig{ Push: "origin/differentPushBranch", }, currentBranchName: "blueberries", baseRefRepo: remoteOrigin.Repo, rems: context.Remotes{ - &remoteOrigin, + &remoteUpstream, + &remoteOther, }, - wantPRRefs: PRRefs{ - BranchName: "differentPushBranch", - HeadRepo: remoteOrigin.Repo, - BaseRepo: remoteOrigin.Repo, - }, - wantErr: nil, + wantPRRefs: PRRefs{}, + wantErr: fmt.Errorf("no remote for %q found in %q", "origin/differentPushBranch", "upstream, other"), }, { name: "When the branch name doesn't match a different branch name in BranchConfig.Push and the remote isn't 'origin', it returns the BranchConfig.Push branch name", @@ -823,12 +821,11 @@ func Test_parsePRRefs(t *testing.T) { t.Run(tt.name, func(t *testing.T) { prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.pushDefault, tt.baseRefRepo, tt.rems) if tt.wantErr != nil { - require.Error(t, err) - assert.Equal(t, tt.wantErr, err) + require.Equal(t, tt.wantErr, err) } else { require.NoError(t, err) } - assert.Equal(t, tt.wantPRRefs, prRefs) + require.Equal(t, tt.wantPRRefs, prRefs) }) } } From 6355ed7c08e1a6a76cb10b5b461750cdee034dcb Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 24 Jan 2025 17:25:38 +0100 Subject: [PATCH 044/100] WIP: push default defaults to simple --- pkg/cmd/pr/shared/finder.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 4aa58af96..57db40a85 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -59,7 +59,17 @@ func NewFinder(factory *cmdutil.Factory) PRFinder { remotesFn: factory.Remotes, httpClient: factory.HttpClient, pushDefault: func() (string, error) { - return factory.GitClient.Config(context.Background(), "push.default") + pushDefault, err := factory.GitClient.Config(context.Background(), "push.default") + if err == nil { + return pushDefault, nil + } + + var gitErr *git.GitError + if ok := errors.As(err, &gitErr); ok && gitErr.ExitCode == 1 { + return "simple", nil + } + + return "", err }, progress: factory.IOStreams, branchConfig: func(s string) (git.BranchConfig, error) { From 5df2b47d1ff607196f8951e04eaa61cbd6d12a17 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Fri, 24 Jan 2025 09:30:46 -0700 Subject: [PATCH 045/100] update tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index f89dcddf2..f023dd03a 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -283,7 +283,7 @@ func TestGetBundle_Retry(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.getBundle("whatever") + b, err := c.getBundle("mybundleurl") require.NoError(t, err) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) @@ -296,7 +296,7 @@ func TestGetBundle_Fail(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.getBundle("whatever") + b, err := c.getBundle("mybundleurl") require.Error(t, err) require.Nil(t, b) mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) From 5a8dd35ba7e99600b43ffa37fd8cace328ce9ab2 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 09:40:02 -0800 Subject: [PATCH 046/100] Add PushDefault method to git client --- git/client.go | 15 +++++++++ git/client_test.go | 62 +++++++++++++++++++++++++++++++++++++ pkg/cmd/pr/shared/finder.go | 12 +------ 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/git/client.go b/git/client.go index 9ef6c152e..c1951f07b 100644 --- a/git/client.go +++ b/git/client.go @@ -482,6 +482,21 @@ func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string return err } +// PushDefault returns the value of push.default in the config. If the value +// is not set, it returns "simple" (the default value). +func (c *Client) PushDefault(ctx context.Context) (string, error) { + pushDefault, err := c.Config(ctx, "push.default") + if err == nil { + return pushDefault, nil + } + + var gitError *GitError + if ok := errors.As(err, &gitError); ok && gitError.ExitCode == 1 { + return "simple", nil + } + return "", err +} + func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error { args := []string{"tag", "-d", tag} cmd, err := c.Command(ctx, args...) diff --git a/git/client_test.go b/git/client_test.go index ba13c889a..4e35879d9 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -1191,6 +1191,68 @@ func Test_parseRemoteURLOrName(t *testing.T) { } } +func TestClientPushDefault(t *testing.T) { + tests := []struct { + name string + commandResult commandResult + wantPushDefault string + wantError *GitError + }{ + { + name: "push default is not set", + commandResult: commandResult{ + ExitStatus: 1, + Stderr: "error: key does not contain a section: remote.pushDefault", + }, + wantPushDefault: "simple", + wantError: nil, + }, + { + name: "push default is set to current", + commandResult: commandResult{ + ExitStatus: 0, + Stdout: "current", + }, + wantPushDefault: "current", + wantError: nil, + }, + { + name: "push default errors", + commandResult: commandResult{ + ExitStatus: 128, + Stderr: "fatal: git error", + }, + wantPushDefault: "", + wantError: &GitError{ + ExitCode: 128, + Stderr: "fatal: git error", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmdCtx := createMockedCommandContext(t, mockedCommands{ + `path/to/git config push.default`: tt.commandResult, + }, + ) + client := Client{ + GitPath: "path/to/git", + commandContext: cmdCtx, + } + pushDefault, err := client.PushDefault(context.Background()) + if tt.wantError != nil { + var gitError *GitError + require.ErrorAs(t, err, &gitError) + assert.Equal(t, tt.wantError.ExitCode, gitError.ExitCode) + assert.Equal(t, tt.wantError.Stderr, gitError.Stderr) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.wantPushDefault, pushDefault) + }) + } +} + func TestClientDeleteLocalTag(t *testing.T) { tests := []struct { name string diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 57db40a85..ce98a9363 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -59,17 +59,7 @@ func NewFinder(factory *cmdutil.Factory) PRFinder { remotesFn: factory.Remotes, httpClient: factory.HttpClient, pushDefault: func() (string, error) { - pushDefault, err := factory.GitClient.Config(context.Background(), "push.default") - if err == nil { - return pushDefault, nil - } - - var gitErr *git.GitError - if ok := errors.As(err, &gitErr); ok && gitErr.ExitCode == 1 { - return "simple", nil - } - - return "", err + return factory.GitClient.PushDefault(context.Background()) }, progress: factory.IOStreams, branchConfig: func(s string) (git.BranchConfig, error) { From e4d8ed0e6091ef4cc5961d3ffe08a293a2a8420c Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 10:20:04 -0800 Subject: [PATCH 047/100] Remove @{push} from branch config --- git/client.go | 21 +++--- git/client_test.go | 109 +++++++++++++++-------------- git/objects.go | 3 +- pkg/cmd/pr/shared/finder.go | 33 +++++---- pkg/cmd/pr/shared/finder_test.go | 115 +++++++++++++++++-------------- 5 files changed, 151 insertions(+), 130 deletions(-) diff --git a/git/client.go b/git/client.go index c1951f07b..c188b27bb 100644 --- a/git/client.go +++ b/git/client.go @@ -408,15 +408,10 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon return BranchConfig{}, err } - // Check to see if we can resolve the @{push} revision syntax. This is the easiest way to get - // the name of the push remote. - // We ignore errors resolving simple push.Default settings as these are handled downstream - revParseOut, _ := c.revParse(ctx, "--verify", "--quiet", "--abbrev-ref", branch+"@{push}") - - return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(remotePushDefaultOut, "\n"), firstLine(revParseOut)), nil + return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(remotePushDefaultOut, "\n")), nil } -func parseBranchConfig(branchConfigLines []string, remotePushDefault string, revParse string) BranchConfig { +func parseBranchConfig(branchConfigLines []string, remotePushDefault string) BranchConfig { var cfg BranchConfig // Read the config lines for the specific branch @@ -460,9 +455,6 @@ func parseBranchConfig(branchConfigLines []string, remotePushDefault string, rev } } - // The value returned by revParse is the easiest way to get the name of the push ref - cfg.Push = revParse - // Some `gh pr` workflows don't work if this is set to 'simple' or 'current' cfg.RemotePushDefault = remotePushDefault @@ -497,6 +489,15 @@ func (c *Client) PushDefault(ctx context.Context) (string, error) { return "", err } +// ParsePushRevision gets the value of the @{push} revision syntax +// An error here doesn't necessarily mean something are broke, but may mean that the @{push} +// revision syntax couldn't be resolved, such as in non-centralized workflows with +// push.default = simple. Downstream consumers should consider how to handle this error. +func (c *Client) ParsePushRevision(ctx context.Context, branch string) (string, error) { + revParseOut, err := c.revParse(ctx, "--abbrev-ref", branch+"@{push}") + return firstLine(revParseOut), err +} + func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error { args := []string{"tag", "-d", tag} cmd, err := c.Command(ctx, args...) diff --git a/git/client_test.go b/git/client_test.go index 4e35879d9..5d8ee9d65 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -760,7 +760,7 @@ func TestClientReadBranchConfig(t *testing.T) { }, }, { - name: "when the git reads the config, pushDefault isn't set, and rev-parse succeeds, it should return the correct BranchConfig", + name: "when the git reads the config, pushDefault isn't set, it should return the correct BranchConfig", cmds: mockedCommands{ `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { Stdout: "branch.trunk.remote origin\n", @@ -768,20 +768,16 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { ExitStatus: 1, }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "origin", PushRemoteName: "origin", - Push: "origin/trunk", }, wantError: nil, }, { - name: "when the git reads the config, pushDefault is set, and rev-parse succeeds, it should return the correct BranchConfig", + name: "when the git reads the config, pushDefault is set, it should return the correct BranchConfig", cmds: mockedCommands{ `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { Stdout: "branch.trunk.remote origin\n", @@ -789,15 +785,11 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { Stdout: "remotePushDefault", }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "origin", PushRemoteName: "remotePushDefault", - Push: "origin/trunk", RemotePushDefault: "remotePushDefault", }, wantError: nil, @@ -811,9 +803,6 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { Stdout: "remotePushDefault", }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - ExitStatus: 1, - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ @@ -850,9 +839,6 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { ExitStatus: 1, }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ @@ -860,7 +846,6 @@ func TestClientReadBranchConfig(t *testing.T) { MergeRef: "refs/heads/trunk", MergeBase: "merge-base", PushRemoteName: "origin", - Push: "origin/trunk", }, wantError: nil, }, @@ -873,16 +858,12 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { ExitStatus: 1, }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk-remote", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "origin", MergeRef: "refs/heads/trunk-remote", PushRemoteName: "origin", - Push: "origin/trunk-remote", }, }, { @@ -894,16 +875,12 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { ExitStatus: 1, }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "origin", MergeRef: "refs/heads/main", PushRemoteName: "origin", - Push: "origin/trunk", }, }, { @@ -915,16 +892,12 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { ExitStatus: 1, }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin", - Push: "origin/trunk", }, }, { @@ -936,16 +909,12 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { Stdout: "origin", }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - Stdout: "origin/trunk", - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "upstream", MergeRef: "refs/heads/main", PushRemoteName: "origin", - Push: "origin/trunk", RemotePushDefault: "origin", }, }, @@ -958,9 +927,6 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { Stdout: "current", }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - ExitStatus: 1, - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ @@ -979,9 +945,6 @@ func TestClientReadBranchConfig(t *testing.T) { `path/to/git config remote.pushDefault`: { Stdout: "origin", }, - `path/to/git rev-parse --verify --quiet --abbrev-ref trunk@{push}`: { - ExitStatus: 1, - }, }, branch: "trunk", wantBranchConfig: BranchConfig{ @@ -1018,7 +981,6 @@ func Test_parseBranchConfig(t *testing.T) { name string configLines []string pushDefault string - revParse string wantBranchConfig BranchConfig }{ { @@ -1050,14 +1012,6 @@ func Test_parseBranchConfig(t *testing.T) { PushRemoteName: "pushremote", }, }, - { - name: "rev parse specified", - configLines: []string{}, - revParse: "origin/trunk", - wantBranchConfig: BranchConfig{ - Push: "origin/trunk", - }, - }, { name: "push default specified", configLines: []string{}, @@ -1108,13 +1062,11 @@ func Test_parseBranchConfig(t *testing.T) { "branch.trunk.merge refs/heads/trunk", }, pushDefault: "remotePushDefault", - revParse: "origin/trunk", wantBranchConfig: BranchConfig{ RemoteName: "remote", PushRemoteName: "pushremote", MergeBase: "gh-merge-base", MergeRef: "refs/heads/trunk", - Push: "origin/trunk", RemotePushDefault: "remotePushDefault", }, }, @@ -1131,12 +1083,11 @@ func Test_parseBranchConfig(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - branchConfig := parseBranchConfig(tt.configLines, tt.pushDefault, tt.revParse) + branchConfig := parseBranchConfig(tt.configLines, tt.pushDefault) assert.Equalf(t, tt.wantBranchConfig.RemoteName, branchConfig.RemoteName, "unexpected RemoteName") assert.Equalf(t, tt.wantBranchConfig.MergeRef, branchConfig.MergeRef, "unexpected MergeRef") assert.Equalf(t, tt.wantBranchConfig.MergeBase, branchConfig.MergeBase, "unexpected MergeBase") assert.Equalf(t, tt.wantBranchConfig.PushRemoteName, branchConfig.PushRemoteName, "unexpected PushRemoteName") - assert.Equalf(t, tt.wantBranchConfig.Push, branchConfig.Push, "unexpected Push") assert.Equalf(t, tt.wantBranchConfig.RemotePushDefault, branchConfig.RemotePushDefault, "unexpected RemotePushDefault") if tt.wantBranchConfig.RemoteURL != nil { assert.Equalf(t, tt.wantBranchConfig.RemoteURL.String(), branchConfig.RemoteURL.String(), "unexpected RemoteURL") @@ -1253,6 +1204,60 @@ func TestClientPushDefault(t *testing.T) { } } +func TestClientParsePushRevision(t *testing.T) { + tests := []struct { + name string + branch string + commandResult commandResult + wantParsedPushRevision string + wantError *GitError + }{ + { + name: "@{push} resolves to origin/branchName", + branch: "branchName", + commandResult: commandResult{ + ExitStatus: 0, + Stdout: "origin/branchName", + }, + wantParsedPushRevision: "origin/branchName", + }, + { + name: "@{push} doesn't resolve", + commandResult: commandResult{ + ExitStatus: 128, + Stderr: "fatal: git error", + }, + wantParsedPushRevision: "", + wantError: &GitError{ + ExitCode: 128, + Stderr: "fatal: git error", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := fmt.Sprintf("path/to/git rev-parse --abbrev-ref %s@{push}", tt.branch) + cmdCtx := createMockedCommandContext(t, mockedCommands{ + args(cmd): tt.commandResult, + }) + client := Client{ + GitPath: "path/to/git", + commandContext: cmdCtx, + } + pushDefault, err := client.ParsePushRevision(context.Background(), tt.branch) + if tt.wantError != nil { + var gitError *GitError + require.ErrorAs(t, err, &gitError) + assert.Equal(t, tt.wantError.ExitCode, gitError.ExitCode) + assert.Equal(t, tt.wantError.Stderr, gitError.Stderr) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.wantParsedPushRevision, pushDefault) + }) + } +} + func TestClientDeleteLocalTag(t *testing.T) { tests := []struct { name string diff --git a/git/objects.go b/git/objects.go index c6176fb73..eab7583ae 100644 --- a/git/objects.go +++ b/git/objects.go @@ -68,8 +68,7 @@ type BranchConfig struct { MergeRef string // These are used to handle triangular workflows. They can be defined by either // a remote.pushDefault or a branch..pushremote value set on the git config. - RemotePushDefault string PushRemoteURL *url.URL PushRemoteName string - Push string + RemotePushDefault string } diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index ce98a9363..d76a62140 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -33,13 +33,14 @@ type progressIndicator interface { } type finder struct { - baseRepoFn func() (ghrepo.Interface, error) - branchFn func() (string, error) - remotesFn func() (remotes.Remotes, error) - httpClient func() (*http.Client, error) - pushDefault func() (string, error) - branchConfig func(string) (git.BranchConfig, error) - progress progressIndicator + baseRepoFn func() (ghrepo.Interface, error) + branchFn func() (string, error) + remotesFn func() (remotes.Remotes, error) + httpClient func() (*http.Client, error) + pushDefault func() (string, error) + parsePushRevision func(string) (string, error) + branchConfig func(string) (git.BranchConfig, error) + progress progressIndicator baseRefRepo ghrepo.Interface prNumber int @@ -61,6 +62,9 @@ func NewFinder(factory *cmdutil.Factory) PRFinder { pushDefault: func() (string, error) { return factory.GitClient.PushDefault(context.Background()) }, + parsePushRevision: func(branch string) (string, error) { + return factory.GitClient.ParsePushRevision(context.Background(), branch) + }, progress: factory.IOStreams, branchConfig: func(s string) (git.BranchConfig, error) { return factory.GitClient.ReadBranchConfig(context.Background(), s) @@ -213,7 +217,10 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return nil, nil, err } - prRefs, err := parsePRRefs(f.branchName, branchConfig, pushDefault, f.baseRefRepo, rems) + // Suppressing the error as we have other means of computing the PRRefs if this fails. + parsedPushRevision, _ := f.parsePushRevision(f.branchName) + + prRefs, err := parsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, f.baseRefRepo, rems) if err != nil { return nil, nil, err } @@ -280,18 +287,18 @@ func (f *finder) parseURL(prURL string) (ghrepo.Interface, int, error) { return repo, prNumber, nil } -func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, pushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { +func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { prRefs := PRRefs{ BaseRepo: baseRefRepo, } // If @{push} resolves, then we have all the information we need to determine the head repo // and branch name. It is of the form /. - if branchConfig.Push != "" { + if parsedPushRevision != "" { for _, r := range rems { // Find the remote who's name matches the push prefix - if strings.HasPrefix(branchConfig.Push, r.Name+"/") { - prRefs.BranchName = strings.TrimPrefix(branchConfig.Push, r.Name+"/") + if strings.HasPrefix(parsedPushRevision, r.Name+"/") { + prRefs.BranchName = strings.TrimPrefix(parsedPushRevision, r.Name+"/") prRefs.HeadRepo = r.Repo return prRefs, nil } @@ -301,7 +308,7 @@ func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, pushDe for i, r := range rems { remoteNames[i] = r.Name } - return PRRefs{}, fmt.Errorf("no remote for %q found in %q", branchConfig.Push, strings.Join(remoteNames, ", ")) + return PRRefs{}, fmt.Errorf("no remote for %q found in %q", parsedPushRevision, strings.Join(remoteNames, ", ")) } // To get the HeadRepo, we look to the git config. The PushRemote{Name | URL} comes from diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index fe01e6b6e..db56ce775 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -16,13 +16,14 @@ import ( ) type args struct { - baseRepoFn func() (ghrepo.Interface, error) - branchFn func() (string, error) - branchConfig func(string) (git.BranchConfig, error) - pushDefault func() (string, error) - selector string - fields []string - baseBranch string + baseRepoFn func() (ghrepo.Interface, error) + branchFn func() (string, error) + branchConfig func(string) (git.BranchConfig, error) + pushDefault func() (string, error) + parsePushRevision func(string) (string, error) + selector string + fields []string + baseBranch string } func TestFind(t *testing.T) { @@ -100,7 +101,8 @@ func TestFind(t *testing.T) { branchConfig: stubBranchConfig(git.BranchConfig{ PushRemoteName: remoteOrigin.Remote.Name, }, nil), - pushDefault: stubPushDefault("simple", nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -212,8 +214,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -254,8 +257,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -295,8 +299,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -328,8 +333,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -379,7 +385,8 @@ func TestFind(t *testing.T) { MergeRef: "refs/heads/blue-upstream-berries", PushRemoteName: "upstream", }, nil), - pushDefault: stubPushDefault("upstream", nil), + pushDefault: stubPushDefault("upstream", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -413,7 +420,8 @@ func TestFind(t *testing.T) { MergeRef: "refs/heads/blue-upstream-berries", PushRemoteURL: remoteUpstream.Remote.FetchURL, }, nil), - pushDefault: stubPushDefault("upstream", nil), + pushDefault: stubPushDefault("upstream", nil), + parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -443,10 +451,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{ - Push: "other/blueberries", - }, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + parsePushRevision: stubParsedPushRevision("other/blueberries", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -567,10 +574,11 @@ func TestFind(t *testing.T) { httpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }, - baseRepoFn: tt.args.baseRepoFn, - branchFn: tt.args.branchFn, - branchConfig: tt.args.branchConfig, - pushDefault: tt.args.pushDefault, + baseRepoFn: tt.args.baseRepoFn, + branchFn: tt.args.branchFn, + branchConfig: tt.args.branchConfig, + pushDefault: tt.args.pushDefault, + parsePushRevision: tt.args.parsePushRevision, remotesFn: stubRemotes(context.Remotes{ &remoteOrigin, &remoteOther, @@ -641,14 +649,15 @@ func Test_parsePRRefs(t *testing.T) { } tests := []struct { - name string - branchConfig git.BranchConfig - pushDefault string - currentBranchName string - baseRefRepo ghrepo.Interface - rems context.Remotes - wantPRRefs PRRefs - wantErr error + name string + branchConfig git.BranchConfig + pushDefault string + parsedPushRevision string + currentBranchName string + baseRefRepo ghrepo.Interface + rems context.Remotes + wantPRRefs PRRefs + wantErr error }{ { name: "When the branch is called 'blueberries' with an empty branch config, it returns the correct PRRefs", @@ -675,12 +684,10 @@ func Test_parsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the branch name doesn't match the branch name in BranchConfig.Push, it returns the BranchConfig.Push branch name", - branchConfig: git.BranchConfig{ - Push: "origin/pushBranch", - }, - currentBranchName: "blueberries", - baseRefRepo: remoteOrigin.Repo, + name: "When the branch name doesn't match the branch name in BranchConfig.Push, it returns the BranchConfig.Push branch name", + parsedPushRevision: "origin/pushBranch", + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, rems: context.Remotes{ &remoteOrigin, }, @@ -692,12 +699,10 @@ func Test_parsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the push revision doesn't match a remote, it returns an error", - branchConfig: git.BranchConfig{ - Push: "origin/differentPushBranch", - }, - currentBranchName: "blueberries", - baseRefRepo: remoteOrigin.Repo, + name: "When the push revision doesn't match a remote, it returns an error", + parsedPushRevision: "origin/differentPushBranch", + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, rems: context.Remotes{ &remoteUpstream, &remoteOther, @@ -706,12 +711,10 @@ func Test_parsePRRefs(t *testing.T) { wantErr: fmt.Errorf("no remote for %q found in %q", "origin/differentPushBranch", "upstream, other"), }, { - name: "When the branch name doesn't match a different branch name in BranchConfig.Push and the remote isn't 'origin', it returns the BranchConfig.Push branch name", - branchConfig: git.BranchConfig{ - Push: "other/pushBranch", - }, - currentBranchName: "blueberries", - baseRefRepo: remoteOrigin.Repo, + name: "When the branch name doesn't match a different branch name in BranchConfig.Push and the remote isn't 'origin', it returns the BranchConfig.Push branch name", + parsedPushRevision: "other/pushBranch", + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, rems: context.Remotes{ &remoteOther, }, @@ -819,7 +822,7 @@ func Test_parsePRRefs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.pushDefault, tt.baseRefRepo, tt.rems) + prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.parsedPushRevision, tt.pushDefault, tt.baseRefRepo, tt.rems) if tt.wantErr != nil { require.Equal(t, tt.wantErr, err) } else { @@ -887,3 +890,9 @@ func stubPushDefault(pushDefault string, err error) func() (string, error) { return pushDefault, err } } + +func stubParsedPushRevision(parsedPushRevision string, err error) func(string) (string, error) { + return func(_ string) (string, error) { + return parsedPushRevision, err + } +} From cdead50d572c4b9f830873f18724ac07afe4d3e4 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 11:05:15 -0800 Subject: [PATCH 048/100] Moved remote.pushDefault out of ReadBranchConfig and into finder --- git/client.go | 46 ++--- git/client_test.go | 308 +++++++++---------------------- git/objects.go | 5 +- pkg/cmd/pr/shared/finder.go | 46 +++-- pkg/cmd/pr/shared/finder_test.go | 95 +++++++++- 5 files changed, 223 insertions(+), 277 deletions(-) diff --git a/git/client.go b/git/client.go index c188b27bb..be747e962 100644 --- a/git/client.go +++ b/git/client.go @@ -402,16 +402,10 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon return BranchConfig{}, nil } - // Check to see if there is a pushDefault ref set for the repo - remotePushDefaultOut, err := c.Config(ctx, "remote.pushDefault") - if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 { - return BranchConfig{}, err - } - - return parseBranchConfig(outputLines(branchCfgOut), strings.TrimSuffix(remotePushDefaultOut, "\n")), nil + return parseBranchConfig(outputLines(branchCfgOut)), nil } -func parseBranchConfig(branchConfigLines []string, remotePushDefault string) BranchConfig { +func parseBranchConfig(branchConfigLines []string) BranchConfig { var cfg BranchConfig // Read the config lines for the specific branch @@ -438,26 +432,6 @@ func parseBranchConfig(branchConfigLines []string, remotePushDefault string) Bra } } - // PushRemote{URL|Name} takes precedence over remotePushDefault, so we'll only - // use remotePushDefault if we don't have a push remote. - if cfg.PushRemoteURL == nil && cfg.PushRemoteName == "" { - // remotePushDefault usually indicates a "triangular" workflow - if remotePushDefault != "" { - pushRemoteURL, pushRemoteName := parseRemoteURLOrName(remotePushDefault) - cfg.PushRemoteURL = pushRemoteURL - cfg.PushRemoteName = pushRemoteName - } else { - // Without a PushRemote{URL|Name} or a remotePushDefault, we assume that the - // push remote ref is the same as the remote ref. This is likely - // a "centralized" workflow. - cfg.PushRemoteName = cfg.RemoteName - cfg.PushRemoteURL = cfg.RemoteURL - } - } - - // Some `gh pr` workflows don't work if this is set to 'simple' or 'current' - cfg.RemotePushDefault = remotePushDefault - return cfg } @@ -489,6 +463,22 @@ func (c *Client) PushDefault(ctx context.Context) (string, error) { return "", err } +// RemotePushDefault returns the value of remote.pushDefault in the config. If +// the value is not set, it returns an empty string. +func (c *Client) RemotePushDefault(ctx context.Context) (string, error) { + remotePushDefault, err := c.Config(ctx, "remote.pushDefault") + if err == nil { + return remotePushDefault, nil + } + + var gitError *GitError + if ok := errors.As(err, &gitError); ok && gitError.ExitCode == 1 { + return "", nil + } + + return "", err +} + // ParsePushRevision gets the value of the @{push} revision syntax // An error here doesn't necessarily mean something are broke, but may mean that the @{push} // revision syntax couldn't be resolved, such as in non-centralized workflows with diff --git a/git/client_test.go b/git/client_test.go index 5d8ee9d65..99f783881 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -760,199 +760,20 @@ func TestClientReadBranchConfig(t *testing.T) { }, }, { - name: "when the git reads the config, pushDefault isn't set, it should return the correct BranchConfig", + name: "when the config is read, it should return the correct BranchConfig", cmds: mockedCommands{ `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\n", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 1, - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "origin", - }, - wantError: nil, - }, - { - name: "when the git reads the config, pushDefault is set, it should return the correct BranchConfig", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\n", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "remotePushDefault", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "remotePushDefault", - RemotePushDefault: "remotePushDefault", - }, - wantError: nil, - }, - { - name: "when git reads the config, pushDefault is set, an the branch hasn't been pushed, it should return the correct BranchConfig", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\n", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "remotePushDefault", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "remotePushDefault", - RemotePushDefault: "remotePushDefault", - }, - wantError: nil, - }, - { - name: "when git reads the config but remotePushDefault fails, it should return and empty BranchConfig and the error", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\n", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 2, - Stderr: "remotePushDefault error", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{}, - wantError: &GitError{ - ExitCode: 2, - Stderr: "remotePushDefault error", - }, - }, - { - name: "read branch config, central", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.gh-merge-base merge-base", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 1, - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - MergeRef: "refs/heads/trunk", - MergeBase: "merge-base", - PushRemoteName: "origin", - }, - wantError: nil, - }, - { - name: "read branch config, central, push.default = upstream", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/trunk-remote", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 1, - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - MergeRef: "refs/heads/trunk-remote", - PushRemoteName: "origin", - }, - }, - { - name: "read branch config, central, push.default = upstream, no existing remote branch", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote origin\nbranch.trunk.merge refs/heads/main", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 1, - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "origin", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - }, - }, - { - name: "read branch config, triangular, push.default = current, has existing remote branch, branch.trunk.pushremote effective", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main\nbranch.trunk.pushremote origin", - }, - `path/to/git config remote.pushDefault`: { - ExitStatus: 1, + Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.pushremote origin\nbranch.trunk.gh-merge-base gh-merge-base\n", }, }, branch: "trunk", wantBranchConfig: BranchConfig{ RemoteName: "upstream", - MergeRef: "refs/heads/main", PushRemoteName: "origin", + MergeRef: "refs/heads/trunk", + MergeBase: "gh-merge-base", }, - }, - { - name: "read branch config, triangular, push.default = current, has existing remote branch, remote.pushDefault effective", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "origin", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - RemotePushDefault: "origin", - }, - }, - { - name: "read branch config, triangular, push.default = current, no existing remote branch, branch.trunk.pushremote effective", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main\nbranch.trunk.pushremote origin", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "current", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - RemotePushDefault: "current", - }, - }, - { - name: "read branch config, triangular, push.default = current, no existing remote branch, remote.pushDefault effective", - cmds: mockedCommands{ - `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/main", - }, - `path/to/git config remote.pushDefault`: { - Stdout: "origin", - }, - }, - branch: "trunk", - wantBranchConfig: BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - PushRemoteName: "origin", - RemotePushDefault: "origin", - }, + wantError: nil, }, } for _, tt := range tests { @@ -980,15 +801,13 @@ func Test_parseBranchConfig(t *testing.T) { tests := []struct { name string configLines []string - pushDefault string wantBranchConfig BranchConfig }{ { name: "remote branch", configLines: []string{"branch.trunk.remote origin"}, wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "origin", + RemoteName: "origin", }, }, { @@ -1006,92 +825,73 @@ func Test_parseBranchConfig(t *testing.T) { }, }, { - name: "push remote", + name: "pushremote", configLines: []string{"branch.trunk.pushremote pushremote"}, wantBranchConfig: BranchConfig{ PushRemoteName: "pushremote", }, }, - { - name: "push default specified", - configLines: []string{}, - pushDefault: "remotePushDefault", - wantBranchConfig: BranchConfig{ - PushRemoteName: "remotePushDefault", - RemotePushDefault: "remotePushDefault", - }, - }, { name: "remote and pushremote are specified by name", configLines: []string{ - "branch.trunk.remote origin", - "branch.trunk.pushremote pushremote", + "branch.trunk.remote upstream", + "branch.trunk.pushremote origin", }, wantBranchConfig: BranchConfig{ - RemoteName: "origin", - PushRemoteName: "pushremote", + RemoteName: "upstream", + PushRemoteName: "origin", }, }, { name: "remote and pushremote are specified by url", configLines: []string{ - "branch.Frederick888/main.remote git@github.com:Frederick888/remote.git", - "branch.Frederick888/main.pushremote git@github.com:Frederick888/pushremote.git", + "branch.trunk.remote git@github.com:UPSTREAMOWNER/REPO.git", + "branch.trunk.pushremote git@github.com:ORIGINOWNER/REPO.git", }, wantBranchConfig: BranchConfig{ RemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", - Path: "/Frederick888/remote.git", + Path: "/UPSTREAMOWNER/REPO.git", }, PushRemoteURL: &url.URL{ Scheme: "ssh", User: url.User("git"), Host: "github.com", - Path: "/Frederick888/pushremote.git", + Path: "/ORIGINOWNER/REPO.git", }, }, }, { - name: "remote, pushremote, gh-merge-base, merge ref, push default, and rev parse all specified", + name: "remote, pushremote, gh-merge-base, and merge ref all specified", configLines: []string{ "branch.trunk.remote remote", "branch.trunk.pushremote pushremote", "branch.trunk.gh-merge-base gh-merge-base", "branch.trunk.merge refs/heads/trunk", }, - pushDefault: "remotePushDefault", - wantBranchConfig: BranchConfig{ - RemoteName: "remote", - PushRemoteName: "pushremote", - MergeBase: "gh-merge-base", - MergeRef: "refs/heads/trunk", - RemotePushDefault: "remotePushDefault", - }, - }, - { - name: "pushremote and pushDefault are not specified, but a remoteName is provided", - configLines: []string{ - "branch.trunk.remote remote", - }, wantBranchConfig: BranchConfig{ RemoteName: "remote", - PushRemoteName: "remote", + PushRemoteName: "pushremote", + MergeBase: "gh-merge-base", + MergeRef: "refs/heads/trunk", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - branchConfig := parseBranchConfig(tt.configLines, tt.pushDefault) + branchConfig := parseBranchConfig(tt.configLines) assert.Equalf(t, tt.wantBranchConfig.RemoteName, branchConfig.RemoteName, "unexpected RemoteName") assert.Equalf(t, tt.wantBranchConfig.MergeRef, branchConfig.MergeRef, "unexpected MergeRef") assert.Equalf(t, tt.wantBranchConfig.MergeBase, branchConfig.MergeBase, "unexpected MergeBase") assert.Equalf(t, tt.wantBranchConfig.PushRemoteName, branchConfig.PushRemoteName, "unexpected PushRemoteName") - assert.Equalf(t, tt.wantBranchConfig.RemotePushDefault, branchConfig.RemotePushDefault, "unexpected RemotePushDefault") if tt.wantBranchConfig.RemoteURL != nil { assert.Equalf(t, tt.wantBranchConfig.RemoteURL.String(), branchConfig.RemoteURL.String(), "unexpected RemoteURL") } + if tt.wantBranchConfig.PushRemoteURL != nil { + assert.Equalf(t, tt.wantBranchConfig.PushRemoteURL.String(), branchConfig.PushRemoteURL.String(), "unexpected PushRemoteURL") + } }) } } @@ -1204,6 +1004,68 @@ func TestClientPushDefault(t *testing.T) { } } +func TestClientRemotePushDefault(t *testing.T) { + tests := []struct { + name string + commandResult commandResult + wantRemotePushDefault string + wantError *GitError + }{ + { + name: "remote.pushDefault is not set", + commandResult: commandResult{ + ExitStatus: 1, + Stderr: "error: key does not contain a section: remote.pushDefault", + }, + wantRemotePushDefault: "", + wantError: nil, + }, + { + name: "remote.pushDefault is set to origin", + commandResult: commandResult{ + ExitStatus: 0, + Stdout: "origin", + }, + wantRemotePushDefault: "origin", + wantError: nil, + }, + { + name: "remote.pushDefault errors", + commandResult: commandResult{ + ExitStatus: 128, + Stderr: "fatal: git error", + }, + wantRemotePushDefault: "", + wantError: &GitError{ + ExitCode: 128, + Stderr: "fatal: git error", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmdCtx := createMockedCommandContext(t, mockedCommands{ + `path/to/git config remote.pushDefault`: tt.commandResult, + }, + ) + client := Client{ + GitPath: "path/to/git", + commandContext: cmdCtx, + } + pushDefault, err := client.RemotePushDefault(context.Background()) + if tt.wantError != nil { + var gitError *GitError + require.ErrorAs(t, err, &gitError) + assert.Equal(t, tt.wantError.ExitCode, gitError.ExitCode) + assert.Equal(t, tt.wantError.Stderr, gitError.Stderr) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.wantRemotePushDefault, pushDefault) + }) + } +} + func TestClientParsePushRevision(t *testing.T) { tests := []struct { name string diff --git a/git/objects.go b/git/objects.go index eab7583ae..d66d5b9bb 100644 --- a/git/objects.go +++ b/git/objects.go @@ -68,7 +68,6 @@ type BranchConfig struct { MergeRef string // These are used to handle triangular workflows. They can be defined by either // a remote.pushDefault or a branch..pushremote value set on the git config. - PushRemoteURL *url.URL - PushRemoteName string - RemotePushDefault string + PushRemoteURL *url.URL + PushRemoteName string } diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index d76a62140..8589840a9 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -38,6 +38,7 @@ type finder struct { remotesFn func() (remotes.Remotes, error) httpClient func() (*http.Client, error) pushDefault func() (string, error) + remotePushDefault func() (string, error) parsePushRevision func(string) (string, error) branchConfig func(string) (git.BranchConfig, error) progress progressIndicator @@ -62,6 +63,9 @@ func NewFinder(factory *cmdutil.Factory) PRFinder { pushDefault: func() (string, error) { return factory.GitClient.PushDefault(context.Background()) }, + remotePushDefault: func() (string, error) { + return factory.GitClient.RemotePushDefault(context.Background()) + }, parsePushRevision: func(branch string) (string, error) { return factory.GitClient.ParsePushRevision(context.Background(), branch) }, @@ -217,10 +221,15 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return nil, nil, err } - // Suppressing the error as we have other means of computing the PRRefs if this fails. + // Suppressing these errors as we have other means of computing the PRRefs when these fail. parsedPushRevision, _ := f.parsePushRevision(f.branchName) - prRefs, err := parsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, f.baseRefRepo, rems) + remotePushDefault, err := f.remotePushDefault() + if err != nil { + return nil, nil, err + } + + prRefs, err := parsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, f.baseRefRepo, rems) if err != nil { return nil, nil, err } @@ -287,7 +296,7 @@ func (f *finder) parseURL(prURL string) (ghrepo.Interface, int, error) { return repo, prNumber, nil } -func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { +func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, remotePushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { prRefs := PRRefs{ BaseRepo: baseRefRepo, } @@ -311,8 +320,15 @@ func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsed return PRRefs{}, fmt.Errorf("no remote for %q found in %q", parsedPushRevision, strings.Join(remoteNames, ", ")) } - // To get the HeadRepo, we look to the git config. The PushRemote{Name | URL} comes from - // one of the following, in order of precedence: + // We assume the PR's branch name is the same as whatever f.BranchFn() returned earlier + // unless the user has specified push.default = upstream or tracking, then we use the + // branch name from the merge ref. + prRefs.BranchName = currentBranchName + if pushDefault == "upstream" || pushDefault == "tracking" { + prRefs.BranchName = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") + } + + // To get the HeadRepo, we look to the git config. The HeadRepo comes from one of the following, in order of precedence: // 1. branch..pushRemote // 2. remote.pushDefault // 3. branch..remote @@ -324,14 +340,18 @@ func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsed if r, err := ghrepo.FromURL(branchConfig.PushRemoteURL); err == nil { prRefs.HeadRepo = r } - } - - // We assume the PR's branch name is the same as whatever f.BranchFn() returned earlier. - // unless the user has specified push.default = upstream or tracking, then we use the - // branch name from the merge ref. - prRefs.BranchName = currentBranchName - if pushDefault == "upstream" || pushDefault == "tracking" { - prRefs.BranchName = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") + } else if remotePushDefault != "" { + if r, err := rems.FindByName(remotePushDefault); err == nil { + prRefs.HeadRepo = r.Repo + } + } else if branchConfig.RemoteName != "" { + if r, err := rems.FindByName(branchConfig.RemoteName); err == nil { + prRefs.HeadRepo = r.Repo + } + } else if branchConfig.RemoteURL != nil { + if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil { + prRefs.HeadRepo = r + } } // The PR merges from a branch in the same repo as the base branch (usually the default branch) diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index db56ce775..89835b028 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -20,6 +20,7 @@ type args struct { branchFn func() (string, error) branchConfig func(string) (git.BranchConfig, error) pushDefault func() (string, error) + remotePushDefault func() (string, error) parsePushRevision func(string) (string, error) selector string fields []string @@ -102,6 +103,7 @@ func TestFind(t *testing.T) { PushRemoteName: remoteOrigin.Remote.Name, }, nil), pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -132,8 +134,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, wantErr: true, }, @@ -154,8 +157,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, httpStub: nil, wantPR: 13, @@ -170,8 +174,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -192,8 +197,9 @@ func TestFind(t *testing.T) { branchFn: func() (string, error) { return "blueberries", nil }, - branchConfig: stubBranchConfig(git.BranchConfig{}, nil), - pushDefault: stubPushDefault("simple", nil), + branchConfig: stubBranchConfig(git.BranchConfig{}, nil), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -217,6 +223,7 @@ func TestFind(t *testing.T) { branchConfig: stubBranchConfig(git.BranchConfig{}, nil), pushDefault: stubPushDefault("simple", nil), parsePushRevision: stubParsedPushRevision("", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -259,6 +266,7 @@ func TestFind(t *testing.T) { }, branchConfig: stubBranchConfig(git.BranchConfig{}, nil), pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -301,6 +309,7 @@ func TestFind(t *testing.T) { }, branchConfig: stubBranchConfig(git.BranchConfig{}, nil), pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -335,6 +344,7 @@ func TestFind(t *testing.T) { }, branchConfig: stubBranchConfig(git.BranchConfig{}, nil), pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -386,6 +396,7 @@ func TestFind(t *testing.T) { PushRemoteName: "upstream", }, nil), pushDefault: stubPushDefault("upstream", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -421,6 +432,7 @@ func TestFind(t *testing.T) { PushRemoteURL: remoteUpstream.Remote.FetchURL, }, nil), pushDefault: stubPushDefault("upstream", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("", nil), }, httpStub: func(r *httpmock.Registry) { @@ -453,6 +465,7 @@ func TestFind(t *testing.T) { }, branchConfig: stubBranchConfig(git.BranchConfig{}, nil), pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), parsePushRevision: stubParsedPushRevision("other/blueberries", nil), }, httpStub: func(r *httpmock.Registry) { @@ -513,7 +526,8 @@ func TestFind(t *testing.T) { branchConfig: stubBranchConfig(git.BranchConfig{ MergeRef: "refs/pull/13/head", }, nil), - pushDefault: stubPushDefault("simple", nil), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", nil), }, httpStub: func(r *httpmock.Registry) { r.Register( @@ -578,6 +592,7 @@ func TestFind(t *testing.T) { branchFn: tt.args.branchFn, branchConfig: tt.args.branchConfig, pushDefault: tt.args.pushDefault, + remotePushDefault: tt.args.remotePushDefault, parsePushRevision: tt.args.parsePushRevision, remotesFn: stubRemotes(context.Remotes{ &remoteOrigin, @@ -653,6 +668,7 @@ func Test_parsePRRefs(t *testing.T) { branchConfig git.BranchConfig pushDefault string parsedPushRevision string + remotePushDefault string currentBranchName string baseRefRepo ghrepo.Interface rems context.Remotes @@ -819,10 +835,63 @@ func Test_parsePRRefs(t *testing.T) { }, wantErr: nil, }, + { + name: "When remote.pushDefault is set, it returns the correct PRRefs", + branchConfig: git.BranchConfig{}, + remotePushDefault: remoteUpstream.Remote.Name, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteUpstream.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the remote name is set on the branch, it returns the correct PRRefs", + branchConfig: git.BranchConfig{ + RemoteName: remoteUpstream.Remote.Name, + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteUpstream.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, + { + name: "When the remote URL is set on the branch, it returns the correct PRRefs", + branchConfig: git.BranchConfig{ + RemoteURL: remoteUpstream.Remote.FetchURL, + }, + currentBranchName: "blueberries", + baseRefRepo: remoteOrigin.Repo, + rems: context.Remotes{ + &remoteOrigin, + &remoteUpstream, + }, + wantPRRefs: PRRefs{ + BranchName: "blueberries", + HeadRepo: remoteUpstream.Repo, + BaseRepo: remoteOrigin.Repo, + }, + wantErr: nil, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.parsedPushRevision, tt.pushDefault, tt.baseRefRepo, tt.rems) + prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.parsedPushRevision, tt.pushDefault, tt.remotePushDefault, tt.baseRefRepo, tt.rems) if tt.wantErr != nil { require.Equal(t, tt.wantErr, err) } else { @@ -891,6 +960,12 @@ func stubPushDefault(pushDefault string, err error) func() (string, error) { } } +func stubRemotePushDefault(remotePushDefault string, err error) func() (string, error) { + return func() (string, error) { + return remotePushDefault, err + } +} + func stubParsedPushRevision(parsedPushRevision string, err error) func(string) (string, error) { return func(_ string) (string, error) { return parsedPushRevision, err From d684834ad9728ff5e8bb5db57d869864c6188170 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 11:51:49 -0800 Subject: [PATCH 049/100] Refactor pr status to use the ParsePRRefs helper on the Finder There was a lot of copy-pasta code between the finder and pr status. After some investigation it was clear that the prSelectorForCurrentBranch code was really just a duplicate of the finder's code without actually making the API call for the PR. Since the ParsePRRefs helper had already extracted much of the logic for determining a PR's head ref branch, I was able to reuse it in gh pr status with a small refactor. --- pkg/cmd/pr/shared/finder.go | 4 +- pkg/cmd/pr/shared/finder_test.go | 4 +- pkg/cmd/pr/status/status.go | 110 +++++------- pkg/cmd/pr/status/status_test.go | 292 +++---------------------------- 4 files changed, 70 insertions(+), 340 deletions(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 8589840a9..ae0ea4553 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -229,7 +229,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return nil, nil, err } - prRefs, err := parsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, f.baseRefRepo, rems) + prRefs, err := ParsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, f.baseRefRepo, rems) if err != nil { return nil, nil, err } @@ -296,7 +296,7 @@ func (f *finder) parseURL(prURL string) (ghrepo.Interface, int, error) { return repo, prNumber, nil } -func parsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, remotePushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { +func ParsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, remotePushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { prRefs := PRRefs{ BaseRepo: baseRefRepo, } diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 89835b028..1d5c1e3ac 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -631,7 +631,7 @@ func TestFind(t *testing.T) { } } -func Test_parsePRRefs(t *testing.T) { +func TestParsePRRefs(t *testing.T) { originOwnerUrl, err := url.Parse("https://github.com/ORIGINOWNER/REPO.git") if err != nil { t.Fatal(err) @@ -891,7 +891,7 @@ func Test_parsePRRefs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prRefs, err := parsePRRefs(tt.currentBranchName, tt.branchConfig, tt.parsedPushRevision, tt.pushDefault, tt.remotePushDefault, tt.baseRefRepo, tt.rems) + prRefs, err := ParsePRRefs(tt.currentBranchName, tt.branchConfig, tt.parsedPushRevision, tt.pushDefault, tt.remotePushDefault, tt.baseRefRepo, tt.rems) if tt.wantErr != nil { require.Equal(t, tt.wantErr, err) } else { diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 89926bfa2..996a178e1 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -7,7 +7,6 @@ import ( "net/http" "regexp" "strconv" - "strings" "time" "github.com/cli/cli/v2/api" @@ -78,27 +77,56 @@ func statusRun(opts *StatusOptions) error { return err } - baseRepo, err := opts.BaseRepo() + baseRefRepo, err := opts.BaseRepo() if err != nil { return err } - var currentBranch string + var currentBranchName string var currentPRNumber int - var currentPRHeadRef string + var currentHeadRefBranchName string if !opts.HasRepoOverride { - currentBranch, err = opts.Branch() + currentBranchName, err = opts.Branch() if err != nil && !errors.Is(err, git.ErrNotOnAnyBranch) { return fmt.Errorf("could not query for pull request for current branch: %w", err) } - remotes, _ := opts.Remotes() - branchConfig, err := opts.GitClient.ReadBranchConfig(ctx, currentBranch) + branchConfig, err := opts.GitClient.ReadBranchConfig(ctx, currentBranchName) if err != nil { return err } - currentPRNumber, currentPRHeadRef, err = prSelectorForCurrentBranch(branchConfig, baseRepo, currentBranch, remotes) + // Determine if the branch is configured to merge to a special PR ref + prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) + if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { + currentPRNumber, _ = strconv.Atoi(m[1]) + } + + if currentPRNumber == 0 { + remotes, err := opts.Remotes() + if err != nil { + return err + } + // Suppressing these errors as we have other means of computing the PRRefs when these fail. + parsedPushRevision, _ := opts.GitClient.ParsePushRevision(ctx, currentBranchName) + + remotePushDefault, err := opts.GitClient.RemotePushDefault(ctx) + if err != nil { + return err + } + + pushDefault, err := opts.GitClient.PushDefault(ctx) + if err != nil { + return err + } + + prRefs, err := shared.ParsePRRefs(currentBranchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, baseRefRepo, remotes) + if err != nil { + return err + } + currentHeadRefBranchName = prRefs.BranchName + } + if err != nil { return fmt.Errorf("could not query for pull request for current branch: %w", err) } @@ -107,7 +135,7 @@ func statusRun(opts *StatusOptions) error { options := requestOptions{ Username: "@me", CurrentPR: currentPRNumber, - HeadRef: currentPRHeadRef, + HeadRef: currentHeadRefBranchName, ConflictStatus: opts.ConflictStatus, } if opts.Exporter != nil { @@ -116,7 +144,7 @@ func statusRun(opts *StatusOptions) error { if opts.Detector == nil { cachedClient := api.NewCachedHTTPClient(httpClient, time.Hour*24) - opts.Detector = fd.NewDetector(cachedClient, baseRepo.RepoHost()) + opts.Detector = fd.NewDetector(cachedClient, baseRefRepo.RepoHost()) } prFeatures, err := opts.Detector.PullRequestFeatures() if err != nil { @@ -124,7 +152,7 @@ func statusRun(opts *StatusOptions) error { } options.CheckRunAndStatusContextCountsSupported = prFeatures.CheckRunAndStatusContextCounts - prPayload, err := pullRequestStatus(httpClient, baseRepo, options) + prPayload, err := pullRequestStatus(httpClient, baseRefRepo, options) if err != nil { return err } @@ -151,21 +179,21 @@ func statusRun(opts *StatusOptions) error { cs := opts.IO.ColorScheme() fmt.Fprintln(out, "") - fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo)) + fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRefRepo)) fmt.Fprintln(out, "") if !opts.HasRepoOverride { shared.PrintHeader(opts.IO, "Current branch") currentPR := prPayload.CurrentPR - if currentPR != nil && currentPR.State != "OPEN" && prPayload.DefaultBranch == currentBranch { + if currentPR != nil && currentPR.State != "OPEN" && prPayload.DefaultBranch == currentBranchName { currentPR = nil } if currentPR != nil { printPrs(opts.IO, 1, *currentPR) - } else if currentPRHeadRef == "" { + } else if currentHeadRefBranchName == "" { shared.PrintMessage(opts.IO, " There is no current branch") } else { - shared.PrintMessage(opts.IO, fmt.Sprintf(" There is no pull request associated with %s", cs.Cyan("["+currentPRHeadRef+"]"))) + shared.PrintMessage(opts.IO, fmt.Sprintf(" There is no pull request associated with %s", cs.Cyan("["+currentHeadRefBranchName+"]"))) } fmt.Fprintln(out) } @@ -189,58 +217,6 @@ func statusRun(opts *StatusOptions) error { return nil } -func prSelectorForCurrentBranch(branchConfig git.BranchConfig, baseRepo ghrepo.Interface, prHeadRef string, rem ghContext.Remotes) (int, string, error) { - // the branch is configured to merge a special PR head ref - prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) - if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { - prNumber, err := strconv.Atoi(m[1]) - if err != nil { - return 0, "", err - } - return prNumber, prHeadRef, nil - } - var branchOwner string - if branchConfig.PushRemoteURL != nil { - // the branch merges from a remote specified by URL - r, err := ghrepo.FromURL(branchConfig.PushRemoteURL) - if err != nil { - // TODO: We aren't returning the error because we discovered that it was shadowed - // before refactoring to its current return pattern. Thus, we aren't confident - // that returning the error won't break existing behavior. - return 0, prHeadRef, nil - } - branchOwner = r.RepoOwner() - } else if branchConfig.PushRemoteName != "" { - // the branch merges from a remote specified by name - r, err := rem.FindByName(branchConfig.PushRemoteName) - if err != nil { - // TODO: We aren't returning the error because we discovered that it was shadowed - // before refactoring to its current return pattern. Thus, we aren't confident - // that returning the error won't break existing behavior. - return 0, prHeadRef, nil - } - branchOwner = r.RepoOwner() - } - - if branchOwner != "" { - selector := prHeadRef - if branchConfig.Push != "" { - // If we have a resolved push revision ref, we defer to that - selector = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/") - } else if (branchConfig.RemotePushDefault == "upstream" || branchConfig.RemotePushDefault == "tracking") && - strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") { - selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/") - } - // prepend `OWNER:` if this branch is pushed to a fork - if !strings.EqualFold(branchOwner, baseRepo.RepoOwner()) { - selector = fmt.Sprintf("%s:%s", branchOwner, selector) - } - return 0, selector, nil - } - - return 0, prHeadRef, nil -} - func totalApprovals(pr *api.PullRequest) int { approvals := 0 for _, review := range pr.LatestReviews.Nodes { diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index 4e9ca19b0..c55604c28 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "net/http" - "net/url" "regexp" "strings" "testing" @@ -96,12 +95,13 @@ func TestPRStatus(t *testing.T) { defer http.Verify(t) http.Register(httpmock.GraphQL(`query PullRequestStatus\b`), httpmock.FileResponse("./fixtures/prStatus.json")) - // stub successful git command + // stub successful git commands rs, cleanup := run.Stub() defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -133,7 +133,8 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -165,7 +166,8 @@ func TestPRStatus_reviewsAndChecksWithStatesByCount(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommandWithDetector(http, "blueberries", true, "", &fd.EnabledDetectorMock{}) if err != nil { @@ -196,7 +198,8 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -231,7 +234,8 @@ func TestPRStatus_currentBranch_defaultBranch(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -272,7 +276,8 @@ func TestPRStatus_currentBranch_Closed(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -296,7 +301,8 @@ func TestPRStatus_currentBranch_Closed_defaultBranch(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -320,7 +326,8 @@ func TestPRStatus_currentBranch_Merged(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -344,7 +351,8 @@ func TestPRStatus_currentBranch_Merged_defaultBranch(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -368,7 +376,8 @@ func TestPRStatus_blankSlate(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref blueberries@{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -428,7 +437,8 @@ func TestPRStatus_detachedHead(t *testing.T) { defer cleanup(t) rs.Register(`git config --get-regexp \^branch\\.`, 0, "") rs.Register(`git config remote.pushDefault`, 0, "") - rs.Register(`git rev-parse --verify --quiet --abbrev-ref @{push}`, 0, "") + rs.Register(`git rev-parse --abbrev-ref @{push}`, 0, "") + rs.Register(`git config push.default`, 0, "") output, err := runCommand(http, "", true, "") if err != nil { @@ -461,259 +471,3 @@ func TestPRStatus_error_ReadBranchConfig(t *testing.T) { _, err := runCommand(initFakeHTTP(), "blueberries", true, "") assert.Error(t, err) } - -func Test_prSelectorForCurrentBranch(t *testing.T) { - tests := []struct { - name string - branchConfig git.BranchConfig - baseRepo ghrepo.Interface - prHeadRef string - remotes context.Remotes - wantPrNumber int - wantSelector string - wantError error - }{ - { - name: "Empty branch config", - branchConfig: git.BranchConfig{}, - prHeadRef: "monalisa/main", - wantPrNumber: 0, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "The branch is configured to merge a special PR head ref", - branchConfig: git.BranchConfig{ - MergeRef: "refs/pull/42/head", - }, - prHeadRef: "monalisa/main", - wantPrNumber: 42, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "Branch merges from a remote specified by URL", - branchConfig: git.BranchConfig{ - PushRemoteURL: &url.URL{ - Scheme: "ssh", - User: url.User("git"), - Host: "github.com", - Path: "monalisa/playground.git", - }, - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "Branch merges from a remote specified by name", - branchConfig: git.BranchConfig{ - RemoteName: "upstream", - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - &context.Remote{ - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "Branch is a fork and merges from a remote specified by URL", - branchConfig: git.BranchConfig{ - PushRemoteURL: &url.URL{ - Scheme: "ssh", - User: url.User("git"), - Host: "github.com", - Path: "forkName/playground.git", - }, - MergeRef: "refs/heads/main", - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "forkName:main", - wantError: nil, - }, - { - name: "Branch is a fork and merges from a remote specified by name", - branchConfig: git.BranchConfig{ - RemoteName: "origin", - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - &context.Remote{ - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "forkName:monalisa/main", - wantError: nil, - }, - { - name: "Branch specifies a mergeRef and merges from a remote specified by name", - branchConfig: git.BranchConfig{ - RemoteName: "upstream", - MergeRef: "refs/heads/main", - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - &context.Remote{ - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "main", - wantError: nil, - }, - { - name: "Branch is a fork, specifies a mergeRef, and merges from a remote specified by name", - branchConfig: git.BranchConfig{ - RemoteName: "origin", - MergeRef: "refs/heads/main", - }, - baseRepo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - &context.Remote{ - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "forkName:main", - wantError: nil, - }, - { - name: "Remote URL errors", - branchConfig: git.BranchConfig{ - PushRemoteURL: &url.URL{ - Scheme: "ssh", - User: url.User("git"), - Host: "github.com", - Path: "/\\invalid?Path/", - }, - }, - prHeadRef: "monalisa/main", - wantPrNumber: 0, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "Remote Name errors", - branchConfig: git.BranchConfig{ - RemoteName: "nonexistentRemote", - }, - prHeadRef: "monalisa/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"), - }, - &context.Remote{ - Remote: &git.Remote{Name: "upstream"}, - Repo: ghrepo.NewWithHost("monalisa", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "monalisa/main", - wantError: nil, - }, - { - name: "Current branch pushes to default upstream", - branchConfig: git.BranchConfig{ - PushRemoteURL: &url.URL{ - Scheme: "ssh", - User: url.User("git"), - Host: "github.com", - Path: "Frederick888/playground.git", - }, - MergeRef: "refs/heads/main", - RemotePushDefault: "upstream", - }, - baseRepo: ghrepo.NewWithHost("octocat", "playground", "github.com"), - prHeadRef: "Frederick888/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("octocat", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "Frederick888:main", - wantError: nil, - }, - { - name: "Current branch pushes to default tracking", - branchConfig: git.BranchConfig{ - PushRemoteURL: &url.URL{ - Scheme: "ssh", - User: url.User("git"), - Host: "github.com", - Path: "Frederick888/playground.git", - }, - MergeRef: "refs/heads/main", - RemotePushDefault: "tracking", - }, - baseRepo: ghrepo.NewWithHost("octocat", "playground", "github.com"), - prHeadRef: "Frederick888/main", - remotes: context.Remotes{ - &context.Remote{ - Remote: &git.Remote{Name: "origin"}, - Repo: ghrepo.NewWithHost("octocat", "playground", "github.com"), - }, - }, - wantPrNumber: 0, - wantSelector: "Frederick888:main", - wantError: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - - prNum, headRef, err := prSelectorForCurrentBranch(tt.branchConfig, tt.baseRepo, tt.prHeadRef, tt.remotes) - assert.Equal(t, tt.wantPrNumber, prNum) - assert.Equal(t, tt.wantSelector, headRef) - assert.Equal(t, tt.wantError, err) - }) - } -} From 4382bdf072b558790358abda47b8cf3d802cd895 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 14:01:44 -0800 Subject: [PATCH 050/100] Fix pr create tests Due to the refactor of BranchConfig, the tests for `pr create` no longer require a handful of stubbed git commands. Additionally, I noticed some overlap between `pr create`'s existing logic with the new finder code. I suspect that the finder's new ParsePRRefs helper could be leveraged here as well, which would allow us to respect non-centralized workflows pr create in the same way we are respecting them in `gh pr view` and `gh pr status` --- pkg/cmd/pr/create/create.go | 5 +- pkg/cmd/pr/create/create_test.go | 87 -------------------------------- pkg/cmd/pr/shared/finder.go | 6 +++ 3 files changed, 10 insertions(+), 88 deletions(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 124bd4b07..6c9d2a2af 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -518,6 +518,7 @@ func initDefaultTitleBody(ctx CreateContext, state *shared.IssueMetadataState, u return nil } +// TODO: Replace with the finder's PRRefs struct // trackingRef represents a ref for a remote tracking branch. type trackingRef struct { remoteName string @@ -685,7 +686,9 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) { return nil, err } if isPushEnabled { - // determine whether the head branch is already pushed to a remote + // TODO: This doesn't respect the @{push} revision resolution or triagular workflows assembled with + // remote.pushDefault, or branch..pushremote config settings. The finder's ParsePRRefs + // may be able to replace this function entirely. if trackingRef, found := tryDetermineTrackingRef(gitClient, remotes, headBranch, headBranchConfig); found { isPushEnabled = false if r, err := remotes.FindByName(trackingRef.remoteName); err == nil { diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 6629b2fa1..c486997ad 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -353,8 +353,6 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, expectedBrowse: "https://github.com/OWNER/REPO/compare/master...feature?body=&expand=1", @@ -384,10 +382,6 @@ func Test_createRun(t *testing.T) { opts.HeadBranch = "feature" return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", }, { @@ -402,10 +396,6 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, expectedOutputs: []string{ "Would have created a Pull Request with:", `title: my title`, @@ -437,10 +427,6 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -501,10 +487,6 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, expectedOutputs: []string{ `Would have created a Pull Request with:`, `Title: my title`, @@ -542,10 +524,6 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -612,10 +590,6 @@ func Test_createRun(t *testing.T) { opts.DryRun = true return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, expectedOut: heredoc.Doc(` Would have created a Pull Request with: Title: TITLE @@ -662,8 +636,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -727,8 +699,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -775,8 +745,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") }, @@ -826,8 +794,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register("git remote rename origin upstream", 0, "") cs.Register(`git remote add origin https://github.com/monalisa/REPO.git`, 0, "") @@ -886,8 +852,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/upstream/feature @@ -925,8 +889,6 @@ func Test_createRun(t *testing.T) { branch.feature.remote origin branch.feature.merge refs/heads/my-feat2 `)) // determineTrackingBranch - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/my-feat2") cs.Register("git show-ref --verify", 0, heredoc.Doc(` deadbeef HEAD deadbeef refs/remotes/origin/my-feat2 @@ -967,8 +929,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "d3476a1\u0000commit 0\u0000\u0000\n7a6ea13\u0000commit 1\u0000\u0000") }, promptStubs: func(pm *prompter.PrompterMock) { @@ -1009,10 +969,6 @@ func Test_createRun(t *testing.T) { opts.Milestone = "big one.oh" return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`), @@ -1100,10 +1056,6 @@ func Test_createRun(t *testing.T) { opts.Finder = shared.NewMockFinder("feature", &api.PullRequest{URL: "https://github.com/OWNER/REPO/pull/123"}, ghrepo.New("OWNER", "REPO")) return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, wantErr: "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123", }, { @@ -1120,8 +1072,6 @@ func Test_createRun(t *testing.T) { httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`)) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -1154,8 +1104,6 @@ func Test_createRun(t *testing.T) { mockRetrieveProjects(t, reg) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "") @@ -1203,8 +1151,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature`, 0, "") cs.Register(`git rev-parse --show-toplevel`, 0, "") }, @@ -1264,8 +1210,6 @@ func Test_createRun(t *testing.T) { })) }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, promptStubs: func(pm *prompter.PrompterMock) { @@ -1314,8 +1258,6 @@ func Test_createRun(t *testing.T) { { name: "web long URL", cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "") }, setup: func(opts *CreateOptions, t *testing.T) func() { @@ -1342,10 +1284,6 @@ func Test_createRun(t *testing.T) { } return func() {} }, - cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") - }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`mutation PullRequestCreate\b`), @@ -1365,8 +1303,6 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1442,8 +1378,6 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1480,8 +1414,6 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register( "git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, @@ -1532,8 +1464,6 @@ func Test_createRun(t *testing.T) { return func() {} }, cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git -c log.ShowSignature=false log --pretty=format:%H%x00%s%x00%b%x00 --cherry origin/master...feature", 0, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", @@ -1593,8 +1523,6 @@ func Test_createRun(t *testing.T) { deadbeef HEAD deadb00f refs/remotes/upstream/feature/feat2 deadbeef refs/remotes/origin/task1`)) // determineTrackingBranch - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref task1@\{push\}`, 1, "") }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", expectedErrOut: "\nCreating pull request for monalisa:task1 into feature/feat2 in OWNER/REPO\n\n", @@ -1705,9 +1633,6 @@ func Test_tryDetermineTrackingRef(t *testing.T) { { name: "empty", cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register(`git show-ref --verify -- HEAD`, 0, "abc HEAD") }, headBranchConfig: git.BranchConfig{}, @@ -1717,9 +1642,6 @@ func Test_tryDetermineTrackingRef(t *testing.T) { { name: "no match", cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 1, "") cs.Register("git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature", 0, "abc HEAD\nbca refs/remotes/upstream/feature") }, headBranchConfig: git.BranchConfig{}, @@ -1739,9 +1661,6 @@ func Test_tryDetermineTrackingRef(t *testing.T) { { name: "match", cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/feature") cs.Register(`git show-ref --verify -- HEAD refs/remotes/upstream/feature refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/upstream/feature @@ -1768,12 +1687,6 @@ func Test_tryDetermineTrackingRef(t *testing.T) { { name: "respect tracking config", cmdStubs: func(cs *run.CommandStubber) { - cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, heredoc.Doc(` - branch.feature.remote origin - branch.feature.merge refs/heads/great-feat - `)) - cs.Register(`git config remote.pushDefault`, 1, "") - cs.Register(`git rev-parse --verify --quiet --abbrev-ref feature@\{push\}`, 0, "origin/great-feat") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/great-feat refs/remotes/origin/feature$`, 0, heredoc.Doc(` deadbeef HEAD deadb00f refs/remotes/origin/feature diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index ae0ea4553..205939931 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -97,6 +97,12 @@ type FindOptions struct { States []string } +// TODO: Does this also need the BaseBranchName? +// PR's are represented by the following: +// baseRef -----PR-----> headRef +// +// A ref is described as "remoteName/branchName", so +// baseRepoName/baseBranchName -----PR-----> headRepoName/headBranchName type PRRefs struct { BranchName string HeadRepo ghrepo.Interface From 62106dc800ada6ed8d4c8deab6270c3c0a008d44 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 24 Jan 2025 14:18:20 -0800 Subject: [PATCH 051/100] Cleanup comment --- git/objects.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/git/objects.go b/git/objects.go index d66d5b9bb..b57d79bec 100644 --- a/git/objects.go +++ b/git/objects.go @@ -66,8 +66,7 @@ type BranchConfig struct { // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. MergeBase string MergeRef string - // These are used to handle triangular workflows. They can be defined by either - // a remote.pushDefault or a branch..pushremote value set on the git config. + // These are usually used to handle triangular workflows. PushRemoteURL *url.URL PushRemoteName string } From 532be61d6bcfca854670250f663cd8d400f8c31b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 14:35:50 +0000 Subject: [PATCH 052/100] Bump github.com/in-toto/attestation from 1.1.0 to 1.1.1 Bumps [github.com/in-toto/attestation](https://github.com/in-toto/attestation) from 1.1.0 to 1.1.1. - [Release notes](https://github.com/in-toto/attestation/releases) - [Commits](https://github.com/in-toto/attestation/compare/v1.1.0...v1.1.1) --- updated-dependencies: - dependency-name: github.com/in-toto/attestation dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ca5549587..ba20dc0bd 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-version v1.3.0 github.com/henvic/httpretty v0.1.4 - github.com/in-toto/attestation v1.1.0 + github.com/in-toto/attestation v1.1.1 github.com/joho/godotenv v1.5.1 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mattn/go-colorable v0.1.14 diff --git a/go.sum b/go.sum index 5a7147b19..9dbbcdcd7 100644 --- a/go.sum +++ b/go.sum @@ -265,8 +265,8 @@ github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef h1:A9HsByNhogrvm9cWb28sjiS3i7tcKCkflWFEkHfuAgM= github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs= -github.com/in-toto/attestation v1.1.0 h1:oRWzfmZPDSctChD0VaQV7MJrywKOzyNrtpENQFq//2Q= -github.com/in-toto/attestation v1.1.0/go.mod h1:DB59ytd3z7cIHgXxwpSX2SABrU6WJUKg/grpdgHVgVs= +github.com/in-toto/attestation v1.1.1 h1:QD3d+oATQ0dFsWoNh5oT0udQ3tUrOsZZ0Fc3tSgWbzI= +github.com/in-toto/attestation v1.1.1/go.mod h1:Dcq1zVwA2V7Qin8I7rgOi+i837wEf/mOZwRm047Sjys= github.com/in-toto/in-toto-golang v0.9.0 h1:tHny7ac4KgtsfrG6ybU8gVOZux2H8jN05AXJ9EBM1XU= github.com/in-toto/in-toto-golang v0.9.0/go.mod h1:xsBVrVsHNsB61++S6Dy2vWosKhuA3lUTQd+eF9HdeMo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= From e31bfd0092e5482247c6a6a90ca1d8c4c6c7a966 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Mon, 27 Jan 2025 16:40:47 -0800 Subject: [PATCH 053/100] Cleaned up some naming and comments --- git/client.go | 20 +++++++------------- git/client_test.go | 20 +++++++++++++------- git/objects.go | 13 +++++++------ pkg/cmd/pr/shared/finder.go | 6 +++--- pkg/cmd/pr/shared/finder_test.go | 10 +++++++--- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/git/client.go b/git/client.go index be747e962..30cb4306d 100644 --- a/git/client.go +++ b/git/client.go @@ -389,13 +389,11 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon return BranchConfig{}, err } - // This is the error we expect if a git command does not run successfully. - // If the ExitCode is 1, then we just didn't find any config for the branch. - // We will use this error to check against the commands that are allowed to - // return an empty result. - var gitError *GitError branchCfgOut, err := cmd.Output() if err != nil { + // This is the error we expect if the git command does not run successfully. + // If the ExitCode is 1, then we just didn't find any config for the branch. + var gitError *GitError if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 { return BranchConfig{}, err } @@ -417,14 +415,9 @@ func parseBranchConfig(branchConfigLines []string) BranchConfig { keys := strings.Split(parts[0], ".") switch keys[len(keys)-1] { case "remote": - remoteURL, remoteName := parseRemoteURLOrName(parts[1]) - cfg.RemoteURL = remoteURL - cfg.RemoteName = remoteName - // pushremote usually indicates a "triangular" workflow + cfg.RemoteURL, cfg.RemoteName = parseRemoteURLOrName(parts[1]) case "pushremote": - pushRemoteURL, pushRemoteName := parseRemoteURLOrName(parts[1]) - cfg.PushRemoteURL = pushRemoteURL - cfg.PushRemoteName = pushRemoteName + cfg.PushRemoteURL, cfg.PushRemoteName = parseRemoteURLOrName(parts[1]) case "merge": cfg.MergeRef = parts[1] case MergeBaseConfig: @@ -449,7 +442,8 @@ func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string } // PushDefault returns the value of push.default in the config. If the value -// is not set, it returns "simple" (the default value). +// is not set, it returns "simple" (the default git value). See +// https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault func (c *Client) PushDefault(ctx context.Context) (string, error) { pushDefault, err := c.Config(ctx, "push.default") if err == nil { diff --git a/git/client_test.go b/git/client_test.go index 99f783881..872c8595b 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -14,6 +14,7 @@ import ( "strings" "testing" + "github.com/MakeNowJust/heredoc" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -763,7 +764,12 @@ func TestClientReadBranchConfig(t *testing.T) { name: "when the config is read, it should return the correct BranchConfig", cmds: mockedCommands{ `path/to/git config --get-regexp ^branch\.trunk\.(remote|merge|pushremote|gh-merge-base)$`: { - Stdout: "branch.trunk.remote upstream\nbranch.trunk.merge refs/heads/trunk\nbranch.trunk.pushremote origin\nbranch.trunk.gh-merge-base gh-merge-base\n", + Stdout: heredoc.Doc(` + branch.trunk.remote upstream + branch.trunk.merge refs/heads/trunk + branch.trunk.pushremote origin + branch.trunk.gh-merge-base gh-merge-base + `), }, }, branch: "trunk", @@ -1869,16 +1875,16 @@ func TestCommandMocking(t *testing.T) { jsonVar, ok := os.LookupEnv("GH_HELPER_PROCESS_RICH_COMMANDS") if !ok { fmt.Fprint(os.Stderr, "missing GH_HELPER_PROCESS_RICH_COMMANDS") - // Exit 1 is reserved for empty command responses by git. This can be desirable in some cases, - // so returning an arbitrary exit code to avoid suppressing this if an exit code 1 is allowed. + // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases, + // so this is returning a non-zero exit code to avoid suppressing this error for those use cases. os.Exit(16) } var commands mockedCommands if err := json.Unmarshal([]byte(jsonVar), &commands); err != nil { fmt.Fprint(os.Stderr, "failed to unmarshal GH_HELPER_PROCESS_RICH_COMMANDS") - // Exit 1 is reserved for empty command responses by git. This can be desirable in some cases, - // so returning an arbitrary exit code to avoid suppressing this if an exit code 1 is allowed. + // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases, + // so this is returning a non-zero exit code to avoid suppressing this error for those use cases. os.Exit(16) } @@ -1888,8 +1894,8 @@ func TestCommandMocking(t *testing.T) { commandResult, ok := commands[args(strings.Join(realArgs, " "))] if !ok { fmt.Fprintf(os.Stderr, "unexpected command: %s\n", strings.Join(realArgs, " ")) - // Exit 1 is reserved for empty command responses by git. This can be desirable in some cases, - // so returning an arbitrary exit code to avoid suppressing this if an exit code 1 is allowed. + // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases, + // so this is returning a non-zero exit code to avoid suppressing this error for those use cases. os.Exit(16) } diff --git a/git/objects.go b/git/objects.go index b57d79bec..9db528b8c 100644 --- a/git/objects.go +++ b/git/objects.go @@ -60,13 +60,14 @@ type Commit struct { Body string } +// These are the keys we read from the git branch. config. type BranchConfig struct { - RemoteName string - RemoteURL *url.URL + RemoteName string // .remote if string + RemoteURL *url.URL // .remote if url + MergeRef string // .merge + PushRemoteName string // .pushremote if string + PushRemoteURL *url.URL // .pushremote if url + // MergeBase is the optional base branch to target in a new PR if `--base` is not specified. MergeBase string - MergeRef string - // These are usually used to handle triangular workflows. - PushRemoteURL *url.URL - PushRemoteName string } diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 205939931..05ec24c9a 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -109,10 +109,10 @@ type PRRefs struct { BaseRepo ghrepo.Interface } -// GetPRLabel returns the string that the GitHub API uses to identify the PR. This is +// GetPRHeadLabel returns the string that the GitHub API uses to identify the PR. This is // either just the branch name or, if the PR is originating from a fork, the fork owner // and the branch name, like :. -func (s *PRRefs) GetPRLabel() string { +func (s *PRRefs) GetPRHeadLabel() string { if ghrepo.IsSame(s.HeadRepo, s.BaseRepo) { return s.BranchName } @@ -240,7 +240,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return nil, nil, err } - pr, err = findForBranch(httpClient, f.baseRefRepo, opts.BaseBranch, prRefs.GetPRLabel(), opts.States, fields.ToSlice()) + pr, err = findForBranch(httpClient, f.baseRefRepo, opts.BaseBranch, prRefs.GetPRHeadLabel(), opts.States, fields.ToSlice()) if err != nil { return pr, f.baseRefRepo, err } diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 1d5c1e3ac..a54412db5 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -419,7 +419,11 @@ func TestFind(t *testing.T) { wantRepo: "https://github.com/ORIGINOWNER/REPO", }, { - name: "the current branch is configured to push to and pull from a URL (upstream, in this example) that is different from what the repo is configured to push to and pull from (origin, in this example) and push.default = upstream, it finds the PR associated with the upstream repo and returns origin as the base repo", + // The current BRANCH is configured to push to and pull from a URL (upstream, in this example) + // which is different from what the REPO is configured to push to and pull from (origin, in this example) + // and push.default = upstream. It should find the PR associated with the upstream repo and return + // origin as the base repo + name: "when push.default = upstream and the current branch is configured to push/pull from a different remote than the repo", args: args{ selector: "", fields: []string{"id", "number"}, @@ -902,7 +906,7 @@ func TestParsePRRefs(t *testing.T) { } } -func TestPRRefs_GetPRLabel(t *testing.T) { +func TestPRRefs_GetPRHeadLabel(t *testing.T) { originRepo := ghrepo.New("ORIGINOWNER", "REPO") upstreamRepo := ghrepo.New("UPSTREAMOWNER", "REPO") tests := []struct { @@ -931,7 +935,7 @@ func TestPRRefs_GetPRLabel(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, tt.prRefs.GetPRLabel()) + assert.Equal(t, tt.want, tt.prRefs.GetPRHeadLabel()) }) } } From 359b4a1d637e31e86e0645385b04f9c74c89b944 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Tue, 28 Jan 2025 17:18:30 +0500 Subject: [PATCH 054/100] Stop progress indicator on failure (`gh run list --workflow`) --- pkg/cmd/run/list/list.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/run/list/list.go b/pkg/cmd/run/list/list.go index 793bfbb85..0b291fae5 100644 --- a/pkg/cmd/run/list/list.go +++ b/pkg/cmd/run/list/list.go @@ -131,6 +131,7 @@ func listRun(opts *ListOptions) error { filters.WorkflowID = workflow.ID filters.WorkflowName = workflow.Name } else { + opts.IO.StopProgressIndicator() return err } } From e8475d9fb8a3cd2193bbbecce3706a547dcdbd51 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Sat, 11 Jan 2025 20:00:44 -0500 Subject: [PATCH 055/100] Initial, working implementation --- pkg/cmd/repo/autolink/autolink.go | 2 + pkg/cmd/repo/autolink/list/list.go | 9 +- pkg/cmd/repo/autolink/shared/autolink.go | 7 ++ pkg/cmd/repo/autolink/view/http.go | 47 ++++++++++ pkg/cmd/repo/autolink/view/view.go | 107 +++++++++++++++++++++++ 5 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 pkg/cmd/repo/autolink/view/http.go create mode 100644 pkg/cmd/repo/autolink/view/view.go diff --git a/pkg/cmd/repo/autolink/autolink.go b/pkg/cmd/repo/autolink/autolink.go index 1fd218354..09b766b85 100644 --- a/pkg/cmd/repo/autolink/autolink.go +++ b/pkg/cmd/repo/autolink/autolink.go @@ -4,6 +4,7 @@ import ( "github.com/MakeNowJust/heredoc" cmdCreate "github.com/cli/cli/v2/pkg/cmd/repo/autolink/create" cmdList "github.com/cli/cli/v2/pkg/cmd/repo/autolink/list" + cmdView "github.com/cli/cli/v2/pkg/cmd/repo/autolink/view" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" ) @@ -24,6 +25,7 @@ func NewCmdAutolink(f *cmdutil.Factory) *cobra.Command { cmd.AddCommand(cmdList.NewCmdList(f, nil)) cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil)) + cmd.AddCommand(cmdView.NewCmdView(f, nil)) return cmd } diff --git a/pkg/cmd/repo/autolink/list/list.go b/pkg/cmd/repo/autolink/list/list.go index a68c03f3c..74267e7a4 100644 --- a/pkg/cmd/repo/autolink/list/list.go +++ b/pkg/cmd/repo/autolink/list/list.go @@ -15,13 +15,6 @@ import ( "github.com/spf13/cobra" ) -var autolinkFields = []string{ - "id", - "isAlphanumeric", - "keyPrefix", - "urlTemplate", -} - type listOptions struct { BaseRepo func() (ghrepo.Interface, error) Browser browser.Browser @@ -70,7 +63,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*listOptions) error) *cobra.Comman } cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List autolink references in the web browser") - cmdutil.AddJSONFlags(cmd, &opts.Exporter, autolinkFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) return cmd } diff --git a/pkg/cmd/repo/autolink/shared/autolink.go b/pkg/cmd/repo/autolink/shared/autolink.go index d79537c31..28e975e7c 100644 --- a/pkg/cmd/repo/autolink/shared/autolink.go +++ b/pkg/cmd/repo/autolink/shared/autolink.go @@ -9,6 +9,13 @@ type Autolink struct { URLTemplate string `json:"url_template"` } +var AutolinkFields = []string{ + "id", + "isAlphanumeric", + "keyPrefix", + "urlTemplate", +} + func (a *Autolink) ExportData(fields []string) map[string]interface{} { return cmdutil.StructExportData(a, fields) } diff --git a/pkg/cmd/repo/autolink/view/http.go b/pkg/cmd/repo/autolink/view/http.go new file mode 100644 index 000000000..d3fdfc86a --- /dev/null +++ b/pkg/cmd/repo/autolink/view/http.go @@ -0,0 +1,47 @@ +package view + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" +) + +type AutolinkViewer struct { + HTTPClient *http.Client +} + +func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, error) { + path := fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), id) + url := ghinstance.RESTPrefix(repo.RepoHost()) + path + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + resp, err := a.HTTPClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusNotFound { + return nil, fmt.Errorf("HTTP 404: Either no autolink with this ID exists for this repository or you are missing admin rights to the repository. (https://api.github.com/%s)", path) + } else if resp.StatusCode > 299 { + return nil, api.HandleHTTPError(resp) + } + + var autolink domain.Autolink + err = json.NewDecoder(resp.Body).Decode(&autolink) + + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + + return &autolink, nil +} diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go new file mode 100644 index 000000000..2d0cfd6c6 --- /dev/null +++ b/pkg/cmd/repo/autolink/view/view.go @@ -0,0 +1,107 @@ +package view + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type viewOptions struct { + BaseRepo func() (ghrepo.Interface, error) + Browser browser.Browser + AutolinkClient AutolinkViewClient + IO *iostreams.IOStreams + Exporter cmdutil.Exporter + + ID string +} + +type AutolinkViewClient interface { + View(repo ghrepo.Interface, id string) (*domain.Autolink, error) +} + +func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Command { + opts := &viewOptions{ + Browser: f.Browser, + IO: f.IOStreams, + } + + cmd := &cobra.Command{ + Use: "view ", + Short: "View an autolink reference", + Long: heredoc.Docf(` + View an autolink reference for a repository. + + Information about autolinks is only available to repository administrators. + `), + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.BaseRepo = f.BaseRepo + httpClient, err := f.HttpClient() + + if err != nil { + return err + } + + opts.ID = args[0] + + if err != nil { + return err + } + + opts.AutolinkClient = &AutolinkViewer{HTTPClient: httpClient} + + if runF != nil { + return runF(opts) + } + + return viewRun(opts) + }, + } + + cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) + + return cmd +} + +func viewRun(opts *viewOptions) error { + repo, err := opts.BaseRepo() + if err != nil { + return err + } + cs := opts.IO.ColorScheme() + + autolink, err := opts.AutolinkClient.View(repo, opts.ID) + + if err != nil { + return fmt.Errorf("%s %w", cs.Red("error viewing autolink:"), err) + } + + if opts.Exporter != nil { + return opts.Exporter.Write(opts.IO, autolink) + } + + msg := heredoc.Docf(` + Autolink in %s + + ID: %d + Key Prefix: %s + URL Template: %s + Alphanumeric: %t + `, + ghrepo.FullName(repo), + autolink.ID, + autolink.KeyPrefix, + autolink.URLTemplate, + autolink.IsAlphanumeric, + ) + fmt.Fprint(opts.IO.Out, msg) + + return nil +} From f941608091c29411cafcfb5173d6f29ab5fab6d4 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Mon, 13 Jan 2025 09:01:12 -0500 Subject: [PATCH 056/100] Partial testing for view --- pkg/cmd/repo/autolink/view/view_test.go | 193 ++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 pkg/cmd/repo/autolink/view/view_test.go diff --git a/pkg/cmd/repo/autolink/view/view_test.go b/pkg/cmd/repo/autolink/view/view_test.go new file mode 100644 index 000000000..4ace02ea5 --- /dev/null +++ b/pkg/cmd/repo/autolink/view/view_test.go @@ -0,0 +1,193 @@ +package view + +import ( + "bytes" + "net/http" + "testing" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/jsonfieldstest" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestJSONFields(t *testing.T) { + jsonfieldstest.ExpectCommandToSupportJSONFields(t, NewCmdView, []string{ + "id", + "isAlphanumeric", + "keyPrefix", + "urlTemplate", + }) +} + +func TestNewCmdView(t *testing.T) { + tests := []struct { + name string + input string + output viewOptions + wantErr bool + wantExporter bool + errMsg string + }{ + { + name: "no argument", + input: "", + wantErr: true, + errMsg: "accepts 1 arg(s), received 0", + }, + { + name: "id provided", + input: "123", + output: viewOptions{ID: "123"}, + }, + { + name: "json flag", + input: "123 --json id", + output: viewOptions{}, + wantExporter: true, + }, + { + name: "invalid json flag", + input: "123 --json invalid", + output: viewOptions{}, + wantErr: true, + errMsg: "Unknown JSON field: \"invalid\"\nAvailable fields:\n id\n isAlphanumeric\n keyPrefix\n urlTemplate", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + f.HttpClient = func() (*http.Client, error) { + return &http.Client{}, nil + } + + argv, err := shlex.Split(tt.input) + require.NoError(t, err) + + var gotOpts *viewOptions + cmd := NewCmdView(f, func(opts *viewOptions) error { + gotOpts = opts + return nil + }) + + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(&bytes.Buffer{}) + cmd.SetErr(&bytes.Buffer{}) + + _, err = cmd.ExecuteC() + if tt.wantErr { + require.EqualError(t, err, tt.errMsg) + } else { + require.NoError(t, err) + assert.Equal(t, tt.wantExporter, gotOpts.Exporter != nil) + } + }) + } +} + +type stubAutoLinkViewer struct { + autolink *domain.Autolink + err error +} + +func (g stubAutoLinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, error) { + return g.autolink, g.err +} + +type testAutolinkClientViewError struct{} + +func (e testAutolinkClientViewError) Error() string { + return "autolink client view error" +} + +func TestViewRun(t *testing.T) { + tests := []struct { + name string + opts *viewOptions + stubViewer stubAutoLinkViewer + expectedErr error + wantStdout string + wantStderr string + }{ + { + name: "view", + opts: &viewOptions{ + ID: "1", + }, + stubViewer: stubAutoLinkViewer{ + autolink: &domain.Autolink{ + ID: 1, + KeyPrefix: "TICKET-", + URLTemplate: "https://example.com/TICKET?query=", + IsAlphanumeric: true, + }, + }, + wantStdout: heredoc.Doc(` + Autolink in OWNER/REPO + + ID: 1 + Key Prefix: TICKET- + URL Template: https://example.com/TICKET?query= + Alphanumeric: true + `), + }, + { + name: "view json", + opts: &viewOptions{ + Exporter: func() cmdutil.Exporter { + exporter := cmdutil.NewJSONExporter() + exporter.SetFields([]string{"id"}) + return exporter + }(), + }, + stubViewer: stubAutoLinkViewer{ + autolink: &domain.Autolink{ + ID: 1, + KeyPrefix: "TICKET-", + URLTemplate: "https://example.com/TICKET?query=", + IsAlphanumeric: true, + }, + }, + wantStdout: "{\"id\":1}\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + + opts := tt.opts + opts.IO = ios + opts.Browser = &browser.Stub{} + + opts.IO = ios + opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } + + opts.AutolinkClient = &tt.stubViewer + err := viewRun(opts) + + if tt.expectedErr != nil { + require.Error(t, err) + require.ErrorIs(t, err, tt.expectedErr) + } else { + require.NoError(t, err) + assert.Equal(t, tt.wantStdout, stdout.String()) + } + + if tt.wantStderr != "" { + assert.Equal(t, tt.wantStderr, stderr.String()) + } + }) + } +} From 11ce0c661c494fd3ef356a6de4f898934802b48e Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Mon, 13 Jan 2025 09:10:12 -0500 Subject: [PATCH 057/100] Updates following renaming domain to shared --- pkg/cmd/repo/autolink/list/list.go | 2 +- pkg/cmd/repo/autolink/view/http.go | 6 +++--- pkg/cmd/repo/autolink/view/view.go | 6 +++--- pkg/cmd/repo/autolink/view/view_test.go | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/repo/autolink/list/list.go b/pkg/cmd/repo/autolink/list/list.go index 74267e7a4..123c5321e 100644 --- a/pkg/cmd/repo/autolink/list/list.go +++ b/pkg/cmd/repo/autolink/list/list.go @@ -63,7 +63,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*listOptions) error) *cobra.Comman } cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List autolink references in the web browser") - cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.AutolinkFields) return cmd } diff --git a/pkg/cmd/repo/autolink/view/http.go b/pkg/cmd/repo/autolink/view/http.go index d3fdfc86a..ddba23f6d 100644 --- a/pkg/cmd/repo/autolink/view/http.go +++ b/pkg/cmd/repo/autolink/view/http.go @@ -8,14 +8,14 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared" ) type AutolinkViewer struct { HTTPClient *http.Client } -func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, error) { +func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*shared.Autolink, error) { path := fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), id) url := ghinstance.RESTPrefix(repo.RepoHost()) + path req, err := http.NewRequest(http.MethodGet, url, nil) @@ -35,7 +35,7 @@ func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolin return nil, api.HandleHTTPError(resp) } - var autolink domain.Autolink + var autolink shared.Autolink err = json.NewDecoder(resp.Body).Decode(&autolink) if err != nil { diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go index 2d0cfd6c6..38a2590a2 100644 --- a/pkg/cmd/repo/autolink/view/view.go +++ b/pkg/cmd/repo/autolink/view/view.go @@ -6,7 +6,7 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/spf13/cobra" @@ -23,7 +23,7 @@ type viewOptions struct { } type AutolinkViewClient interface { - View(repo ghrepo.Interface, id string) (*domain.Autolink, error) + View(repo ghrepo.Interface, id string) (*shared.Autolink, error) } func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Command { @@ -65,7 +65,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Comman }, } - cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.AutolinkFields) return cmd } diff --git a/pkg/cmd/repo/autolink/view/view_test.go b/pkg/cmd/repo/autolink/view/view_test.go index 4ace02ea5..849a2828a 100644 --- a/pkg/cmd/repo/autolink/view/view_test.go +++ b/pkg/cmd/repo/autolink/view/view_test.go @@ -8,7 +8,7 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/jsonfieldstest" @@ -97,11 +97,11 @@ func TestNewCmdView(t *testing.T) { } type stubAutoLinkViewer struct { - autolink *domain.Autolink + autolink *shared.Autolink err error } -func (g stubAutoLinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, error) { +func (g stubAutoLinkViewer) View(repo ghrepo.Interface, id string) (*shared.Autolink, error) { return g.autolink, g.err } @@ -126,7 +126,7 @@ func TestViewRun(t *testing.T) { ID: "1", }, stubViewer: stubAutoLinkViewer{ - autolink: &domain.Autolink{ + autolink: &shared.Autolink{ ID: 1, KeyPrefix: "TICKET-", URLTemplate: "https://example.com/TICKET?query=", @@ -152,7 +152,7 @@ func TestViewRun(t *testing.T) { }(), }, stubViewer: stubAutoLinkViewer{ - autolink: &domain.Autolink{ + autolink: &shared.Autolink{ ID: 1, KeyPrefix: "TICKET-", URLTemplate: "https://example.com/TICKET?query=", From 58d84a4088a8cff75414dc4ce6129d3dcb6224bb Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Tue, 28 Jan 2025 12:03:03 -0500 Subject: [PATCH 058/100] Shorten description --- pkg/cmd/repo/autolink/view/view.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go index 38a2590a2..064fc22ca 100644 --- a/pkg/cmd/repo/autolink/view/view.go +++ b/pkg/cmd/repo/autolink/view/view.go @@ -35,12 +35,8 @@ func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Comman cmd := &cobra.Command{ Use: "view ", Short: "View an autolink reference", - Long: heredoc.Docf(` - View an autolink reference for a repository. - - Information about autolinks is only available to repository administrators. - `), - Args: cobra.ExactArgs(1), + Long: "View an autolink reference for a repository.", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.BaseRepo = f.BaseRepo httpClient, err := f.HttpClient() From 32ee4db48444c8ee0229a0e90056cf50f6128ca5 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Tue, 28 Jan 2025 13:33:50 -0500 Subject: [PATCH 059/100] Autolink view HTTP tests --- pkg/cmd/repo/autolink/list/http_test.go | 2 +- pkg/cmd/repo/autolink/view/http.go | 1 - pkg/cmd/repo/autolink/view/http_test.go | 86 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 pkg/cmd/repo/autolink/view/http_test.go diff --git a/pkg/cmd/repo/autolink/list/http_test.go b/pkg/cmd/repo/autolink/list/http_test.go index 0fa918f61..65289c419 100644 --- a/pkg/cmd/repo/autolink/list/http_test.go +++ b/pkg/cmd/repo/autolink/list/http_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestAutoLinkLister_List(t *testing.T) { +func TestAutolinkLister_List(t *testing.T) { tests := []struct { name string repo ghrepo.Interface diff --git a/pkg/cmd/repo/autolink/view/http.go b/pkg/cmd/repo/autolink/view/http.go index ddba23f6d..8dd6dc12d 100644 --- a/pkg/cmd/repo/autolink/view/http.go +++ b/pkg/cmd/repo/autolink/view/http.go @@ -39,7 +39,6 @@ func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*shared.Autolin err = json.NewDecoder(resp.Body).Decode(&autolink) if err != nil { - fmt.Println(err.Error()) return nil, err } diff --git a/pkg/cmd/repo/autolink/view/http_test.go b/pkg/cmd/repo/autolink/view/http_test.go new file mode 100644 index 000000000..5475c1f8a --- /dev/null +++ b/pkg/cmd/repo/autolink/view/http_test.go @@ -0,0 +1,86 @@ +package view + +import ( + "fmt" + "net/http" + "testing" + + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAutolinkViewer_View(t *testing.T) { + repo := ghrepo.New("OWNER", "REPO") + testAutolink := shared.Autolink{ + ID: 123, + KeyPrefix: "TICKET-", + URLTemplate: "https://example.com/TICKET?query=", + IsAlphanumeric: true, + } + + tests := []struct { + name string + id string + stubStatus int + stubRespJSON string + + expectedAutolink *shared.Autolink + expectErr bool + expectedErrMsg string + }{ + { + name: "200 successful view", + id: "123", + stubStatus: 200, + stubRespJSON: `{ + "id": 123, + "key_prefix": "TICKET-", + "url_template": "https://example.com/TICKET?query=", + "is_alphanumeric": true + }`, + expectedAutolink: &testAutolink, + }, + { + name: "404 repo or autolink not found", + id: "123", + stubStatus: 404, + stubRespJSON: `{ + "message": "Not Found", + "documentation_url": "https://docs.github.com/rest/repos/autolinks#get-an-autolink-reference-of-a-repository", + "status": "404" + }`, + expectErr: true, + expectedErrMsg: "HTTP 404: Either no autolink with this ID exists for this repository or you are missing admin rights to the repository. (https://api.github.com/repos/OWNER/REPO/autolinks/123)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST( + http.MethodGet, + fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), tt.id), + ), + httpmock.StatusStringResponse(tt.stubStatus, tt.stubRespJSON), + ) + defer reg.Verify(t) + + autolinkCreator := &AutolinkViewer{ + HTTPClient: &http.Client{Transport: reg}, + } + + autolink, err := autolinkCreator.View(repo, tt.id) + + if tt.expectErr { + require.EqualError(t, err, tt.expectedErrMsg) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedAutolink, autolink) + } + }) + } +} From 8f55dbcf61dcda17653a2cb7755a2cf0097a8ac9 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Tue, 28 Jan 2025 14:07:48 -0500 Subject: [PATCH 060/100] Error case test for viewRun --- pkg/cmd/repo/autolink/view/view_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/cmd/repo/autolink/view/view_test.go b/pkg/cmd/repo/autolink/view/view_test.go index 849a2828a..3951d0cca 100644 --- a/pkg/cmd/repo/autolink/view/view_test.go +++ b/pkg/cmd/repo/autolink/view/view_test.go @@ -161,6 +161,16 @@ func TestViewRun(t *testing.T) { }, wantStdout: "{\"id\":1}\n", }, + { + name: "client error", + opts: &viewOptions{}, + stubViewer: stubAutoLinkViewer{ + autolink: nil, + err: testAutolinkClientViewError{}, + }, + expectedErr: testAutolinkClientViewError{}, + wantStderr: "", + }, } for _, tt := range tests { From de7c40789ef082fbf13f4a6cdaa504c95c176fe9 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Wed, 29 Jan 2025 08:22:32 +0500 Subject: [PATCH 061/100] Use `defer` instead --- pkg/cmd/run/list/list.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/run/list/list.go b/pkg/cmd/run/list/list.go index 0b291fae5..3113fdabd 100644 --- a/pkg/cmd/run/list/list.go +++ b/pkg/cmd/run/list/list.go @@ -119,6 +119,8 @@ func listRun(opts *ListOptions) error { } opts.IO.StartProgressIndicator() + defer opts.IO.StopProgressIndicator() + if opts.WorkflowSelector != "" { // initially the workflow state is limited to 'active' states := []workflowShared.WorkflowState{workflowShared.Active} @@ -131,7 +133,6 @@ func listRun(opts *ListOptions) error { filters.WorkflowID = workflow.ID filters.WorkflowName = workflow.Name } else { - opts.IO.StopProgressIndicator() return err } } From 8bab4b47535560f4bf86fa56eab0bd5fdfb97a60 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Wed, 29 Jan 2025 18:30:43 +0500 Subject: [PATCH 062/100] [gh project item-list] Add `iterationId` field in ProjectV2ItemFieldIterationValue --- pkg/cmd/project/shared/queries/queries.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/project/shared/queries/queries.go b/pkg/cmd/project/shared/queries/queries.go index 080a43097..5855e906e 100644 --- a/pkg/cmd/project/shared/queries/queries.go +++ b/pkg/cmd/project/shared/queries/queries.go @@ -235,10 +235,11 @@ type FieldValueNodes struct { Field ProjectField } `graphql:"... on ProjectV2ItemFieldDateValue"` ProjectV2ItemFieldIterationValue struct { - Title string - StartDate string - Duration int - Field ProjectField + Title string + StartDate string + Duration int + Field ProjectField + IterationId string } `graphql:"... on ProjectV2ItemFieldIterationValue"` ProjectV2ItemFieldLabelValue struct { Labels struct { @@ -1488,9 +1489,10 @@ func projectFieldValueData(v FieldValueNodes) interface{} { return v.ProjectV2ItemFieldDateValue.Date case "ProjectV2ItemFieldIterationValue": return map[string]interface{}{ - "title": v.ProjectV2ItemFieldIterationValue.Title, - "startDate": v.ProjectV2ItemFieldIterationValue.StartDate, - "duration": v.ProjectV2ItemFieldIterationValue.Duration, + "title": v.ProjectV2ItemFieldIterationValue.Title, + "startDate": v.ProjectV2ItemFieldIterationValue.StartDate, + "duration": v.ProjectV2ItemFieldIterationValue.Duration, + "iterationId": v.ProjectV2ItemFieldIterationValue.IterationId, } case "ProjectV2ItemFieldNumberValue": return v.ProjectV2ItemFieldNumberValue.Number From 8b7d6d1b3f6a36f00b46af2bd53b83783a735be0 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Wed, 29 Jan 2025 09:34:29 -0500 Subject: [PATCH 063/100] Remove unused test field --- pkg/cmd/repo/autolink/view/view_test.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/view_test.go b/pkg/cmd/repo/autolink/view/view_test.go index 3951d0cca..f66901f59 100644 --- a/pkg/cmd/repo/autolink/view/view_test.go +++ b/pkg/cmd/repo/autolink/view/view_test.go @@ -118,7 +118,6 @@ func TestViewRun(t *testing.T) { stubViewer stubAutoLinkViewer expectedErr error wantStdout string - wantStderr string }{ { name: "view", @@ -169,7 +168,6 @@ func TestViewRun(t *testing.T) { err: testAutolinkClientViewError{}, }, expectedErr: testAutolinkClientViewError{}, - wantStderr: "", }, } @@ -194,10 +192,6 @@ func TestViewRun(t *testing.T) { require.NoError(t, err) assert.Equal(t, tt.wantStdout, stdout.String()) } - - if tt.wantStderr != "" { - assert.Equal(t, tt.wantStderr, stderr.String()) - } }) } } From 21a5267be4405bf059a359a434d31ecb3a3d66a0 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Wed, 29 Jan 2025 09:35:05 -0500 Subject: [PATCH 064/100] Remove unused test field --- pkg/cmd/repo/autolink/view/view_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/view_test.go b/pkg/cmd/repo/autolink/view/view_test.go index f66901f59..bc0a1bf7f 100644 --- a/pkg/cmd/repo/autolink/view/view_test.go +++ b/pkg/cmd/repo/autolink/view/view_test.go @@ -173,7 +173,7 @@ func TestViewRun(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ios, _, stdout, stderr := iostreams.Test() + ios, _, stdout, _ := iostreams.Test() opts := tt.opts opts.IO = ios @@ -187,7 +187,7 @@ func TestViewRun(t *testing.T) { if tt.expectedErr != nil { require.Error(t, err) - require.ErrorIs(t, err, tt.expectedErr) + assert.ErrorIs(t, err, tt.expectedErr) } else { require.NoError(t, err) assert.Equal(t, tt.wantStdout, stdout.String()) From af538b277d8be77f6cf8e9de0c74469a26f82a1d Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Wed, 29 Jan 2025 10:10:42 -0500 Subject: [PATCH 065/100] Expand test case - add autolink view case for numeric - simplifying local variable usage --- pkg/cmd/repo/autolink/view/http_test.go | 32 ++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/http_test.go b/pkg/cmd/repo/autolink/view/http_test.go index 5475c1f8a..2402fb2d7 100644 --- a/pkg/cmd/repo/autolink/view/http_test.go +++ b/pkg/cmd/repo/autolink/view/http_test.go @@ -14,12 +14,6 @@ import ( func TestAutolinkViewer_View(t *testing.T) { repo := ghrepo.New("OWNER", "REPO") - testAutolink := shared.Autolink{ - ID: 123, - KeyPrefix: "TICKET-", - URLTemplate: "https://example.com/TICKET?query=", - IsAlphanumeric: true, - } tests := []struct { name string @@ -32,7 +26,7 @@ func TestAutolinkViewer_View(t *testing.T) { expectedErrMsg string }{ { - name: "200 successful view", + name: "200 successful alphanumeric view", id: "123", stubStatus: 200, stubRespJSON: `{ @@ -41,7 +35,29 @@ func TestAutolinkViewer_View(t *testing.T) { "url_template": "https://example.com/TICKET?query=", "is_alphanumeric": true }`, - expectedAutolink: &testAutolink, + expectedAutolink: &shared.Autolink{ + ID: 123, + KeyPrefix: "TICKET-", + URLTemplate: "https://example.com/TICKET?query=", + IsAlphanumeric: true, + }, + }, + { + name: "200 successful numeric view", + id: "124", + stubStatus: 200, + stubRespJSON: `{ + "id": 124, + "key_prefix": "TICKET-", + "url_template": "https://example.com/TICKET?query=", + "is_alphanumeric": false + }`, + expectedAutolink: &shared.Autolink{ + ID: 124, + KeyPrefix: "TICKET-", + URLTemplate: "https://example.com/TICKET?query=", + IsAlphanumeric: false, + }, }, { name: "404 repo or autolink not found", From fe21e10ae8b42315575fe03f24ec967f4c65fab3 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Wed, 29 Jan 2025 10:16:02 -0500 Subject: [PATCH 066/100] Keeping autolink IDs the same in all test cases --- pkg/cmd/repo/autolink/view/http_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/http_test.go b/pkg/cmd/repo/autolink/view/http_test.go index 2402fb2d7..5bfe9369f 100644 --- a/pkg/cmd/repo/autolink/view/http_test.go +++ b/pkg/cmd/repo/autolink/view/http_test.go @@ -44,16 +44,16 @@ func TestAutolinkViewer_View(t *testing.T) { }, { name: "200 successful numeric view", - id: "124", + id: "123", stubStatus: 200, stubRespJSON: `{ - "id": 124, + "id": 123, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=", "is_alphanumeric": false }`, expectedAutolink: &shared.Autolink{ - ID: 124, + ID: 123, KeyPrefix: "TICKET-", URLTemplate: "https://example.com/TICKET?query=", IsAlphanumeric: false, From 077f064f4a360600061db0f2a93553641a4ce053 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Wed, 29 Jan 2025 10:21:11 -0500 Subject: [PATCH 067/100] Remove v1 project 'add to board' automation from prauto workflow --- .github/workflows/prauto.yml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 2596fa76c..67602b2ee 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -43,26 +43,12 @@ jobs: -q ".data.repository.project.columns.nodes[] | select(.name | startswith(\"$1\")) | .id" } - addToBoard () { - gh api graphql --silent -f query=' - mutation($colID:ID!, $prID:ID!) { addProjectCard(input: { projectColumnId: $colID, contentId: $prID }) { clientMutationId } } - ' -f colID="$(colID "Needs review")" -f prID="$PRID" - } - if [ "$PR_AUTHOR_TYPE" = "Bot" ] || gh api orgs/cli/public_members/$PRAUTHOR --silent 2>/dev/null then if [ "$PR_AUTHOR_TYPE" != "Bot" ] then gh pr edit $PRNUM --add-assignee $PRAUTHOR fi - if ! errtext="$(addToBoard 2>&1)" - then - cat <<<"$errtext" >&2 - if ! grep -iq 'project already has the associated issue' <<<"$errtext" - then - exit 1 - fi - fi exit 0 fi @@ -86,5 +72,4 @@ jobs: commentPR "Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message." fi - addToBoard exit 0 From e4d720ba228d8c4221049ca30e14b05a4dc30c17 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Wed, 29 Jan 2025 11:12:28 -0500 Subject: [PATCH 068/100] Remove redundant error check --- pkg/cmd/repo/autolink/view/view.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go index 064fc22ca..f892c0724 100644 --- a/pkg/cmd/repo/autolink/view/view.go +++ b/pkg/cmd/repo/autolink/view/view.go @@ -38,19 +38,13 @@ func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Comman Long: "View an autolink reference for a repository.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - opts.BaseRepo = f.BaseRepo httpClient, err := f.HttpClient() - if err != nil { return err } + opts.BaseRepo = f.BaseRepo opts.ID = args[0] - - if err != nil { - return err - } - opts.AutolinkClient = &AutolinkViewer{HTTPClient: httpClient} if runF != nil { From e0f624ba240546f980f1190a93dd0264a84fa2c8 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Wed, 29 Jan 2025 11:41:23 -0800 Subject: [PATCH 069/100] Rename PRRefs to PullRequestRefs and PR comment cleanup --- git/client.go | 2 +- git/client_test.go | 2 +- pkg/cmd/pr/create/create.go | 2 +- pkg/cmd/pr/shared/finder.go | 12 ++++---- pkg/cmd/pr/shared/finder_test.go | 50 ++++++++++++++++---------------- pkg/cmd/pr/status/status.go | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/git/client.go b/git/client.go index 30cb4306d..11a2e2e20 100644 --- a/git/client.go +++ b/git/client.go @@ -474,7 +474,7 @@ func (c *Client) RemotePushDefault(ctx context.Context) (string, error) { } // ParsePushRevision gets the value of the @{push} revision syntax -// An error here doesn't necessarily mean something are broke, but may mean that the @{push} +// An error here doesn't necessarily mean something is broken, but may mean that the @{push} // revision syntax couldn't be resolved, such as in non-centralized workflows with // push.default = simple. Downstream consumers should consider how to handle this error. func (c *Client) ParsePushRevision(ctx context.Context, branch string) (string, error) { diff --git a/git/client_test.go b/git/client_test.go index 872c8595b..9fa076199 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -769,7 +769,7 @@ func TestClientReadBranchConfig(t *testing.T) { branch.trunk.merge refs/heads/trunk branch.trunk.pushremote origin branch.trunk.gh-merge-base gh-merge-base - `), + `), }, }, branch: "trunk", diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 6c9d2a2af..b2abe0938 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -518,7 +518,7 @@ func initDefaultTitleBody(ctx CreateContext, state *shared.IssueMetadataState, u return nil } -// TODO: Replace with the finder's PRRefs struct +// TODO: Replace with the finder's PullRequestRefs struct // trackingRef represents a ref for a remote tracking branch. type trackingRef struct { remoteName string diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 05ec24c9a..e4f89502c 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -103,7 +103,7 @@ type FindOptions struct { // // A ref is described as "remoteName/branchName", so // baseRepoName/baseBranchName -----PR-----> headRepoName/headBranchName -type PRRefs struct { +type PullRequestRefs struct { BranchName string HeadRepo ghrepo.Interface BaseRepo ghrepo.Interface @@ -112,7 +112,7 @@ type PRRefs struct { // GetPRHeadLabel returns the string that the GitHub API uses to identify the PR. This is // either just the branch name or, if the PR is originating from a fork, the fork owner // and the branch name, like :. -func (s *PRRefs) GetPRHeadLabel() string { +func (s *PullRequestRefs) GetPRHeadLabel() string { if ghrepo.IsSame(s.HeadRepo, s.BaseRepo) { return s.BranchName } @@ -227,7 +227,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return nil, nil, err } - // Suppressing these errors as we have other means of computing the PRRefs when these fail. + // Suppressing these errors as we have other means of computing the PullRequestRefs when these fail. parsedPushRevision, _ := f.parsePushRevision(f.branchName) remotePushDefault, err := f.remotePushDefault() @@ -302,8 +302,8 @@ func (f *finder) parseURL(prURL string) (ghrepo.Interface, int, error) { return repo, prNumber, nil } -func ParsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, remotePushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PRRefs, error) { - prRefs := PRRefs{ +func ParsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsedPushRevision string, pushDefault string, remotePushDefault string, baseRefRepo ghrepo.Interface, rems remotes.Remotes) (PullRequestRefs, error) { + prRefs := PullRequestRefs{ BaseRepo: baseRefRepo, } @@ -323,7 +323,7 @@ func ParsePRRefs(currentBranchName string, branchConfig git.BranchConfig, parsed for i, r := range rems { remoteNames[i] = r.Name } - return PRRefs{}, fmt.Errorf("no remote for %q found in %q", parsedPushRevision, strings.Join(remoteNames, ", ")) + return PullRequestRefs{}, fmt.Errorf("no remote for %q found in %q", parsedPushRevision, strings.Join(remoteNames, ", ")) } // We assume the PR's branch name is the same as whatever f.BranchFn() returned earlier diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index a54412db5..694e0c20d 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -676,15 +676,15 @@ func TestParsePRRefs(t *testing.T) { currentBranchName string baseRefRepo ghrepo.Interface rems context.Remotes - wantPRRefs PRRefs + wantPRRefs PullRequestRefs wantErr error }{ { - name: "When the branch is called 'blueberries' with an empty branch config, it returns the correct PRRefs", + name: "When the branch is called 'blueberries' with an empty branch config, it returns the correct PullRequestRefs", branchConfig: git.BranchConfig{}, currentBranchName: "blueberries", baseRefRepo: remoteOrigin.Repo, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteOrigin.Repo, @@ -692,11 +692,11 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the branch is called 'otherBranch' with an empty branch config, it returns the correct PRRefs", + name: "When the branch is called 'otherBranch' with an empty branch config, it returns the correct PullRequestRefs", branchConfig: git.BranchConfig{}, currentBranchName: "otherBranch", baseRefRepo: remoteOrigin.Repo, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "otherBranch", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteOrigin.Repo, @@ -711,7 +711,7 @@ func TestParsePRRefs(t *testing.T) { rems: context.Remotes{ &remoteOrigin, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "pushBranch", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteOrigin.Repo, @@ -727,7 +727,7 @@ func TestParsePRRefs(t *testing.T) { &remoteUpstream, &remoteOther, }, - wantPRRefs: PRRefs{}, + wantPRRefs: PullRequestRefs{}, wantErr: fmt.Errorf("no remote for %q found in %q", "origin/differentPushBranch", "upstream, other"), }, { @@ -738,7 +738,7 @@ func TestParsePRRefs(t *testing.T) { rems: context.Remotes{ &remoteOther, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "pushBranch", HeadRepo: remoteOther.Repo, BaseRepo: remoteOrigin.Repo, @@ -746,7 +746,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the push remote is the same as the baseRepo, it returns the baseRepo as the PRRefs HeadRepo", + name: "When the push remote is the same as the baseRepo, it returns the baseRepo as the PullRequestRefs HeadRepo", branchConfig: git.BranchConfig{ PushRemoteName: remoteOrigin.Remote.Name, }, @@ -756,7 +756,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteOrigin.Repo, @@ -764,7 +764,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the push remote is different from the baseRepo, it returns the push remote repo as the PRRefs HeadRepo", + name: "When the push remote is different from the baseRepo, it returns the push remote repo as the PullRequestRefs HeadRepo", branchConfig: git.BranchConfig{ PushRemoteName: remoteOrigin.Remote.Name, }, @@ -774,7 +774,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteUpstream.Repo, @@ -782,7 +782,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the push remote defined by a URL and the baseRepo is different from the push remote, it returns the push remote repo as the PRRefs HeadRepo", + name: "When the push remote defined by a URL and the baseRepo is different from the push remote, it returns the push remote repo as the PullRequestRefs HeadRepo", branchConfig: git.BranchConfig{ PushRemoteURL: remoteOrigin.Remote.FetchURL, }, @@ -792,7 +792,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteOrigin.Repo, BaseRepo: remoteUpstream.Repo, @@ -812,7 +812,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blue-upstream-berries", HeadRepo: remoteUpstream.Repo, BaseRepo: remoteOrigin.Repo, @@ -832,7 +832,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blue-upstream-berries", HeadRepo: remoteUpstream.Repo, BaseRepo: remoteOrigin.Repo, @@ -840,7 +840,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When remote.pushDefault is set, it returns the correct PRRefs", + name: "When remote.pushDefault is set, it returns the correct PullRequestRefs", branchConfig: git.BranchConfig{}, remotePushDefault: remoteUpstream.Remote.Name, currentBranchName: "blueberries", @@ -849,7 +849,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteUpstream.Repo, BaseRepo: remoteOrigin.Repo, @@ -857,7 +857,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the remote name is set on the branch, it returns the correct PRRefs", + name: "When the remote name is set on the branch, it returns the correct PullRequestRefs", branchConfig: git.BranchConfig{ RemoteName: remoteUpstream.Remote.Name, }, @@ -867,7 +867,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteUpstream.Repo, BaseRepo: remoteOrigin.Repo, @@ -875,7 +875,7 @@ func TestParsePRRefs(t *testing.T) { wantErr: nil, }, { - name: "When the remote URL is set on the branch, it returns the correct PRRefs", + name: "When the remote URL is set on the branch, it returns the correct PullRequestRefs", branchConfig: git.BranchConfig{ RemoteURL: remoteUpstream.Remote.FetchURL, }, @@ -885,7 +885,7 @@ func TestParsePRRefs(t *testing.T) { &remoteOrigin, &remoteUpstream, }, - wantPRRefs: PRRefs{ + wantPRRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: remoteUpstream.Repo, BaseRepo: remoteOrigin.Repo, @@ -911,12 +911,12 @@ func TestPRRefs_GetPRHeadLabel(t *testing.T) { upstreamRepo := ghrepo.New("UPSTREAMOWNER", "REPO") tests := []struct { name string - prRefs PRRefs + prRefs PullRequestRefs want string }{ { name: "When the HeadRepo and BaseRepo match, it returns the branch name", - prRefs: PRRefs{ + prRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: originRepo, BaseRepo: originRepo, @@ -925,7 +925,7 @@ func TestPRRefs_GetPRHeadLabel(t *testing.T) { }, { name: "When the HeadRepo and BaseRepo do not match, it returns the prepended HeadRepo owner to the branch name", - prRefs: PRRefs{ + prRefs: PullRequestRefs{ BranchName: "blueberries", HeadRepo: originRepo, BaseRepo: upstreamRepo, diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 996a178e1..3877fb1cf 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -107,7 +107,7 @@ func statusRun(opts *StatusOptions) error { if err != nil { return err } - // Suppressing these errors as we have other means of computing the PRRefs when these fail. + // Suppressing these errors as we have other means of computing the PullRequestRefs when these fail. parsedPushRevision, _ := opts.GitClient.ParsePushRevision(ctx, currentBranchName) remotePushDefault, err := opts.GitClient.RemotePushDefault(ctx) From b23cf6e8d768d9a929950c8c33eff780af79272b Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Wed, 29 Jan 2025 16:24:47 -0500 Subject: [PATCH 070/100] Update formatting for autolink output --- pkg/cmd/repo/autolink/create/create.go | 4 ++-- pkg/cmd/repo/autolink/list/list.go | 4 +++- pkg/cmd/repo/autolink/view/view.go | 29 +++++++++++++------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/repo/autolink/create/create.go b/pkg/cmd/repo/autolink/create/create.go index ac3035c79..aad71a941 100644 --- a/pkg/cmd/repo/autolink/create/create.go +++ b/pkg/cmd/repo/autolink/create/create.go @@ -109,9 +109,9 @@ func createRun(opts *createOptions) error { cs := opts.IO.ColorScheme() fmt.Fprintf(opts.IO.Out, - "%s Created repository autolink %d on %s\n", + "%s Created repository autolink %s on %s\n", cs.SuccessIconWithColor(cs.Green), - autolink.ID, + cs.Cyanf("%d", autolink.ID), ghrepo.FullName(repo)) return nil diff --git a/pkg/cmd/repo/autolink/list/list.go b/pkg/cmd/repo/autolink/list/list.go index 123c5321e..402479c44 100644 --- a/pkg/cmd/repo/autolink/list/list.go +++ b/pkg/cmd/repo/autolink/list/list.go @@ -104,8 +104,10 @@ func listRun(opts *listOptions) error { tp := tableprinter.New(opts.IO, tableprinter.WithHeader("ID", "KEY PREFIX", "URL TEMPLATE", "ALPHANUMERIC")) + cs := opts.IO.ColorScheme() + for _, autolink := range autolinks { - tp.AddField(fmt.Sprintf("%d", autolink.ID)) + tp.AddField(cs.Cyanf("%d", autolink.ID)) tp.AddField(autolink.KeyPrefix) tp.AddField(autolink.URLTemplate) tp.AddField(strconv.FormatBool(autolink.IsAlphanumeric)) diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go index f892c0724..0b212aece 100644 --- a/pkg/cmd/repo/autolink/view/view.go +++ b/pkg/cmd/repo/autolink/view/view.go @@ -3,7 +3,6 @@ package view import ( "fmt" - "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared" @@ -65,6 +64,8 @@ func viewRun(opts *viewOptions) error { if err != nil { return err } + + out := opts.IO.Out cs := opts.IO.ColorScheme() autolink, err := opts.AutolinkClient.View(repo, opts.ID) @@ -77,21 +78,19 @@ func viewRun(opts *viewOptions) error { return opts.Exporter.Write(opts.IO, autolink) } - msg := heredoc.Docf(` - Autolink in %s + fmt.Fprintf(out, "Autolink in %s\n\n", ghrepo.FullName(repo)) - ID: %d - Key Prefix: %s - URL Template: %s - Alphanumeric: %t - `, - ghrepo.FullName(repo), - autolink.ID, - autolink.KeyPrefix, - autolink.URLTemplate, - autolink.IsAlphanumeric, - ) - fmt.Fprint(opts.IO.Out, msg) + fmt.Fprint(out, cs.Bold("ID: ")) + fmt.Fprintln(out, cs.Cyanf("%d", autolink.ID)) + + fmt.Fprint(out, cs.Bold("Key Prefix: ")) + fmt.Fprintln(out, autolink.KeyPrefix) + + fmt.Fprint(out, cs.Bold("URL Template: ")) + fmt.Fprintln(out, autolink.URLTemplate) + + fmt.Fprint(out, cs.Bold("Alphanumeric: ")) + fmt.Fprintln(out, autolink.IsAlphanumeric) return nil } From 9e14550f11bb6408e95a6f96535770d03dc539ad Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Thu, 30 Jan 2025 10:21:18 +0500 Subject: [PATCH 071/100] [gh api] Fix mutual exclusion messages of `--slurp` flag --- pkg/cmd/api/api.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index c8eac9115..a6eb69718 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -239,17 +239,19 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command return err } - if opts.Slurp && !opts.Paginate { - return cmdutil.FlagErrorf("`--paginate` required when passing `--slurp`") - } + if opts.Slurp { + if err := cmdutil.MutuallyExclusive( + "the `--slurp` option is not supported with `--jq` or `--template`", + opts.Slurp, + opts.FilterOutput != "", + opts.Template != "", + ); err != nil { + return err + } - if err := cmdutil.MutuallyExclusive( - "the `--slurp` option is not supported with `--jq` or `--template`", - opts.Slurp, - opts.FilterOutput != "", - opts.Template != "", - ); err != nil { - return err + if !opts.Paginate { + return cmdutil.FlagErrorf("`--paginate` required when passing `--slurp`") + } } if err := cmdutil.MutuallyExclusive( From 3bc7b7e7e25b75fd5d6bf9b1e3b667a21c003147 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Thu, 30 Jan 2025 11:40:42 +0500 Subject: [PATCH 072/100] [gh extensions install] Improve help text and error message --- pkg/cmd/extension/command.go | 10 +++++++++- pkg/cmd/extension/manager.go | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/extension/command.go b/pkg/cmd/extension/command.go index dad4021b6..ae9a917a6 100644 --- a/pkg/cmd/extension/command.go +++ b/pkg/cmd/extension/command.go @@ -298,9 +298,17 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command { Long: heredoc.Docf(` Install a GitHub CLI extension from a GitHub or local repository. - For GitHub repositories, the repository argument can be specified in %[1]sOWNER/REPO%[1]s format or as a full repository URL. + For GitHub repositories, the repository argument can be specified in + %[1]sOWNER/REPO%[1]s format or as a full repository URL. The URL format is useful when the repository is not hosted on github.com. + For remote repositories, the GitHub CLI first looks for the release artifacts assuming + that it's a binary extension i.e. prebuilt binaries provided as part of the release. + In the absence of a release, the repository itself is cloned assuming that it's a + script extension i.e. prebuilt executable or script exists on its root. + The %[1]s--pin%[1]s flag may be used to specify a tag or commit for binary and script + extensions respectively, the latest one is used otherwise. + For local repositories, often used while developing extensions, use %[1]s.%[1]s as the value of the repository argument. Note the following: diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index c6947f479..e83a3ed22 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -268,7 +268,7 @@ func (m *Manager) Install(repo ghrepo.Interface, target string) error { return err } if !hs { - return errors.New("extension is not installable: missing executable") + return fmt.Errorf("extension is not installable: no acceptable release artifact or script found in %s", repo) } return m.installGit(repo, target) From 978d3fce87d2178e64b9cc9f9d363dc474ffcb45 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Thu, 30 Jan 2025 13:05:18 +0500 Subject: [PATCH 073/100] [gh issue/pr list] Improve help text --- pkg/cmd/issue/list/list.go | 3 ++- pkg/cmd/pr/list/list.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/issue/list/list.go b/pkg/cmd/issue/list/list.go index 47da51ae4..46c0e2cb0 100644 --- a/pkg/cmd/issue/list/list.go +++ b/pkg/cmd/issue/list/list.go @@ -59,7 +59,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman Use: "list", Short: "List issues in a repository", Long: heredoc.Doc(` - List issues in a GitHub repository. + List issues in a GitHub repository. By default, this only lists open issues. The search query syntax is documented here: @@ -70,6 +70,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman $ gh issue list --assignee "@me" $ gh issue list --milestone "The big 1.0" $ gh issue list --search "error no:assignee sort:created-asc" + $ gh issue list --state all `), Aliases: []string{"ls"}, Args: cmdutil.NoArgsQuoteReminder, diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index 93a9b8b4b..bf0c2f8ed 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -55,7 +55,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman Use: "list", Short: "List pull requests in a repository", Long: heredoc.Doc(` - List pull requests in a GitHub repository. + List pull requests in a GitHub repository. By default, this only lists open PRs. The search query syntax is documented here: From 029a5a577b2ab4ac180e4ff93ea6b74ebc09d75b Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Thu, 30 Jan 2025 19:23:07 +0500 Subject: [PATCH 074/100] Update test --- pkg/cmd/project/shared/queries/export_data_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/project/shared/queries/export_data_test.go b/pkg/cmd/project/shared/queries/export_data_test.go index adfdce63f..c9f991ed4 100644 --- a/pkg/cmd/project/shared/queries/export_data_test.go +++ b/pkg/cmd/project/shared/queries/export_data_test.go @@ -321,7 +321,7 @@ func TestJSONProjectItem_DraftIssue_ProjectV2ItemFieldIterationValue(t *testing. assert.NoError(t, err) assert.JSONEq( t, - `{"items":[{"sprint":{"title":"Iteration Title","startDate":"","duration":0},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title","id":"draftIssueId"},"id":"draftIssueId"}],"totalCount":5}`, + `{"items":[{"sprint":{"title":"Iteration Title","startDate":"","duration":0,"iterationId":""},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title","id":"draftIssueId"},"id":"draftIssueId"}],"totalCount":5}`, string(out)) } From ddb8855198ea7a5bc9547e205dbfda8b7b5f1b80 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 30 Jan 2025 09:12:48 -0700 Subject: [PATCH 075/100] flip bundle fetching logic Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 24 ++++++++++++------------ pkg/cmd/attestation/api/client_test.go | 8 ++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 37579b7bc..746b04411 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -176,23 +176,23 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ return fmt.Errorf("attestation has no bundle or bundle URL") } - // for now, we fallback to the bundle field if the bundle URL is empty - if a.BundleURL == "" { - c.logger.VerbosePrintf("Bundle URL is empty. Falling back to bundle field\n\n") - fetched[i] = &Attestation{ - Bundle: a.Bundle, + // If the bundle field is nil, try to fetch the bundle with the provided URL + if a.Bundle == nil { + b, err := c.GetBundle(a.BundleURL) + if err != nil { + return fmt.Errorf("failed to fetch bundle with URL: %w", err) + } + fetched[i] = &Attestation{ + Bundle: b, } - return nil } - // otherwise fetch the bundle with the provided URL - b, err := c.GetBundle(a.BundleURL) - if err != nil { - return fmt.Errorf("failed to fetch bundle with URL: %w", err) - } + // otherwise fall back to the bundle field + c.logger.VerbosePrintf("Bundle URL is empty. Falling back to bundle field\n\n") fetched[i] = &Attestation{ - Bundle: b, + Bundle: a.Bundle, } + return nil }) } diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 65f8d59ca..083a5c5c8 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -180,7 +180,7 @@ func TestGetByDigest_Error(t *testing.T) { require.Nil(t, attestations) } -func TestFetchBundleFromAttestations(t *testing.T) { +func TestFetchBundleFromAttestations_Fallback_Bundle_Field(t *testing.T) { httpClient := &mockHttpClient{} client := LiveClient{ httpClient: httpClient, @@ -194,7 +194,7 @@ func TestFetchBundleFromAttestations(t *testing.T) { require.NoError(t, err) require.Len(t, fetched, 2) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", fetched[0].Bundle.GetMediaType()) - httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 2) + httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 0) } func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { @@ -220,7 +220,10 @@ func TestFetchBundleFromAttestations_Fail(t *testing.T) { } att1 := makeTestAttestation() + att1.Bundle = nil att2 := makeTestAttestation() + att2.Bundle = nil + // zero out the bundle field so it tries fetching by URL attestations := []*Attestation{&att1, &att2} fetched, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) @@ -237,6 +240,7 @@ func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { } a := makeTestAttestation() + a.Bundle = nil attestations := []*Attestation{&a} bundle, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) From 70ae9f39ef3668bab984a77be34b2ec3be4162b5 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 30 Jan 2025 09:26:41 -0700 Subject: [PATCH 076/100] update tests to account for logic flip Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 4 +++- pkg/cmd/attestation/api/client_test.go | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 746b04411..6054bc98e 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -178,6 +178,7 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ // If the bundle field is nil, try to fetch the bundle with the provided URL if a.Bundle == nil { + c.logger.VerbosePrintf("Bundle field is empty. Trying to fetch with bundle URL\n\n") b, err := c.GetBundle(a.BundleURL) if err != nil { return fmt.Errorf("failed to fetch bundle with URL: %w", err) @@ -185,10 +186,11 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([ fetched[i] = &Attestation{ Bundle: b, } + return nil } // otherwise fall back to the bundle field - c.logger.VerbosePrintf("Bundle URL is empty. Falling back to bundle field\n\n") + c.logger.VerbosePrintf("Fetching bundle from Bundle field\n\n") fetched[i] = &Attestation{ Bundle: a.Bundle, } diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 083a5c5c8..3d180af8f 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -180,7 +180,7 @@ func TestGetByDigest_Error(t *testing.T) { require.Nil(t, attestations) } -func TestFetchBundleFromAttestations_Fallback_Bundle_Field(t *testing.T) { +func TestFetchBundleFromAttestations_BundleURL(t *testing.T) { httpClient := &mockHttpClient{} client := LiveClient{ httpClient: httpClient, @@ -188,13 +188,16 @@ func TestFetchBundleFromAttestations_Fallback_Bundle_Field(t *testing.T) { } att1 := makeTestAttestation() + att1.Bundle = nil att2 := makeTestAttestation() + att2.Bundle = nil + // zero out the bundle field so it tries fetching by URL attestations := []*Attestation{&att1, &att2} fetched, err := client.fetchBundleFromAttestations(attestations) require.NoError(t, err) require.Len(t, fetched, 2) - require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", fetched[0].Bundle.GetMediaType()) - httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 0) + require.NotNil(t, "application/vnd.dev.sigstore.bundle.v0.3+json", fetched[0].Bundle.GetMediaType()) + httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 2) } func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { @@ -211,7 +214,7 @@ func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { require.Nil(t, fetched, 2) } -func TestFetchBundleFromAttestations_Fail(t *testing.T) { +func TestFetchBundleFromAttestations_Fail_BundleURL(t *testing.T) { httpClient := &failAfterOneCallHttpClient{} c := &LiveClient{ From e9f7761423e3ee24f1debc2621dad92af65ece38 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 30 Jan 2025 09:56:06 -0700 Subject: [PATCH 077/100] dont retry when parsing fails Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index 76ef6688a..a950e76eb 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -209,19 +209,19 @@ func (c *LiveClient) getBundle(url string) (*bundle.Bundle, error) { var out []byte decompressed, err := snappy.Decode(out, body) if err != nil { - return fmt.Errorf("failed to decompress with snappy: %w", err) + return backoff.Permanent(fmt.Errorf("failed to decompress with snappy: %w", err)) } var pbBundle v1.Bundle if err = protojson.Unmarshal(decompressed, &pbBundle); err != nil { - return fmt.Errorf("failed to unmarshal to bundle: %w", err) + return backoff.Permanent(fmt.Errorf("failed to unmarshal to bundle: %w", err)) } c.logger.VerbosePrintf("Successfully fetched bundle\n\n") sgBundle, err = bundle.NewBundle(&pbBundle) if err != nil { - return fmt.Errorf("failed to create new bundle: %w", err) + return backoff.Permanent(fmt.Errorf("failed to create new bundle: %w", err)) } return nil From d1b5facb48f11d95af08b12b4697f342ce8db025 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Thu, 30 Jan 2025 12:59:29 -0500 Subject: [PATCH 078/100] Remove unused env var, PRID --- .github/workflows/prauto.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 67602b2ee..b7ed90183 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -16,7 +16,6 @@ jobs: env: GH_REPO: ${{ github.repository }} GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN }} - PRID: ${{ github.event.pull_request.node_id }} PRBODY: ${{ github.event.pull_request.body }} PRNUM: ${{ github.event.pull_request.number }} PRHEAD: ${{ github.event.pull_request.head.label }} From df250a2b538e2860f41b2b370361f291c687407f Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Thu, 30 Jan 2025 13:07:21 -0500 Subject: [PATCH 079/100] Update deployment.yml Fix failing tag validate job --- .github/workflows/deployment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 31cd4b8e7..efc729711 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -33,7 +33,7 @@ jobs: steps: - name: Validate tag name format run: | - if [[ ! "${{ inputs.tag_name }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$]]; then + if [[ ! "${{ inputs.tag_name }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Invalid tag name format. Must be in the form v1.2.3" exit 1 fi From 795263524d1415592588761051b5d9ed97e93bc5 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 30 Jan 2025 11:11:41 -0700 Subject: [PATCH 080/100] change permanent backoff error condition Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index a950e76eb..1e99a2a06 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -193,7 +193,7 @@ func (c *LiveClient) getBundle(url string) (*bundle.Bundle, error) { err := backoff.Retry(func() error { resp, err := c.httpClient.Get(url) if err != nil { - return backoff.Permanent(err) + return fmt.Errorf("request to fetch bundle from URL failed: %w", err) } if resp.StatusCode >= 500 && resp.StatusCode <= 599 { From fc2f18c896605b7274f83a45def22a121a9a7d69 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 30 Jan 2025 11:12:58 -0700 Subject: [PATCH 081/100] consolidate tests around getBundle func when possible Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client_test.go | 102 ++++++++++-------- .../attestation/api/mock_httpClient_test.go | 24 ++++- 2 files changed, 75 insertions(+), 51 deletions(-) diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index cb39fb78a..787408a4e 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -170,10 +170,7 @@ func TestFetchBundleFromAttestations_BundleURL(t *testing.T) { } att1 := makeTestAttestation() - att1.Bundle = nil att2 := makeTestAttestation() - att2.Bundle = nil - // zero out the bundle field so it tries fetching by URL attestations := []*Attestation{&att1, &att2} fetched, err := client.fetchBundleFromAttestations(attestations) require.NoError(t, err) @@ -182,56 +179,28 @@ func TestFetchBundleFromAttestations_BundleURL(t *testing.T) { httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 2) } -func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) { +func TestFetchBundleFromAttestations_MissingBundleAndBundleURLFields(t *testing.T) { httpClient := &mockHttpClient{} client := LiveClient{ httpClient: httpClient, logger: io.NewTestHandler(), } + // If both the BundleURL and Bundle fields are empty, the function should + // return an error indicating that att1 := Attestation{} attestations := []*Attestation{&att1} - fetched, err := client.fetchBundleFromAttestations(attestations) - require.Error(t, err) - require.Nil(t, fetched, 2) + bundles, err := client.fetchBundleFromAttestations(attestations) + require.ErrorContains(t, err, "attestation has no bundle or bundle URL") + require.Nil(t, bundles, 2) } -func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) { - mockHTTPClient := &failHttpClient{} - // failAfterOneCallHttpClient - - c := &LiveClient{ - httpClient: mockHTTPClient, - logger: io.NewTestHandler(), - } - - a := makeTestAttestation() - attestations := []*Attestation{&a} - bundle, err := c.fetchBundleFromAttestations(attestations) - require.Error(t, err) - require.Nil(t, bundle) -} - -func TestFetchBundleFromAttestations_FailWithGetErr(t *testing.T) { - mockHTTPClient := &failHttpClient{} - - c := &LiveClient{ - httpClient: mockHTTPClient, - logger: io.NewTestHandler(), - } - - a := makeTestAttestation() - a.Bundle = nil - attestations := []*Attestation{&a} - bundle, err := c.fetchBundleFromAttestations(attestations) - require.Error(t, err) - require.Nil(t, bundle) - mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) -} - -func TestFetchBundleFromAttestations_FetchByURL5XX_Resp(t *testing.T) { +func TestFetchBundleFromAttestations_FailOnTheSecondAttestation(t *testing.T) { mockHTTPClient := &failAfterNCallsHttpClient{ - FailOnCallN: 1, + // the initial HTTP request will succeed, which returns a bundle for the first attestation + // all following HTTP requests will fail, which means the function fails to fetch a bundle + // for the second attestation and the function returns an error + FailOnCallN: 2, FailOnAllSubsequentCalls: true, } @@ -240,11 +209,28 @@ func TestFetchBundleFromAttestations_FetchByURL5XX_Resp(t *testing.T) { logger: io.NewTestHandler(), } + att1 := makeTestAttestation() + att2 := makeTestAttestation() + attestations := []*Attestation{&att1, &att2} + bundles, err := c.fetchBundleFromAttestations(attestations) + require.Error(t, err) + require.Nil(t, bundles) +} + +func TestFetchBundleFromAttestations_FailAfterRetrying(t *testing.T) { + mockHTTPClient := &reqFailHttpClient{} + + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + a := makeTestAttestation() attestations := []*Attestation{&a} bundle, err := c.fetchBundleFromAttestations(attestations) require.Error(t, err) require.Nil(t, bundle) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetReqFail", 4) } func TestFetchBundleFromAttestations_FallbackToBundleField(t *testing.T) { @@ -255,6 +241,7 @@ func TestFetchBundleFromAttestations_FallbackToBundleField(t *testing.T) { logger: io.NewTestHandler(), } + // If the bundle URL is empty, the code will fallback to the bundle field a := Attestation{Bundle: data.SigstoreBundle(t)} attestations := []*Attestation{&a} fetched, err := c.fetchBundleFromAttestations(attestations) @@ -263,6 +250,7 @@ func TestFetchBundleFromAttestations_FallbackToBundleField(t *testing.T) { mockHTTPClient.AssertNotCalled(t, "OnGetSuccess") } +// getBundle successfully fetches a bundle on the first HTTP request attempt func TestGetBundle(t *testing.T) { mockHTTPClient := &mockHttpClient{} @@ -271,13 +259,15 @@ func TestGetBundle(t *testing.T) { logger: io.NewTestHandler(), } - b, err := c.getBundle("whatever") + b, err := c.getBundle("https://mybundleurl.com") require.NoError(t, err) require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", b.GetMediaType()) mockHTTPClient.AssertNumberOfCalls(t, "OnGetSuccess", 1) } -func TestGetBundle_Retry(t *testing.T) { +// getBundle retries successfully when the initial HTTP request returns +// a 5XX status code +func TestGetBundle_SuccessfulRetry(t *testing.T) { mockHTTPClient := &failAfterNCallsHttpClient{ FailOnCallN: 1, FailOnAllSubsequentCalls: false, @@ -294,8 +284,26 @@ func TestGetBundle_Retry(t *testing.T) { mockHTTPClient.AssertNumberOfCalls(t, "OnGetFailAfterNCalls", 2) } -func TestGetBundle_Fail(t *testing.T) { - mockHTTPClient := &failHttpClient{} +// getBundle does not retry when the function fails with a permanent backoff error condition +func TestGetBundle_PermanentBackoffFail(t *testing.T) { + mockHTTPClient := &invalidBundleClient{} + c := &LiveClient{ + httpClient: mockHTTPClient, + logger: io.NewTestHandler(), + } + + b, err := c.getBundle("mybundleurl") + // var permanent *backoff.PermanentError + //require.IsType(t, &backoff.PermanentError{}, err) + require.Error(t, err) + require.Nil(t, b) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetInvalidBundle", 1) +} + +// getBundle retries when the HTTP request fails +func TestGetBundle_RequestFail(t *testing.T) { + mockHTTPClient := &reqFailHttpClient{} + c := &LiveClient{ httpClient: mockHTTPClient, logger: io.NewTestHandler(), @@ -304,7 +312,7 @@ func TestGetBundle_Fail(t *testing.T) { b, err := c.getBundle("mybundleurl") require.Error(t, err) require.Nil(t, b) - mockHTTPClient.AssertNumberOfCalls(t, "OnGetFail", 1) + mockHTTPClient.AssertNumberOfCalls(t, "OnGetReqFail", 4) } func TestGetTrustDomain(t *testing.T) { diff --git a/pkg/cmd/attestation/api/mock_httpClient_test.go b/pkg/cmd/attestation/api/mock_httpClient_test.go index e11266a60..26933ae2e 100644 --- a/pkg/cmd/attestation/api/mock_httpClient_test.go +++ b/pkg/cmd/attestation/api/mock_httpClient_test.go @@ -27,13 +27,29 @@ func (m *mockHttpClient) Get(url string) (*http.Response, error) { }, nil } -type failHttpClient struct { +type invalidBundleClient struct { mock.Mock } -func (m *failHttpClient) Get(url string) (*http.Response, error) { - m.On("OnGetFail").Return() - m.MethodCalled("OnGetFail") +func (m *invalidBundleClient) Get(url string) (*http.Response, error) { + m.On("OnGetInvalidBundle").Return() + m.MethodCalled("OnGetInvalidBundle") + + var compressed []byte + compressed = snappy.Encode(compressed, []byte("invalid bundle bytes")) + return &http.Response{ + StatusCode: 200, + Body: io.NopCloser(bytes.NewReader(compressed)), + }, nil +} + +type reqFailHttpClient struct { + mock.Mock +} + +func (m *reqFailHttpClient) Get(url string) (*http.Response, error) { + m.On("OnGetReqFail").Return() + m.MethodCalled("OnGetReqFail") return &http.Response{ StatusCode: 500, From 29e57ee8e769261303e4fced19daf9636bc3eb10 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 13:47:21 -0800 Subject: [PATCH 082/100] Add tests for using the pr Finder outside of repo This includes an acceptance test for `gh pr view` and a unit test on `TestFind` --- .../testdata/pr/pr-view-outside-repo.txtar | 28 ++++++++++++++++ pkg/cmd/pr/shared/finder_test.go | 32 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 acceptance/testdata/pr/pr-view-outside-repo.txtar diff --git a/acceptance/testdata/pr/pr-view-outside-repo.txtar b/acceptance/testdata/pr/pr-view-outside-repo.txtar new file mode 100644 index 000000000..edfb37ed4 --- /dev/null +++ b/acceptance/testdata/pr/pr-view-outside-repo.txtar @@ -0,0 +1,28 @@ +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes $ORG/$SCRIPT_NAME-$RANDOM_STRING + +# Clone the repo +exec gh repo clone $ORG/$SCRIPT_NAME-$RANDOM_STRING + +# Prepare a branch to PR +cd $SCRIPT_NAME-$RANDOM_STRING +exec git checkout -b feature-branch +exec git commit --allow-empty -m 'Empty Commit' +exec git push -u origin feature-branch + +# Create the PR +exec gh pr create --title 'Feature Title' --body 'Feature Body' +stdout2env PR_URL + +# Change directories +cd .. + +# View the PR +exec gh pr view $PR_URL +stdout 'Feature Title' diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 694e0c20d..435377d9c 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -211,6 +211,38 @@ func TestFind(t *testing.T) { wantPR: 13, wantRepo: "https://example.org/OWNER/REPO", }, + { + name: "PR URL argument and not in a local git repo", + args: args{ + selector: "https://example.org/OWNER/REPO/pull/13/files", + fields: []string{"id", "number"}, + baseRepoFn: nil, + branchFn: func() (string, error) { + return "", &git.GitError{ + Stderr: "fatal: not a git repository (or any of the parent directories): .git", + ExitCode: 128, + } + }, + branchConfig: stubBranchConfig(git.BranchConfig{}, &git.GitError{ + Stderr: "fatal: not a git repository (or any of the parent directories): .git", + ExitCode: 128, + }), + pushDefault: stubPushDefault("simple", nil), + remotePushDefault: stubRemotePushDefault("", &git.GitError{ + Stderr: "fatal: not a git repository (or any of the parent directories): .git", + ExitCode: 128, + }), + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequestByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequest":{"number":13} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://example.org/OWNER/REPO", + }, { name: "when provided branch argument with an open and closed PR for that branch name, it returns the open PR", args: args{ From 058cb2c220d0200779a9742f8bba9886f3e98592 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 13:48:08 -0800 Subject: [PATCH 083/100] Refactor finder to work with URL selectors --- git/command.go | 5 ++ git/command_test.go | 98 +++++++++++++++++++++++++++++++++ git/errors.go | 1 + pkg/cmd/pr/shared/finder.go | 106 ++++++++++++++++++++---------------- 4 files changed, 163 insertions(+), 47 deletions(-) create mode 100644 git/command_test.go diff --git a/git/command.go b/git/command.go index 8065ffd86..b68b59525 100644 --- a/git/command.go +++ b/git/command.go @@ -6,6 +6,7 @@ import ( "errors" "io" "os/exec" + "strings" "github.com/cli/cli/v2/internal/run" ) @@ -48,6 +49,10 @@ func (gc *Command) Output() ([]byte, error) { ge.Stderr = string(exitError.Stderr) ge.ExitCode = exitError.ExitCode() } + + if strings.Contains(ge.Stderr, "fatal: not a git repository") { + ge.err = ErrNoGitRepository + } err = &ge } return out, err diff --git a/git/command_test.go b/git/command_test.go new file mode 100644 index 000000000..15fd291fc --- /dev/null +++ b/git/command_test.go @@ -0,0 +1,98 @@ +package git + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestOutput(t *testing.T) { + tests := []struct { + name string + exitCode int + stdout string + stderr string + wantErr error + }{ + { + name: "successful command", + stdout: "hello world", + stderr: "", + exitCode: 0, + wantErr: nil, + }, + { + name: "not a repo failure", + stdout: "", + stderr: "fatal: not a git repository (or any of the parent directories): .git", + exitCode: 128, + wantErr: ErrNoGitRepository, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + cmd := Command{ + &exec.Cmd{ + Path: createMockExecutable(t, tt.stdout, tt.stderr, tt.exitCode), + }, + } + + out, err := cmd.Output() + if tt.wantErr != nil { + require.Error(t, err) + var gitError *GitError + require.ErrorAs(t, err, &gitError) + assert.Equal(t, tt.wantErr, gitError.err) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.stdout, string(out)) + }) + } +} + +func createMockExecutable(t *testing.T, stdout string, stderr string, exitCode int) string { + tmpDir := t.TempDir() + sourcePath := filepath.Join(tmpDir, "main.go") + binaryPath := filepath.Join(tmpDir, "mockexec") + if runtime.GOOS == "windows" { + binaryPath += ".exe" + } + + // Create Go source + source := buildCommandSourceCode(exitCode, stdout, stderr) + + // Write source file + if err := os.WriteFile(sourcePath, []byte(source), 0600); err != nil { + t.Fatal(err) + } + + // Compile + cmd := exec.Command("go", "build", "-o", binaryPath, sourcePath) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("failed to compile: %v\n%s", err, out) + } + return binaryPath + +} + +func buildCommandSourceCode(exitCode int, stdout, stderr string) string { + return fmt.Sprintf(`package main + import ( + "fmt" + "os" + ) + func main() { + fmt.Printf(%q) + fmt.Fprintf(os.Stderr, %q) + os.Exit(%d) + }`, stdout, stderr, exitCode) +} diff --git a/git/errors.go b/git/errors.go index a3f1645aa..9d433e906 100644 --- a/git/errors.go +++ b/git/errors.go @@ -7,6 +7,7 @@ import ( // ErrNotOnAnyBranch indicates that the user is in detached HEAD state. var ErrNotOnAnyBranch = errors.New("git: not on any branch") +var ErrNoGitRepository = errors.New("git: not in a git repository") type NotInstalled struct { message string diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index e4f89502c..0ba3ec734 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -120,6 +120,8 @@ func (s *PullRequestRefs) GetPRHeadLabel() string { } func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, error) { + // If we have a URL, we don't need git stuff + if len(opts.Fields) == 0 { return nil, nil, errors.New("Find error: no fields specified") } @@ -137,37 +139,70 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err f.baseRefRepo = repo } - if f.prNumber == 0 && opts.Selector != "" { - // If opts.Selector is a valid number then assume it is the - // PR number unless opts.BaseBranch is specified. This is a - // special case for PR create command which will always want - // to assume that a numerical selector is a branch name rather - // than PR number. - prNumber, err := strconv.Atoi(strings.TrimPrefix(opts.Selector, "#")) - if opts.BaseBranch == "" && err == nil { - f.prNumber = prNumber - } else { - f.branchName = opts.Selector - } - } else { + var prRefs PullRequestRefs + if opts.Selector == "" { + // You must be in a git repo for this case to work currentBranchName, err := f.branchFn() if err != nil { return nil, nil, err } f.branchName = currentBranchName - } - // Get the branch config for the current branchName - branchConfig, err := f.branchConfig(f.branchName) - if err != nil { - return nil, nil, err - } + // Get the branch config for the current branchName + branchConfig, err := f.branchConfig(f.branchName) + if err != nil { + return nil, nil, err + } - // Determine if the branch is configured to merge to a special PR ref - prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) - if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { - prNumber, _ := strconv.Atoi(m[1]) - f.prNumber = prNumber + // Determine if the branch is configured to merge to a special PR ref + prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) + if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { + prNumber, _ := strconv.Atoi(m[1]) + f.prNumber = prNumber + } + + // Determine the PullRequestRefs from config + if f.prNumber == 0 { + rems, err := f.remotesFn() + if err != nil { + return nil, nil, err + } + + // Suppressing these errors as we have other means of computing the PullRequestRefs when these fail. + parsedPushRevision, _ := f.parsePushRevision(f.branchName) + + pushDefault, err := f.pushDefault() + if err != nil { + return nil, nil, err + } + + remotePushDefault, err := f.remotePushDefault() + if err != nil { + return nil, nil, err + } + + prRefs, err = ParsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, f.baseRefRepo, rems) + if err != nil { + return nil, nil, err + } + } + + } else if f.prNumber == 0 { + // You gave me a selector but I couldn't find a PR number (it wasn't a URL) + + // Try to get a PR number from the selector + prNumber, err := strconv.Atoi(strings.TrimPrefix(opts.Selector, "#")) + // If opts.Selector is a valid number then assume it is the + // PR number unless opts.BaseBranch is specified. This is a + // special case for PR create command which will always want + // to assume that a numerical selector is a branch name rather + // than PR number. + if opts.BaseBranch == "" && err == nil { + f.prNumber = prNumber + } else { + f.branchName = opts.Selector + prRefs, _ = ParsePRRefs(f.branchName, git.BranchConfig{}, "", "", "", f.baseRefRepo, remotes.Remotes{}) + } } // Set up HTTP client @@ -217,29 +252,6 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err return pr, f.baseRefRepo, err } } else { - rems, err := f.remotesFn() - if err != nil { - return nil, nil, err - } - - pushDefault, err := f.pushDefault() - if err != nil { - return nil, nil, err - } - - // Suppressing these errors as we have other means of computing the PullRequestRefs when these fail. - parsedPushRevision, _ := f.parsePushRevision(f.branchName) - - remotePushDefault, err := f.remotePushDefault() - if err != nil { - return nil, nil, err - } - - prRefs, err := ParsePRRefs(f.branchName, branchConfig, parsedPushRevision, pushDefault, remotePushDefault, f.baseRefRepo, rems) - if err != nil { - return nil, nil, err - } - pr, err = findForBranch(httpClient, f.baseRefRepo, opts.BaseBranch, prRefs.GetPRHeadLabel(), opts.States, fields.ToSlice()) if err != nil { return pr, f.baseRefRepo, err From fa5325aec943685fbdc3ff2a8f084414fb12ed03 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 14:21:29 -0800 Subject: [PATCH 084/100] Add comment to status recognizing when we require a repo --- pkg/cmd/pr/status/status.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 3877fb1cf..2436934bf 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -87,6 +87,7 @@ func statusRun(opts *StatusOptions) error { var currentHeadRefBranchName string if !opts.HasRepoOverride { + // We must be in a repo without the override currentBranchName, err = opts.Branch() if err != nil && !errors.Is(err, git.ErrNotOnAnyBranch) { return fmt.Errorf("could not query for pull request for current branch: %w", err) From 601cf888523f02e8c0b6abe3f03030e702b577c1 Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 14:37:19 -0800 Subject: [PATCH 085/100] Handle error from ParsePRRefs when the selector is provided --- pkg/cmd/pr/shared/finder.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 0ba3ec734..332baa857 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -201,7 +201,11 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err f.prNumber = prNumber } else { f.branchName = opts.Selector - prRefs, _ = ParsePRRefs(f.branchName, git.BranchConfig{}, "", "", "", f.baseRefRepo, remotes.Remotes{}) + // We don't expect an error here because parsedPushRevision is empty + prRefs, err = ParsePRRefs(f.branchName, git.BranchConfig{}, "", "", "", f.baseRefRepo, remotes.Remotes{}) + if err != nil { + return nil, nil, err + } } } From e428b9c66c9662541785618525d8ce3c9288ecba Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 14:49:38 -0800 Subject: [PATCH 086/100] Remove unused ErrNoGitRepository error --- git/command.go | 5 ----- git/command_test.go | 12 +++++++++--- git/errors.go | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/git/command.go b/git/command.go index b68b59525..8065ffd86 100644 --- a/git/command.go +++ b/git/command.go @@ -6,7 +6,6 @@ import ( "errors" "io" "os/exec" - "strings" "github.com/cli/cli/v2/internal/run" ) @@ -49,10 +48,6 @@ func (gc *Command) Output() ([]byte, error) { ge.Stderr = string(exitError.Stderr) ge.ExitCode = exitError.ExitCode() } - - if strings.Contains(ge.Stderr, "fatal: not a git repository") { - ge.err = ErrNoGitRepository - } err = &ge } return out, err diff --git a/git/command_test.go b/git/command_test.go index 15fd291fc..033492e01 100644 --- a/git/command_test.go +++ b/git/command_test.go @@ -18,7 +18,7 @@ func TestOutput(t *testing.T) { exitCode int stdout string stderr string - wantErr error + wantErr *GitError }{ { name: "successful command", @@ -32,7 +32,11 @@ func TestOutput(t *testing.T) { stdout: "", stderr: "fatal: not a git repository (or any of the parent directories): .git", exitCode: 128, - wantErr: ErrNoGitRepository, + wantErr: &GitError{ + ExitCode: 128, + Stderr: "fatal: not a git repository (or any of the parent directories): .git", + err: &exec.ExitError{}, + }, }, } @@ -50,7 +54,9 @@ func TestOutput(t *testing.T) { require.Error(t, err) var gitError *GitError require.ErrorAs(t, err, &gitError) - assert.Equal(t, tt.wantErr, gitError.err) + assert.Equal(t, tt.wantErr.ExitCode, gitError.ExitCode) + assert.Equal(t, tt.wantErr.Stderr, gitError.Stderr) + assert.Equal(t, tt.wantErr.Error(), gitError.Error()) } else { require.NoError(t, err) } diff --git a/git/errors.go b/git/errors.go index 9d433e906..a3f1645aa 100644 --- a/git/errors.go +++ b/git/errors.go @@ -7,7 +7,6 @@ import ( // ErrNotOnAnyBranch indicates that the user is in detached HEAD state. var ErrNotOnAnyBranch = errors.New("git: not on any branch") -var ErrNoGitRepository = errors.New("git: not in a git repository") type NotInstalled struct { message string From 877871f1e0091ba74249693d21782b05dd4d87ba Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 31 Jan 2025 14:55:24 -0800 Subject: [PATCH 087/100] Address PR comments --- pkg/cmd/pr/shared/finder.go | 1 - pkg/cmd/pr/shared/finder_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index 332baa857..a54528527 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -121,7 +121,6 @@ func (s *PullRequestRefs) GetPRHeadLabel() string { func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, error) { // If we have a URL, we don't need git stuff - if len(opts.Fields) == 0 { return nil, nil, errors.New("Find error: no fields specified") } diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go index 435377d9c..36551ab42 100644 --- a/pkg/cmd/pr/shared/finder_test.go +++ b/pkg/cmd/pr/shared/finder_test.go @@ -219,17 +219,17 @@ func TestFind(t *testing.T) { baseRepoFn: nil, branchFn: func() (string, error) { return "", &git.GitError{ - Stderr: "fatal: not a git repository (or any of the parent directories): .git", + Stderr: "fatal: branchFn error", ExitCode: 128, } }, branchConfig: stubBranchConfig(git.BranchConfig{}, &git.GitError{ - Stderr: "fatal: not a git repository (or any of the parent directories): .git", + Stderr: "fatal: branchConfig error", ExitCode: 128, }), pushDefault: stubPushDefault("simple", nil), remotePushDefault: stubRemotePushDefault("", &git.GitError{ - Stderr: "fatal: not a git repository (or any of the parent directories): .git", + Stderr: "fatal: remotePushDefault error", ExitCode: 128, }), }, From 59031c645ec004809fdfd41a8a110894a9368ec7 Mon Sep 17 00:00:00 2001 From: nilvng Date: Sat, 1 Feb 2025 11:20:06 +1100 Subject: [PATCH 088/100] Revert color changes This reverts commit 36eaf14857dc8110c47b3d91b823589771aae305. --- pkg/iostreams/color.go | 2 +- pkg/iostreams/color_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/iostreams/color.go b/pkg/iostreams/color.go index 33df6bddf..c8d48168f 100644 --- a/pkg/iostreams/color.go +++ b/pkg/iostreams/color.go @@ -252,5 +252,5 @@ func (c *ColorScheme) HexToRGB(hex string, x string) string { r, _ := strconv.ParseInt(hex[0:2], 16, 64) g, _ := strconv.ParseInt(hex[2:4], 16, 64) b, _ := strconv.ParseInt(hex[4:6], 16, 64) - return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[39m", r, g, b, x) + return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, x) } diff --git a/pkg/iostreams/color_test.go b/pkg/iostreams/color_test.go index f3370da29..59fea53ed 100644 --- a/pkg/iostreams/color_test.go +++ b/pkg/iostreams/color_test.go @@ -18,7 +18,7 @@ func TestColorFromRGB(t *testing.T) { name: "truecolor", hex: "fc0303", text: "red", - wants: "\033[38;2;252;3;3mred\033[39m", + wants: "\033[38;2;252;3;3mred\033[0m", cs: NewColorScheme(true, true, true), }, { @@ -62,7 +62,7 @@ func TestHexToRGB(t *testing.T) { name: "truecolor", hex: "fc0303", text: "red", - wants: "\033[38;2;252;3;3mred\033[39m", + wants: "\033[38;2;252;3;3mred\033[0m", cs: NewColorScheme(true, true, true), }, { From 7311b5cdd12990e9e9e7b41366fd6ee4fff8ebed Mon Sep 17 00:00:00 2001 From: nilvng Date: Sat, 1 Feb 2025 11:30:25 +1100 Subject: [PATCH 089/100] revert isEqualSet to private --- pkg/cmd/pr/shared/finder.go | 4 ++-- pkg/cmd/pr/shared/lister.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index b1fec267e..e4a70eb22 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -548,7 +548,7 @@ func (m *mockFinder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, if m.expectSelector != opts.Selector { return nil, nil, fmt.Errorf("mockFinder: expected selector %q, got %q", m.expectSelector, opts.Selector) } - if len(m.expectFields) > 0 && !IsEqualSet(m.expectFields, opts.Fields) { + if len(m.expectFields) > 0 && !isEqualSet(m.expectFields, opts.Fields) { return nil, nil, fmt.Errorf("mockFinder: expected fields %v, got %v", m.expectFields, opts.Fields) } if m.called { @@ -568,7 +568,7 @@ func (m *mockFinder) ExpectFields(fields []string) { m.expectFields = fields } -func IsEqualSet(a, b []string) bool { +func isEqualSet(a, b []string) bool { if len(a) != len(b) { return false } diff --git a/pkg/cmd/pr/shared/lister.go b/pkg/cmd/pr/shared/lister.go index 004f726d5..454ecbfcf 100644 --- a/pkg/cmd/pr/shared/lister.go +++ b/pkg/cmd/pr/shared/lister.go @@ -183,7 +183,7 @@ func (m *mockLister) List(opt ListOptions) (*api.PullRequestAndTotalCount, error if m.expectFields != nil { - if !IsEqualSet(m.expectFields, opt.Fields) { + if !isEqualSet(m.expectFields, opt.Fields) { return nil, fmt.Errorf("unexpected fields: %v", opt.Fields) } } From 42c9d244f7ebb2a097c2c9ad81ceeabdb8427ffe Mon Sep 17 00:00:00 2001 From: nilvng Date: Sat, 1 Feb 2025 11:45:22 +1100 Subject: [PATCH 090/100] remove duplicated Prompter type --- pkg/cmd/pr/checkout/checkout.go | 6 +++--- pkg/cmd/pr/shared/commentable.go | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index b265ea25e..c73d90339 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -28,7 +28,7 @@ type CheckoutOptions struct { Branch func() (string, error) Finder shared.PRFinder - Prompter shared.Prompter + Prompter shared.Prompt Lister shared.PRLister Interactive bool @@ -287,7 +287,7 @@ func executeCmds(client *git.Client, credentialPattern git.CredentialPattern, cm return nil } -func resolvePR(baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, prLister shared.PRLister, io *iostreams.IOStreams) (*api.PullRequest, error) { +func resolvePR(baseRepo ghrepo.Interface, prompter shared.Prompt, pullRequestSelector string, isInteractive bool, pullRequestFinder shared.PRFinder, prLister shared.PRLister, io *iostreams.IOStreams) (*api.PullRequest, error) { // When non-interactive if pullRequestSelector != "" { pr, _, err := pullRequestFinder.Find(shared.FindOptions{ @@ -338,7 +338,7 @@ func resolvePR(baseRepo ghrepo.Interface, prompter shared.Prompter, pullRequestS return pr, err } -func promptForPR(prompter shared.Prompter, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { +func promptForPR(prompter shared.Prompt, jobs api.PullRequestAndTotalCount) (*api.PullRequest, error) { candidates := []string{} for _, pr := range jobs.PullRequests { candidates = append(candidates, fmt.Sprintf("%d\t%s %s [%s]", diff --git a/pkg/cmd/pr/shared/commentable.go b/pkg/cmd/pr/shared/commentable.go index 342898687..7a38286d2 100644 --- a/pkg/cmd/pr/shared/commentable.go +++ b/pkg/cmd/pr/shared/commentable.go @@ -19,10 +19,6 @@ import ( type InputType int -type Prompter interface { - Select(string, string, []string) (int, error) -} - const ( InputTypeEditor InputType = iota InputTypeInline From e144168dde109026d0ca4f09a158320245b77ce1 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Mon, 3 Feb 2025 15:26:05 +0500 Subject: [PATCH 091/100] [gh repo edit] Allow setting commit title defaults --- pkg/cmd/repo/edit/edit.go | 75 +++++++++++++---- pkg/cmd/repo/edit/edit_test.go | 142 ++++++++++++++++++++++++++++++++- 2 files changed, 199 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/repo/edit/edit.go b/pkg/cmd/repo/edit/edit.go index daa700cfb..79e32385b 100644 --- a/pkg/cmd/repo/edit/edit.go +++ b/pkg/cmd/repo/edit/edit.go @@ -70,23 +70,27 @@ type EditRepositoryInput struct { enableSecretScanning *bool enableSecretScanningPushProtection *bool - AllowForking *bool `json:"allow_forking,omitempty"` - AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` - DefaultBranch *string `json:"default_branch,omitempty"` - DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` - Description *string `json:"description,omitempty"` - EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"` - EnableIssues *bool `json:"has_issues,omitempty"` - EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"` - EnableProjects *bool `json:"has_projects,omitempty"` - EnableDiscussions *bool `json:"has_discussions,omitempty"` - EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` - EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"` - EnableWiki *bool `json:"has_wiki,omitempty"` - Homepage *string `json:"homepage,omitempty"` - IsTemplate *bool `json:"is_template,omitempty"` - SecurityAndAnalysis *SecurityAndAnalysisInput `json:"security_and_analysis,omitempty"` - Visibility *string `json:"visibility,omitempty"` + AllowForking *bool `json:"allow_forking,omitempty"` + AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` + DefaultBranch *string `json:"default_branch,omitempty"` + DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` + Description *string `json:"description,omitempty"` + EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"` + EnableIssues *bool `json:"has_issues,omitempty"` + EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"` + MergeCommitTitle string `json:"merge_commit_title,omitempty"` + MergeCommitMessage string `json:"merge_commit_message,omitempty"` + EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"` + SquashMergeCommitTitle string `json:"squash_merge_commit_title,omitempty"` + SquashMergeCommitMessage string `json:"squash_merge_commit_message,omitempty"` + EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` + EnableProjects *bool `json:"has_projects,omitempty"` + EnableDiscussions *bool `json:"has_discussions,omitempty"` + EnableWiki *bool `json:"has_wiki,omitempty"` + Homepage *string `json:"homepage,omitempty"` + IsTemplate *bool `json:"is_template,omitempty"` + SecurityAndAnalysis *SecurityAndAnalysisInput `json:"security_and_analysis,omitempty"` + Visibility *string `json:"visibility,omitempty"` } func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobra.Command { @@ -120,6 +124,22 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr When the %[1]s--visibility%[1]s flag is used, %[1]s--accept-visibility-change-consequences%[1]s flag is required. For information on all the potential consequences, see + + For merge commit (%[1]s--enable-merge-commit%[1]s) and squash merge (%[1]s--enable-squash-merge%[1]s), + following are the valid combinations to set their respective default title and message options: + + - Merge commit (%[1]s--merge-commit-title%[1]s and %[1]s--merge-commit-message%[1]s respectively): + - MERGE_MESSAGE and PR_TITLE (default message) + - PR_TITLE and BLANK (pull request title) + - PR_TITLE and PR_BODY (pull request title and description) + + - Squash merge (%[1]s--squash-merge-commit-title%[1]s and %[1]s--squash-merge-commit-message%[1]s respectively): + - COMMIT_OR_PR_TITLE and COMMIT_MESSAGES (default message) + - PR_TITLE and BLANK (pull request title) + - PR_TITLE and PR_BODY (pull request title and description) + - PR_TITLE and COMMIT_MESSAGES (pull request title and commit details) + + Note: To set the message option, its respective title is required to be set also. `, "`"), Args: cobra.MaximumNArgs(1), Example: heredoc.Doc(` @@ -128,6 +148,12 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr # disable projects gh repo edit --enable-projects=false + + # Set merge commit default commit message (pull request title and description) + $ gh repo edit --merge-commit-title=PR_TITLE --merge-commit-message=PR_BODY + + # Set squash merge commit default message (pull request title and commit details) + $ gh repo edit --squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES `), RunE: func(cmd *cobra.Command, args []string) error { if len(args) > 0 { @@ -166,6 +192,14 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr opts.Edits.SecurityAndAnalysis = transformSecurityAndAnalysisOpts(opts) } + if opts.Edits.MergeCommitMessage != "" && opts.Edits.MergeCommitTitle == "" { + return cmdutil.FlagErrorf("`--merge-commit-message` must be used in conjunction with `--merge-commit-title`") + } + + if opts.Edits.SquashMergeCommitMessage != "" && opts.Edits.SquashMergeCommitTitle == "" { + return cmdutil.FlagErrorf("`--squash-merge-commit-message` must be used in conjunction with `--squash-merge-commit-title`") + } + if runF != nil { return runF(opts) } @@ -182,8 +216,15 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableProjects, "enable-projects", "", "Enable projects in the repository") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableWiki, "enable-wiki", "", "Enable wiki in the repository") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableDiscussions, "enable-discussions", "", "Enable discussions in the repository") + cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableMergeCommit, "enable-merge-commit", "", "Enable merging pull requests via merge commit") + cmdutil.StringEnumFlag(cmd, &opts.Edits.MergeCommitTitle, "merge-commit-title", "", "", []string{"PR_TITLE", "MERGE_MESSAGE"}, "Default value for a merge commit title") + cmdutil.StringEnumFlag(cmd, &opts.Edits.MergeCommitMessage, "merge-commit-message", "", "", []string{"PR_BODY", "PR_TITLE", "BLANK"}, "Default value for a merge commit message") + cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableSquashMerge, "enable-squash-merge", "", "Enable merging pull requests via squashed commit") + cmdutil.StringEnumFlag(cmd, &opts.Edits.SquashMergeCommitTitle, "squash-merge-commit-title", "", "", []string{"PR_TITLE", "COMMIT_OR_PR_TITLE"}, "Default value for a squash merge commit title") + cmdutil.StringEnumFlag(cmd, &opts.Edits.SquashMergeCommitMessage, "squash-merge-commit-message", "", "", []string{"PR_BODY", "COMMIT_MESSAGES", "BLANK"}, "Default value for a squash merge commit message") + cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableRebaseMerge, "enable-rebase-merge", "", "Enable merging pull requests via rebase") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableAutoMerge, "enable-auto-merge", "", "Enable auto-merge functionality") cmdutil.NilBoolFlag(cmd, &opts.Edits.enableAdvancedSecurity, "enable-advanced-security", "", "Enable advanced security in the repository") diff --git a/pkg/cmd/repo/edit/edit_test.go b/pkg/cmd/repo/edit/edit_test.go index 868e300fa..ed4aa68c1 100644 --- a/pkg/cmd/repo/edit/edit_test.go +++ b/pkg/cmd/repo/edit/edit_test.go @@ -91,6 +91,145 @@ func TestNewCmdEdit(t *testing.T) { }, }, }, + { + name: "enable merge commit", + args: "--enable-merge-commit=true", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + EnableMergeCommit: bp(true), + }, + }, + }, + { + name: "disable merge commit", + args: "--enable-merge-commit=false", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + EnableMergeCommit: bp(false), + }, + }, + }, + { + name: "set merge commit default commit message (default message)", + args: "--merge-commit-title=MERGE_MESSAGE --merge-commit-message=PR_TITLE", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + MergeCommitTitle: "MERGE_MESSAGE", + MergeCommitMessage: "PR_TITLE", + }, + }, + }, + { + name: "set merge commit default commit message (pull request title)", + args: "--merge-commit-title=PR_TITLE --merge-commit-message=BLANK", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + MergeCommitTitle: "PR_TITLE", + MergeCommitMessage: "BLANK", + }, + }, + }, + { + name: "set merge commit default commit message (pull request title and description)", + args: "--merge-commit-title=PR_TITLE --merge-commit-message=PR_BODY", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + MergeCommitTitle: "PR_TITLE", + MergeCommitMessage: "PR_BODY", + }, + }, + }, + { + name: "set merge commit default commit message (message flag only)", + args: "--merge-commit-message=BLANK", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + MergeCommitMessage: "BLANK", + }, + }, + wantErr: "`--merge-commit-message` must be used in conjunction with `--merge-commit-title`", + }, + { + name: "enable squash merge commit", + args: "--enable-squash-merge=true", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + EnableSquashMerge: bp(true), + }, + }, + }, + { + name: "disable squash merge commit", + args: "--enable-squash-merge=false", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + EnableSquashMerge: bp(false), + }, + }, + }, + { + name: "set squash merge commit default commit message (default message)", + args: "--squash-merge-commit-title=COMMIT_OR_PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + SquashMergeCommitTitle: "COMMIT_OR_PR_TITLE", + SquashMergeCommitMessage: "COMMIT_MESSAGES", + }, + }, + }, + { + name: "set squash merge commit default commit message (pull request title)", + args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=BLANK", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + SquashMergeCommitTitle: "PR_TITLE", + SquashMergeCommitMessage: "BLANK", + }, + }, + }, + { + name: "set squash merge commit default commit message (pull request title and description)", + args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=PR_BODY", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + SquashMergeCommitTitle: "PR_TITLE", + SquashMergeCommitMessage: "PR_BODY", + }, + }, + }, + { + name: "set squash merge commit default commit message (pull request title and commit details)", + args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + SquashMergeCommitTitle: "PR_TITLE", + SquashMergeCommitMessage: "COMMIT_MESSAGES", + }, + }, + }, + { + name: "set squash merge commit default commit message (message flag only)", + args: "--squash-merge-commit-message=COMMIT_MESSAGES", + wantOpts: EditOptions{ + Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), + Edits: EditRepositoryInput{ + SquashMergeCommitMessage: "COMMIT_MESSAGES", + }, + }, + wantErr: "`--squash-merge-commit-message` must be used in conjunction with `--squash-merge-commit-title`", + }, } for _, tt := range tests { @@ -300,7 +439,8 @@ func Test_editRun_interactive(t *testing.T) { "Template Repository", "Topics", "Visibility", - "Wikis"} + "Wikis", + } tests := []struct { name string From 6c21ab2284aed52fa92020590fdfbb9e2d8f3475 Mon Sep 17 00:00:00 2001 From: Tim Rogers Date: Mon, 3 Feb 2025 11:49:21 +0000 Subject: [PATCH 092/100] Error when `gh repo rename` is used with a new repo name that contains an owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `gh repo rename` can only rename a repo, but not change its owner (i.e. transfer it). As flagged in #10034, at the moment, the CLI behaves like it can do this, and produces weird results. If I ask to rename my repo to `polyseam/frappe-containers`: ```bash gh repo rename polyseam/frappe-containers ``` ..the preview suggests that it will do the right thing: ``` ? Rename polyseam/containers to polyseam/frappe-containers? Yes ``` ...but the repo gets renamed wrongly: ``` ✓ Renamed repository polyseam/polyseam-frappe-containers ``` This adds an upfront validation that looks for a slash and tells you that the command can't transfer a repo. Fixes #10034. --- .../repo/repo-rename-transfer-ownership.txtar | 9 +++++++++ pkg/cmd/repo/rename/rename.go | 7 ++++++- pkg/cmd/repo/rename/rename_test.go | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 acceptance/testdata/repo/repo-rename-transfer-ownership.txtar diff --git a/acceptance/testdata/repo/repo-rename-transfer-ownership.txtar b/acceptance/testdata/repo/repo-rename-transfer-ownership.txtar new file mode 100644 index 000000000..6b1a3e3fb --- /dev/null +++ b/acceptance/testdata/repo/repo-rename-transfer-ownership.txtar @@ -0,0 +1,9 @@ +# Create a repository with a file so it has a default branch +exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private + +# Attempt to rename the repo with a slash in the name +! exec gh repo rename $ORG/new-name --repo=$ORG/$SCRIPT_NAME-$RANDOM_STRING --yes +stderr 'New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see .' + +# Defer repo deletion +defer gh repo delete $ORG/$SCRIPT_NAME-$RANDOM_STRING --yes \ No newline at end of file diff --git a/pkg/cmd/repo/rename/rename.go b/pkg/cmd/repo/rename/rename.go index 74523e2e7..172b8b552 100644 --- a/pkg/cmd/repo/rename/rename.go +++ b/pkg/cmd/repo/rename/rename.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" @@ -58,7 +59,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co with %[1]s--repo%[1]s is renamed. To transfer repository ownership to another user account or organization, - you must follow additional steps on GitHub.com + you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see: @@ -124,6 +125,10 @@ func renameRun(opts *RenameOptions) error { } } + if strings.Contains(newRepoName, "/") { + return fmt.Errorf("New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see .") + } + if opts.DoConfirm { var confirmed bool if confirmed, err = opts.Prompter.Confirm(fmt.Sprintf( diff --git a/pkg/cmd/repo/rename/rename_test.go b/pkg/cmd/repo/rename/rename_test.go index da40e51b0..e33e14438 100644 --- a/pkg/cmd/repo/rename/rename_test.go +++ b/pkg/cmd/repo/rename/rename_test.go @@ -113,6 +113,8 @@ func TestRenameRun(t *testing.T) { promptStubs func(*prompter.MockPrompter) wantOut string tty bool + wantErr bool + errMsg string }{ { name: "none argument", @@ -217,6 +219,16 @@ func TestRenameRun(t *testing.T) { }, wantOut: "", }, + + { + name: "error on name with slash", + tty: true, + opts: RenameOptions{ + newRepoSelector: "org/new-name", + }, + wantErr: true, + errMsg: "New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see .", + }, } for _, tt := range testCases { @@ -268,6 +280,10 @@ func TestRenameRun(t *testing.T) { t.Run(tt.name, func(t *testing.T) { defer reg.Verify(t) err := renameRun(&tt.opts) + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } assert.NoError(t, err) assert.Equal(t, tt.wantOut, stdout.String()) }) From 430e5abe2550c7a91c7c30ece2de57cb2a6889c6 Mon Sep 17 00:00:00 2001 From: Tim Rogers Date: Mon, 3 Feb 2025 12:04:42 +0000 Subject: [PATCH 093/100] Drop accidental help text change --- pkg/cmd/repo/rename/rename.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/repo/rename/rename.go b/pkg/cmd/repo/rename/rename.go index 172b8b552..e50095d76 100644 --- a/pkg/cmd/repo/rename/rename.go +++ b/pkg/cmd/repo/rename/rename.go @@ -59,7 +59,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co with %[1]s--repo%[1]s is renamed. To transfer repository ownership to another user account or organization, - you must follow additional steps on GitHub.com. + you must follow additional steps on GitHub.com For more information on transferring repository ownership, see: From 5f54201fc5189fa41dfcb60bf6419b94fdbfa7ed Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 3 Feb 2025 15:25:15 -0500 Subject: [PATCH 094/100] Ensure item iteration field is populated in tests Updating tests to ensure `ProjectV2ItemFieldIterationValue` actually populates `IterationId` appropriately. --- pkg/cmd/project/shared/queries/export_data_test.go | 3 ++- pkg/cmd/project/shared/queries/queries_test.go | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/project/shared/queries/export_data_test.go b/pkg/cmd/project/shared/queries/export_data_test.go index c9f991ed4..25e3fbace 100644 --- a/pkg/cmd/project/shared/queries/export_data_test.go +++ b/pkg/cmd/project/shared/queries/export_data_test.go @@ -295,6 +295,7 @@ func TestJSONProjectItem_DraftIssue_ProjectV2ItemFieldIterationValue(t *testing. iterationFieldValue := FieldValueNodes{Type: "ProjectV2ItemFieldIterationValue"} iterationFieldValue.ProjectV2ItemFieldIterationValue.Title = "Iteration Title" iterationFieldValue.ProjectV2ItemFieldIterationValue.Field = iterationField + iterationFieldValue.ProjectV2ItemFieldIterationValue.IterationId = "iterationId" draftIssue := ProjectItem{ Id: "draftIssueId", @@ -321,7 +322,7 @@ func TestJSONProjectItem_DraftIssue_ProjectV2ItemFieldIterationValue(t *testing. assert.NoError(t, err) assert.JSONEq( t, - `{"items":[{"sprint":{"title":"Iteration Title","startDate":"","duration":0,"iterationId":""},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title","id":"draftIssueId"},"id":"draftIssueId"}],"totalCount":5}`, + `{"items":[{"sprint":{"title":"Iteration Title","startDate":"","duration":0,"iterationId":"iterationId"},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title","id":"draftIssueId"},"id":"draftIssueId"}],"totalCount":5}`, string(out)) } diff --git a/pkg/cmd/project/shared/queries/queries_test.go b/pkg/cmd/project/shared/queries/queries_test.go index bfdd4d95c..57ec9ed02 100644 --- a/pkg/cmd/project/shared/queries/queries_test.go +++ b/pkg/cmd/project/shared/queries/queries_test.go @@ -395,8 +395,9 @@ func TestProjectItems_FieldTitle(t *testing.T) { "fieldValues": map[string]interface{}{ "nodes": []map[string]interface{}{ { - "__typename": "ProjectV2ItemFieldIterationValue", - "title": "Iteration Title 1", + "__typename": "ProjectV2ItemFieldIterationValue", + "title": "Iteration Title 1", + "iterationId": "iterationId1", }, { "__typename": "ProjectV2ItemFieldMilestoneValue", @@ -426,6 +427,7 @@ func TestProjectItems_FieldTitle(t *testing.T) { assert.Len(t, project.Items.Nodes, 1) assert.Len(t, project.Items.Nodes[0].FieldValues.Nodes, 2) assert.Equal(t, project.Items.Nodes[0].FieldValues.Nodes[0].ProjectV2ItemFieldIterationValue.Title, "Iteration Title 1") + assert.Equal(t, project.Items.Nodes[0].FieldValues.Nodes[0].ProjectV2ItemFieldIterationValue.IterationId, "iterationId1") assert.Equal(t, project.Items.Nodes[0].FieldValues.Nodes[1].ProjectV2ItemFieldMilestoneValue.Milestone.Title, "Milestone Title 1") } From 800a645e0e69ad026f03abacb7e724bb5a6b0857 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Tue, 4 Feb 2025 18:16:13 +0500 Subject: [PATCH 095/100] [gh config] Escape pipe symbol in Long desc for website manual --- internal/docs/markdown.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/docs/markdown.go b/internal/docs/markdown.go index 7ae8c6862..b7018c640 100644 --- a/internal/docs/markdown.go +++ b/internal/docs/markdown.go @@ -142,7 +142,8 @@ func genMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.UseLine()) } if hasLong { - fmt.Fprintf(w, "%s\n\n", cmd.Long) + longWithEscapedPipe := strings.ReplaceAll(cmd.Long, "|", "\\|") + fmt.Fprintf(w, "%s\n\n", longWithEscapedPipe) } for _, g := range root.GroupedCommands(cmd) { From a4b86bff777791b0c07bddbde80beb5994a7f88d Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Tue, 4 Feb 2025 18:48:56 +0500 Subject: [PATCH 096/100] Use HTML numeric code instead of escaping with slash --- internal/docs/markdown.go | 2 +- pkg/cmd/config/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/docs/markdown.go b/internal/docs/markdown.go index b7018c640..05d8686b8 100644 --- a/internal/docs/markdown.go +++ b/internal/docs/markdown.go @@ -142,7 +142,7 @@ func genMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.UseLine()) } if hasLong { - longWithEscapedPipe := strings.ReplaceAll(cmd.Long, "|", "\\|") + longWithEscapedPipe := strings.ReplaceAll(cmd.Long, "|", "|") fmt.Fprintf(w, "%s\n\n", longWithEscapedPipe) } diff --git a/pkg/cmd/config/config.go b/pkg/cmd/config/config.go index 2661c3369..66051f83a 100644 --- a/pkg/cmd/config/config.go +++ b/pkg/cmd/config/config.go @@ -16,7 +16,7 @@ import ( func NewCmdConfig(f *cmdutil.Factory) *cobra.Command { longDoc := strings.Builder{} longDoc.WriteString("Display or change configuration settings for gh.\n\n") - longDoc.WriteString("Current respected settings:\n") + longDoc.WriteString("Current respected settings:\n\n") for _, co := range config.Options { longDoc.WriteString(fmt.Sprintf("- `%s`: %s", co.Key, co.Description)) if len(co.AllowedValues) > 0 { From ae20633ba68634c15dc5301a7bdf48e3d59d8a16 Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:55:37 -0700 Subject: [PATCH 097/100] Revert "[gh repo edit] Allow setting commit message defaults" --- pkg/cmd/repo/edit/edit.go | 75 ++++------------- pkg/cmd/repo/edit/edit_test.go | 142 +-------------------------------- 2 files changed, 18 insertions(+), 199 deletions(-) diff --git a/pkg/cmd/repo/edit/edit.go b/pkg/cmd/repo/edit/edit.go index 79e32385b..daa700cfb 100644 --- a/pkg/cmd/repo/edit/edit.go +++ b/pkg/cmd/repo/edit/edit.go @@ -70,27 +70,23 @@ type EditRepositoryInput struct { enableSecretScanning *bool enableSecretScanningPushProtection *bool - AllowForking *bool `json:"allow_forking,omitempty"` - AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` - DefaultBranch *string `json:"default_branch,omitempty"` - DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` - Description *string `json:"description,omitempty"` - EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"` - EnableIssues *bool `json:"has_issues,omitempty"` - EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"` - MergeCommitTitle string `json:"merge_commit_title,omitempty"` - MergeCommitMessage string `json:"merge_commit_message,omitempty"` - EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"` - SquashMergeCommitTitle string `json:"squash_merge_commit_title,omitempty"` - SquashMergeCommitMessage string `json:"squash_merge_commit_message,omitempty"` - EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` - EnableProjects *bool `json:"has_projects,omitempty"` - EnableDiscussions *bool `json:"has_discussions,omitempty"` - EnableWiki *bool `json:"has_wiki,omitempty"` - Homepage *string `json:"homepage,omitempty"` - IsTemplate *bool `json:"is_template,omitempty"` - SecurityAndAnalysis *SecurityAndAnalysisInput `json:"security_and_analysis,omitempty"` - Visibility *string `json:"visibility,omitempty"` + AllowForking *bool `json:"allow_forking,omitempty"` + AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` + DefaultBranch *string `json:"default_branch,omitempty"` + DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` + Description *string `json:"description,omitempty"` + EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"` + EnableIssues *bool `json:"has_issues,omitempty"` + EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"` + EnableProjects *bool `json:"has_projects,omitempty"` + EnableDiscussions *bool `json:"has_discussions,omitempty"` + EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` + EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"` + EnableWiki *bool `json:"has_wiki,omitempty"` + Homepage *string `json:"homepage,omitempty"` + IsTemplate *bool `json:"is_template,omitempty"` + SecurityAndAnalysis *SecurityAndAnalysisInput `json:"security_and_analysis,omitempty"` + Visibility *string `json:"visibility,omitempty"` } func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobra.Command { @@ -124,22 +120,6 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr When the %[1]s--visibility%[1]s flag is used, %[1]s--accept-visibility-change-consequences%[1]s flag is required. For information on all the potential consequences, see - - For merge commit (%[1]s--enable-merge-commit%[1]s) and squash merge (%[1]s--enable-squash-merge%[1]s), - following are the valid combinations to set their respective default title and message options: - - - Merge commit (%[1]s--merge-commit-title%[1]s and %[1]s--merge-commit-message%[1]s respectively): - - MERGE_MESSAGE and PR_TITLE (default message) - - PR_TITLE and BLANK (pull request title) - - PR_TITLE and PR_BODY (pull request title and description) - - - Squash merge (%[1]s--squash-merge-commit-title%[1]s and %[1]s--squash-merge-commit-message%[1]s respectively): - - COMMIT_OR_PR_TITLE and COMMIT_MESSAGES (default message) - - PR_TITLE and BLANK (pull request title) - - PR_TITLE and PR_BODY (pull request title and description) - - PR_TITLE and COMMIT_MESSAGES (pull request title and commit details) - - Note: To set the message option, its respective title is required to be set also. `, "`"), Args: cobra.MaximumNArgs(1), Example: heredoc.Doc(` @@ -148,12 +128,6 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr # disable projects gh repo edit --enable-projects=false - - # Set merge commit default commit message (pull request title and description) - $ gh repo edit --merge-commit-title=PR_TITLE --merge-commit-message=PR_BODY - - # Set squash merge commit default message (pull request title and commit details) - $ gh repo edit --squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES `), RunE: func(cmd *cobra.Command, args []string) error { if len(args) > 0 { @@ -192,14 +166,6 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr opts.Edits.SecurityAndAnalysis = transformSecurityAndAnalysisOpts(opts) } - if opts.Edits.MergeCommitMessage != "" && opts.Edits.MergeCommitTitle == "" { - return cmdutil.FlagErrorf("`--merge-commit-message` must be used in conjunction with `--merge-commit-title`") - } - - if opts.Edits.SquashMergeCommitMessage != "" && opts.Edits.SquashMergeCommitTitle == "" { - return cmdutil.FlagErrorf("`--squash-merge-commit-message` must be used in conjunction with `--squash-merge-commit-title`") - } - if runF != nil { return runF(opts) } @@ -216,15 +182,8 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableProjects, "enable-projects", "", "Enable projects in the repository") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableWiki, "enable-wiki", "", "Enable wiki in the repository") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableDiscussions, "enable-discussions", "", "Enable discussions in the repository") - cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableMergeCommit, "enable-merge-commit", "", "Enable merging pull requests via merge commit") - cmdutil.StringEnumFlag(cmd, &opts.Edits.MergeCommitTitle, "merge-commit-title", "", "", []string{"PR_TITLE", "MERGE_MESSAGE"}, "Default value for a merge commit title") - cmdutil.StringEnumFlag(cmd, &opts.Edits.MergeCommitMessage, "merge-commit-message", "", "", []string{"PR_BODY", "PR_TITLE", "BLANK"}, "Default value for a merge commit message") - cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableSquashMerge, "enable-squash-merge", "", "Enable merging pull requests via squashed commit") - cmdutil.StringEnumFlag(cmd, &opts.Edits.SquashMergeCommitTitle, "squash-merge-commit-title", "", "", []string{"PR_TITLE", "COMMIT_OR_PR_TITLE"}, "Default value for a squash merge commit title") - cmdutil.StringEnumFlag(cmd, &opts.Edits.SquashMergeCommitMessage, "squash-merge-commit-message", "", "", []string{"PR_BODY", "COMMIT_MESSAGES", "BLANK"}, "Default value for a squash merge commit message") - cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableRebaseMerge, "enable-rebase-merge", "", "Enable merging pull requests via rebase") cmdutil.NilBoolFlag(cmd, &opts.Edits.EnableAutoMerge, "enable-auto-merge", "", "Enable auto-merge functionality") cmdutil.NilBoolFlag(cmd, &opts.Edits.enableAdvancedSecurity, "enable-advanced-security", "", "Enable advanced security in the repository") diff --git a/pkg/cmd/repo/edit/edit_test.go b/pkg/cmd/repo/edit/edit_test.go index ed4aa68c1..868e300fa 100644 --- a/pkg/cmd/repo/edit/edit_test.go +++ b/pkg/cmd/repo/edit/edit_test.go @@ -91,145 +91,6 @@ func TestNewCmdEdit(t *testing.T) { }, }, }, - { - name: "enable merge commit", - args: "--enable-merge-commit=true", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - EnableMergeCommit: bp(true), - }, - }, - }, - { - name: "disable merge commit", - args: "--enable-merge-commit=false", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - EnableMergeCommit: bp(false), - }, - }, - }, - { - name: "set merge commit default commit message (default message)", - args: "--merge-commit-title=MERGE_MESSAGE --merge-commit-message=PR_TITLE", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - MergeCommitTitle: "MERGE_MESSAGE", - MergeCommitMessage: "PR_TITLE", - }, - }, - }, - { - name: "set merge commit default commit message (pull request title)", - args: "--merge-commit-title=PR_TITLE --merge-commit-message=BLANK", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - MergeCommitTitle: "PR_TITLE", - MergeCommitMessage: "BLANK", - }, - }, - }, - { - name: "set merge commit default commit message (pull request title and description)", - args: "--merge-commit-title=PR_TITLE --merge-commit-message=PR_BODY", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - MergeCommitTitle: "PR_TITLE", - MergeCommitMessage: "PR_BODY", - }, - }, - }, - { - name: "set merge commit default commit message (message flag only)", - args: "--merge-commit-message=BLANK", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - MergeCommitMessage: "BLANK", - }, - }, - wantErr: "`--merge-commit-message` must be used in conjunction with `--merge-commit-title`", - }, - { - name: "enable squash merge commit", - args: "--enable-squash-merge=true", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - EnableSquashMerge: bp(true), - }, - }, - }, - { - name: "disable squash merge commit", - args: "--enable-squash-merge=false", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - EnableSquashMerge: bp(false), - }, - }, - }, - { - name: "set squash merge commit default commit message (default message)", - args: "--squash-merge-commit-title=COMMIT_OR_PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - SquashMergeCommitTitle: "COMMIT_OR_PR_TITLE", - SquashMergeCommitMessage: "COMMIT_MESSAGES", - }, - }, - }, - { - name: "set squash merge commit default commit message (pull request title)", - args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=BLANK", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - SquashMergeCommitTitle: "PR_TITLE", - SquashMergeCommitMessage: "BLANK", - }, - }, - }, - { - name: "set squash merge commit default commit message (pull request title and description)", - args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=PR_BODY", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - SquashMergeCommitTitle: "PR_TITLE", - SquashMergeCommitMessage: "PR_BODY", - }, - }, - }, - { - name: "set squash merge commit default commit message (pull request title and commit details)", - args: "--squash-merge-commit-title=PR_TITLE --squash-merge-commit-message=COMMIT_MESSAGES", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - SquashMergeCommitTitle: "PR_TITLE", - SquashMergeCommitMessage: "COMMIT_MESSAGES", - }, - }, - }, - { - name: "set squash merge commit default commit message (message flag only)", - args: "--squash-merge-commit-message=COMMIT_MESSAGES", - wantOpts: EditOptions{ - Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"), - Edits: EditRepositoryInput{ - SquashMergeCommitMessage: "COMMIT_MESSAGES", - }, - }, - wantErr: "`--squash-merge-commit-message` must be used in conjunction with `--squash-merge-commit-title`", - }, } for _, tt := range tests { @@ -439,8 +300,7 @@ func Test_editRun_interactive(t *testing.T) { "Template Repository", "Topics", "Visibility", - "Wikis", - } + "Wikis"} tests := []struct { name string From 91c75596ee19b13fd9dea799388b80b30d5a7db0 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Wed, 5 Feb 2025 12:14:39 +0500 Subject: [PATCH 098/100] Address review comments --- pkg/cmd/extension/command.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/extension/command.go b/pkg/cmd/extension/command.go index ae9a917a6..93c385374 100644 --- a/pkg/cmd/extension/command.go +++ b/pkg/cmd/extension/command.go @@ -306,8 +306,9 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command { that it's a binary extension i.e. prebuilt binaries provided as part of the release. In the absence of a release, the repository itself is cloned assuming that it's a script extension i.e. prebuilt executable or script exists on its root. + The %[1]s--pin%[1]s flag may be used to specify a tag or commit for binary and script - extensions respectively, the latest one is used otherwise. + extensions respectively, the latest version is used otherwise. For local repositories, often used while developing extensions, use %[1]s.%[1]s as the value of the repository argument. Note the following: From 50e7b00327520ffdbce9f637f54f3d3f1188c344 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Wed, 5 Feb 2025 12:21:29 +0500 Subject: [PATCH 099/100] Address review comments --- pkg/cmd/extension/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index e83a3ed22..b7f1f1c0c 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -268,7 +268,7 @@ func (m *Manager) Install(repo ghrepo.Interface, target string) error { return err } if !hs { - return fmt.Errorf("extension is not installable: no acceptable release artifact or script found in %s", repo) + return fmt.Errorf("extension is not installable: no usable release artifact or script found in %s", repo) } return m.installGit(repo, target) From 09ec38e8d776790089eac7865c6cfcf0a0e79834 Mon Sep 17 00:00:00 2001 From: Azeem Sajid Date: Thu, 6 Feb 2025 22:50:26 +0500 Subject: [PATCH 100/100] Update tests --- pkg/cmd/cache/delete/delete_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/cache/delete/delete_test.go b/pkg/cmd/cache/delete/delete_test.go index a9ec88dea..57ecb9381 100644 --- a/pkg/cmd/cache/delete/delete_test.go +++ b/pkg/cmd/cache/delete/delete_test.go @@ -217,7 +217,7 @@ func TestDeleteRun(t *testing.T) { }, { name: "no caches to delete when deleting all", - opts: DeleteOptions{Identifier: "123", DeleteAll: true}, + opts: DeleteOptions{DeleteAll: true}, stubs: func(reg *httpmock.Registry) { reg.Register( httpmock.REST("GET", "repos/OWNER/REPO/actions/caches"), @@ -233,7 +233,7 @@ func TestDeleteRun(t *testing.T) { }, { name: "no caches to delete when deleting all but succeed on no cache tty", - opts: DeleteOptions{Identifier: "123", DeleteAll: true, SucceedOnNoCaches: true}, + opts: DeleteOptions{DeleteAll: true, SucceedOnNoCaches: true}, stubs: func(reg *httpmock.Registry) { reg.Register( httpmock.REST("GET", "repos/OWNER/REPO/actions/caches"), @@ -249,7 +249,7 @@ func TestDeleteRun(t *testing.T) { }, { name: "no caches to delete when deleting all but succeed on no cache non-tty", - opts: DeleteOptions{Identifier: "123", DeleteAll: true, SucceedOnNoCaches: true}, + opts: DeleteOptions{DeleteAll: true, SucceedOnNoCaches: true}, stubs: func(reg *httpmock.Registry) { reg.Register( httpmock.REST("GET", "repos/OWNER/REPO/actions/caches"), @@ -261,7 +261,7 @@ func TestDeleteRun(t *testing.T) { }, tty: false, wantErr: false, - wantStdout: "", + wantStdout: "✓ No caches to delete\n", }, }