Skip to content

Instantly share code, notes, and snippets.

@ishustava
Created April 17, 2019 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishustava/abfbadeec9d8e38a1167e9c06347829d to your computer and use it in GitHub Desktop.
Save ishustava/abfbadeec9d8e38a1167e9c06347829d to your computer and use it in GitHub Desktop.
Get all Github hooks of the relateiq org
package main
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v24/github"
"golang.org/x/oauth2"
)
func main() {
token := os.Getenv("GITHUB_API_TOKEN")
if token == "" {
panic("GITHUB_API_TOKEN env var is required")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
fmt.Println("Retrieving all repositories...")
repos, err := getAllRepos(ctx, client)
if err != nil {
panic(err)
}
printAllHooks(ctx, client, repos)
}
func getAllRepos(ctx context.Context, client *github.Client) ([]*github.Repository, error) {
// get all pages of results
var allRepos []*github.Repository
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 30},
}
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, "relateiq", opt)
if err != nil {
return nil, err
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allRepos, nil
}
func printAllHooks(ctx context.Context, client *github.Client, repos []*github.Repository) {
for _, repo := range repos {
// fmt.Println(repo)
hooks, _, err := client.Repositories.ListHooks(ctx, *repo.Owner.Login, *repo.Name, nil)
if err != nil {
panic(err)
}
var activeHooksURLs []interface{}
for _, hook := range hooks {
if *hook.Active {
activeHooksURLs = append(activeHooksURLs, hook.Config["url"])
}
}
if activeHooksURLs != nil {
fmt.Printf("Repository: %s,\t WebHooks: %#v\n", *repo.Name, activeHooksURLs)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment