Skip to content

Instantly share code, notes, and snippets.

@isaacrlevin
Last active June 19, 2020 23:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacrlevin/78f8d1a4297c1f54f61d77c384ee9a6f to your computer and use it in GitHub Desktop.
Save isaacrlevin/78f8d1a4297c1f54f61d77c384ee9a6f to your computer and use it in GitHub Desktop.
using Octokit;
using System;
using System.Threading.Tasks;
using System.Diagnostics;
namespace UpdateGitHub
{
class Program
{
static async Task Main(string[] args)
{
string token = string.Empty;
string login = string.Empty;
string repo = string.Empty;
if (args.Length == 2 && !string.IsNullOrEmpty(args[0]) && !string.IsNullOrEmpty(args[1]) && !string.IsNullOrEmpty(args[1]))
{
token = args[0];
login = args[1];
repo = args[2];
}
else
{
token = "YOUR TOKEN";
login = "YOUR LOGIN";
//Optional if you only want one repo
repo = "Repo You want to Change";
}
var client = new GitHubClient(new ProductHeaderValue("update-github"));
var basicAuth = new Octokit.Credentials(token);
client.Credentials = basicAuth;
var user = await client.User.Get(login);
var repos = await client.Repository.GetAllForUser(login);
foreach (var repo in repos)
{
if (string.IsNullOrEmpty(repo.Name) && repo.Name == repo)
{
if (repo.DefaultBranch != "main")
{
LibGit2Sharp.Repository.Clone(repo.CloneUrl, @"C:\dev\Temp\" + repo.Name);
RunCmd(repo.Name, "git branch -m master main && git push -u origin main");
RepositoryUpdate update = new RepositoryUpdate(repo.Name);
update.DefaultBranch = "main";
await client.Repository.Edit(login, repo.Name, update);
RunCmd(repo.Name, "git push -d origin master");
}
}
}
}
private static void RunCmd(string repo, string cmd)
{
var processStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C " + cmd,
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = @"C:\dev\Temp\" + repo
};
var process = Process.Start(processStartInfo);
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment