better error handle

This commit is contained in:
Yuki Osaki 2021-10-30 00:37:45 +09:00
parent a71db3355a
commit 61d08dc4af
3 changed files with 16 additions and 27 deletions

View file

@ -2,34 +2,31 @@ package commands
import (
"encoding/json"
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
func (c *GitCommand) GithubMostRecentPRs() map[string]models.GithubPullRequest {
func (c *GitCommand) GithubMostRecentPRs() (map[string]models.GithubPullRequest, error) {
commandOutput, err := c.OSCommand.RunCommandWithOutput("gh pr list --limit 50 --state all --json state,url,number,headRefName,headRepositoryOwner")
if err != nil {
fmt.Println(1, err)
return nil
return nil, err
}
prs := []models.GithubPullRequest{}
err = json.Unmarshal([]byte(commandOutput), &prs)
if err != nil {
fmt.Println(2, err)
return nil
return nil, err
}
res := map[string]models.GithubPullRequest{}
for _, pr := range prs {
res[pr.HeadRepositoryOwner.Login+":"+pr.HeadRefName] = pr
}
return res
return res, nil
}
func (c *GitCommand) InjectGithubPullRequests(prs map[string]models.GithubPullRequest, branches []*models.Branch) bool {
func (c *GitCommand) FoundBranchWithGithubPullRequest(prs map[string]models.GithubPullRequest, branches []*models.Branch) bool {
if len(prs) == 0 {
return false
}
@ -42,23 +39,11 @@ func (c *GitCommand) InjectGithubPullRequests(prs map[string]models.GithubPullRe
foundBranchWithGithubPullRequest := false
for _, branch := range branches {
if branch.UpstreamName == "" {
continue
}
_, has_pr := presentation.GetPr(branch, remotesToOwnersMap, prs)
remoteAndName := strings.SplitN(branch.UpstreamName, "/", 2)
owner, foundRemoteOwner := remotesToOwnersMap[remoteAndName[0]]
if len(remoteAndName) != 2 || !foundRemoteOwner {
continue
if has_pr {
foundBranchWithGithubPullRequest = true
}
pr, hasPr := prs[owner+":"+remoteAndName[1]]
if !hasPr {
continue
}
foundBranchWithGithubPullRequest = true
branch.PR = &pr
}
return foundBranchWithGithubPullRequest

View file

@ -68,8 +68,9 @@ func TestGithubMostRecentPRs(t *testing.T) {
return secureexec.Command("echo", s.response)
}
res := gitCmd.GithubMostRecentPRs()
res, _ := gitCmd.GithubMostRecentPRs()
assert.Equal(t, s.expect, res)
})
}
}

View file

@ -72,9 +72,12 @@ func (gui *Gui) refreshReflogCommitsConsideringStartup() {
})
go utils.Safe(func() {
// The github cli can be quite slow so we load the github PRs sparately
gui.refreshGithubPullRequests()
wg.Wait()
gui.State.BranchesWithGithubPullRequests = gui.GitCommand.FoundBranchWithGithubPullRequest(gui.State.GithubRecentPRs, gui.State.Branches)
if gui.State.BranchesWithGithubPullRequests {
gui.refreshGithubPullRequests()
}
wg.Wait()
_ = gui.postRefreshUpdate(gui.State.Contexts.Branches)
gui.refreshStatus()
})