Octokit.net Creating new repository

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 13:03:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!