LibGit2Sharp:列出所有拉取请求的作者

LIbGit2Sharp: list all the pull request's authors

使用 LibGit2Sharp 库,我试图在回购的主分支上列出拉取请求的所有作者。我在文档、智能感知或通过搜索中没有看到任何有这方面示例的内容。任何指针? 我构建了下面的代码,但收到以下错误消息 An unhandled exception of type 'LibGit2Sharp.RepositoryNotFoundException' occurred in LibGit2Sharp.dll。浏览了一下,我发现的引用似乎是本地克隆的 repos,而不是远程存储库。

static void Main(string[] args)
    {

        var co = new CloneOptions();
        co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
        {
            Username = "username",
            Password = "password"
        };

        var clonedRepoPath = Repository.Clone(url, "path/to/clone", co);

        using (var repo = new Repository(clonedRepoPath))
        {
            foreach (Commit commit in repo.Commits)
            {

                Console.WriteLine(commit.Author.Name);

            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }        



    }
}

}

...list all the pull requests ......

首先,“pull requests”是一种DVCS工作流方法,并不是git的特征。大多数人 天生地 ,并且错误地认为它是 git 的一部分。 Github.com(和其他人)有一个 pull request 工作流系统,其中包括 git merge、主题讨论、持续集成 (CI) 挂钩、问题引用、用户权限等项目,等等.. 只有 git merge 实际上来自 git DVCS.

也就是说,在 git 存储库中,Github-style 拉取请求是两个 commit-ishs 之间的合并(通常是从主题分支合并到主分支,但这不是必需的) 和 因此 'pull request' 提交有两个 parents.

仅供参考:对于具有三个 (+) parents 的合并,请参阅此 answer

回到你的问题:

list the authors of all the pull requests on the master branch of a repo

该语句变为以下 git cmd:

git log master --merges --pretty=format:"%an %s" 变为:

将其翻译成 libgit2sharp

        // find the master branch in the repo
        var masterBranch = repo.Branches.Single (branch => branch.FriendlyName == "master");

        // Filter the branch's commits to ones that are merges
        var mergeList = masterBranch.Commits.Where (p => p.Parents.Count () >= 2);

        // Display the merge commits (pull requests) 
        foreach (Commit commit in mergeList)
        {
            Console.WriteLine("{0}\t{1}", commit.Author.Name, commit.MessageShort);
        }

使用拉取请求的 github 回购的示例输出:

João Matos      Merge pull request #1966 from angeloc/master
Zoltan Varga    Merge pull request #1965 from akoeplinger/fix-flaky-test
João Matos      Merge pull request #1963 from angeloc/patch-1
Rodrigo Kumpera Merge pull request #1912 from ludovic-henry/threadpool-managed-asyncresult
Zoltan Varga    Merge pull request #1959 from alexrp/master
Zoltan Varga    Merge pull request #1958 from rolfbjarne/aot-error-reporting
Marek Safar     Merge pull request #1955 from LogosBible/servicepoint_nre
...

更新:

根据评论,libgit2sharp 不会给用户他们想要的东西,您需要使用 Github api.

通过 Octokit 库使用 Github Api(您可以直接进行 Github REST 调用或使用其他库。),您可以相当轻松地请求所有打开的拉取请求:

public static async Task getPullRequests ()
{
    var client = new GitHubClient (new ProductHeaderValue ("PlayScript"));
    // Login Credentials if you need them for an enterprise acct/repo
    // client.Credentials = GithubHelper.Credentials;
    var connection =  new Connection (new ProductHeaderValue ("PlayScript"));
    var api = new ApiConnection (connection);
    var pullrequests = new PullRequestsClient (api);
    pulls =  await pullrequests.GetAllForRepository ("PlayScriptRedux", "playscript");
} 
....

Task.WaitAll(getPullRequests());
foreach (var pullrequest in pulls) {
    Console.WriteLine (pullrequest.IssueUrl);
}

这将在 PlayScriptRedux 组织下为我的 playscript 仓库列出一个开放的拉取请求,即控制台输出:

https://api.github.com/repos/PlayScriptRedux/playscript/issues/89