Skip to content

Commit

Permalink
Merge pull request #1195 from rogertinsley/issue-1189
Browse files Browse the repository at this point in the history
New method on RepositoryCommits client - Get the sha1 of a commit reference
  • Loading branch information
ryangribble committed Mar 22, 2016
2 parents d01b510 + dd63d1b commit e147837
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@ public interface IObservableRepositoryCommitsClient
/// <param name="request">Used to filter list of commits returned</param>
/// <returns></returns>
IObservable<GitHubCommit> GetAll(string owner, string name, CommitRequest request);

/// <summary>
/// Get the SHA-1 of a commit reference
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The repository reference</param>
/// <returns></returns>
IObservable<string> GetSha1(string owner, string name, string reference);
}
}
16 changes: 16 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,21 @@ public IObservable<GitHubCommit> GetAll(string owner, string name, CommitRequest
return _connection.GetAndFlattenAllPages<GitHubCommit>(ApiUrls.RepositoryCommits(owner, name),
request.ToParametersDictionary());
}

/// <summary>
/// Get the SHA-1 of a commit reference
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The repository reference</param>
/// <returns></returns>
public IObservable<string> 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();
}
}
}
22 changes: 20 additions & 2 deletions Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Reference> CreateTheWorld()
{
var master = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master");

Expand All @@ -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<TreeResponse> CreateTree(IDictionary<string, string> treeContents)
Expand Down
35 changes: 35 additions & 0 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,5 +789,40 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentException>(() => client.EditBranch("owner", "repo", "", update));
}
}

public class TheGetSha1Method
{
[Fact]
public void EnsuresNonNullArguments()
{
var client = new RepositoryCommitsClient(Substitute.For<IApiConnection>());

Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("", "name", "reference"));
Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("owner", "", "reference"));
Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("owner", "name", ""));
}

[Fact]
public async Task EnsuresNonEmptyArguments()
{
var client = new RepositoryCommitsClient(Substitute.For<IApiConnection>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1(null, "name", "reference"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1("owner", null, "reference"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1("owner", "name", null));
}

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

client.GetSha1("owner", "name", "reference");

connection.Received()
.Get<string>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/commits/reference"), null, AcceptHeaders.CommitReferenceSha1Preview);
}
}
}
}
1 change: 1 addition & 0 deletions Octokit.Tests/Octokit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
<Compile Include="Reactive\ObservableReleasesClientTests.cs" />
<Compile Include="Reactive\ObservablePullRequestReviewCommentsClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoriesClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoryCommitsClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoryDeployKeysClientTests.cs" />
<Compile Include="Reactive\ObservableGistsTests.cs" />
<Compile Include="Reactive\ObservableStarredClientTests.cs" />
Expand Down
59 changes: 59 additions & 0 deletions Octokit.Tests/Reactive/ObservableRepositoryCommitsClientTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>(() => new ObservableRepositoryCommitsClient(null));
}
}

public class TheGetSha1Method
{
[Fact]
public void EnsuresNonEmptyArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("", "name", "reference").ToTask());
Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("owner", "", "reference").ToTask());
Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1("owner", "name", "").ToTask());
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1(null, "name", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1("owner", null, "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSha1("owner", "name", null).ToTask());
}

[Fact]
public void GetsCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableRepositoryCommitsClient(gitHubClient);

client.GetSha1("owner", "name", "reference");

gitHubClient
.Received()
.Repository
.Commit
.GetSha1("owner", "name", "reference");
}
}
}
}
9 changes: 9 additions & 0 deletions Octokit/Clients/IRepositoryCommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ public interface IRepositoryCommitsClient
/// <param name="request">Used to filter list of commits returned</param>
/// <returns></returns>
Task<IReadOnlyList<GitHubCommit>> GetAll(string owner, string name, CommitRequest request);

/// <summary>
/// Get the SHA-1 of a commit reference
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The repository reference</param>
/// <returns></returns>
Task<string> GetSha1(string owner, string name, string reference);
}
}
16 changes: 16 additions & 0 deletions Octokit/Clients/RepositoryCommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,21 @@ public Task<IReadOnlyList<GitHubCommit>> GetAll(string owner, string name, Commi
return _apiConnection.GetAll<GitHubCommit>(ApiUrls.RepositoryCommits(owner, name),
request.ToParametersDictionary());
}

/// <summary>
/// Get the SHA-1 of a commit reference
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The repository reference</param>
/// <returns></returns>
public Task<string> GetSha1(string owner, string name, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");

return _apiConnection.Get<string>(ApiUrls.RepositoryCommit(owner, name, reference), null, AcceptHeaders.CommitReferenceSha1Preview);
}
}
}
2 changes: 2 additions & 0 deletions Octokit/Helpers/AcceptHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

0 comments on commit e147837

Please sign in to comment.