octokit.net icon indicating copy to clipboard operation
octokit.net copied to clipboard

client.Repository.Content.GetAllContents for root not currently possible.

Open DavidStrickland0 opened this issue 10 years ago • 8 comments
trafficstars

Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path); Will currently only work with subdirectories ie: client.Repository.Content.GetAllContents("octokit", "octokit.net", "Octokit") returns 18 items. However client.Repository.Content.GetAllContents("octokit", "octokit.net", "") Throws a ArgumentException("String cannot be empty", name)

For a quick fixed I hacked GetAllContents as follows locally but a better option is probably needed.

public async Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path)
        {
            Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            //Ensure.ArgumentNotNullOrEmptyString(path, "path");
            System.Uri url;
            if (string.IsNullOrEmpty(path))
            {
                url = ApiUrls.RepositoryContent(owner, name);
            }
            else
            {
                url = ApiUrls.RepositoryContent(owner, name, path);
            }
            return await ApiConnection.GetAll<RepositoryContent>(url);
        }

DavidStrickland0 avatar May 11 '15 19:05 DavidStrickland0

I am able to get the contents of a repository's root directory by passing in "/" as the path, but I am unsure if this is due to changes made after this issue was opened.

SplitInfinity avatar Jun 23 '15 21:06 SplitInfinity

:+1:

"/" only works if you're retrieving master branch. If you're retrieving some other branch it defaults to master branch, because the generated url is actually invalid: "contents//?ref=branch-name". Remove the check for empty string when specifying path.

ostruk avatar Nov 03 '15 19:11 ostruk

You could get all the contents of the branch using the overload client.Repository.Content.GetAllContents which takes the reference as the last parameter.

I am able to get all the contents of the branch and master including the sub-directories. Here is a sample.

async Task Main(string[] args)
{
    var owner = "octokit";
    var reponame = "octokit.net";

    GitHubClient client = new GitHubClient(new Octokit.ProductHeaderValue("Octokit.samples"));
    client.Credentials = new Credentials(Util.GetPassword("github"));
    var branchcontents = await client.Repository.Content.GetAllContents(owner,reponame,"/Octokit.Reactive/Properties","codeformatter");
    var mastercontents = await client.Repository.Content.GetAllContents(owner,reponame,"/Octokit.Reactive/Properties");
    branchcontents .Dump();
    mastercontents.Dump();
    branchcontents = await client.Repository.Content.GetAllContents(owner,reponame,"/","codeformatter");
    mastercontents = await client.Repository.Content.GetAllContents(owner,reponame,"/");

    branchcontents .Dump();
    mastercontents.Dump();
}

naveensrinivasan avatar Nov 03 '15 19:11 naveensrinivasan

I have 7 files on my master branch, and 6 on my custom branch ("ostruk-branch" does not have "added.js").

When I call

var all = await client.Repository.Content.GetAllContents("UW-demo", "multiJs", "/", "ostruk-branch");

I get 7 files, same as if I called without "ostruk-branch". When I inspect the code, I can see it attempts to make a call to

https://api.github.com/repos/UW-demo/multiJs/contents//?ref=ostruk-branch

where you can see that all files have "?ref=master" by them, indicating they are being pulled from the master branch. If I change the url to

https://api.github.com/repos/UW-demo/multiJs/contents/?ref=ostruk-branch

, which is equivalent to passing "'" for path argument, then response is correct.

I'm using 0.16.0.

Thanks!

ostruk avatar Nov 03 '15 19:11 ostruk

@ostruk Yes you are right. It is a bug.

naveensrinivasan avatar Nov 03 '15 19:11 naveensrinivasan

Can we reopen this issue? The fix was accidentally undone during https://github.com/octokit/octokit.net/pull/1348 and I'm running into it again.

jamesqo avatar Jun 08 '17 20:06 jamesqo

Whoops!

Need to dig into this to see why there isn't an integration test that failed

ryangribble avatar Jun 09 '17 04:06 ryangribble

@jamesqo just looking at this one... are you referring to the fact that GetAllContents(string owner, string name, string path) is enforcing path to not be null or empty?

If that's the case can't you just call GetAllContents(string owner, string name) overload instead?

ryangribble avatar Jun 26 '17 13:06 ryangribble

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

github-actions[bot] avatar Jul 24 '23 01:07 github-actions[bot]