diff --git a/pkg/commands/github.go b/pkg/commands/github.go index c34fce824..b9053c897 100644 --- a/pkg/commands/github.go +++ b/pkg/commands/github.go @@ -7,26 +7,22 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" ) -func (c *GitCommand) GithubMostRecentPRs() (map[string]models.GithubPullRequest, error) { +func (c *GitCommand) GithubMostRecentPRs() ([]*models.GithubPullRequest, error) { commandOutput, err := c.OSCommand.RunCommandWithOutput("gh pr list --limit 50 --state all --json state,url,number,headRefName,headRepositoryOwner") if err != nil { return nil, err } - prs := []models.GithubPullRequest{} + prs := []*models.GithubPullRequest{} err = json.Unmarshal([]byte(commandOutput), &prs) if err != nil { return nil, err } - res := map[string]models.GithubPullRequest{} - for _, pr := range prs { - res[pr.HeadRepositoryOwner.Login+":"+pr.HeadRefName] = pr - } - return res, nil + return prs, nil } -func (c *GitCommand) GenerateGithubPullRequestMap(prs map[string]models.GithubPullRequest, branches []*models.Branch) (map[*models.Branch]*models.GithubPullRequest, bool) { +func (c *GitCommand) GenerateGithubPullRequestMap(prs []*models.GithubPullRequest, branches []*models.Branch) (map[*models.Branch]*models.GithubPullRequest, bool) { res := map[*models.Branch]*models.GithubPullRequest{} if len(prs) == 0 { @@ -40,6 +36,12 @@ func (c *GitCommand) GenerateGithubPullRequestMap(prs map[string]models.GithubPu foundBranchWithGithubPullRequest := false + prWithStringKey := map[string]models.GithubPullRequest{} + + for _, pr := range prs { + prWithStringKey[pr.HeadRepositoryOwner.Login+":"+pr.HeadRefName] = *pr + } + for _, branch := range branches { if branch.UpstreamName == "" { continue @@ -51,7 +53,7 @@ func (c *GitCommand) GenerateGithubPullRequestMap(prs map[string]models.GithubPu continue } - pr, hasPr := prs[owner+":"+remoteAndName[1]] + pr, hasPr := prWithStringKey[owner+":"+remoteAndName[1]] if !hasPr { continue } diff --git a/pkg/commands/github_test.go b/pkg/commands/github_test.go index e5dc89d20..d8c09f970 100644 --- a/pkg/commands/github_test.go +++ b/pkg/commands/github_test.go @@ -13,7 +13,7 @@ func TestGithubMostRecentPRs(t *testing.T) { scenarios := []struct { testName string response string - expect map[string]models.GithubPullRequest + expect []*models.GithubPullRequest }{ { "no response", @@ -28,7 +28,7 @@ func TestGithubMostRecentPRs(t *testing.T) { { "empty response", "[]", - map[string]models.GithubPullRequest{}, + []*models.GithubPullRequest{}, }, { "response with data", @@ -43,19 +43,13 @@ func TestGithubMostRecentPRs(t *testing.T) { "login": "jesseduffield" } }]`, - map[string]models.GithubPullRequest{ - "jesseduffield:command-log-2": { - HeadRefName: "command-log-2", - Number: 1249, - State: "MERGED", - Url: "https://github.com/jesseduffield/lazygit/pull/1249", - HeadRepositoryOwner: models.GithubRepositoryOwner{ - ID: "MDQ6VXNlcjg0NTY2MzM=", - Name: "Jesse Duffield", - Login: "jesseduffield", - }, - }, - }, + []*models.GithubPullRequest{{ + HeadRefName: "command-log-2", + Number: 1249, + State: "MERGED", + Url: "https://github.com/jesseduffield/lazygit/pull/1249", + HeadRepositoryOwner: models.GithubRepositoryOwner{ID: "MDQ6VXNlcjg0NTY2MzM=", Name: "Jesse Duffield", Login: "jesseduffield"}, + }}, }, } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 3a9e6776e..eb84b4810 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -291,7 +291,7 @@ type guiState struct { CommitFileManager *filetree.CommitFileManager Submodules []*models.SubmoduleConfig Branches []*models.Branch - GithubRecentPRs map[string]models.GithubPullRequest + GithubRecentPRs []*models.GithubPullRequest BranchesWithGithubPullRequests bool Commits []*models.Commit StashEntries []*models.StashEntry