Skip to content

Commit

Permalink
Add optional PR status display in Github module
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
baustinanki committed Jul 13, 2018
1 parent 236005a commit 3ee6304
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
24 changes: 23 additions & 1 deletion github/display.go
Expand Up @@ -3,6 +3,7 @@ package github
import (
"fmt"

"github.com/google/go-github/github"
"github.com/senorprogrammer/wtf/wtf"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "? "
}
26 changes: 26 additions & 0 deletions github/github_repo.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 3ee6304

Please sign in to comment.