0
votes

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



    }
}

}

1
See the answer (bullet point one) @ stackoverflow.com/questions/20185412/… - SushiHangover
@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

1 Answers

1
votes

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