问题
I have a problem with creating new repository with Octokit.net.
This is my code:
public async Task stvoriNovi(FormCollection collection)
{
string name = collection.Get("name");
NewRepository newRepo = new NewRepository(name);
newRepo.AutoInit = true;
var accessToken = Session["OAuthToken"] as string;
if (accessToken != null)
{
client.Credentials = new Credentials(accessToken);
}
await client.Repository.Create(newRepo);
}
I've put breakpoints and there I see that everything is OK. http://prntscr.com/7h62iq which can be seen here. And when I let the program run the code for creating a new repository, this is my result: http://prntscr.com/7h63fz I get ctokit.NotFoundException: Not Found. I have tried everything and each time error occurs. What am I doing wrong?
回答1:
I think there is an inner error in the line 60 at await client.Respository.Create(newRepo); Try this to create a client with basicAuth, a repository with a MIT licence. And with the try catch, see the error.Message if any.
using Octokit;
// Authentification
var basicAuth = new Credentials(Owner, Password);
var Client = new GitHubClient(new ProductHeaderValue("my-cool-app"));
Client.Credentials = basicAuth;
// Create
try {
var repository = new NewRepository(RepositoryName) {
AutoInit = false,
Description = "",
LicenseTemplate = "mit",
Private = false
};
var context = Client.Repository.Create(repository);
RespositoryGitHub = context.Result;
Console.WriteLine($"The respository {RepositoryName} was created.");
} catch (AggregateException e) {
Console.WriteLine($"E: For some reason, the repository {RepositoryName} can't be created. It may already exist. {e.Message}");
}
}
If the repository already exists, you can remove the old one. Warning, this code definitely remove the repository without confirmation.
// Remove the previous repository if exists
var contextDelete = Client.Repository.Get(Owner, RepositoryName).Result;
var repositoryID = contextDelete.Id;
var context = Client.Repository.Delete(repositoryID);
Console.WriteLine($"The respository {RepositoryName} was deleted.");
回答2:
I'd change @pushStack's answer slightly, Repository.Create
is asynchronous, so needs to be run as a task:
private void OctoKitRepositoryCreation(string sUsername, string sPassword, string sRepoName)
{
// Authentification
var basicAuth = new Credentials(sUsername, sPassword);
var Client = new GitHubClient(new ProductHeaderValue("my-cool-app"));
Client.Credentials = basicAuth;
// Create
try
{
var repository = new NewRepository(sRepoName)
{
AutoInit = false,
Description = "",
LicenseTemplate = "mit",
Private = true
};
var newRepository = System.Threading.Tasks.Task.Run(async () => await Client.Repository.Create(repository)).GetAwaiter().GetResult();
Console.WriteLine("The respository {0} was created.", newRepository.FullName);
}
catch (AggregateException e)
{
Console.WriteLine("Error occured. {0}", e.Message);
}
}
来源:https://stackoverflow.com/questions/30843631/octokit-net-creating-new-repository