2017-08-09 81 views
0

我有一类GitHub的一个方法,它应该返回所有的名单将提交对特定的用户名和回购在GitHub上:如何获得文件的列表,每有octokit和C#提交

using System; 
using Octokit; 
using System.Threading.Tasks; 
using System.Collections.Generic; 

namespace ReadRepo 
{ 
    public class GitHub 
    { 
     public GitHub() 
     { 
     } 

     public async Task<List<GitHubCommit>> getAllCommits() 
     {    
      string username = "lukalopusina"; 
      string repo = "flask-microservices-main"; 

      var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp")); 
      var repository = await github.Repository.Get(username, repo); 
      var commits = await github.Repository.Commit.GetAll(repository.Id); 

      List<GitHubCommit> commitList = new List<GitHubCommit>(); 

      foreach(GitHubCommit commit in commits) { 
       commitList.Add(commit); 
      } 

      return commitList; 
     } 

    } 
} 

而且我有主其中呼吁getAllCommits方法函数:

using System; 
using Octokit; 
using System.Threading.Tasks; 
using System.Collections.Generic; 

namespace ReadRepo 
{ 
    class MainClass 
    { 

     public static void Main(string[] args) 
     {    

      GitHub github = new GitHub(); 

      Task<List<GitHubCommit>> commits = github.getAllCommits(); 
      commits.Wait(10000); 

      foreach(GitHubCommit commit in commits.Result) {     
       foreach (GitHubCommitFile file in commit.Files) 
        Console.WriteLine(file.Filename);  
      } 

     } 
    } 
} 

当我运行此我得到以下错误:

enter image description here

问题是因为这个变量commit.Files是空的,可能是因为异步调用,但我不知道如何解决它。请帮忙吗?

回答

1

我的猜测是,如果你需要得到的文件列表供您将需要得到每个单独的提交使用

foreach(GitHubCommit commit in commits) 
{ 
    var commitDetails = github.Repository.Commit.Get(commit.Sha); 
    var files = commitDetails.Files; 
} 

看看this所有提交。还有另外一种方法可以实现你的目标 - 首先获取存储库中所有文件的列表,然后获取每个文件的提交列表。

+0

为了获得每次提交需要的文件以执行一次更多查询,您是对的,而且您认为更好的策略是获取文件列表,然后为每个文件配置所有版本。谢谢。 –

+0

我还编辑了您的代码示例与我测试的工作示例。 –

相关问题