Update GithubMostRecentPRs method

This commit is contained in:
Yuki Osaki 2021-10-31 15:14:41 +09:00
parent 9d774c2450
commit c1f3fd6b3f
3 changed files with 21 additions and 25 deletions

View file

@ -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
}

View file

@ -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"},
}},
},
}

View file

@ -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