Skip to content

Instantly share code, notes, and snippets.

@xinau
Created May 7, 2020 11:38
Show Gist options
  • Save xinau/5d2beed08a576b8172747d6857a12396 to your computer and use it in GitHub Desktop.
Save xinau/5d2beed08a576b8172747d6857a12396 to your computer and use it in GitHub Desktop.
script for geneareting a flat-file database on github repositories and releases
package main
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v31/github"
"golang.org/x/oauth2"
)
func getrepos(ctx context.Context, client *github.Client) ([]*github.Repository, error) {
var repos []*github.Repository
opts := &github.SearchOptions{
Sort: "stars",
Order: "desc",
}
for i := 0; i < 5; i++ {
res, resp, err := client.Search.Repositories(ctx, "stars:>0", opts)
if err != nil {
return nil, err
}
repos = append(repos, res.Repositories...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return repos, nil
}
func getreleases(ctx context.Context, client *github.Client, repo *github.Repository) ([]*github.RepositoryRelease, error) {
var releases []*github.RepositoryRelease
opts := &github.ListOptions{}
for {
res, resp, err := client.Repositories.ListReleases(ctx, *repo.Owner.Login, *repo.Name, opts)
if err != nil {
return nil, err
}
releases = append(releases, res...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return releases, nil
}
func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "personal access token"},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
repos, err := getrepos(ctx, client)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, repo := range repos {
releases, _ := getreleases(ctx, client, repo)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, release := range releases {
fmt.Printf("%s %s\n", *repo.FullName, *release.TagName)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment