Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New method on RepositoryCommits client - Get the sha1 of a commit reference #1195

Merged
merged 15 commits into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -790,5 +790,40 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentException>(() => client.EditBranch("owner", "repo", "", update));
}
}

public class TheSha1Method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be TheGetSha1Method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - updated it, I'm getting there 😄

{
[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);
}
}
}
}
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";
}
}