Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created March 23, 2016 18:07
Show Gist options
  • Save tmcw/88fe6f8c45494735b7fd to your computer and use it in GitHub Desktop.
Save tmcw/88fe6f8c45494735b7fd to your computer and use it in GitHub Desktop.
package main
import "github.com/google/go-github/github"
import "golang.org/x/oauth2"
import "strings"
import "fmt"
import "log"
import "os"
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func main() {
organizationName := "mapbox"
if os.Getenv("PatrolGitHubAccessToken") == "" {
log.Fatal("PatrolGitHubAccessToken is required")
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("PatrolGitHubAccessToken")},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
listOptions := &github.ListOptions{PerPage: 100}
// collect all repositories
var allRepos []github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(organizationName, opt)
if err != nil {
log.Fatal(err)
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
// break
opt.ListOptions.Page = resp.NextPage
}
// for testing only
if os.Getenv("ENV") == "test" {
allRepos = allRepos[0:5]
}
for _, repo := range allRepos {
hooks, _, err := client.Repositories.ListHooks(organizationName, *repo.Name, listOptions)
if err != nil {
log.Fatal(err)
}
for _, hook := range hooks {
topic, ok := hook.Config["sns_topic"].(string)
if ok && *hook.Name == "amazonsns" {
if strings.Contains(topic, "publisher-production-Watchbot") {
fmt.Printf("Deleting publisher hook in %v...\n", *repo.Name)
_, err := client.Repositories.DeleteHook(organizationName, *repo.Name, *hook.ID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("✔ Deleted publisher for %v\n", *repo.Name)
// log.Fatal("ending early")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment