From f172edef5bf43340922f116c2d28012d8f781b8c Mon Sep 17 00:00:00 2001 From: Timothy Haagenson Date: Mon, 21 Jul 2014 13:41:34 -0500 Subject: [PATCH] [WIP] Gists get Commits and Forks - [ ] Finish Gists API Implementation - [ ] [Add method to get gist commits](https://developer.github.com/v3/gists/#list-gist-commits) - [ ] [Add method to get gist forks](https://developer.github.com/v3/gists/#list-gist-forks) Fixes #328, Fixes #216 Added implementation for the remaining pieces of the Gists API. The others mentioned in #328 and #216 were completed through other PRs. --- .../Clients/IObservableGistsClient.cs | 18 ++++++ .../Clients/ObservableGistsClient.cs | 28 +++++++++ .../Clients/GistsClientTests.cs | 12 ++++ Octokit.Tests/Clients/GistsClientTests.cs | 38 +++++++++++++ Octokit.Tests/Octokit.Tests.csproj | 1 + .../Reactive/ObservableGistsTests.cs | 57 +++++++++++++++++++ Octokit/Clients/GistsClient.cs | 28 +++++++++ Octokit/Clients/IGistsClient.cs | 18 ++++++ Octokit/Helpers/ApiUrls.cs | 13 +++++ 9 files changed, 213 insertions(+) create mode 100644 Octokit.Tests/Reactive/ObservableGistsTests.cs diff --git a/Octokit.Reactive/Clients/IObservableGistsClient.cs b/Octokit.Reactive/Clients/IObservableGistsClient.cs index 31ea9da5..88afc627 100644 --- a/Octokit.Reactive/Clients/IObservableGistsClient.cs +++ b/Octokit.Reactive/Clients/IObservableGistsClient.cs @@ -91,6 +91,24 @@ namespace Octokit.Reactive /// Only gists updated at or after this time are returned IObservable GetAllForUser(string user, DateTimeOffset since); + /// + /// List gist commits + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-commits + /// + /// The id of the gist + IObservable GetCommits(string id); + + /// + /// List gist forks + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-forks + /// + /// The id of the gist + IObservable GetForks(string id); + /// /// Creates a new gist /// diff --git a/Octokit.Reactive/Clients/ObservableGistsClient.cs b/Octokit.Reactive/Clients/ObservableGistsClient.cs index 3525b870..755417f0 100644 --- a/Octokit.Reactive/Clients/ObservableGistsClient.cs +++ b/Octokit.Reactive/Clients/ObservableGistsClient.cs @@ -180,6 +180,34 @@ namespace Octokit.Reactive return _connection.GetAndFlattenAllPages(ApiUrls.UsersGists(user), request.ToParametersDictionary()); } + /// + /// List gist commits + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-commits + /// + /// The id of the gist + public IObservable GetCommits(string id) + { + Ensure.ArgumentNotNullOrEmptyString(id, "id"); + + return _connection.GetAndFlattenAllPages(ApiUrls.GistCommits(id)); + } + + /// + /// List gist forks + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-forks + /// + /// The id of the gist + public IObservable GetForks(string id) + { + Ensure.ArgumentNotNullOrEmptyString(id, "id"); + + return _connection.GetAndFlattenAllPages(ApiUrls.ForkGist(id)); + } + /// /// Edits a gist /// diff --git a/Octokit.Tests.Integration/Clients/GistsClientTests.cs b/Octokit.Tests.Integration/Clients/GistsClientTests.cs index b7d1f6bf..28be892e 100644 --- a/Octokit.Tests.Integration/Clients/GistsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/GistsClientTests.cs @@ -126,4 +126,16 @@ public class GistsClientTests await _fixture.Delete(createdGist.Id); } + + [IntegrationTest] + public async Task CanGetGistChildren() + { + // Test History/Commits + var commits = await _fixture.GetCommits(testGistId); + Assert.NotNull(commits); + + // Test Forks + var forks = await _fixture.GetForks(testGistId); + Assert.NotNull(forks); + } } diff --git a/Octokit.Tests/Clients/GistsClientTests.cs b/Octokit.Tests/Clients/GistsClientTests.cs index 53c56a68..aa6dc80a 100644 --- a/Octokit.Tests/Clients/GistsClientTests.cs +++ b/Octokit.Tests/Clients/GistsClientTests.cs @@ -124,6 +124,44 @@ public class GistsClientTests } } + public class TheGetChildrenMethods + { + [Fact] + public void EnsureNonNullArguments() + { + var connection = Substitute.For(); + var client = new GistsClient(connection); + + Assert.Throws(() => client.GetCommits(null)); + Assert.Throws(() => client.GetCommits("")); + + Assert.Throws(() => client.GetForks(null)); + Assert.Throws(() => client.GetForks("")); + } + + [Fact] + public void RequestsCorrectGetCommitsUrl() + { + var connection = Substitute.For(); + var client = new GistsClient(connection); + + client.GetCommits("9257657"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "gists/9257657/commits")); + } + + [Fact] + public void RequestsCorrectGetForksUrl() + { + var connection = Substitute.For(); + var client = new GistsClient(connection); + + client.GetForks("9257657"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "gists/9257657/forks")); + } + } + public class TheCreateMethod { [Fact] diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index c3477890..f090987f 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -162,6 +162,7 @@ + diff --git a/Octokit.Tests/Reactive/ObservableGistsTests.cs b/Octokit.Tests/Reactive/ObservableGistsTests.cs new file mode 100644 index 00000000..b4dadfb4 --- /dev/null +++ b/Octokit.Tests/Reactive/ObservableGistsTests.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Threading.Tasks; +using NSubstitute; +using Octokit; +using Octokit.Internal; +using Octokit.Reactive; +using Octokit.Reactive.Internal; +using Octokit.Tests.Helpers; +using Xunit; +using Xunit.Extensions; + +namespace Octokit.Tests.Reactive +{ + public class ObservableGistsTests + { + public class TheGetChildrenMethods + { + [Fact] + public async Task EnsureNonNullArguments() + { + var client = new ObservableGistsClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.GetCommits(null)); + await AssertEx.Throws(async () => await client.GetCommits("")); + + await AssertEx.Throws(async () => await client.GetForks(null)); + await AssertEx.Throws(async () => await client.GetForks("")); + } + + [Fact] + public void RequestsCorrectGetCommitsUrl() + { + var github = Substitute.For(); + var client = new ObservableGistsClient(github); + var expected = new Uri("gists/9257657/commits", UriKind.Relative); + + client.GetCommits("9257657"); + + github.Connection.Received(1).Get>(expected, Arg.Any>(), null); + } + + [Fact] + public void RequestsCorrectGetForksUrl() + { + var github = Substitute.For(); + var client = new ObservableGistsClient(github); + var expected = new Uri("gists/9257657/forks", UriKind.Relative); + + client.GetForks("9257657"); + + github.Connection.Received(1).Get>(expected, Arg.Any>(), null); + } + } + } +} \ No newline at end of file diff --git a/Octokit/Clients/GistsClient.cs b/Octokit/Clients/GistsClient.cs index fcafeb79..124380d8 100644 --- a/Octokit/Clients/GistsClient.cs +++ b/Octokit/Clients/GistsClient.cs @@ -196,6 +196,34 @@ namespace Octokit return ApiConnection.GetAll(ApiUrls.UsersGists(user), request.ToParametersDictionary()); } + /// + /// List gist commits + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-commits + /// + /// The id of the gist + public Task> GetCommits(string id) + { + Ensure.ArgumentNotNullOrEmptyString(id, "id"); + + return ApiConnection.GetAll(ApiUrls.GistCommits(id)); + } + + /// + /// List gist forks + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-forks + /// + /// The id of the gist + public Task> GetForks(string id) + { + Ensure.ArgumentNotNullOrEmptyString(id, "id"); + + return ApiConnection.GetAll(ApiUrls.ForkGist(id)); + } + /// /// Edits a gist /// diff --git a/Octokit/Clients/IGistsClient.cs b/Octokit/Clients/IGistsClient.cs index c344c576..476a1fb2 100644 --- a/Octokit/Clients/IGistsClient.cs +++ b/Octokit/Clients/IGistsClient.cs @@ -98,6 +98,24 @@ namespace Octokit /// Only gists updated at or after this time are returned Task> GetAllForUser(string user, DateTimeOffset since); + /// + /// List gist commits + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-commits + /// + /// The id of the gist + Task> GetCommits(string id); + + /// + /// List gist forks + /// + /// + /// http://developer.github.com/v3/gists/#list-gists-forks + /// + /// The id of the gist + Task> GetForks(string id); + /// /// Creates a new gist /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 925f40f1..929d9054 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -646,6 +646,10 @@ namespace Octokit return "gists/{0}".FormatUri(id); } + /// + /// Returns the for the forks for the specified gist. + /// + /// The id of the gist public static Uri ForkGist(string id) { return "gists/{0}/forks".FormatUri(id); @@ -680,6 +684,15 @@ namespace Octokit return "gists/{0}/comments".FormatUri(gistId); } + /// + /// Returns the for the commits for the specified gist. + /// + /// The id of the gist + public static Uri GistCommits(string id) + { + return "gists/{0}/commits".FormatUri(id); + } + /// /// Returns the that returns the specified pull request. ///