我在使用Octokit.net创建新存储库时遇到问题。
这是我的代码:
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);
}
我已经设置了断点,我发现一切都很好。 http://prntscr.com/7h62iq可以在这里看到。当我让程序运行用于创建新存储库的代码时,这是我的结果:http://prntscr.com/7h63fz我得到ctokit.NotFoundException:Not Found。我已经尝试了一切,每次都发生错误。我做错了什么?
答案 0 :(得分:1)
我认为在 await client.Respository.Create(newRepo); 的第60行中存在内部错误 尝试使用 basicAuth 创建一个客户端,这是一个具有MIT许可证的存储库。 使用try catch,请参阅error.Message(如果有)。
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}");
}
}
如果存储库已存在,则可以删除旧存储库。警告,此代码肯定在未经确认的情况下删除存储库。
// 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.");
答案 1 :(得分:0)
我会稍微更改@pushStack的答案,Repository.Create
是异步的,因此需要作为任务运行:
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);
}
}