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

Add since parameter for public repositories #774

Merged
merged 4 commits into from
Apr 12, 2015
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ public interface IObservableRepositoriesClient
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "Makes a network request")]
IObservable<Repository> GetAllPublic();


/// <summary>
/// Retrieves every public <see cref="Repository"/> since the last repository seen.
/// </summary>
/// <remarks>
/// The default page size on GitHub.com is 30.
/// </remarks>
/// <param name="request">Search parameters of the last repository seen</param>
/// <returns>A <see cref="IReadOnlyPagedCollection{Repository}"/> of <see cref="Repository"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "Makes a network request")]
IObservable<Repository> GetAllPublic(PublicRepositoryRequest request);

/// <summary>
/// Retrieves every <see cref="Repository"/> that belongs to the current user.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public IObservable<Repository> GetAllPublic()
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.AllPublicRepositories());
}

/// <summary>
/// Retrieves every public <see cref="Repository"/> since the last repository seen.
/// </summary>
/// <remarks>
/// The default page size on GitHub.com is 30.
/// </remarks>
/// <param name="request">Search parameters of the last repository seen</param>
/// <returns>A <see cref="IReadOnlyPagedCollection{Repository}"/> of <see cref="Repository"/>.</returns>
public IObservable<Repository> GetAllPublic(PublicRepositoryRequest request)
{
Ensure.ArgumentNotNull(request, "request");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.AllPublicRepositories(), request.ToParametersDictionary());
}

/// <summary>
/// Retrieves every <see cref="Repository"/> that belongs to the current user.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,21 @@ public async Task ReturnsAllPublicRepositories()

Assert.True(repositories.Count > 80);
}

[IntegrationTest]
public async Task ReturnsAllPublicRepositoriesSinceLastSeen()
{
var github = Helper.GetAuthenticatedClient();

var request = new PublicRepositoryRequest(32732250);
var repositories = await github.Repository.GetAllPublic(request);

Assert.NotNull(repositories);
Assert.True(repositories.Any());
Assert.Equal(32732252, repositories[0].Id);
Assert.False(repositories[0].Private);
Assert.Equal("zad19", repositories[0].Name);
}
}

public class TheGetAllForOrgMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reactive.Linq;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit.Reactive;
using Xunit;
Expand Down Expand Up @@ -27,5 +28,22 @@ public async Task ReturnsSpecifiedRepository()
Assert.False(repository2.Fork);
}
}

public class TheGetAllPublicSinceMethod
{
[IntegrationTest(Skip = "This will take a very long time to return, so will skip it for now.")]
public async Task ReturnsAllPublicReposSinceLastSeen()
{
var github = Helper.GetAuthenticatedClient();

var client = new ObservableRepositoriesClient(github);
var request = new PublicRepositoryRequest(32732250);
var repositories = await client.GetAllPublic(request).ToArray();
Assert.NotEmpty(repositories);
Assert.Equal(32732252, repositories[0].Id);
Assert.False(repositories[0].Private);
Assert.Equal("zad19", repositories[0].Name);
}
}
}
}
31 changes: 31 additions & 0 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ public void RequestsTheCorrectUrlAndReturnsRepositories()
}
}


public class TheGetAllPublicSinceMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);

client.GetAllPublic(new PublicRepositoryRequest(364));

connection.Received()
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/repositories"),
Arg.Any<Dictionary<string, string>>());
}

[Fact]
public void SendsTheCorrectParameter()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);

client.GetAllPublic(new PublicRepositoryRequest(364));

connection.Received()
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/repositories"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
&& d["since"] == "364"));
}
}

public class TheGetAllForCurrentMethod
{
[Fact]
Expand Down
59 changes: 59 additions & 0 deletions Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,65 @@ public async Task StopsMakingNewRequestsWhenTakeIsFulfilled()
}
}

public class TheGetAllPublicRepositoriesSinceMethod
{
[Fact]
public async Task ReturnsEveryPageOfRepositories()
{
var firstPageUrl = new Uri("/repositories", UriKind.Relative);
var secondPageUrl = new Uri("https://example.com/page/2");
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
IApiResponse<List<Repository>> firstPageResponse = new ApiResponse<List<Repository>>(
CreateResponseWithApiInfo(firstPageLinks),
new List<Repository>
{
new Repository(364),
new Repository(365),
new Repository(366)
});

var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
IApiResponse<List<Repository>> secondPageResponse = new ApiResponse<List<Repository>>
(
CreateResponseWithApiInfo(secondPageLinks),
new List<Repository>
{
new Repository(367),
new Repository(368),
new Repository(369)
});

IApiResponse<List<Repository>> lastPageResponse = new ApiResponse<List<Repository>>(
new Response(),
new List<Repository>
{
new Repository(370)
});

var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.Get<List<Repository>>(firstPageUrl,
Arg.Is<Dictionary<string, string>>(d => d.Count == 1
&& d["since"] == "364"), null)
.Returns(Task.FromResult(firstPageResponse));
gitHubClient.Connection.Get<List<Repository>>(secondPageUrl, null, null)
.Returns(Task.FromResult(secondPageResponse));
gitHubClient.Connection.Get<List<Repository>>(thirdPageUrl, null, null)
.Returns(Task.FromResult(lastPageResponse));

var repositoriesClient = new ObservableRepositoriesClient(gitHubClient);

var results = await repositoriesClient.GetAllPublic(new PublicRepositoryRequest(364)).ToArray();

Assert.Equal(7, results.Length);
gitHubClient.Connection.Received(1).Get<List<Repository>>(firstPageUrl,
Arg.Is<Dictionary<string, string>>(d=>d.Count == 1
&& d["since"] == "364"), null);
gitHubClient.Connection.Received(1).Get<List<Repository>>(secondPageUrl, null, null);
gitHubClient.Connection.Received(1).Get<List<Repository>>(thirdPageUrl, null, null);
}
}

public class TheGetAllBranchesMethod
{
[Fact]
Expand Down
14 changes: 14 additions & 0 deletions Octokit/Clients/IRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ public interface IRepositoriesClient
Justification = "Makes a network request")]
Task<IReadOnlyList<Repository>> GetAllPublic();


/// <summary>
/// Gets all public repositories since the integer ID of the last Repository that you've seen.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/#list-all-public-repositories">API documentation</a> for more information.
/// The default page size on GitHub.com is 30.
/// </remarks>
/// <param name="request">Search parameters of the last repository seen</param>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Repository}"/> of <see cref="Repository"/>.</returns>
Task<IReadOnlyList<Repository>> GetAllPublic(PublicRepositoryRequest request);

/// <summary>
/// Gets all repositories owned by the current user.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
#if NET_45
using System.Collections.Generic;
#endif
Expand Down Expand Up @@ -187,6 +188,24 @@ public Task<IReadOnlyList<Repository>> GetAllPublic()
return ApiConnection.GetAll<Repository>(ApiUrls.AllPublicRepositories());
}

/// <summary>
/// Gets all public repositories since the integer ID of the last Repository that you've seen.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/#list-all-public-repositories">API documentation</a> for more information.
/// The default page size on GitHub.com is 30.
/// </remarks>
/// <param name="request">Search parameters of the last repository seen</param>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Repository}"/> of <see cref="Repository"/>.</returns>
public Task<IReadOnlyList<Repository>> GetAllPublic(PublicRepositoryRequest request)
{
Ensure.ArgumentNotNull(request, "request");

return ApiConnection.GetAll<Repository>(ApiUrls.AllPublicRepositories(), request.ToParametersDictionary());
}

/// <summary>
/// Gets all repositories owned by the current user.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions Octokit/Models/Request/PublicRepositoryRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PublicRepositoryRequest : RequestParameters
{
public PublicRepositoryRequest(int since)
{
Ensure.ArgumentNotNull(since, "since");

Since = since;
}

public long Since { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth making this a required parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, certainly as if you wanted to find ALL public repos you'd just call the parameterless GetAllPublic method 🙈


internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Since: {0} ", Since);
}
}
}
}
1 change: 1 addition & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
<Compile Include="Models\Response\License.cs" />
<Compile Include="Models\Response\LicenseMetadata.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@
<Compile Include="Models\Response\License.cs" />
<Compile Include="Models\Response\LicenseMetadata.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
<Compile Include="Models\Response\License.cs" />
<Compile Include="Models\Response\LicenseMetadata.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
<Compile Include="Models\Response\License.cs" />
<Compile Include="Models\Response\LicenseMetadata.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
<Compile Include="Models\Response\License.cs" />
<Compile Include="Models\Response\LicenseMetadata.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<Compile Include="Http\ProductHeaderValue.cs" />
<Compile Include="Models\Request\GistFileUpdate.cs" />
<Compile Include="Models\Request\NewMerge.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
<Compile Include="Models\Request\ReleaseAssetUpload.cs" />
<Compile Include="Models\Request\RepositoryRequest.cs" />
<Compile Include="Models\Request\Signature.cs" />
Expand Down