From f68f9af017bcff2ba79e523e82e6c3bb835534bd Mon Sep 17 00:00:00 2001 From: John Nye Date: Wed, 6 Nov 2013 09:06:31 +0000 Subject: [PATCH] Implement Create commit method --- .../Clients/IObservableCommitsClient.cs | 15 ++++++- .../Clients/ObservableCommitsClient.cs | 9 ++++ Octokit.Tests/Clients/CommitsClientTests.cs | 34 ++++++++++++++ .../Reactive/ObservableCommitsClientTests.cs | 44 ++++++++++++++++++- Octokit/Clients/CommitsClient.cs | 9 ++++ Octokit/Clients/ICommitsClient.cs | 22 +++++----- Octokit/Octokit-Mono.csproj | 4 ++ Octokit/Octokit-netcore45.csproj | 1 + Octokit/Octokit.csproj | 1 + 9 files changed, 126 insertions(+), 13 deletions(-) diff --git a/Octokit.Reactive/Clients/IObservableCommitsClient.cs b/Octokit.Reactive/Clients/IObservableCommitsClient.cs index 8c175792..fa646142 100644 --- a/Octokit.Reactive/Clients/IObservableCommitsClient.cs +++ b/Octokit.Reactive/Clients/IObservableCommitsClient.cs @@ -17,6 +17,19 @@ namespace Octokit.Reactive /// [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Method makes a network request")] - IObservable Get(string owner, string name, string reference); + IObservable Get(string owner, string name, string reference); + + /// + /// Create a commit for a given repository + /// + /// + /// http://developer.github.com/v3/git/commits/#create-a-commit + /// + /// The owner of the repository + /// The name of the repository + /// The commit to create + /// + IObservable Create(string owner, string name, NewCommit commit); + } } \ No newline at end of file diff --git a/Octokit.Reactive/Clients/ObservableCommitsClient.cs b/Octokit.Reactive/Clients/ObservableCommitsClient.cs index 371cd621..41e4cddc 100644 --- a/Octokit.Reactive/Clients/ObservableCommitsClient.cs +++ b/Octokit.Reactive/Clients/ObservableCommitsClient.cs @@ -21,5 +21,14 @@ namespace Octokit.Reactive return _client.Get(owner, name, reference).ToObservable(); } + + public IObservable Create(string owner, string name, NewCommit commit) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNull(commit, "commit"); + + return _client.Create(owner, name, commit).ToObservable(); + } } } \ No newline at end of file diff --git a/Octokit.Tests/Clients/CommitsClientTests.cs b/Octokit.Tests/Clients/CommitsClientTests.cs index acdc37ab..c36cb9a4 100644 --- a/Octokit.Tests/Clients/CommitsClientTests.cs +++ b/Octokit.Tests/Clients/CommitsClientTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using NSubstitute; using Octokit; using Octokit.Tests.Helpers; @@ -33,6 +35,38 @@ public class CommitsClientTests } } + public class TheCreateMethod + { + [Fact] + public void PostsToTheCorrectUrl() + { + var connection = Substitute.For(); + var client = new CommitsClient(connection); + + var parents = new List { "sha-reference1", "sha-reference2" }; + var newCommit = new NewCommit("message", "tree", parents); + client.Create("owner", "repo", newCommit); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/owner/repo/git/commits"), + Arg.Is(nc => nc.Message == "message" + && nc.Tree == "tree" + && nc.Parents.Count() == 2)); + } + + [Fact] + public async void EnsuresNonNullArguments() + { + var client = new CommitsClient(Substitute.For()); + + var newCommit = new NewCommit("message", "tree", new[]{"parent1", "parent2"}); + await AssertEx.Throws(async () => await client.Create(null, "name", newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", null, newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", "name", null)); + await AssertEx.Throws(async () => await client.Create("", "name", newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", "", newCommit)); + } + } + public class TheCtor { [Fact] diff --git a/Octokit.Tests/Reactive/ObservableCommitsClientTests.cs b/Octokit.Tests/Reactive/ObservableCommitsClientTests.cs index af1419c2..f247ef31 100644 --- a/Octokit.Tests/Reactive/ObservableCommitsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableCommitsClientTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; @@ -21,6 +22,7 @@ namespace Octokit.Tests.Reactive public class TheGetMethod { + [Fact] public async Task EnsureNonNullArguments() { var client = new ObservableCommitsClient(Substitute.For()); @@ -31,7 +33,47 @@ namespace Octokit.Tests.Reactive await AssertEx.Throws(async () => await client.Get("", "name", "reference")); await AssertEx.Throws(async () => await client.Get("owner", "", "reference")); await AssertEx.Throws(async () => await client.Get("owner", "name", "")); - } + } + + [Fact] + public async Task RequestsCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableCommitsClient(gitHubClient); + + client.Get("owner", "name", "reference"); + + gitHubClient.Connection.GetAsync>( + new Uri("repos/owner/name/commits/reference", UriKind.Relative), null, null); + } + } + + public class TheCreateMethod + { + [Fact] + public async Task EnsureNonNullArguments() + { + var client = new ObservableCommitsClient(Substitute.For()); + var newCommit = new NewCommit("message", "tree", new[] { "parent1", "parent2" }); + + await AssertEx.Throws(async () => await client.Create(null, "name", newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", null, newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", "name", null)); + await AssertEx.Throws(async () => await client.Create("", "name", newCommit)); + await AssertEx.Throws(async () => await client.Create("owner", "", newCommit)); + } + + [Fact] + public async Task RequestsCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableCommitsClient(gitHubClient); + var newCommit = new NewCommit("message", "tree", new[] { "parent1", "parent2" }); + + client.Create("owner", "name", newCommit); + + gitHubClient.GitDatabase.Commit.Received().Create("owner", "name", newCommit); + } } } } \ No newline at end of file diff --git a/Octokit/Clients/CommitsClient.cs b/Octokit/Clients/CommitsClient.cs index 9dd55dde..9e896785 100644 --- a/Octokit/Clients/CommitsClient.cs +++ b/Octokit/Clients/CommitsClient.cs @@ -17,5 +17,14 @@ namespace Octokit return ApiConnection.Get(ApiUrls.Commit(owner, name, reference)); } + + public Task Create(string owner, string name, NewCommit commit) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNull(commit, "commit"); + + return ApiConnection.Post(ApiUrls.CreateCommit(owner, name), commit); + } } } \ No newline at end of file diff --git a/Octokit/Clients/ICommitsClient.cs b/Octokit/Clients/ICommitsClient.cs index d8649096..6f187fd3 100644 --- a/Octokit/Clients/ICommitsClient.cs +++ b/Octokit/Clients/ICommitsClient.cs @@ -19,16 +19,16 @@ namespace Octokit Justification = "Method makes a network request")] Task Get(string owner, string name, string reference); - ///// - ///// Create a commit for a given repository - ///// - ///// - ///// http://developer.github.com/v3/git/commits/#create-a-commit - ///// - ///// The owner of the repository - ///// The name of the repository - ///// The commit to create - ///// - //Task Create(string owner, string name, NewTag tag); + /// + /// Create a commit for a given repository + /// + /// + /// http://developer.github.com/v3/git/commits/#create-a-commit + /// + /// The owner of the repository + /// The name of the repository + /// The commit to create + /// + Task Create(string owner, string name, NewCommit commit); } } \ No newline at end of file diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 767a7a04..1aaef779 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -42,8 +42,10 @@ Properties\SolutionInfo.cs + + @@ -61,10 +63,12 @@ + + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index fd682e4e..f182cd8f 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -140,6 +140,7 @@ + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index a7b7e46f..9eb9e81b 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -74,6 +74,7 @@ +