0

Using the LibGit2Sharp library, I'm trying to list all authors of pull requests on the master branch of a repo. I don't see anything in the docs, intellisense, or through search that has an example of this. Any pointers?? I have constructed the code below, but I'm getting the following error message An unhandled exception of type 'LibGit2Sharp.RepositoryNotFoundException' occurred in LibGit2Sharp.dll. Browsing around, it seems that the references I find are to local cloned repos and not remote repositories.

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();
        }        



    }
}

}

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
DBS
  • 1,107
  • 1
  • 12
  • 24
  • 1
    See the answer (bullet point one) @ http://stackoverflow.com/questions/20185412/how-to-connect-to-a-github-repo-using-libgit2 – SushiHangover Nov 12 '15 at 20:40
  • @RobertN Thanks. I edited my question to reflect the correct solution. I would like to graduate and list all the pull requests on the master branch of a repo. I don't see anything in the docs, intellisense, or through search that has an example of this. Any pointers?? Thanks again!! – DBS Nov 13 '15 at 06:30

1 Answers1

1

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

First of all, “pull requests” are a DVCS workflow method, and are not a feature of git. Most people inherently, and incorrectly, think it is a part of git. Github.com (and others) have a pull request workflow system that includes items such as a git merge, topic discussion, continuous integration (CI) hooks, issue referencing, user permissions, etc.. with ONLY the git merge being actually from the git DVCS.

That said, within a git repository, Github-style pull requests are merges between two commit-ishs (usually merging from topic branch to a master branch, but this is not a requirement) and thus the 'pull request' commit have two parents.

FYI: For merges that have three(+) parents, see this answer

So back to your question:

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

That statement becomes the following git cmd:

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

In translating that to 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);
        }

Example output of a github repo that uses pull requests:

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
...

Update:

Based upon the comment, libgit2sharp is not going to give the user what they want, you need to use the Github api.

Using Github Api via the Octokit library (you can directly make the Github REST calls or use another lib.), you can request all the open pull requests fairly easily:

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);
}

That would list one open pull request for my playscript repo under the PlayScriptRedux organization, i.e. console output:

https://api.github.com/repos/PlayScriptRedux/playscript/issues/89
Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • @RobertN...Thanks again. We were using a Python library that lets you return all the `open` pull requests submitted to a repo and I'm trying to replicate that behavior as we now have a requirement to use libgit2hsarp/C#. I'm basically looking for a way to pull in the information located here, https://developer.github.com/v3/pulls/#list-pull-requests and then create report based on the returned key values. – DBS Nov 13 '15 at 14:24
  • Ahh... libgit2sharp is not going to help you, you would need to use the Github api. Since you are using C#/.Net, Octokit is the most popular and listed by Github... see my updated answer. – SushiHangover Nov 13 '15 at 15:22
  • @DBS Besides this amazing answer, you also may be willing to take a look at http://stackoverflow.com/a/31490829/335418 This demonstrates how to locally fetch the commits from PullRequests hosted on GitHub and leverage the retrieve information. – nulltoken Nov 13 '15 at 18:39
  • RobertN Thanks again for the pointers and greatly helpful answer. @nulltoken I will investigate this, but it looks like octokit may be the best way to go here after playing around with it some. – DBS Nov 16 '15 at 18:03