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

Add optional PR status display in Github module

While spamming refresh on a pull request today to see if required
builds finished yet, it occurred to me that it'd be awesome to have
WTF tell me when a PR was ready to be merged. So, here it is! An
icon will now display next to PRs in the "My Pull Requests" section
detailing whether GitHub thinks they can be merged.

This is behind a new, opt-in config flag called "enableStatus",
due to the fact that in order to function, this feature has to hit
the GitHub API individually for each PR in order to get an updated
status check - there's a comment in the code with a link that
explains why (otherwise, `pr.GetMergeableState()` returns an empty
string). For a large number of PRs, this can slow down refreshes a
bit and _might_ even wind up rate limiting you (while testing I had
some instances of GH refusing to return me any repository info,
though it didn't actually give me an error, usually after I had
been spamming it with requests for 30 PRs in a row for a bit). So,
for that reason, use at your own risk (but it's probably fine).

I am not an emoji expert, so suggestions on the display are welcome
if you can think of anything awesome. A lot of the ones I tried
seemed to render funny and mess up spacing.
This commit is contained in:
Bryan Austin 2018-07-12 18:39:55 -07:00
parent 236005ab48
commit 3ee6304e63
2 changed files with 49 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package github
import ( import (
"fmt" "fmt"
"github.com/google/go-github/github"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
@ -37,7 +38,7 @@ func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) s
str := "" str := ""
for _, pr := range prs { 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 return str
@ -72,3 +73,24 @@ func (widget *Widget) displayStats(repo *GithubRepo) string {
func (widget *Widget) title(repo *GithubRepo) string { func (widget *Widget) title(repo *GithubRepo) string {
return fmt.Sprintf("[green]%s - %s[white]", repo.Owner, repo.Name) 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 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 // myReviewRequests returns a list of pull requests for which username has been
// requested to do a code review // requested to do a code review
func (repo *GithubRepo) myReviewRequests(username string) []*ghb.PullRequest { func (repo *GithubRepo) myReviewRequests(username string) []*ghb.PullRequest {