【问题标题】:Octokit.net Creating new repositoryOctokit.net 创建新的存储库
【发布时间】:2015-08-30 20:35:26
【问题描述】:

我在使用 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。我已经尝试了一切,每次发生错误。我做错了什么?

【问题讨论】:

  • 我的直觉是OAuth令牌没有public_reporepo的权限
  • 你是对的。我忘了把解决方案放在这里。

标签: git repository octokit.net


【解决方案1】:

我会稍微改变@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);
    }
}

【讨论】:

    【解决方案2】:

    我认为 await client.Respository.Create(newRepo); 处的 line 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.");
    

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多