Last active
January 24, 2016 18:34
-
-
Save adria0/684a9322cba50c06020d to your computer and use it in GitHub Desktop.
reviewer-quick-and-dirty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"github.com/google/go-github/github" | |
"golang.org/x/oauth2" | |
) | |
const ( | |
pagesize = 10 | |
requiredScore = 1 | |
token = "..." | |
owner = "amassanet" | |
repo = "reviewertest" | |
) | |
func processPullRequest(client *github.Client, pr *github.PullRequest) { | |
pr, _, _ := client.PullRequests.Get(owner,repo, *pr.Number) | |
// -- check if mergeable | |
if pr.Mergeable != nil { | |
if !(*pr.Mergeable) { | |
fmt.Println(" Discarded, not mergeable") | |
return | |
} | |
} else { | |
// I don't know WHY this field is not present :-? | |
fmt.Println(" No mergeable info") | |
} | |
// -- get combined status for the head commit of the pull request | |
head := *pr.Head.SHA | |
combinedStatus, _, _ := client.Repositories.GetCombinedStatus(owner, repo, head, nil) | |
if *combinedStatus.State != "success" { | |
fmt.Println(" Discarded, status is ", *combinedStatus.State) | |
return | |
} | |
// -- check if the comments score +1/-1 is ok | |
pullScore := 0 | |
pageNo := 0 | |
for { | |
opt := &github.IssueListCommentsOptions{ | |
ListOptions: github.ListOptions{PerPage: pagesize, Page: pageNo}, | |
} | |
comments, _, _ := client.Issues.ListComments(owner, repo, *pr.Number, opt) | |
for _, comment := range comments { | |
if comment.Body == nil { | |
continue | |
} | |
commentScore := 0 | |
if strings.Contains(*comment.Body, "+1") { | |
commentScore++ | |
} | |
if strings.Contains(*comment.Body, "-1") { | |
commentScore-- | |
} | |
pullScore += commentScore | |
} | |
if len(comments) < pagesize { | |
break | |
} | |
pageNo++ | |
} | |
if pullScore < requiredScore { | |
fmt.Println(" Discarded, score is ", pullScore) | |
return | |
} | |
// -- let's merge it | |
result, _, _ := client.PullRequests.Merge(owner, repo, *pr.Number, "Automatically merged with reviewer") | |
fmt.Printf(" MERGE!: merged=%v message=%v\n", *result.Merged, *result.Message) | |
} | |
func main() { | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: token}, | |
) | |
tc := oauth2.NewClient(oauth2.NoContext, ts) | |
client := github.NewClient(tc) | |
pageNo := 0 | |
for { | |
opt := &github.PullRequestListOptions{ | |
ListOptions: github.ListOptions{PerPage: pagesize, Page: pageNo}, | |
} | |
pullRequests, _, _ := client.PullRequests.List(owner, repo, opt) | |
for _, pullRequest := range pullRequests { | |
title := *pullRequest.Title | |
fmt.Println(title) | |
processPullRequest(client, &pullRequest) | |
} | |
if len(pullRequests) < pagesize { | |
break | |
} | |
pageNo++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment