diff --git a/github/display.go b/github/display.go index 2fa4528e..b158ee29 100644 --- a/github/display.go +++ b/github/display.go @@ -3,6 +3,7 @@ package github import ( "fmt" + "github.com/google/go-github/github" "github.com/senorprogrammer/wtf/wtf" ) @@ -37,7 +38,7 @@ func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) s str := "" for _, pr := range prs { - str = str + fmt.Sprintf(" [green]%4d[white] %s\n", *pr.Number, *pr.Title) + str = str + fmt.Sprintf(" %s[green]%4d[white] %s\n", mergeString(pr), *pr.Number, *pr.Title) } return str @@ -72,3 +73,24 @@ func (widget *Widget) displayStats(repo *GithubRepo) string { func (widget *Widget) title(repo *GithubRepo) string { return fmt.Sprintf("[green]%s - %s[white]", repo.Owner, repo.Name) } + +func showStatus() bool { + return wtf.Config.UBool("wtf.mods.github.enableStatus", false) +} + +var mergeIcons = map[string]string{ + "dirty": "[red]![white] ", + "clean": "[green]✔[white] ", + "unstable": "[red]✖[white] ", + "blocked": "[red]✖[white] ", +} + +func mergeString(pr *github.PullRequest) string { + if !showStatus() { + return "" + } + if str, ok := mergeIcons[pr.GetMergeableState()]; ok { + return str + } + return "? " +} diff --git a/github/github_repo.go b/github/github_repo.go index af2db77e..80640f5a 100644 --- a/github/github_repo.go +++ b/github/github_repo.go @@ -103,9 +103,35 @@ func (repo *GithubRepo) myPullRequests(username string) []*ghb.PullRequest { } } + if showStatus() { + prs = repo.individualPRs(prs) + } + return prs } +// individualPRs takes a list of pull requests (presumably returned from +// github.PullRequests.List) and fetches them individually to get more detailed +// status info on each. see: https://developer.github.com/v3/git/#checking-mergeability-of-pull-requests +func (repo *GithubRepo) individualPRs(prs []*ghb.PullRequest) []*ghb.PullRequest { + github, err := repo.githubClient() + if err != nil { + return prs + } + + var ret []*ghb.PullRequest + for i := range prs { + pr, _, err := github.PullRequests.Get(context.Background(), repo.Owner, repo.Name, prs[i].GetNumber()) + if err != nil { + // worst case, just keep the original one + ret = append(ret, prs[i]) + } else { + ret = append(ret, pr) + } + } + return ret +} + // myReviewRequests returns a list of pull requests for which username has been // requested to do a code review func (repo *GithubRepo) myReviewRequests(username string) []*ghb.PullRequest {