diff --git a/Octokit.Reactive/Clients/IObservableRepositoryCommitsClients.cs b/Octokit.Reactive/Clients/IObservableRepositoryCommitsClients.cs index 927b34c03e..29d4d55ead 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoryCommitsClients.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryCommitsClients.cs @@ -43,5 +43,14 @@ public interface IObservableRepositoryCommitsClient /// Used to filter list of commits returned /// IObservable GetAll(string owner, string name, CommitRequest request); + + /// + /// Get the SHA-1 of a commit reference + /// + /// The owner of the repository + /// The name of the repository + /// The repository reference + /// + IObservable GetSha1(string owner, string name, string reference); } } diff --git a/Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs b/Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs index 7982485e39..629142d7dc 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs @@ -73,5 +73,21 @@ public IObservable GetAll(string owner, string name, CommitRequest return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryCommits(owner, name), request.ToParametersDictionary()); } + + /// + /// Get the SHA-1 of a commit reference + /// + /// The owner of the repository + /// The name of the repository + /// The repository reference + /// + public IObservable GetSha1(string owner, string name, string reference) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(reference, "reference"); + + return _commit.GetSha1(owner, name, reference).ToObservable(); + } } } diff --git a/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs index 9546491755..74c4e43e53 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs @@ -87,6 +87,14 @@ public async Task CanGetCommitWithRenamedFiles() .Where(file => file.Status == "renamed") .All(file => string.IsNullOrEmpty(file.PreviousFileName) == false)); } + + [IntegrationTest] + public async Task CanGetSha1() + { + var sha1 = await _fixture.GetSha1("octokit", "octokit.net", "master"); + + Assert.NotNull(sha1); + } } public class TestsWithNewRepository : IDisposable @@ -158,7 +166,17 @@ public async Task CanCompareUsingSha() Assert.Equal(0, result.BehindBy); } - async Task CreateTheWorld() + [IntegrationTest] + public async Task GetSha1FromRepository() + { + var reference = await CreateTheWorld(); + + var sha1 = await _fixture.GetSha1(Helper.UserName, _context.RepositoryName, "master"); + + Assert.Equal(reference.Object.Sha, sha1); + } + + async Task CreateTheWorld() { var master = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); @@ -174,7 +192,7 @@ async Task CreateTheWorld() var newFeature = await CreateCommit("this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha); // create branch - await _github.Git.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", newFeature.Sha)); + return await _github.Git.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", newFeature.Sha)); } async Task CreateTree(IDictionary treeContents) diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index e74cc97b83..dab4474d7b 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -790,5 +790,40 @@ public async Task EnsuresNonNullArguments() await Assert.ThrowsAsync(() => client.EditBranch("owner", "repo", "", update)); } } + + public class TheGetSha1Method + { + [Fact] + public void EnsuresNonNullArguments() + { + var client = new RepositoryCommitsClient(Substitute.For()); + + Assert.ThrowsAsync(() => client.GetSha1("", "name", "reference")); + Assert.ThrowsAsync(() => client.GetSha1("owner", "", "reference")); + Assert.ThrowsAsync(() => client.GetSha1("owner", "name", "")); + } + + [Fact] + public async Task EnsuresNonEmptyArguments() + { + var client = new RepositoryCommitsClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetSha1(null, "name", "reference")); + await Assert.ThrowsAsync(() => client.GetSha1("owner", null, "reference")); + await Assert.ThrowsAsync(() => client.GetSha1("owner", "name", null)); + } + + [Fact] + public void GetsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryCommitsClient(connection); + + client.GetSha1("owner", "name", "reference"); + + connection.Received() + .Get(Arg.Is(u => u.ToString() == "repos/owner/name/commits/reference"), null, AcceptHeaders.CommitReferenceSha1Preview); + } + } } } diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 5c6484c309..9f226d4e3a 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -212,6 +212,7 @@ + diff --git a/Octokit.Tests/Reactive/ObservableRepositoryCommitsClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoryCommitsClientTests.cs new file mode 100644 index 0000000000..bcd91f377e --- /dev/null +++ b/Octokit.Tests/Reactive/ObservableRepositoryCommitsClientTests.cs @@ -0,0 +1,59 @@ +using System; +using System.Reactive.Threading.Tasks; +using System.Threading.Tasks; +using NSubstitute; +using Octokit.Reactive; +using Xunit; + +namespace Octokit.Tests.Reactive +{ + public class ObservableRepositoryCommitsClientTests + { + public class TheCtor + { + [Fact] + public void EnsuresArgument() + { + Assert.Throws(() => new ObservableRepositoryCommitsClient(null)); + } + } + + public class TheGetSha1Method + { + [Fact] + public void EnsuresNonEmptyArguments() + { + var client = new ObservableRepositoryCommitsClient(Substitute.For()); + + Assert.ThrowsAsync(() => client.GetSha1("", "name", "reference").ToTask()); + Assert.ThrowsAsync(() => client.GetSha1("owner", "", "reference").ToTask()); + Assert.ThrowsAsync(() => client.GetSha1("owner", "name", "").ToTask()); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new ObservableRepositoryCommitsClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetSha1(null, "name", "reference").ToTask()); + await Assert.ThrowsAsync(() => client.GetSha1("owner", null, "reference").ToTask()); + await Assert.ThrowsAsync(() => client.GetSha1("owner", "name", null).ToTask()); + } + + [Fact] + public void GetsCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryCommitsClient(gitHubClient); + + client.GetSha1("owner", "name", "reference"); + + gitHubClient + .Received() + .Repository + .Commit + .GetSha1("owner", "name", "reference"); + } + } + } +} diff --git a/Octokit/Clients/IRepositoryCommitsClient.cs b/Octokit/Clients/IRepositoryCommitsClient.cs index f2b09f2b49..bd48619266 100644 --- a/Octokit/Clients/IRepositoryCommitsClient.cs +++ b/Octokit/Clients/IRepositoryCommitsClient.cs @@ -50,5 +50,14 @@ public interface IRepositoryCommitsClient /// Used to filter list of commits returned /// Task> GetAll(string owner, string name, CommitRequest request); + + /// + /// Get the SHA-1 of a commit reference + /// + /// The owner of the repository + /// The name of the repository + /// The repository reference + /// + Task GetSha1(string owner, string name, string reference); } } diff --git a/Octokit/Clients/RepositoryCommitsClient.cs b/Octokit/Clients/RepositoryCommitsClient.cs index 0e8ad1b26f..e5fa961346 100644 --- a/Octokit/Clients/RepositoryCommitsClient.cs +++ b/Octokit/Clients/RepositoryCommitsClient.cs @@ -79,5 +79,21 @@ public Task> GetAll(string owner, string name, Commi return _apiConnection.GetAll(ApiUrls.RepositoryCommits(owner, name), request.ToParametersDictionary()); } + + /// + /// Get the SHA-1 of a commit reference + /// + /// The owner of the repository + /// The name of the repository + /// The repository reference + /// + public Task GetSha1(string owner, string name, string reference) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(reference, "reference"); + + return _apiConnection.Get(ApiUrls.RepositoryCommit(owner, name, reference), null, AcceptHeaders.CommitReferenceSha1Preview); + } } } \ No newline at end of file diff --git a/Octokit/Helpers/AcceptHeaders.cs b/Octokit/Helpers/AcceptHeaders.cs index d94e7e1502..21c0047eb5 100644 --- a/Octokit/Helpers/AcceptHeaders.cs +++ b/Octokit/Helpers/AcceptHeaders.cs @@ -13,5 +13,7 @@ public static class AcceptHeaders public const string ProtectedBranchesApiPreview = "application/vnd.github.loki-preview+json"; public const string StarCreationTimestamps = "application/vnd.github.v3.star+json"; + + public const string CommitReferenceSha1Preview = "application/vnd.github.chitauri-preview+sha"; } }