Skip to content

Instantly share code, notes, and snippets.

@santrancisco
Last active August 22, 2018 04:52
Show Gist options
  • Save santrancisco/da33dcb6687ce092e2b029169df70c92 to your computer and use it in GitHub Desktop.
Save santrancisco/da33dcb6687ce092e2b029169df70c92 to your computer and use it in GitHub Desktop.
Get all organisations exist in Github - require github token due to rate limiting
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func GetConn() (c *github.Client, err error) {
err = nil
ctx := context.Background()
c = github.NewClient(&http.Client{})
if os.Getenv("GITHUB_AUTH_TOKEN") != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
c = github.NewClient(tc)
}
return c, nil
}
// type org struct {
// Id int `json:"id"`
// Login string `json:"login"`
// Url string `json:"url"`
// }
func getListOrg(ctx context.Context, c *github.Client, since int, f *os.File) []string {
result := []string{}
orgs, r, err := c.Organizations.ListAll(ctx, &github.OrganizationsListOptions{Since: since, ListOptions: github.ListOptions{PerPage: 100}})
// Poor man way to handle rate limit error :p
for (err != nil) && (r.StatusCode == 403) {
fmt.Println(err.Error())
fmt.Println("Reached rate limit - backing off for 10 minutes")
time.Sleep(time.Duration(10 * time.Minute))
orgs, _, err = c.Organizations.ListAll(ctx, &github.OrganizationsListOptions{Since: since, ListOptions: github.ListOptions{PerPage: 100}})
}
if err != nil {
log.Fatal(err)
}
for _, o := range orgs {
if *o.ID > since {
since = *o.ID
}
result = append(result, *o.Login)
}
if len(orgs) > 0 {
f.Write([]byte(strings.Join(result, "\n")))
result = append(result, getListOrg(ctx, c, since, f)...)
}
return result
}
func main() {
c, err := GetConn()
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("orgs.lst", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
since := 0
_ = getListOrg(ctx, c, since, f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment