Created
October 29, 2016 09:10
-
-
Save ggirtsou/d189e55367bba4663753214ee7f17b14 to your computer and use it in GitHub Desktop.
Delete all fork repositories from your Github account in one go, using this Golang script :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* CAUTION!!! READ THIS VERY CAREFULLY!!! | |
* This will delete ALL your Github fork repositories. | |
* DO NOT use it if you don't know what it does! | |
* | |
* Usage: | |
* $ go get # get dependencies | |
* $ go run client.go # initiate chaos | |
*/ | |
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/google/go-github/github" | |
"golang.org/x/oauth2" | |
) | |
var allRepos []*github.Repository | |
const ( | |
user = `` // change that to your Github username | |
accessToken = `` // enter your Github access token here (need delete permissions) | |
) | |
func main() { | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: accessToken}, | |
) | |
tc := oauth2.NewClient(oauth2.NoContext, ts) | |
client := github.NewClient(tc) | |
opt := &github.RepositoryListOptions{ | |
ListOptions: github.ListOptions{PerPage: 50}, | |
} | |
// get all pages of results | |
for { | |
// list all repositories for the authenticated user | |
repos, resp, err := client.Repositories.List("", opt) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
allRepos = append(allRepos, repos...) | |
// reached last page of results, exit the loop | |
if resp.NextPage == 0 { | |
break | |
} | |
opt.ListOptions.Page = resp.NextPage | |
} | |
for _, repo := range allRepos { | |
if *repo.Fork { | |
_, err := client.Repositories.Delete(user, *repo.Name) | |
fmt.Println(*repo.FullName, "is now deleted!") | |
if err != nil { | |
// We're not using log.Fatalln() here because if you re-run | |
// this program, Github will return cached response with deleted | |
// repositories, and repository deletion will fail. | |
fmt.Println(err) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment