1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge pull request #242 from baustinanki/github-issue-status

Add optional PR status display in Github module
This commit is contained in:
Chris Cummer 2018-07-14 20:57:04 -04:00 committed by GitHub
commit 46f42672e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

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

View File

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