Last active
July 18, 2020 06:37
-
-
Save maxandersen/1dac989e2a9cfc55d79cbb3e0d242e22 to your computer and use it in GitHub Desktop.
small https://jbang.dev script to delete all repos under a user or org matching a regular expression. Used it to cleanup many years of weird empty repo experiments ;)
This file contains 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
//usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS info.picocli:picocli:4.2.0 | |
//DEPS org.kohsuke:github-api:1.101 | |
import org.kohsuke.github.GitHub; | |
import org.kohsuke.github.GitHubBuilder; | |
import org.kohsuke.github.extras.OkHttpConnector; | |
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.Option; | |
import picocli.CommandLine.Parameters; | |
import java.io.IOException; | |
import java.util.concurrent.Callable; | |
import java.util.regex.Pattern; | |
@Command(name = "delrepo", mixinStandardHelpOptions = true, version = "delrepo 0.1", | |
description = "delrepo made with jbang.%n%nBE CAREFUL: Once you use -f this script WILL DELETE ANY MATCHING REPOSITORY! No undo!") | |
class delrepo implements Callable<Integer> { | |
@Option(names = { "--token"}, defaultValue = "${GITHUB_TOKEN}") | |
private String token; | |
@Option(names = { "--user", "--org"},required=true) | |
private String user; | |
@Option(names={"-f", "--force"}, description = "Actually delete repository matching the pattern") | |
boolean force; | |
@Parameters(index="0") | |
private String repopattern; | |
public static void main(String... args) { | |
int exitCode = new CommandLine(new delrepo()).execute(args); | |
System.exit(exitCode); | |
} | |
@Override | |
public Integer call() throws Exception { | |
if(force) { | |
System.out.println("Finding repos to forcifully delete matching " + repopattern); | |
} else { | |
System.out.println("Finding repos matching " + repopattern); | |
System.out.println("Use -f to actually forcefully delete."); | |
} | |
Pattern pattern = Pattern.compile(repopattern); | |
GitHub github = GitHub.connectUsingOAuth(token); | |
var ghRepo = github.getUser(user).listRepositories().iterator(); | |
ghRepo.forEachRemaining(repo -> { | |
if (repo.getName().matches(repopattern)) { | |
System.out.println(repo.getHtmlUrl()); | |
if(force) { | |
try { | |
repo.delete(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment