Skip to content

Instantly share code, notes, and snippets.

@ppetko
Last active April 18, 2019 02:51
Show Gist options
  • Save ppetko/ca57867bd63c7b1bc516a5eea273ff32 to your computer and use it in GitHub Desktop.
Save ppetko/ca57867bd63c7b1bc516a5eea273ff32 to your computer and use it in GitHub Desktop.
Total starred repos of each member in Gjango Github organization
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
// Global variables
var (
githubAPI = "https://api.github.com/"
djangoMembers = githubAPI + "orgs/django/members?page=1&per_page=100"
totalReposPerPage int64 = 30
token string
)
// checkError
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
// User
type User struct {
Login string `json:"login"`
}
// Started
type Started struct {
URL string `json:"url"`
}
// Check if token exist
func init() {
token = os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present")
}
}
// getStars - Return the number of starrted repos
func getStars(user string) {
endpoint := "https://api.github.com/users/" + user + "/starred"
started := []*Started{}
client := &http.Client{}
req, _ := http.NewRequest("HEAD", endpoint, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "token "+token)
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
contentlength := resp.Header
if _, ok := contentlength["Link"]; ok {
pagination := string(contentlength["Link"][0])
words := strings.Fields(pagination)
lastPageURL := strings.Trim(words[2], "<>;")
delimiter := "="
lastPage := strings.Join(strings.Split(lastPageURL, delimiter)[1:], delimiter)
client = &http.Client{}
req, _ = http.NewRequest("GET", lastPageURL, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "token "+token)
resp, err = client.Do(req)
checkError(err)
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(result, &started)
checkError(err)
lpage, _ := strconv.ParseInt(lastPage, 10, 64)
pages := lpage - 1
totalStars := pages*totalReposPerPage + int64(len(started))
fmt.Printf("User %s has total starred repos: %d\n", user, totalStars)
} else {
req, _ := http.NewRequest("GET", endpoint, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "token "+token)
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(result, &started)
checkError(err)
fmt.Printf("User %s has total starred repos: %d\n", user, len(started))
}
}
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", djangoMembers, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "token "+token)
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
gitusers := []*User{}
err = json.Unmarshal(result, &gitusers)
checkError(err)
for _, j := range gitusers {
getStars(j.Login)
}
fmt.Printf("Total number of users in Django org: %d\n", len(gitusers))
}
/*
## Run the program
$ export GITHUB_AUTH_TOKEN="Generate your own token" ## Settings -> Developer settings -> Personal access tokens
$ go build starred.go
$ ./starred
*/
/*
## Program Output
User aaugustin has total starred repos: 1
User adamchainz has total starred repos: 863
User adrianholovaty has total starred repos: 24
User adriennefriend has total starred repos: 1
User akaariai has total starred repos: 23
User alex has total starred repos: 149
User andrewgodwin has total starred repos: 24
User avelino has total starred repos: 6074
User benkonrath has total starred repos: 61
User bmispelon has total starred repos: 0
User brosner has total starred repos: 15
User bryanveloso has total starred repos: 943
User carljm has total starred repos: 451
User carltongibson has total starred repos: 629
User charettes has total starred repos: 278
User claudep has total starred repos: 12
User DrMeers has total starred repos: 84
User dstufft has total starred repos: 109
User felixxm has total starred repos: 47
User frankwiles has total starred repos: 203
User freakboy3742 has total starred repos: 13
User gchp has total starred repos: 638
User gdub has total starred repos: 29
User glasnt has total starred repos: 61
User HonzaKral has total starred repos: 53
User idan has total starred repos: 242
User jacobian has total starred repos: 195
User jarshwah has total starred repos: 39
User jbronn has total starred repos: 4
User jdufresne has total starred repos: 227
User jdunck has total starred repos: 759
User jefftriplett has total starred repos: 4762
User jkocherhans has total starred repos: 229
User jphalip has total starred repos: 43
User jtauber has total starred repos: 302
User kmtracey has total starred repos: 3
User loic has total starred repos: 8
User MarkusH has total starred repos: 77
User mjtamlyn has total starred repos: 47
User mxsasha has total starred repos: 114
User pelme has total starred repos: 43
User phalt has total starred repos: 467
User proofit404 has total starred repos: 25
User ptone has total starred repos: 160
User ramiro has total starred repos: 23
User shaib has total starred repos: 17
User sir-sigurd has total starred repos: 38
User SmileyChris has total starred repos: 62
User spookylukey has total starred repos: 58
User timgraham has total starred repos: 0
User tobiasmcnulty has total starred repos: 16
User tomchristie has total starred repos: 498
User treyhunner has total starred repos: 868
User ubernostrum has total starred repos: 0
User williln has total starred repos: 189
Total number of users in Django org: 55
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment