如何使用octokit和c#获取每次提交的文件列表
How to get list of files for every commit with octokit and c#
我有一个 class GitHub 和一种方法,应该 return 列出特定用户名的所有提交和 GitHub 上的回购:
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);
}
}
}
}
当我 运行 出现以下错误时:
问题是因为这个变量 commit.Files 是 Null 可能是因为异步调用,但我不确定如何解决它。请帮忙?
我的猜测是,如果您需要获取所有提交的文件列表,您将需要使用
分别获取每个提交
foreach(GitHubCommit commit in commits)
{
var commitDetails = github.Repository.Commit.Get(commit.Sha);
var files = commitDetails.Files;
}
看看this。还描述了另一种实现目标的方法 - 首先获取存储库中所有文件的列表,然后获取每个文件的提交列表。
我有一个 class GitHub 和一种方法,应该 return 列出特定用户名的所有提交和 GitHub 上的回购:
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);
}
}
}
}
当我 运行 出现以下错误时:
问题是因为这个变量 commit.Files 是 Null 可能是因为异步调用,但我不确定如何解决它。请帮忙?
我的猜测是,如果您需要获取所有提交的文件列表,您将需要使用
分别获取每个提交foreach(GitHubCommit commit in commits)
{
var commitDetails = github.Repository.Commit.Get(commit.Sha);
var files = commitDetails.Files;
}
看看this。还描述了另一种实现目标的方法 - 首先获取存储库中所有文件的列表,然后获取每个文件的提交列表。