diff --git a/Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs index e7146a10..56b0b51b 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs @@ -28,6 +28,17 @@ namespace Octokit.Reactive /// The name of the repository. IObservable GetAll(string owner, string name); + /// + /// Get all deploy keys for a repository. + /// + /// + /// See the API documentation for more information. + /// + /// The owner of the repository. + /// The name of the repository. + /// Options for changing the API response + IObservable GetAll(string owner, string name, ApiOptions options); + /// /// Creates a new deploy key for a repository. /// diff --git a/Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs b/Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs index 305ab1c2..182460b1 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs @@ -48,7 +48,25 @@ namespace Octokit.Reactive Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); - return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryDeployKeys(owner, name)); + return GetAll(owner, name, ApiOptions.None); + } + + /// + /// Get all deploy keys for a repository. + /// + /// + /// See the API documentation for more information. + /// + /// The owner of the repository. + /// The name of the repository. + /// Options for changing the API response + public IObservable GetAll(string owner, string name, ApiOptions options) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNull(options, "options"); + + return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryDeployKeys(owner, name), options); } /// diff --git a/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs index 78487674..e740359e 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Octokit; using Octokit.Tests.Integration; @@ -56,6 +57,115 @@ public class RepositoryDeployKeysClientTests : IDisposable Assert.Equal(_keyTitle, deployKeys[0].Title); } + [IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")] + public async Task ReturnsCorrectCountOfHooksWithoutStart() + { + var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); + Assert.Equal(0, deployKeys.Count); + + var list = new List(); + var hooksCount = 5; + for (int i = 0; i < hooksCount; i++) + { + var item = new NewDeployKey + { + Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how + Title = "KeyTitle" + i + }; + list.Add(item); + } + + foreach (var key in list) + { + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key); + } + + var options = new ApiOptions + { + PageSize = hooksCount, + PageCount = 1 + }; + + deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, options); + + Assert.Equal(hooksCount, deployKeys.Count); + } + + [IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")] + public async Task ReturnsCorrectCountOfHooksWithStart() + { + var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); + Assert.Equal(0, deployKeys.Count); + + var list = new List(); + var hooksCount = 5; + for (int i = 0; i < hooksCount; i++) + { + var item = new NewDeployKey + { + Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how + Title = "KeyTitle" + i + }; + list.Add(item); + } + + foreach (var key in list) + { + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key); + } + + var options = new ApiOptions + { + PageSize = 2, + PageCount = 1, + StartPage = 3 + }; + + deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, options); + + Assert.Equal(1, deployKeys.Count); + } + + [IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")] + public async Task ReturnsDistinctResultsBasedOnStartPage() + { + var list = new List(); + var hooksCount = 5; + for (int i = 0; i < hooksCount; i++) + { + var item = new NewDeployKey + { + Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how + Title = "KeyTitle" + i + }; + list.Add(item); + } + + foreach (var key in list) + { + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key); + } + + var startOptions = new ApiOptions + { + PageSize = 2, + PageCount = 1 + }; + + var firstPage = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, startOptions); + + var skipStartOptions = new ApiOptions + { + PageSize = 2, + PageCount = 1, + StartPage = 2 + }; + + var secondPage = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, skipStartOptions); + + Assert.NotEqual(firstPage[0].Id, secondPage[0].Id); + } + [IntegrationTest(Skip = "see https://github.com/octokit/octokit.net/issues/533 for the resolution to this failing test")] public async Task CanRetrieveADeployKey() { diff --git a/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs b/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs index 2362fef4..59c28f36 100644 --- a/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs @@ -55,7 +55,27 @@ namespace Octokit.Tests.Clients deployKeysClient.GetAll("user", "repo"); - apiConnection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/user/repo/keys")); + apiConnection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/user/repo/keys"), Args.ApiOptions); + } + + [Fact] + public void GetsAListOfDeployKeysWithApiOptions() + { + var connection = Substitute.For(); + var client = new RepositoryDeployKeysClient(connection); + + var options = new ApiOptions + { + PageCount = 1, + PageSize = 1, + StartPage = 1 + }; + + client.GetAll("user", "repo", options); + + connection.Received(1) + .GetAll(Arg.Is(u => u.ToString() == "repos/user/repo/keys"), + options); } [Fact] @@ -63,10 +83,22 @@ namespace Octokit.Tests.Clients { var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For()); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, null)); await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, "repo")); - await Assert.ThrowsAsync(() => deployKeysClient.GetAll("", "repo")); await Assert.ThrowsAsync(() => deployKeysClient.GetAll("user", null)); + + await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, null, null)); + + await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, null, ApiOptions.None)); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, "repo", null)); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll("user", null, null)); + + await Assert.ThrowsAsync(() => deployKeysClient.GetAll(null, "repo", ApiOptions.None)); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll("user", null, ApiOptions.None)); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll("user", "repo", null)); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll("user", "")); + await Assert.ThrowsAsync(() => deployKeysClient.GetAll("", "repo")); } } diff --git a/Octokit.Tests/Reactive/ObservableRepositoryDeployKeysClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoryDeployKeysClientTests.cs index 81a4f439..08cb24a1 100644 --- a/Octokit.Tests/Reactive/ObservableRepositoryDeployKeysClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableRepositoryDeployKeysClientTests.cs @@ -53,7 +53,53 @@ namespace Octokit.Tests.Reactive deployKeysClient.GetAll("user", "repo"); githubClient.Connection.Received(1).Get>( - new Uri("repos/user/repo/keys", UriKind.Relative), null, null); + new Uri("repos/user/repo/keys", UriKind.Relative), Arg.Is>(dictionary => dictionary.Count == 0), null); + } + + [Fact] + public void GetsCorrectUrlWithApiOptions() + { + var gitHubClient = Substitute.For(); + var deployKeysClient = new ObservableRepositoryDeployKeysClient(gitHubClient); + var expectedUrl = string.Format("repos/{0}/{1}/keys", "user", "repo"); + + // all properties are setted => only 2 options (StartPage, PageSize) in dictionary + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + deployKeysClient.GetAll("user", "repo", options); + gitHubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 2), + null); + + // StartPage is setted => only 1 option (StartPage) in dictionary + options = new ApiOptions + { + StartPage = 1 + }; + + deployKeysClient.GetAll("user", "repo", options); + gitHubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 1), + null); + + // PageCount is setted => none of options in dictionary + options = new ApiOptions + { + PageCount = 1 + }; + + deployKeysClient.GetAll("user", "repo", options); + gitHubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 0), + null); } [Fact] @@ -61,10 +107,22 @@ namespace Octokit.Tests.Reactive { var deployKeysClient = new ObservableRepositoryDeployKeysClient(Substitute.For()); + Assert.Throws(() => deployKeysClient.GetAll(null, null)); Assert.Throws(() => deployKeysClient.GetAll(null, "repo")); - Assert.Throws(() => deployKeysClient.GetAll("", "repo")); Assert.Throws(() => deployKeysClient.GetAll("user", null)); + + Assert.Throws(() => deployKeysClient.GetAll(null, null, null)); + + Assert.Throws(() => deployKeysClient.GetAll(null, null, ApiOptions.None)); + Assert.Throws(() => deployKeysClient.GetAll(null, "repo", null)); + Assert.Throws(() => deployKeysClient.GetAll("user", null, null)); + + Assert.Throws(() => deployKeysClient.GetAll(null, "repo", ApiOptions.None)); + Assert.Throws(() => deployKeysClient.GetAll("user", null, ApiOptions.None)); + Assert.Throws(() => deployKeysClient.GetAll("user", "repo", null)); + Assert.Throws(() => deployKeysClient.GetAll("user", "")); + Assert.Throws(() => deployKeysClient.GetAll("", "repo")); } } diff --git a/Octokit/Clients/IRepositoryDeployKeysClient.cs b/Octokit/Clients/IRepositoryDeployKeysClient.cs index adbcbe43..9eb2a0e4 100644 --- a/Octokit/Clients/IRepositoryDeployKeysClient.cs +++ b/Octokit/Clients/IRepositoryDeployKeysClient.cs @@ -34,6 +34,17 @@ namespace Octokit /// The name of the repository. Task> GetAll(string owner, string name); + /// + /// Get all deploy keys for a repository. + /// + /// + /// See the API documentation for more information. + /// + /// The owner of the repository. + /// The name of the repository. + /// Options for changing the API response + Task> GetAll(string owner, string name, ApiOptions options); + /// /// Creates a new deploy key for a repository. /// diff --git a/Octokit/Clients/RepositoryDeployKeysClient.cs b/Octokit/Clients/RepositoryDeployKeysClient.cs index 9a448741..78f1ddd8 100644 --- a/Octokit/Clients/RepositoryDeployKeysClient.cs +++ b/Octokit/Clients/RepositoryDeployKeysClient.cs @@ -53,7 +53,25 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); - return ApiConnection.GetAll(ApiUrls.RepositoryDeployKeys(owner, name)); + return GetAll(owner, name, ApiOptions.None); + } + + /// + /// Get all deploy keys for a repository. + /// + /// + /// See the API documentation for more information. + /// + /// The owner of the repository. + /// The name of the repository. + /// Options for changing the API response + public Task> GetAll(string owner, string name, ApiOptions options) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNull(options, "options"); + + return ApiConnection.GetAll(ApiUrls.RepositoryDeployKeys(owner, name), options); } ///