export GITHUB_TOKEN=<your github token>
go run find-stale-branches.go cucumber aruba
Last active
September 7, 2017 20:46
-
-
Save maxmeyer/2df331e2894dadee058e3083d272523c to your computer and use it in GitHub Desktop.
This parses stale branches in a given repository
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" | |
"sort" | |
"time" | |
"gopkg.in/cheggaaa/pb.v1" | |
"github.com/Sirupsen/logrus" | |
"github.com/google/go-github/github" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2" | |
) | |
type Branch struct { | |
Name string | |
Owner string | |
Repository string | |
LastModified time.Time | |
AuthorLastCommit string | |
CommiterLastCommit string | |
} | |
type ByLastModified []Branch | |
func (a ByLastModified) Len() int { return len(a) } | |
func (a ByLastModified) Swap(i, j int) { a[i], a[j] = a[j], a[i] } | |
func (a ByLastModified) Less(i, j int) bool { return a[i].LastModified.Before(a[j].LastModified) } | |
func main() { | |
token := os.Getenv("GITHUB_TOKEN") | |
if token == "" { | |
logrus.WithFields(logrus.Fields{ | |
"env_var": "GITHUB_TOKEN", | |
"message": "Token not found", | |
}).Fatal("Read github token") | |
} | |
ctx := context.Background() | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: token}, | |
) | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
if len(os.Args) < 3 { | |
logrus.WithFields(logrus.Fields{ | |
"message": "Owner and/or repository not found on command line", | |
}).Fatal("Read repository/owner from arguments") | |
} | |
owner := os.Args[1] | |
repo := os.Args[2] | |
branches, _, err := client.Repositories.ListBranches(ctx, owner, repo, nil) | |
if _, ok := err.(*github.RateLimitError); ok { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"message": "Hit rate limit", | |
}).Fatal("Listed branches in repository") | |
} | |
if err != nil { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"message": err.Error(), | |
}).Fatal("Listed branches in repository") | |
} | |
if len(branches) == 0 { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"message": "Listing returned no branches", | |
}).Fatal("Listed branches in repository") | |
} | |
unmergedBranches := []Branch{} | |
mergedBranches := []Branch{} | |
bar := pb.StartNew(len(branches)) | |
for _, b := range branches { | |
bar.Increment() | |
//b := branches[0] | |
branch, _, err := client.Repositories.GetBranch(ctx, owner, repo, *b.Name) | |
if err != nil { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"message": err.Error(), | |
}).Fatal("Read information about branch") | |
} | |
baseBranch := "master" | |
commits, _, err := client.Repositories.CompareCommits(ctx, owner, repo, baseBranch, *b.Name) | |
if err != nil { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"head_branch": *b.Name, | |
"base_branch": baseBranch, | |
"message": err.Error(), | |
}).Fatal("Compare branches") | |
} | |
ct, _, err := client.Repositories.GetCommit(ctx, owner, repo, *branch.Commit.SHA) | |
if err != nil { | |
logrus.WithFields(logrus.Fields{ | |
"owner": owner, | |
"repository": repo, | |
"sha": *branch.Commit.SHA, | |
"message": err.Error(), | |
}).Fatal("Requested information about commit") | |
} | |
author := *ct.Commit.Author.Name | |
committer := *ct.Commit.Committer.Name | |
timeLastModified := *ct.Commit.Author.Date | |
if *commits.TotalCommits > 0 { | |
unmergedBranches = append(unmergedBranches, Branch{ | |
Name: *b.Name, | |
Owner: owner, | |
Repository: repo, | |
AuthorLastCommit: author, | |
CommiterLastCommit: committer, | |
LastModified: timeLastModified, | |
}) | |
} else { | |
mergedBranches = append(mergedBranches, Branch{ | |
Name: *b.Name, | |
Owner: owner, | |
Repository: repo, | |
AuthorLastCommit: author, | |
CommiterLastCommit: committer, | |
LastModified: timeLastModified, | |
}) | |
} | |
} | |
bar.FinishPrint("Finished reading information about branches") | |
// Sort output | |
sort.Sort(ByLastModified(unmergedBranches)) | |
sort.Sort(ByLastModified(mergedBranches)) | |
formatString := "%60s %25s %25s %31s\n" | |
fmt.Println("#### Unmerged branches ####") | |
fmt.Printf(formatString, "branch", "author", "committer", "last modified") | |
for _, b := range unmergedBranches { | |
fmt.Printf(formatString, b.Repository+"/"+b.Owner+"/"+b.Name, b.AuthorLastCommit, b.CommiterLastCommit, b.LastModified) | |
} | |
fmt.Println("") | |
fmt.Println("") | |
fmt.Println("#### Merged branches ####") | |
fmt.Printf(formatString, "branch", "author", "committer", "last modified") | |
for _, b := range mergedBranches { | |
fmt.Printf(formatString, b.Repository+"/"+b.Owner+"/"+b.Name, b.AuthorLastCommit, b.CommiterLastCommit, b.LastModified) | |
} | |
fmt.Println("") | |
fmt.Println("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment