Skip to content

Instantly share code, notes, and snippets.

@bltavares
Created October 17, 2014 14:28
Show Gist options
  • Save bltavares/b9e0685c68572c01585e to your computer and use it in GitHub Desktop.
Save bltavares/b9e0685c68572c01585e to your computer and use it in GitHub Desktop.
Contributor stats
package main
import (
"code.google.com/p/goauth2/oauth"
"fmt"
"github.com/google/go-github/github"
"os"
"strconv"
)
type SummarisedStat struct {
Author *github.Contributor
Additions *int
Deletions *int
Total *int
}
func (s SummarisedStat) Ratio() int {
return (*s.Additions - *s.Deletions) / *s.Total
}
func (s SummarisedStat) Summary() string {
return "+" + strconv.Itoa(*s.Additions) + " -" + strconv.Itoa(*s.Deletions)
}
func sumStats(contrib github.ContributorStats) *SummarisedStat {
additions := 0
deletions := 0
for _, weekly := range contrib.Weeks {
additions += *weekly.Additions
deletions += *weekly.Deletions
}
return &SummarisedStat{contrib.Author, &additions, &deletions, contrib.Total}
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage:", os.Args[0], "<user>", "<repo>")
os.Exit(1)
}
user := os.Args[1]
repo := os.Args[2]
fmt.Printf("Summary for: %s/%s\n\n", user, repo)
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: os.Getenv("GITHUB_API_TOKEN")},
}
client := github.NewClient(t.Client())
stats, _, err := client.Repositories.ListContributorsStats(user, repo)
if err != nil {
fmt.Println("error", err)
os.Exit(2)
}
for _, contrib := range stats {
summary := sumStats(contrib)
fmt.Println("Author:", *summary.Author.Login)
fmt.Println("Commits:", *summary.Total)
fmt.Println("Summary:", summary.Summary())
fmt.Println("Ratio:", summary.Ratio(), "lines per commit")
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment