Skip to content

Commit

Permalink
Merge pull request #512 from alfhenrik/repo-deploykeys-client
Browse files Browse the repository at this point in the history
Implement Repository Deploy Keys client
  • Loading branch information
haacked committed Jul 3, 2014
2 parents 2f251e9 + 986dae1 commit be34685
Show file tree
Hide file tree
Showing 28 changed files with 922 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,13 @@ public interface IObservableRepositoriesClient
/// See the <a href="http://developer.github.com/v3/pulls/">Pull Requests API documentation</a> for more details
/// </remarks>
IObservablePullRequestsClient PullRequest { get; }

/// <summary>
/// Client for managing deploy keys
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/">Repository Deploy Keys API documentation</a> for more information.
/// </remarks>
IObservableRepositoryDeployKeysClient DeployKeys { get; }
}
}
59 changes: 59 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;

namespace Octokit.Reactive
{
public interface IObservableRepositoryDeployKeysClient
{
/// <summary>
/// Get a single deploy key by number for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#get"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="number">The id of the deploy key.</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get")]
IObservable<DeployKey> Get(string owner, string name, int number);

/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
IObservable<DeployKey> GetAll(string owner, string name);

/// <summary>
/// Creates a new deploy key for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#create"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="newDeployKey">The deploy key to create for the repository.</param>
/// <returns></returns>
IObservable<DeployKey> Create(string owner, string name, NewDeployKey newDeployKey);

/// <summary>
/// Deletes a deploy key from a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#delete"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="number">The id of the deploy key to delete.</param>
/// <returns></returns>
IObservable<Unit> Delete(string owner, string name, int number);
}
}
9 changes: 9 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ObservableRepositoriesClient(IGitHubClient client)
PullRequest = new ObservablePullRequestsClient(client);
RepositoryComments = new ObservableRepositoryCommentsClient(client);
Commits = new ObservableRepositoryCommitsClient(client);
DeployKeys = new ObservableRepositoryDeployKeysClient(client);
}

/// <summary>
Expand Down Expand Up @@ -365,5 +366,13 @@ public IObservable<CompareResult> Compare(string owner, string name, string @bas
/// See the <a href="http://developer.github.com/v3/pulls/">Pull Requests API documentation</a> for more details
/// </remarks>
public IObservablePullRequestsClient PullRequest { get; private set; }

/// <summary>
/// Client for managing deploy keys
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/">Repository Deploy Keys API documentation</a> for more information.
/// </remarks>
public IObservableRepositoryDeployKeysClient DeployKeys { get; private set; }
}
}
102 changes: 102 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Threading.Tasks;
using System.Text;
using System.Threading.Tasks;
using Octokit.Reactive.Internal;

namespace Octokit.Reactive
{
public class ObservableRepositoryDeployKeysClient : IObservableRepositoryDeployKeysClient
{
readonly IRepositoryDeployKeysClient _client;
readonly IConnection _connection;

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

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

/// <summary>
/// Get a single deploy key by number for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#get"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="number">The id of the deploy key.</param>
public IObservable<DeployKey> Get(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _client.Get(owner, name, number).ToObservable();
}

/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
public IObservable<DeployKey> GetAll(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _connection.GetAndFlattenAllPages<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name));
}

/// <summary>
/// Creates a new deploy key for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#create"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="newDeployKey">The deploy key to create for the repository.</param>
/// <returns></returns>
public IObservable<DeployKey> Create(string owner, string name, NewDeployKey newDeployKey)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(newDeployKey, "newDeployKey");


if (string.IsNullOrWhiteSpace(newDeployKey.Title))
throw new ArgumentException("The new deploy key's title must not be null.");

if (string.IsNullOrWhiteSpace(newDeployKey.Key))
throw new ArgumentException("The new deploy key's key must not be null.");

return _client.Create(owner, name, newDeployKey).ToObservable();
}

/// <summary>
/// Deletes a deploy key from a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#delete"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="number">The id of the deploy key to delete.</param>
/// <returns></returns>
public IObservable<Unit> Delete(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _client.Delete(owner, name, number).ToObservable();
}
}
}
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
<Compile Include="Clients\ObservableRepositoryCommitsClients.cs" />
<Compile Include="Clients\IObservableOauthClient.cs" />
<Compile Include="Clients\ObservableOauthClient.cs" />
<Compile Include="Clients\IObservableRepositoryDeployKeysClient.cs" />
<Compile Include="Clients\ObservableRepositoryDeployKeysClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
<Compile Include="Clients\ObservableRepositoryCommitsClients.cs" />
<Compile Include="Clients\IObservableOauthClient.cs" />
<Compile Include="Clients\ObservableOauthClient.cs" />
<Compile Include="Clients\IObservableRepositoryDeployKeysClient.cs" />
<Compile Include="Clients\ObservableRepositoryDeployKeysClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
<Compile Include="Clients\ObservableRepositoryCommitsClients.cs" />
<Compile Include="Clients\IObservableOauthClient.cs" />
<Compile Include="Clients\ObservableOauthClient.cs" />
<Compile Include="Clients\IObservableRepositoryDeployKeysClient.cs" />
<Compile Include="Clients\ObservableRepositoryDeployKeysClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
</Compile>
<Compile Include="Clients\IObservableOauthClient.cs" />
<Compile Include="Clients\IObservableRepositoryCommitsClients.cs" />
<Compile Include="Clients\IObservableRepositoryDeployKeysClient.cs" />
<Compile Include="Clients\ObservableRepositoryDeployKeysClient.cs" />
<Compile Include="Clients\ObservableOauthClient.cs" />
<Compile Include="Clients\ObservableRepositoryCommentsClient.cs" />
<Compile Include="Clients\IObservableRepositoryCommentsClient.cs" />
Expand Down
109 changes: 109 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Xunit;

public class RepositoryDeployKeysClientTests : IDisposable
{
const string _key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDB8IE5+RppLpeW+6lqo0fpfvMunKg6W4bhYCfVJIOYbpKoHP95nTUMZPBT++9NLeB4/YsuNTCrrpnpjc4f2IVpGvloRiVXjAzoJk9QIL6uzn1zRFdvaxSJ3Urhe9LcLHcIgccgZgSdWGzaZI3xtMvGC4diwWNsPjvVc/RyDM/MPqAim0X5XVOQwEFsSsUSraezJ+VgYMYzLYBcKWW0B86HVVhL4ZtmcY/RN2544bljnzw2M3aQvXNPTvkuiUoqLOI+5/qzZ8PfkruO55YtweEd0lkY6oZvrBPMD6dLODEqMHb4tD6htx60wSipNqjPwpOMpzp0Bk3G909unVXi6Fw5";
const string _keyTitle = "octokit@github";
readonly IGitHubClient _client;
IRepositoryDeployKeysClient _fixture;
Repository _repository;
DeployKey _deployKey;
string _owner;

public RepositoryDeployKeysClientTests()
{
_client = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};

var repoName = Helper.MakeNameWithTimestamp("public-repo");
_fixture = _client.Repository.DeployKeys;
_repository = _client.Repository.Create(new NewRepository { Name = repoName, AutoInit = true }).Result;
_owner = _repository.Owner.Login;

}

[IntegrationTest]
public async Task CanCreateADeployKey()
{
var deployKey = new NewDeployKey()
{
Key = _key,
Title = _keyTitle
};

var deployKeyResult = await _fixture.Create(_owner, _repository.Name, deployKey);
Assert.NotNull(deployKeyResult);
Assert.Equal(_key, deployKeyResult.Key);
Assert.Equal(_keyTitle, deployKeyResult.Title);
}


[IntegrationTest]
public async Task CanRetrieveAllDeployKeys()
{
var deployKeys = await _fixture.GetAll(_owner, _repository.Name);
Assert.Equal(0, deployKeys.Count);

var deployKey = new NewDeployKey()
{
Key = _key,
Title = _keyTitle
};

await _fixture.Create(_owner, _repository.Name, deployKey);

deployKeys = await _fixture.GetAll(_owner, _repository.Name);
Assert.Equal(1, deployKeys.Count);
Assert.Equal(_key, deployKeys[0].Key);
Assert.Equal(_keyTitle, deployKeys[0].Title);
}

[IntegrationTest]
public async Task CanRetrieveADeployKey()
{
var newDeployKey = new NewDeployKey()
{
Key = _key,
Title = _keyTitle
};
var deployKeyResult = await _fixture.Create(_owner, _repository.Name, newDeployKey);

var deployKey = await _fixture.Get(_owner, _repository.Name, deployKeyResult.Id);
Assert.NotNull(deployKey);
Assert.Equal(deployKeyResult.Id, deployKey.Id);
Assert.Equal(_key, deployKey.Key);
Assert.Equal(_keyTitle, deployKey.Title);
}

[IntegrationTest]
public async Task CanRemoveADeployKey()
{
var newDeployKey = new NewDeployKey()
{
Key = _key,
Title = _keyTitle
};

await _fixture.Create(_owner, _repository.Name, newDeployKey);

var deployKeys = await _fixture.GetAll(_owner, _repository.Name);
Assert.Equal(1, deployKeys.Count);
Assert.Equal(_key, deployKeys[0].Key);
Assert.Equal(_keyTitle, deployKeys[0].Title);

await _fixture.Delete(_owner, _repository.Name, deployKeys[0].Id);
deployKeys = await _fixture.GetAll(_owner, _repository.Name);
Assert.Equal(0, deployKeys.Count);
}

public void Dispose()
{
Helper.DeleteRepo(_repository);
}
}
2 changes: 2 additions & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="Clients\PullRequestsClientTests.cs" />
<Compile Include="Clients\ReferencesClientTests.cs" />
<Compile Include="Clients\RepositoryCommitsClientTests.cs" />
<Compile Include="Clients\RepositoryDeployKeysClientTests.cs" />
<Compile Include="Clients\SearchClientTests.cs" />
<Compile Include="Clients\StatisticsClientTests.cs" />
<Compile Include="Clients\TreeClientTests.cs" />
Expand All @@ -91,6 +92,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Clients\UsersClientTests.cs" />
<Compile Include="Reactive\ObservableRespositoryDeployKeysClientTests.cs" />
<Compile Include="Reactive\ObservableUserEmailsClientTests.cs" />
<Compile Include="Reactive\ObservableTeamsClientTests.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit be34685

Please sign in to comment.