Skip to content

Commit

Permalink
Merge pull request #104 from octokit/haacked/commit-status-api
Browse files Browse the repository at this point in the history
Commit Status API
  • Loading branch information
haacked committed Oct 30, 2013
2 parents e287a77 + dea45fa commit fe9cece
Show file tree
Hide file tree
Showing 21 changed files with 507 additions and 61 deletions.
28 changes: 28 additions & 0 deletions Octokit.Reactive/Clients/IObservableCommitStatusClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace Octokit.Reactive
{
public interface IObservableCommitStatusClient
{
/// <summary>
/// Retrieves commit statuses for the specified reference. A reference can be a commit SHA, a branch name, or
/// a tag name.
/// </summary>
/// <remarks>Only users with pull access can see this.</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The reference (SHA, branch name, or tag name) to list commits for.</param>
/// <returns></returns>
IObservable<CommitStatus> GetAll(string owner, string name, string reference);

/// <summary>
/// Creates a commit status for the specified ref.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The reference (SHA, branch name, or tag name) to list commits for.</param>
/// <param name="commitStatus">The commit status to create.</param>
/// <returns></returns>
IObservable<CommitStatus> Create(string owner, string name, string reference, NewCommitStatus commitStatus);
}
}
10 changes: 10 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,15 @@ public interface IObservableRepositoriesClient
/// <param name="name">The name of the repository.</param>
/// <returns></returns>
IObservable<string> GetReadmeHtml(string owner, string name);

/// <summary>
/// A client for GitHub's Commit Status API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/statuses/">Commit Status API documentation</a> for more
/// details. Also check out the <a href="https://github.com/blog/1227-commit-status-api">blog post</a>
/// that announced this feature.
/// </remarks>
IObservableCommitStatusClient CommitStatus { get; }
}
}
47 changes: 47 additions & 0 deletions Octokit.Reactive/Clients/ObservableCommitStatusClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;

namespace Octokit.Reactive
{
public class ObservableCommitStatusClient : IObservableCommitStatusClient
{
readonly ICommitStatusClient _client;
readonly IConnection _connection;

public ObservableCommitStatusClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Repository.CommitStatus;
_connection = client.Connection;
}

/// <summary>
/// Retrieves commit statuses for the specified reference. A reference can be a commit SHA, a branch name, or
/// a tag name.
/// </summary>
/// <remarks>Only users with pull access can see this.</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The reference (SHA, branch name, or tag name) to list commits for.</param>
/// <returns></returns>
public IObservable<CommitStatus> GetAll(string owner, string name, string reference)
{
return _connection.GetAndFlattenAllPages<CommitStatus>(ApiUrls.CommitStatus(owner, name, reference));
}

/// <summary>
/// Creates a commit status for the specified ref.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="reference">The reference (SHA, branch name, or tag name) to list commits for.</param>
/// <param name="commitStatus">The commit status to create.</param>
/// <returns></returns>
public IObservable<CommitStatus> Create(string owner, string name, string reference, NewCommitStatus commitStatus)
{
return _client.Create(owner, name, reference, commitStatus).ToObservable();
}
}
}
1 change: 0 additions & 1 deletion Octokit.Reactive/Clients/ObservableOrganizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class ObservableOrganizationsClient : IObservableOrganizationsClient
readonly IOrganizationsClient _client;
readonly IConnection _connection;


public ObservableOrganizationsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");
Expand Down
3 changes: 3 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public ObservableRepositoriesClient(IGitHubClient client)

_client = client.Repository;
_connection = client.Connection;
CommitStatus = new ObservableCommitStatusClient(client);
}

/// <summary>
Expand Down Expand Up @@ -105,5 +106,7 @@ public IObservable<string> GetReadmeHtml(string owner, string name)

return _client.GetReadmeHtml(owner, name).ToObservable();
}

public IObservableCommitStatusClient CommitStatus { get; private set; }
}
}
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\IObservableCommitStatusClient.cs" />
<Compile Include="Clients\ObservableCommitStatusClient.cs" />
<Compile Include="Clients\ObservableNotificationsClient.cs" />
<Compile Include="Clients\ObservableAuthorizationsClient.cs" />
<Compile Include="Clients\ObservableMiscellaneousClient.cs" />
Expand Down
31 changes: 31 additions & 0 deletions Octokit.Tests.Integration/CommitStatusClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Xunit;

namespace Octokit.Tests.Integration
{
public class CommitStatusClientTests
{
public class TheGetAllMethod
{
[IntegrationTest]
public async Task CanRetrieveStatuses()
{
// Figured it was easier to grab the public status of a public repository for now than
// to go through the rigamarole of creating it all. But ideally, that's exactly what we'd do.

var githubClient = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var statuses = await githubClient.Repository.CommitStatus.GetAll(
"rails",
"rails",
"94b857899506612956bb542e28e292308accb908");
Assert.Equal(2, statuses.Count);
Assert.Equal(CommitState.Failure, statuses[0].State);
Assert.Equal(CommitState.Pending, statuses[1].State);
}
}
}
}
1 change: 1 addition & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AssigneesClientTests.cs" />
<Compile Include="CommitStatusClientTests.cs" />
<Compile Include="MilestonesClientTests.cs" />
<Compile Include="IntegrationTestAttribute.cs" />
<Compile Include="IssuesClientTests.cs" />
Expand Down
91 changes: 91 additions & 0 deletions Octokit.Tests/Clients/CommitStatusClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Tests.Helpers;
using Xunit;

namespace Octokit.Tests.Clients
{
public class CommitStatusClientTests
{
public class TheGetMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new CommitStatusClient(connection);

client.GetAll("fake", "repo", "sha");

connection.Received()
.GetAll<CommitStatus>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/statuses/sha"), null);
}

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

await AssertEx.Throws<ArgumentException>(async () =>
await client.GetAll("", "name", "sha"));
await AssertEx.Throws<ArgumentException>(async () =>
await client.GetAll("owner", "", "sha"));
await AssertEx.Throws<ArgumentException>(async () =>
await client.GetAll("owner", "name", ""));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.GetAll(null, "name", "sha"));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.GetAll("owner", null, "sha"));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.GetAll("owner", "name", null));
}
}

public class TheCreateMethodForUser
{
[Fact]
public void PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new CommitStatusClient(connection);

client.Create("owner", "repo", "sha", new NewCommitStatus { State = CommitState.Success });

connection.Received().Post<CommitStatus>(Arg.Is<Uri>(u =>
u.ToString() == "repos/owner/repo/statuses/sha"),
Arg.Is<NewCommitStatus>(s => s.State == CommitState.Success));
}

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

await AssertEx.Throws<ArgumentException>(async () =>
await client.Create("", "name", "sha", new NewCommitStatus()));
await AssertEx.Throws<ArgumentException>(async () =>
await client.Create("owner", "", "sha", new NewCommitStatus()));
await AssertEx.Throws<ArgumentException>(async () =>
await client.Create("owner", "name", "", new NewCommitStatus()));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.Create(null, "name", "sha", new NewCommitStatus()));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.Create("owner", null, "sha", new NewCommitStatus()));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.Create("owner", "name", null, new NewCommitStatus()));
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.Create("owner", "name", "sha", null));
}
}

public class TheConstructor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new CommitStatusClient(null));
}
}
}
}
Loading

0 comments on commit fe9cece

Please sign in to comment.