Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for ref value in get contents api #730

Merged
merged 5 commits into from
Jul 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reactive;

namespace Octokit.Reactive
@@ -77,6 +78,22 @@ public interface IObservableRepositoryContentsClient
/// </returns>
IObservable<RepositoryContent> GetAllContents(string owner, string name, string path);

/// <summary>
/// Returns the contents of a file or directory in a repository.
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The content path</param>
/// <param name="reference">The name of the commit/branch/tag. Default: the repository’s default branch (usually master)</param>
/// <returns>
/// A collection of <see cref="RepositoryContent"/> representing the content at the specified path
/// </returns>
IObservable<RepositoryContent> GetAllContents(string owner, string name, string path, string reference);

/// <summary>
/// Creates a commit that creates a new file in a repository.
/// </summary>
25 changes: 25 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoryContentsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;
using System.Collections.Generic;

namespace Octokit.Reactive
{
@@ -123,6 +124,30 @@ public IObservable<RepositoryContent> GetAllContents(string owner, string name,
.GetAndFlattenAllPages<RepositoryContent>(ApiUrls.RepositoryContent(owner, name, path));
}

/// <summary>
/// Returns the contents of a file or directory in a repository.
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The content path</param>
/// <param name="reference">The name of the commit/branch/tag. Default: the repository’s default branch (usually master)</param>
/// <returns>
/// A collection of <see cref="RepositoryContent"/> representing the content at the specified path
/// </returns>
public IObservable<RepositoryContent> GetAllContents(string owner, string name, string path, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");

return _client.Connection.GetAndFlattenAllPages<RepositoryContent>(ApiUrls.RepositoryContent(owner, name, path, reference));
}

/// <summary>
/// Creates a commit that creates a new file in a repository.
/// </summary>
19 changes: 19 additions & 0 deletions Octokit.Tests/Clients/RepositoryContentsClientTests.cs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
using NSubstitute;
using Octokit.Tests.Helpers;
using Xunit;
using System.Collections.Generic;

namespace Octokit.Tests.Clients
{
@@ -108,5 +109,23 @@ public async Task EnsuresArgumentsNotNull()
AssertEx.Throws<ArgumentException>(async () => await contentsClient.GetArchiveLink("owner", ""));
}
}

public class TheGetContentsMethod
{
[Fact]
public async Task ReturnsContents()
{
List<RepositoryContent> result = new List<RepositoryContent>() { new RepositoryContent() { } };

var connection = Substitute.For<IApiConnection>();
connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
var contentsClient = new RepositoryContentsClient(connection);

var contents = await contentsClient.GetAllContents("fake", "repo", "readme.md", "master");

connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/readme.md?ref=master"));
Assert.Equal(1, contents.Count);
}
}
}
}
17 changes: 17 additions & 0 deletions Octokit/Clients/IRepositoryContentsClient.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ public interface IRepositoryContentsClient
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
@@ -23,6 +24,22 @@ public interface IRepositoryContentsClient
/// </returns>
Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path);

/// <summary>
/// Returns the contents of a file or directory in a repository.
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The content path</param>
/// <param name="reference">The name of the commit/branch/tag. Default: the repository�s default branch (usually master)</param>
/// <returns>
/// A collection of <see cref="RepositoryContent"/> representing the content at the specified path
/// </returns>
Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path, string reference);

/// <summary>
/// Gets the preferred README for the specified repository.
/// </summary>
27 changes: 27 additions & 0 deletions Octokit/Clients/RepositoryContentsClient.cs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ public RepositoryContentsClient(IApiConnection apiConnection) : base(apiConnecti
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
@@ -35,6 +36,32 @@ public async Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner,
return await ApiConnection.GetAll<RepositoryContent>(url);
}

/// <summary>
/// Returns the contents of a file or directory in a repository.
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// See the <a href="https://developer.github.com/v3/repos/contents/#get-contents">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The content path</param>
/// <param name="reference">The name of the commit/branch/tag. Default: the repository�s default branch (usually master)</param>
/// <returns>
/// A collection of <see cref="RepositoryContent"/> representing the content at the specified path
/// </returns>
public async Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");

var url = ApiUrls.RepositoryContent(owner, name, path, reference);

return await ApiConnection.GetAll<RepositoryContent>(url);
}

/// <summary>
/// Gets the preferred README for the specified repository.
/// </summary>
13 changes: 13 additions & 0 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
@@ -1472,5 +1472,18 @@ public static Uri RepositoryArchiveLink(string owner, string name, ArchiveFormat
{
return "repos/{0}/{1}/{2}/{3}".FormatUri(owner, name, archiveFormat.ToParameter(), reference);
}

/// <summary>
/// Creates the relative <see cref="Uri"/> for getting the contents of the specified repository and path
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The path of the contents to get</param>
/// <param name="reference">The name of the commit/branch/tag. Default: the repository’s default branch (usually master)</param>
/// <returns>The <see cref="Uri"/> for getting the contents of the specified repository and path</returns>
public static Uri RepositoryContent(string owner, string name, string path, string reference)
{
return "repos/{0}/{1}/contents/{2}?ref={3}".FormatUri(owner, name, path, reference);
}
}
}