Skip to content

Instantly share code, notes, and snippets.

@benileo
Last active April 22, 2017 02:57
Show Gist options
  • Save benileo/c4f266151b464a523a97bf11c6c1f1b1 to your computer and use it in GitHub Desktop.
Save benileo/c4f266151b464a523a97bf11c6c1f1b1 to your computer and use it in GitHub Desktop.
My github contributions
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
)
type PullRequest struct {
RepositoryUrl string `json:"repository_url"`
}
type SearchResults struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
PR []PullRequest `json:"items"`
}
var (
url = "https://api.github.com/search/issues?q=type:pr+state:closed+author:benileo&per_page=1000&page=1"
)
type Contribution struct {
Repo string
Description string
Contributions int
FullName string
}
type ContributionSorter []*Contribution
func (c ContributionSorter) Len() int {
return len(c)
}
func (c ContributionSorter) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c ContributionSorter) Less(i, j int) bool {
return c[i].Contributions > c[j].Contributions
}
func main() {
response, err := http.Get(url)
fmt.Println(response.StatusCode)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var searchResults SearchResults
err = json.Unmarshal(body, &searchResults)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(searchResults)
var reposWithPRs map[string]*Contribution
reposWithPRs = make(map[string]*Contribution)
for _, pr := range searchResults.PR {
// dont include my repos - this is a hack.
if strings.Contains(pr.RepositoryUrl, "benileo") {
continue
}
blacklist := map[string]bool{
"https://api.github.com/repos/ryandury/VVV": true,
"https://api.github.com/retreatguru/rbg-connect": true,
}
if _, ok := blacklist[pr.RepositoryUrl]; ok {
continue
}
if _, ok := reposWithPRs[pr.RepositoryUrl]; !ok {
reposWithPRs[pr.RepositoryUrl] = &Contribution{}
}
}
fmt.Println(reposWithPRs)
for repoUrl, contribution := range reposWithPRs {
response, err := http.Get(repoUrl)
fmt.Println(response.StatusCode)
if response.StatusCode == 403 {
fmt.Println("rate limited")
os.Exit(1)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
body, _ := ioutil.ReadAll(response.Body)
type repoResp struct {
FullName string `json:"full_name"`
HtmlUrl string `json:"html_url"`
ContributorUrl string `json:"contributors_url"`
Description string `json:"description"`
}
var repoResults repoResp
err = json.Unmarshal(body, &repoResults)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(repoResults)
contribution.Repo = repoResults.HtmlUrl
contribution.Description = repoResults.Description
contribution.FullName = repoResults.FullName
var morePages bool
page := 1
for !morePages {
type ContResp struct {
Login string `json:"login"`
Contributions int `json:"contributions"`
}
contResp := make([]ContResp, 0)
fmt.Println(repoResults.ContributorUrl)
ueo := fmt.Sprintf("%s?page=%d", repoResults.ContributorUrl, page)
fmt.Println(ueo)
nresponse, err := http.Get(ueo)
if nresponse.StatusCode == 403 {
fmt.Println("rate limited")
os.Exit(1)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
nbody, _ := ioutil.ReadAll(nresponse.Body)
err = json.Unmarshal(nbody, &contResp)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// fmt.Println(len(contResp)) 30
for _, c := range contResp {
if c.Login == "benileo" {
contribution.Contributions = c.Contributions
break
}
}
if contribution.Contributions > 0 {
fmt.Println("contributions found")
break
}
if len(contResp) != 30 {
fmt.Println("response is less than 30")
fmt.Println(len(contResp))
}
page++
}
}
// https://github.com/letsencrypt/boulder/commits?author=benileo
// create template
templateHtml := `
{{range .}}
<tr>
<td><a href="{{.Repo}}" target="_blank">{{.FullName}}</a></td>
<td>{{.Description}}</td>
<td><a href="{{.Repo}}/commits?author=benileo" target="_blank">{{.Contributions}}</a></td>
</tr>
{{end}}
`
dirtyHack := make([]*Contribution, 0)
for _, v := range reposWithPRs {
dirtyHack = append(dirtyHack, v)
}
sort.Sort(ContributionSorter(dirtyHack))
// sort the dirtyhack now ugh..
fmt.Println(dirtyHack)
t := template.New("t")
t, err = t.Parse(templateHtml)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = t.Execute(os.Stdout, dirtyHack)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment