Additional GitHub Apps APIs (#1854)

* Adding functionality to query installations for user

* Rename method

* Adding installation methods

* Adding tests

* Adding observable client methods

* Adding InstallationResponse

* Adding GitHub Apps Installations Client

* Tweaking doc comments

* Undoing unintentional changes and cleaning up

* Reordering functions in clients to keep the same code look and feel

* Making sure all methods are documented with their authentication requirements

* Syntax error

* Renaming methods and tests

* Renaming property

* Test cleanup

* XmlDoc comment fixups and consistency

* rename User -To-Server auth methods from xxxForUser to xxxForCurrentUser

* rename GitHubAppsInstallationsClient to GitHubAppInstallationsClient to be consistent with single/plural naming conventions

* make method order match the order on github docs site

* tidy up usings

* correct implementation of GetALlInstallationsForCurrent method to be consistent

* Add missing unit and integration tests for ObservableGitHubAppsClient

* fix renamed method in observable tests

* Add EnsuresNonEmptyArguments tests and fixup asserts in GitHubAppsClient

* Add tests for new Observable client methods and fixup Null/Empty asserts in Observable client

* change non paginated call to call through to other method but with ApiOptions.None

* add unit tests for observable client and fixup errors they found

* add integration tests for new GitHubAppsClient methods, fixed an incorrect route that the tests found!

* add integration tests for extra methods on observable client

* add integration tests for new clients GitHubAppInstallationsClient and ObservableGitHubAppInstallationsClient

* deprecate renamed method properly, to avoid breaking change
This commit is contained in:
Stanley Goldman
2018-09-03 06:36:33 -04:00
committed by Ryan Gribble
parent ac86133f18
commit 5f1421bd34
20 changed files with 1811 additions and 45 deletions

View File

@@ -0,0 +1,41 @@
using System;
namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub Applications Installations API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/installations/">GitHub Apps Installations API documentation</a> for more information.
/// </remarks>
public interface IObservableGitHubAppInstallationsClient
{
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
IObservable<RepositoriesResponse> GetAllRepositoriesForCurrent();
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
IObservable<RepositoriesResponse> GetAllRepositoriesForCurrent(ApiOptions options);
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
IObservable<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId);
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
IObservable<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId, ApiOptions options);
}
}

View File

@@ -3,7 +3,7 @@
namespace Octokit.Reactive namespace Octokit.Reactive
{ {
/// <summary> /// <summary>
/// A client for GitHub Applications API. Provides the methods required to get GitHub applications and installations. /// A client for GitHub Applications API.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information. /// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information.
@@ -11,40 +11,68 @@ namespace Octokit.Reactive
public interface IObservableGitHubAppsClient public interface IObservableGitHubAppsClient
{ {
/// <summary> /// <summary>
/// Get a single GitHub App. /// Access GitHub's Apps Installations API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developer.github.com/v3/apps/installations/
/// </remarks>
IObservableGitHubAppInstallationsClient Installation { get; }
/// <summary>
/// Get a single GitHub App (if private, requires Personal Access Token or GitHubApp auth)
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks>
/// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param> /// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param>
IObservable<GitHubApp> Get(string slug); IObservable<GitHubApp> Get(string slug);
/// <summary> /// <summary>
/// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp JWT token auth). /// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks>
IObservable<GitHubApp> GetCurrent(); IObservable<GitHubApp> GetCurrent();
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
IObservable<Installation> GetAllInstallationsForCurrent(); IObservable<Installation> GetAllInstallationsForCurrent();
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <param name="options">Options for changing the API response</param> /// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
IObservable<Installation> GetAllInstallationsForCurrent(ApiOptions options); IObservable<Installation> GetAllInstallationsForCurrent(ApiOptions options);
/// <summary> /// <summary>
/// Get a single GitHub App Installation (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
[Obsolete("This method will be removed in a future release. Please use GetInstallationForCurrent() instead")]
IObservable<Installation> GetInstallation(long installationId); IObservable<Installation> GetInstallation(long installationId);
/// <summary> /// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param>
IObservable<Installation> GetInstallationForCurrent(long installationId);
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
IObservable<InstallationsResponse> GetAllInstallationsForCurrentUser();
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
IObservable<InstallationsResponse> GetAllInstallationsForCurrentUser(ApiOptions options);
/// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// https://developer.github.com/v3/apps/#create-a-new-installation-token /// https://developer.github.com/v3/apps/#create-a-new-installation-token
@@ -53,5 +81,34 @@ namespace Octokit.Reactive
/// </remarks> /// </remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
IObservable<AccessToken> CreateInstallationToken(long installationId); IObservable<AccessToken> CreateInstallationToken(long installationId);
/// <summary>
/// Enables an authenticated GitHub App to find the organization's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-organization-installation</remarks>
/// <param name="organization">The name of the organization</param>
IObservable<Installation> GetOrganizationInstallationForCurrent(string organization);
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="owner">The owner of the repo</param>
/// <param name="repo">The name of the repo</param>
IObservable<Installation> GetRepositoryInstallationForCurrent(string owner, string repo);
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="repositoryId">The Id of the repository</param>
IObservable<Installation> GetRepositoryInstallationForCurrent(long repositoryId);
/// <summary>
/// Enables an authenticated GitHub App to find the users's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-user-installation</remarks>
/// <param name="user">The name of the user</param>
IObservable<Installation> GetUserInstallationForCurrent(string user);
} }
} }

View File

@@ -0,0 +1,65 @@
using System;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub Applications Installations API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/installations/">GitHub Apps Installations API documentation</a> for more information.
/// </remarks>
public class ObservableGitHubAppInstallationsClient : IObservableGitHubAppInstallationsClient
{
private IGitHubAppInstallationsClient _client;
private readonly IConnection _connection;
public ObservableGitHubAppInstallationsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, nameof(client));
_client = client.GitHubApps.Installation;
_connection = client.Connection;
}
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
public IObservable<RepositoriesResponse> GetAllRepositoriesForCurrent()
{
return GetAllRepositoriesForCurrent(ApiOptions.None);
}
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
public IObservable<RepositoriesResponse> GetAllRepositoriesForCurrent(ApiOptions options)
{
return _connection.GetAndFlattenAllPages<RepositoriesResponse>(ApiUrls.InstallationRepositories(), null, AcceptHeaders.GitHubAppsPreview, options);
}
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
public IObservable<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId)
{
return GetAllRepositoriesForCurrentUser(installationId, ApiOptions.None);
}
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
public IObservable<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId, ApiOptions options)
{
return _connection.GetAndFlattenAllPages<RepositoriesResponse>(ApiUrls.UserInstallationRepositories(installationId), null, AcceptHeaders.GitHubAppsPreview, options);
}
}
}

View File

@@ -1,11 +1,11 @@
using Octokit.Reactive.Internal; using System;
using System;
using System.Reactive.Threading.Tasks; using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive namespace Octokit.Reactive
{ {
/// <summary> /// <summary>
/// A client for GitHub Applications API. Provides the methods required to get GitHub applications and installations. /// A client for GitHub Applications API.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information. /// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information.
@@ -19,17 +19,34 @@ namespace Octokit.Reactive
{ {
Ensure.ArgumentNotNull(client, "client"); Ensure.ArgumentNotNull(client, "client");
Installation = new ObservableGitHubAppInstallationsClient(client);
_client = client.GitHubApps; _client = client.GitHubApps;
_connection = client.Connection; _connection = client.Connection;
} }
/// <summary>
/// Access GitHub's Apps Installations API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developer.github.com/v3/apps/installations/
/// </remarks>
public IObservableGitHubAppInstallationsClient Installation { get; private set; }
/// <summary>
/// Get a single GitHub App (if private, requires Personal Access Token or GitHubApp auth)
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks>
/// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param>
public IObservable<GitHubApp> Get(string slug) public IObservable<GitHubApp> Get(string slug)
{ {
Ensure.ArgumentNotNullOrEmptyString(slug, nameof(slug));
return _client.Get(slug).ToObservable(); return _client.Get(slug).ToObservable();
} }
/// <summary> /// <summary>
/// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp JWT token auth). /// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks>
public IObservable<GitHubApp> GetCurrent() public IObservable<GitHubApp> GetCurrent()
@@ -38,16 +55,16 @@ namespace Octokit.Reactive
} }
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
public IObservable<Installation> GetAllInstallationsForCurrent() public IObservable<Installation> GetAllInstallationsForCurrent()
{ {
return _connection.GetAndFlattenAllPages<Installation>(ApiUrls.Installations(), null, AcceptHeaders.GitHubAppsPreview); return GetAllInstallationsForCurrent(ApiOptions.None);
} }
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <param name="options">Options for changing the API response</param> /// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
@@ -59,17 +76,46 @@ namespace Octokit.Reactive
} }
/// <summary> /// <summary>
/// Get a single GitHub App Installation (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
[Obsolete("This method will be removed in a future release. Please use GetInstallationForCurrent() instead")]
public IObservable<Installation> GetInstallation(long installationId) public IObservable<Installation> GetInstallation(long installationId)
{ {
return _client.GetInstallation(installationId).ToObservable(); return GetInstallationForCurrent(installationId);
} }
/// <summary> /// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param>
public IObservable<Installation> GetInstallationForCurrent(long installationId)
{
return _client.GetInstallationForCurrent(installationId).ToObservable();
}
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
public IObservable<InstallationsResponse> GetAllInstallationsForCurrentUser()
{
return _connection.GetAndFlattenAllPages<InstallationsResponse>(ApiUrls.UserInstallations(), null, AcceptHeaders.GitHubAppsPreview);
}
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
public IObservable<InstallationsResponse> GetAllInstallationsForCurrentUser(ApiOptions options)
{
return _connection.GetAndFlattenAllPages<InstallationsResponse>(ApiUrls.UserInstallations(), null, AcceptHeaders.GitHubAppsPreview, options);
}
/// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// https://developer.github.com/v3/apps/#create-a-new-installation-token /// https://developer.github.com/v3/apps/#create-a-new-installation-token
@@ -81,5 +127,53 @@ namespace Octokit.Reactive
{ {
return _client.CreateInstallationToken(installationId).ToObservable(); return _client.CreateInstallationToken(installationId).ToObservable();
} }
/// <summary>
/// Enables an authenticated GitHub App to find the organization's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-organization-installation</remarks>
/// <param name="organization">The name of the organization</param>
public IObservable<Installation> GetOrganizationInstallationForCurrent(string organization)
{
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
return _client.GetOrganizationInstallationForCurrent(organization).ToObservable();
}
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="owner">The owner of the repo</param>
/// <param name="repo">The name of the repo</param>
public IObservable<Installation> GetRepositoryInstallationForCurrent(string owner, string repo)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(repo, nameof(repo));
return _client.GetRepositoryInstallationForCurrent(owner, repo).ToObservable();
}
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="repositoryId">The Id of the repository</param>
public IObservable<Installation> GetRepositoryInstallationForCurrent(long repositoryId)
{
return _client.GetRepositoryInstallationForCurrent(repositoryId).ToObservable();
}
/// <summary>
/// Enables an authenticated GitHub App to find the users's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-user-installation</remarks>
/// <param name="user">The name of the user</param>
public IObservable<Installation> GetUserInstallationForCurrent(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
return _client.GetUserInstallationForCurrent(user).ToObservable();
}
} }
} }

View File

@@ -0,0 +1,52 @@
using System.Threading.Tasks;
using Octokit.Tests.Integration;
using Xunit;
namespace Octokit.Tests.Clients
{
public class GitHubAppInstallationsClientTests
{
public class TheGetAllRepositoriesForCurrentMethod
{
IGitHubClient _github;
public TheGetAllRepositoriesForCurrentMethod()
{
// Authenticate as a GitHubApp Installation
_github = Helper.GetAuthenticatedGitHubAppInstallationForOwner(Helper.UserName);
}
[GitHubAppsTest]
public async Task GetsAllRepositories()
{
var result = await _github.GitHubApps.Installation.GetAllRepositoriesForCurrent();
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
public class TheGetAllRepositoriesForCurrentUserMethod
{
IGitHubClient _github;
public TheGetAllRepositoriesForCurrentUserMethod()
{
// Need to Authenticate as User to Server but not possible without receiving redirect from github.com
//_github = Helper.GetAuthenticatedUserToServer();
_github = null;
}
[GitHubAppsTest(Skip = "Not possible to authenticate with User to Server auth")]
public async Task GetsAllRepositories()
{
var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id;
var result = await _github.GitHubApps.Installation.GetAllRepositoriesForCurrentUser(installationId);
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
}
}

View File

@@ -1,5 +1,4 @@
using Octokit.Tests.Integration.Helpers; using System;
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
@@ -77,11 +76,11 @@ namespace Octokit.Tests.Integration.Clients
} }
} }
public class TheGetInstallationMethod public class TheGetInstallationForCurrentMethod
{ {
IGitHubClient _github; IGitHubClient _github;
public TheGetInstallationMethod() public TheGetInstallationForCurrentMethod()
{ {
// Authenticate as a GitHubApp // Authenticate as a GitHubApp
_github = Helper.GetAuthenticatedGitHubAppsClient(); _github = Helper.GetAuthenticatedGitHubAppsClient();
@@ -94,7 +93,7 @@ namespace Octokit.Tests.Integration.Clients
var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id; var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id;
// Get the installation by Id // Get the installation by Id
var result = await _github.GitHubApps.GetInstallation(installationId); var result = await _github.GitHubApps.GetInstallationForCurrent(installationId);
Assert.True(result.AppId == Helper.GitHubAppId); Assert.True(result.AppId == Helper.GitHubAppId);
Assert.Equal(Helper.GitHubAppId, result.AppId); Assert.Equal(Helper.GitHubAppId, result.AppId);
@@ -106,6 +105,27 @@ namespace Octokit.Tests.Integration.Clients
} }
} }
public class TheGetAllInstallationsForCurrentUserMethod
{
IGitHubClient _github;
public TheGetAllInstallationsForCurrentUserMethod()
{
// Need to Authenticate as User to Server but not possible without receiving redirect from github.com
//_github = Helper.GetAuthenticatedUserToServer();
_github = null;
}
[GitHubAppsTest(Skip ="Not possible to authenticate with User to Server auth")]
public async Task GetsAllInstallationsForCurrentUser()
{
var result = await _github.GitHubApps.GetAllInstallationsForCurrentUser();
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
public class TheCreateInstallationTokenMethod public class TheCreateInstallationTokenMethod
{ {
IGitHubClient _github; IGitHubClient _github;
@@ -129,5 +149,84 @@ namespace Octokit.Tests.Integration.Clients
Assert.True(DateTimeOffset.Now < result.ExpiresAt); Assert.True(DateTimeOffset.Now < result.ExpiresAt);
} }
} }
public class TheGetOrganizationInstallationForCurrentMethod
{
IGitHubClient _github;
public TheGetOrganizationInstallationForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = Helper.GetAuthenticatedGitHubAppsClient();
}
[GitHubAppsTest]
public async Task GetsOrganizationInstallations()
{
var result = await _github.GitHubApps.GetOrganizationInstallationForCurrent(Helper.Organization);
Assert.NotNull(result);
}
}
public class TheGetRepositoryInstallationForCurrentMethod
{
IGitHubClient _github;
IGitHubClient _githubAppInstallation;
public TheGetRepositoryInstallationForCurrentMethod()
{
// Autheticate as a GitHubApp
_github = Helper.GetAuthenticatedGitHubAppsClient();
// Authenticate as a GitHubApp Installation
_githubAppInstallation = Helper.GetAuthenticatedGitHubAppInstallationForOwner(Helper.UserName);
}
[GitHubAppsTest]
public async Task GetsRepositoryInstallations()
{
// Find a repo under the installation
var repos = await _githubAppInstallation.GitHubApps.Installation.GetAllRepositoriesForCurrent();
var repo = repos.Repositories.First();
// Now, using the GitHub App auth, find this repository installation
var result = await _github.GitHubApps.GetRepositoryInstallationForCurrent(repo.Owner.Login, repo.Name);
Assert.NotNull(result);
}
[GitHubAppsTest]
public async Task GetsRepositoryInstallationsWithRepositoryId()
{
// Find a repo under the installation
var repos = await _githubAppInstallation.GitHubApps.Installation.GetAllRepositoriesForCurrent();
var repo = repos.Repositories.First();
// Now, using the GitHub App auth, find this repository installation
var result = await _github.GitHubApps.GetRepositoryInstallationForCurrent(repo.Id);
Assert.NotNull(result);
}
}
public class TheGetUserInstallationForCurrentMethod
{
IGitHubClient _github;
public TheGetUserInstallationForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = Helper.GetAuthenticatedGitHubAppsClient();
}
[GitHubAppsTest]
public async Task GetsUserInstallations()
{
var result = await _github.GitHubApps.GetUserInstallationForCurrent(Helper.UserName);
Assert.NotNull(result);
}
}
} }
} }

View File

@@ -0,0 +1,54 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit.Reactive;
using Octokit.Tests.Integration;
using Xunit;
namespace Octokit.Tests.Clients
{
public class ObservableGitHubAppInstallationsClientTests
{
public class TheGetAllRepositoriesForCurrentMethod
{
IObservableGitHubClient _github;
public TheGetAllRepositoriesForCurrentMethod()
{
// Authenticate as a GitHubApp Installation
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppInstallationForOwner(Helper.UserName));
}
[GitHubAppsTest]
public async Task GetsAllRepositories()
{
var result = await _github.GitHubApps.Installation.GetAllRepositoriesForCurrent();
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
public class TheGetAllRepositoriesForCurrentUserMethod
{
IObservableGitHubClient _github;
public TheGetAllRepositoriesForCurrentUserMethod()
{
// Need to Authenticate as User to Server but not possible without receiving redirect from github.com
//_github = new ObservableGitHubClient(Helper.GetAuthenticatedUserToServer());
_github = null;
}
[GitHubAppsTest(Skip = "Not possible to authenticate with User to Server auth")]
public async Task GetsAllRepositories()
{
var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id;
var result = await _github.GitHubApps.Installation.GetAllRepositoriesForCurrentUser(installationId);
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
}
}

View File

@@ -0,0 +1,234 @@
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit.Reactive;
using Xunit;
namespace Octokit.Tests.Integration.Clients
{
public class ObservableGitHubAppsClientTests
{
public class TheGetMethod
{
IObservableGitHubClient _github;
public TheGetMethod()
{
// Regular authentication
_github = new ObservableGitHubClient(Helper.GetAuthenticatedClient());
}
[GitHubAppsTest]
public async Task GetsApp()
{
var result = await _github.GitHubApps.Get(Helper.GitHubAppSlug);
Assert.Equal(Helper.GitHubAppId, result.Id);
Assert.False(string.IsNullOrEmpty(result.Name));
Assert.NotNull(result.Owner);
}
}
public class TheGetCurrentMethod
{
IObservableGitHubClient _github;
public TheGetCurrentMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task GetsCurrentApp()
{
var result = await _github.GitHubApps.GetCurrent();
Assert.Equal(Helper.GitHubAppId, result.Id);
Assert.False(string.IsNullOrEmpty(result.Name));
Assert.NotNull(result.Owner);
}
}
public class TheGetAllInstallationsForCurrentMethod
{
IObservableGitHubClient _github;
public TheGetAllInstallationsForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task GetsAllInstallations()
{
var result = await _github.GitHubApps.GetAllInstallationsForCurrent().ToList();
foreach (var installation in result)
{
Assert.Equal(Helper.GitHubAppId, installation.AppId);
Assert.NotNull(installation.Account);
Assert.NotNull(installation.Permissions);
Assert.Equal(InstallationPermissionLevel.Read, installation.Permissions.Metadata);
Assert.False(string.IsNullOrEmpty(installation.HtmlUrl));
Assert.NotEqual(0, installation.TargetId);
}
}
}
public class TheGetInstallationForCurrentMethod
{
IObservableGitHubClient _github;
public TheGetInstallationForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task GetsInstallation()
{
// Get the installation Id
var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id;
// Get the installation by Id
var result = await _github.GitHubApps.GetInstallationForCurrent(installationId);
Assert.True(result.AppId == Helper.GitHubAppId);
Assert.Equal(Helper.GitHubAppId, result.AppId);
Assert.NotNull(result.Account);
Assert.NotNull(result.Permissions);
Assert.Equal(InstallationPermissionLevel.Read, result.Permissions.Metadata);
Assert.False(string.IsNullOrEmpty(result.HtmlUrl));
Assert.NotEqual(0, result.TargetId);
}
}
public class TheGetAllInstallationsForCurrentUserMethod
{
IObservableGitHubClient _github;
public TheGetAllInstallationsForCurrentUserMethod()
{
// Need to Authenticate as User to Server but not possible without receiving redirect from github.com
//_github = new ObservableGitHubClient(Helper.GetAuthenticatedUserToServer());
_github = null;
}
[GitHubAppsTest(Skip = "Not possible to authenticate with User to Server auth")]
public async Task GetsAllInstallationsForCurrentUser()
{
var result = await _github.GitHubApps.GetAllInstallationsForCurrentUser();
Assert.NotNull(result);
Assert.True(result.TotalCount > 0);
}
}
public class TheCreateInstallationTokenMethod
{
IObservableGitHubClient _github;
public TheCreateInstallationTokenMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task CreatesInstallationToken()
{
// Get the installation Id
var installationId = Helper.GetGitHubAppInstallationForOwner(Helper.UserName).Id;
// Create installation token
var result = await _github.GitHubApps.CreateInstallationToken(installationId);
Assert.NotNull(result.Token);
Assert.True(DateTimeOffset.Now < result.ExpiresAt);
}
}
public class TheGetOrganizationInstallationForCurrentMethod
{
IObservableGitHubClient _github;
public TheGetOrganizationInstallationForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task GetsOrganizationInstallations()
{
var result = await _github.GitHubApps.GetOrganizationInstallationForCurrent(Helper.Organization);
Assert.NotNull(result);
}
}
public class TheGetRepositoryInstallationForCurrentMethod
{
IObservableGitHubClient _github;
IObservableGitHubClient _githubAppInstallation;
public TheGetRepositoryInstallationForCurrentMethod()
{
// Autheticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
// Authenticate as a GitHubApp Installation
_githubAppInstallation = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppInstallationForOwner(Helper.UserName));
}
[GitHubAppsTest]
public async Task GetsRepositoryInstallations()
{
// Find a repo under the installation
var repos = await _githubAppInstallation.GitHubApps.Installation.GetAllRepositoriesForCurrent();
var repo = repos.Repositories.First();
// Now, using the GitHub App auth, find this repository installation
var result = await _github.GitHubApps.GetRepositoryInstallationForCurrent(repo.Owner.Login, repo.Name);
Assert.NotNull(result);
}
[GitHubAppsTest]
public async Task GetsRepositoryInstallationsWithRepositoryId()
{
// Find a repo under the installation
var repos = await _githubAppInstallation.GitHubApps.Installation.GetAllRepositoriesForCurrent();
var repo = repos.Repositories.First();
// Now, using the GitHub App auth, find this repository installation
var result = await _github.GitHubApps.GetRepositoryInstallationForCurrent(repo.Id);
Assert.NotNull(result);
}
}
public class TheGetUserInstallationForCurrentMethod
{
IObservableGitHubClient _github;
public TheGetUserInstallationForCurrentMethod()
{
// Authenticate as a GitHubApp
_github = new ObservableGitHubClient(Helper.GetAuthenticatedGitHubAppsClient());
}
[GitHubAppsTest]
public async Task GetsUserInstallations()
{
var result = await _github.GitHubApps.GetUserInstallationForCurrent(Helper.UserName);
Assert.NotNull(result);
}
}
}
}

View File

@@ -0,0 +1,100 @@
using System;
using System.Linq;
using NSubstitute;
using Octokit.Clients;
using Xunit;
namespace Octokit.Tests.Clients
{
public class GitHubAppInstallationsClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new GitHubAppInstallationsClient(null));
}
}
public class TheGetAllRepositoriesForCurrentMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppInstallationsClient(connection);
client.GetAllRepositoriesForCurrent();
connection.Received().GetAll<RepositoriesResponse>(
Arg.Is<Uri>(u => u.ToString() == "installation/repositories"),
null,
"application/vnd.github.machine-man-preview+json",
Args.ApiOptions);
}
[Fact]
public void GetsFromCorrectUrllWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppInstallationsClient(connection);
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};
client.GetAllRepositoriesForCurrent(options);
connection.Received().GetAll<RepositoriesResponse>(
Arg.Is<Uri>(u => u.ToString() == "installation/repositories"),
null,
"application/vnd.github.machine-man-preview+json",
options);
}
}
public class TheGetAllRepositoriesForCurrentUserMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppInstallationsClient(connection);
client.GetAllRepositoriesForCurrentUser(1234);
connection.Received().GetAll<RepositoriesResponse>(
Arg.Is<Uri>(u => u.ToString() == "user/installations/1234/repositories"),
null,
"application/vnd.github.machine-man-preview+json",
Args.ApiOptions);
}
[Fact]
public void GetsFromCorrectUrllWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppInstallationsClient(connection);
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};
client.GetAllRepositoriesForCurrentUser(1234, options);
connection.Received().GetAll<RepositoriesResponse>(
Arg.Is<Uri>(u => u.ToString() == "user/installations/1234/repositories"),
null,
"application/vnd.github.machine-man-preview+json",
options);
}
}
}
}

View File

@@ -16,6 +16,38 @@ namespace Octokit.Tests.Clients
} }
} }
public class TheGetMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null));
}
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentException>(() => client.Get(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
client.Get("foobar");
connection.Received().Get<GitHubApp>(Arg.Is<Uri>(u => u.ToString() == "apps/foobar"), null, "application/vnd.github.machine-man-preview+json");
}
}
public class TheGetCurrentMethod public class TheGetCurrentMethod
{ {
[Fact] [Fact]
@@ -49,10 +81,9 @@ namespace Octokit.Tests.Clients
client.GetAllInstallationsForCurrent(); client.GetAllInstallationsForCurrent();
connection.Received().GetAll<Installation>(Arg.Is<Uri>(u => u.ToString() == "app/installations"), null, "application/vnd.github.machine-man-preview+json"); connection.Received().GetAll<Installation>(Arg.Is<Uri>(u => u.ToString() == "app/installations"), null, "application/vnd.github.machine-man-preview+json", Args.ApiOptions);
} }
[Fact] [Fact]
public void RequestsTheCorrectUrlWithApiOptions() public void RequestsTheCorrectUrlWithApiOptions()
{ {
@@ -72,7 +103,7 @@ namespace Octokit.Tests.Clients
} }
} }
public class TheGetInstallationMethod public class TheGetInstallationForCurrentMethod
{ {
[Fact] [Fact]
public void GetsFromCorrectUrl() public void GetsFromCorrectUrl()
@@ -80,12 +111,44 @@ namespace Octokit.Tests.Clients
var connection = Substitute.For<IApiConnection>(); var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection); var client = new GitHubAppsClient(connection);
client.GetInstallation(123); client.GetInstallationForCurrent(123);
connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "app/installations/123"), null, "application/vnd.github.machine-man-preview+json"); connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "app/installations/123"), null, "application/vnd.github.machine-man-preview+json");
} }
} }
public class TheGetAllInstallationsForCurrentUserMethod
{
[Fact]
public async Task GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await client.GetAllInstallationsForCurrentUser();
await connection.Received().GetAll<InstallationsResponse>(Arg.Is<Uri>(u => u.ToString() == "user/installations"), null, "application/vnd.github.machine-man-preview+json");
}
[Fact]
public async Task GetsFromCorrectUrlWithOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};
await client.GetAllInstallationsForCurrentUser(options);
await connection.Received().GetAll<InstallationsResponse>(Arg.Is<Uri>(u => u.ToString() == "user/installations"), null, "application/vnd.github.machine-man-preview+json", options);
}
}
public class TheCreateInstallationTokenMethod public class TheCreateInstallationTokenMethod
{ {
[Fact] [Fact]
@@ -101,5 +164,114 @@ namespace Octokit.Tests.Clients
connection.Received().Post<AccessToken>(Arg.Is<Uri>(u => u.ToString() == "app/installations/3141/access_tokens"), string.Empty, "application/vnd.github.machine-man-preview+json"); connection.Received().Post<AccessToken>(Arg.Is<Uri>(u => u.ToString() == "app/installations/3141/access_tokens"), string.Empty, "application/vnd.github.machine-man-preview+json");
} }
} }
public class TheGetOrganizationInstallationForCurrentMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetOrganizationInstallationForCurrent(null));
}
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentException>(() => client.GetOrganizationInstallationForCurrent(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
client.GetOrganizationInstallationForCurrent("ducks");
connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "orgs/ducks/installation"), null, "application/vnd.github.machine-man-preview+json");
}
}
public class TheGetRepositoryInstallationForCurrentMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetRepositoryInstallationForCurrent(null, "ducks"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetRepositoryInstallationForCurrent("mighty", null));
}
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentException>(() => client.GetRepositoryInstallationForCurrent("", "ducks"));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetRepositoryInstallationForCurrent("mighty", ""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
client.GetRepositoryInstallationForCurrent("mighty", "ducks");
connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "repos/mighty/ducks/installation"), null, "application/vnd.github.machine-man-preview+json");
}
[Fact]
public void GetsFromCorrectUrlByRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
client.GetRepositoryInstallationForCurrent(1234);
connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "repositories/1234/installation"), null, "application/vnd.github.machine-man-preview+json");
}
}
public class TheGetUserInstallationForCurrentMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetUserInstallationForCurrent(null));
}
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
await Assert.ThrowsAsync<ArgumentException>(() => client.GetUserInstallationForCurrent(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GitHubAppsClient(connection);
client.GetUserInstallationForCurrent("ducks");
connection.Received().Get<Installation>(Arg.Is<Uri>(u => u.ToString() == "users/ducks/installation"), null, "application/vnd.github.machine-man-preview+json");
}
}
} }
} }

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NSubstitute;
using Octokit.Clients;
using Octokit.Reactive;
using Xunit;
namespace Octokit.Tests.Clients
{
public class ObservableGitHubAppInstallationsClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new ObservableGitHubAppInstallationsClient(null));
}
}
public class TheGetAllRepositoriesForCurrentMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppInstallationsClient(gitHubClient);
client.GetAllRepositoriesForCurrent();
connection.Received().Get<List<RepositoriesResponse>>(
Arg.Is<Uri>(u => u.ToString() == "installation/repositories"),
Args.EmptyDictionary,
"application/vnd.github.machine-man-preview+json");
}
[Fact]
public void GetsFromCorrectUrllWithApiOptions()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppInstallationsClient(gitHubClient);
var options = new ApiOptions
{
PageSize = 1
};
client.GetAllRepositoriesForCurrent(options);
connection.Received().Get<List<RepositoriesResponse>>(
Arg.Is<Uri>(u => u.ToString() == "installation/repositories"),
Arg.Is<Dictionary<string, string>>(x =>
x.Count == 1
&& x["per_page"] == "1"),
"application/vnd.github.machine-man-preview+json");
}
}
public class TheGetAllRepositoriesForCurrentUserMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppInstallationsClient(gitHubClient);
client.GetAllRepositoriesForCurrentUser(1234);
connection.Received().Get<List<RepositoriesResponse>>(
Arg.Is<Uri>(u => u.ToString() == "user/installations/1234/repositories"),
Args.EmptyDictionary,
"application/vnd.github.machine-man-preview+json");
}
[Fact]
public void GetsFromCorrectUrllWithApiOptions()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppInstallationsClient(gitHubClient);
var options = new ApiOptions
{
PageSize = 1
};
client.GetAllRepositoriesForCurrentUser(1234, options);
connection.Received().Get<List<RepositoriesResponse>>(
Arg.Is<Uri>(u => u.ToString() == "user/installations/1234/repositories"),
Arg.Is<Dictionary<string, string>>(x =>
x.Count == 1
&& x["per_page"] == "1"),
"application/vnd.github.machine-man-preview+json");
}
}
}
}

View File

@@ -0,0 +1,295 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Reactive;
using Xunit;
namespace Octokit.Tests.Clients
{
public class ObservableGitHubAppsClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new ObservableGitHubAppsClient(null));
}
}
public class TheGetMethod
{
[Fact]
public void EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.Get(null));
}
[Fact]
public void EnsuresNonEmptyArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentException>(() => client.Get(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.Get("foobar");
gitHubClient.GitHubApps.Received().Get("foobar");
}
}
public class TheGetCurrentMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetCurrent();
gitHubClient.GitHubApps.Received().GetCurrent();
}
}
public class TheGetAllInstallationsForCurrentMethod
{
[Fact]
public void EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.GetAllInstallationsForCurrent(null));
}
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetAllInstallationsForCurrent();
connection.Received().Get<List<Installation>>(
Arg.Is<Uri>(u => u.ToString() == "app/installations"),
Args.EmptyDictionary,
"application/vnd.github.machine-man-preview+json");
}
[Fact]
public void RequestsTheCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppsClient(gitHubClient);
var options = new ApiOptions
{
PageSize = 1
};
client.GetAllInstallationsForCurrent(options);
connection.Received().Get<List<Installation>>(
Arg.Is<Uri>(u => u.ToString() == "app/installations"),
Arg.Is<Dictionary<string, string>>(x =>
x.Count == 1
&& x["per_page"] == "1"),
"application/vnd.github.machine-man-preview+json");
}
}
public class TheGetInstallationForCurrentMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetInstallationForCurrent(123);
gitHubClient.GitHubApps.Received().GetInstallationForCurrent(123);
}
}
public class TheGetAllInstallationsForCurrentUserMethod
{
[Fact]
public void GetsFromCorrectUrl()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetAllInstallationsForCurrentUser();
connection.Received().Get<List<InstallationsResponse>>(
Arg.Is<Uri>(u => u.ToString() == "user/installations"),
null,
"application/vnd.github.machine-man-preview+json");
}
[Fact]
public void GetsFromCorrectUrlWithOptions()
{
var connection = Substitute.For<IConnection>();
var gitHubClient = new GitHubClient(connection);
var client = new ObservableGitHubAppsClient(gitHubClient);
var options = new ApiOptions
{
PageSize = 1
};
client.GetAllInstallationsForCurrentUser(options);
connection.Received().Get<List<InstallationsResponse>>(
Arg.Is<Uri>(u => u.ToString() == "user/installations"),
Arg.Is<Dictionary<string, string>>(x =>
x.Count == 1
&& x["per_page"] == "1"),
"application/vnd.github.machine-man-preview+json");
}
}
public class TheCreateInstallationTokenMethod
{
[Fact]
public void PostsToCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
int fakeInstallationId = 3141;
client.CreateInstallationToken(fakeInstallationId);
gitHubClient.GitHubApps.Received().CreateInstallationToken(fakeInstallationId);
}
}
public class TheGetOrganizationInstallationForCurrentMethod
{
[Fact]
public void EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.GetOrganizationInstallationForCurrent(null));
}
[Fact]
public void EnsuresNonEmptyArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentException>(() => client.GetOrganizationInstallationForCurrent(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetOrganizationInstallationForCurrent("ducks");
gitHubClient.GitHubApps.Received().GetOrganizationInstallationForCurrent("ducks");
}
}
public class TheGetRepositoryInstallationForCurrentMethod
{
[Fact]
public void EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.GetRepositoryInstallationForCurrent(null, "ducks"));
Assert.Throws<ArgumentNullException>(() => client.GetRepositoryInstallationForCurrent("mighty", null));
}
[Fact]
public void EnsuresNonEmptyArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentException>(() => client.GetRepositoryInstallationForCurrent("", "ducks"));
Assert.Throws<ArgumentException>(() => client.GetRepositoryInstallationForCurrent("mighty", ""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetRepositoryInstallationForCurrent("mighty", "ducks");
gitHubClient.GitHubApps.Received().GetRepositoryInstallationForCurrent("mighty", "ducks");
}
[Fact]
public void GetsFromCorrectUrlByRepositoryId()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetRepositoryInstallationForCurrent(1234);
gitHubClient.GitHubApps.Received().GetRepositoryInstallationForCurrent(1234);
}
}
public class TheGetUserInstallationForCurrentMethod
{
[Fact]
public void EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.GetUserInstallationForCurrent(null));
}
[Fact]
public void EnsuresNonEmptyArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
Assert.Throws<ArgumentException>(() => client.GetUserInstallationForCurrent(""));
}
[Fact]
public void GetsFromCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableGitHubAppsClient(gitHubClient);
client.GetUserInstallationForCurrent("ducks");
gitHubClient.GitHubApps.Received().GetUserInstallationForCurrent("ducks");
}
}
}
}

View File

@@ -24,7 +24,9 @@
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64> <s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64> <s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_IN_SINGLELINE_ANONYMOUS_METHOD/@EntryValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_IN_SINGLELINE_ANONYMOUS_METHOD/@EntryValue">True</s:Boolean>
@@ -262,8 +264,13 @@ II.2.12 &lt;HandlesEvent /&gt;&#xD;
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary> </wpf:ResourceDictionary>

View File

@@ -0,0 +1,70 @@
using System.Linq;
using System.Threading.Tasks;
namespace Octokit.Clients
{
/// <summary>
/// A client for GitHub Applications Installations API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/installations/">GitHub Apps Installations API documentation</a> for more information.
/// </remarks>
public class GitHubAppInstallationsClient : ApiClient, IGitHubAppInstallationsClient
{
public GitHubAppInstallationsClient(IApiConnection apiConnection) : base(apiConnection)
{
}
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
public Task<RepositoriesResponse> GetAllRepositoriesForCurrent()
{
return GetAllRepositoriesForCurrent(ApiOptions.None);
}
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
public async Task<RepositoriesResponse> GetAllRepositoriesForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var results = await ApiConnection.GetAll<RepositoriesResponse>(ApiUrls.InstallationRepositories(), null, AcceptHeaders.GitHubAppsPreview, options).ConfigureAwait(false);
return new RepositoriesResponse(
results.Count > 0 ? results.Max(x => x.TotalCount) : 0,
results.SelectMany(x => x.Repositories).ToList());
}
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
public Task<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId)
{
return GetAllRepositoriesForCurrentUser(installationId, ApiOptions.None);
}
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
public async Task<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var results = await ApiConnection.GetAll<RepositoriesResponse>(ApiUrls.UserInstallationRepositories(installationId), null, AcceptHeaders.GitHubAppsPreview, options).ConfigureAwait(false);
return new RepositoriesResponse(
results.Count > 0 ? results.Max(x => x.TotalCount) : 0,
results.SelectMany(x => x.Repositories).ToList());
}
}
}

View File

@@ -1,10 +1,13 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Octokit.Clients;
namespace Octokit namespace Octokit
{ {
/// <summary> /// <summary>
/// A client for GitHub Applications API. Provides the methods required to get GitHub applications and installations. /// A client for GitHub Applications API.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information. /// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information.
@@ -13,20 +16,33 @@ namespace Octokit
{ {
public GitHubAppsClient(IApiConnection apiConnection) : base(apiConnection) public GitHubAppsClient(IApiConnection apiConnection) : base(apiConnection)
{ {
Ensure.ArgumentNotNull(apiConnection, nameof(apiConnection));
Installation = new GitHubAppInstallationsClient(apiConnection);
} }
/// <summary> /// <summary>
/// Get a single GitHub App. /// Access GitHub's Apps Installations API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developer.github.com/v3/apps/installations/
/// </remarks>
public IGitHubAppInstallationsClient Installation { get; }
/// <summary>
/// Get a single GitHub App (if private, requires Personal Access Token or GitHubApp auth)
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks>
/// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param> /// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param>
public Task<GitHubApp> Get(string slug) public Task<GitHubApp> Get(string slug)
{ {
Ensure.ArgumentNotNullOrEmptyString(slug, nameof(slug));
return ApiConnection.Get<GitHubApp>(ApiUrls.App(slug), null, AcceptHeaders.GitHubAppsPreview); return ApiConnection.Get<GitHubApp>(ApiUrls.App(slug), null, AcceptHeaders.GitHubAppsPreview);
} }
/// <summary> /// <summary>
/// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp JWT token auth). /// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks>
public Task<GitHubApp> GetCurrent() public Task<GitHubApp> GetCurrent()
@@ -35,16 +51,16 @@ namespace Octokit
} }
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
public Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent() public Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent()
{ {
return ApiConnection.GetAll<Installation>(ApiUrls.Installations(), null, AcceptHeaders.GitHubAppsPreview); return GetAllInstallationsForCurrent(ApiOptions.None);
} }
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <param name="options">Options for changing the API response</param> /// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
@@ -56,17 +72,56 @@ namespace Octokit
} }
/// <summary> /// <summary>
/// Get a single GitHub App Installation (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
[Obsolete("This method will be removed in a future release. Please use GetInstallationForCurrent() instead")]
public Task<Installation> GetInstallation(long installationId) public Task<Installation> GetInstallation(long installationId)
{
return GetInstallationForCurrent(installationId);
}
/// <summary>
/// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param>
public Task<Installation> GetInstallationForCurrent(long installationId)
{ {
return ApiConnection.Get<Installation>(ApiUrls.Installation(installationId), null, AcceptHeaders.GitHubAppsPreview); return ApiConnection.Get<Installation>(ApiUrls.Installation(installationId), null, AcceptHeaders.GitHubAppsPreview);
} }
/// <summary> /// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp JWT token auth). /// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
public async Task<InstallationsResponse> GetAllInstallationsForCurrentUser()
{
var results = await ApiConnection.GetAll<InstallationsResponse>(ApiUrls.UserInstallations(), null, AcceptHeaders.GitHubAppsPreview).ConfigureAwait(false);
return new InstallationsResponse(
results.Count > 0 ? results.Max(x => x.TotalCount) : 0,
results.SelectMany(x => x.Installations).ToList());
}
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
public async Task<InstallationsResponse> GetAllInstallationsForCurrentUser(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var results = await ApiConnection.GetAll<InstallationsResponse>(ApiUrls.UserInstallations(), null, AcceptHeaders.GitHubAppsPreview, options).ConfigureAwait(false);
return new InstallationsResponse(
results.Count > 0 ? results.Max(x => x.TotalCount) : 0,
results.SelectMany(x => x.Installations).ToList());
}
/// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// https://developer.github.com/v3/apps/#create-a-new-installation-token /// https://developer.github.com/v3/apps/#create-a-new-installation-token
@@ -78,5 +133,53 @@ namespace Octokit
{ {
return ApiConnection.Post<AccessToken>(ApiUrls.AccessTokens(installationId), string.Empty, AcceptHeaders.GitHubAppsPreview); return ApiConnection.Post<AccessToken>(ApiUrls.AccessTokens(installationId), string.Empty, AcceptHeaders.GitHubAppsPreview);
} }
/// <summary>
/// Enables an authenticated GitHub App to find the organization's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-organization-installation</remarks>
/// <param name="organization">The name of the organization</param>
public Task<Installation> GetOrganizationInstallationForCurrent(string organization)
{
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
return ApiConnection.Get<Installation>(ApiUrls.OrganizationInstallation(organization), null, AcceptHeaders.GitHubAppsPreview);
}
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="owner">The owner of the repo</param>
/// <param name="repo">The name of the repo</param>
public Task<Installation> GetRepositoryInstallationForCurrent(string owner, string repo)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(repo, nameof(repo));
return ApiConnection.Get<Installation>(ApiUrls.RepoInstallation(owner, repo), null, AcceptHeaders.GitHubAppsPreview);
}
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="repositoryId">The Id of the repository</param>
public Task<Installation> GetRepositoryInstallationForCurrent(long repositoryId)
{
return ApiConnection.Get<Installation>(ApiUrls.RepoInstallation(repositoryId), null, AcceptHeaders.GitHubAppsPreview);
}
/// <summary>
/// Enables an authenticated GitHub App to find the users's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-user-installation</remarks>
/// <param name="user">The name of the user</param>
public Task<Installation> GetUserInstallationForCurrent(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
return ApiConnection.Get<Installation>(ApiUrls.UserInstallation(user), null, AcceptHeaders.GitHubAppsPreview);
}
} }
} }

View File

@@ -0,0 +1,41 @@
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// A client for GitHub Applications Installations API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/installations/">GitHub Apps Installations API documentation</a> for more information.
/// </remarks>
public interface IGitHubAppInstallationsClient
{
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
Task<RepositoriesResponse> GetAllRepositoriesForCurrent();
/// <summary>
/// List repositories of the authenticated GitHub App Installation (requires GitHubApp Installation-Token auth).
/// </summary>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories</remarks>
Task<RepositoriesResponse> GetAllRepositoriesForCurrent(ApiOptions options);
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
Task<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId);
/// <summary>
/// List repositories accessible to the user for an installation (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <param name="installationId">The Id of the installation</param>
/// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation</remarks>
Task<RepositoriesResponse> GetAllRepositoriesForCurrentUser(long installationId, ApiOptions options);
}
}

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Octokit namespace Octokit
{ {
/// <summary> /// <summary>
/// A client for GitHub Applications API. Provides the methods required to get GitHub applications and installations. /// A client for GitHub Applications API.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information. /// See the <a href="http://developer.github.com/v3/apps/">GitHub Apps API documentation</a> for more information.
@@ -12,40 +13,68 @@ namespace Octokit
public interface IGitHubAppsClient public interface IGitHubAppsClient
{ {
/// <summary> /// <summary>
/// Get a single GitHub App. /// Access GitHub's Apps Installations API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developer.github.com/v3/apps/installations/
/// </remarks>
IGitHubAppInstallationsClient Installation { get; }
/// <summary>
/// Get a single GitHub App (if private, requires Personal Access Token or GitHubApp auth)
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-github-app</remarks>
/// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param> /// <param name="slug">The URL-friendly name of your GitHub App. You can find this on the settings page for your GitHub App.</param>
Task<GitHubApp> Get(string slug); Task<GitHubApp> Get(string slug);
/// <summary> /// <summary>
/// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp JWT token auth). /// Returns the GitHub App associated with the authentication credentials used (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-the-authenticated-github-app</remarks>
Task<GitHubApp> GetCurrent(); Task<GitHubApp> GetCurrent();
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent(); Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent();
/// <summary> /// <summary>
/// List installations of the authenticated GitHub App (requires GitHubApp JWT token auth). /// List installations of the authenticated GitHub App (requires GitHubApp auth).
/// </summary> /// </summary>
/// <param name="options">Options for changing the API response</param> /// <param name="options">Options for changing the API response</param>
/// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks> /// <remarks>https://developer.github.com/v3/apps/#find-installations</remarks>
Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent(ApiOptions options); Task<IReadOnlyList<Installation>> GetAllInstallationsForCurrent(ApiOptions options);
/// <summary> /// <summary>
/// Get a single GitHub App Installation (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks> /// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
[Obsolete("This method will be removed in a future release. Please use GetInstallationForCurrent() instead")]
Task<Installation> GetInstallation(long installationId); Task<Installation> GetInstallation(long installationId);
/// <summary> /// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp JWT token auth). /// Get a single GitHub App Installation (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#get-a-single-installation</remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param>
Task<Installation> GetInstallationForCurrent(long installationId);
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
Task<InstallationsResponse> GetAllInstallationsForCurrentUser();
/// <summary>
/// List installations for the currently authenticated user (requires GitHubApp User-To-Server Auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#list-installations-for-user</remarks>
Task<InstallationsResponse> GetAllInstallationsForCurrentUser(ApiOptions options);
/// <summary>
/// Create a time bound access token for a GitHubApp Installation that can be used to access other API endpoints (requires GitHubApp auth).
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// https://developer.github.com/v3/apps/#create-a-new-installation-token /// https://developer.github.com/v3/apps/#create-a-new-installation-token
@@ -54,5 +83,34 @@ namespace Octokit
/// </remarks> /// </remarks>
/// <param name="installationId">The Id of the GitHub App Installation</param> /// <param name="installationId">The Id of the GitHub App Installation</param>
Task<AccessToken> CreateInstallationToken(long installationId); Task<AccessToken> CreateInstallationToken(long installationId);
/// <summary>
/// Enables an authenticated GitHub App to find the organization's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-organization-installation</remarks>
/// <param name="organization">The name of the organization</param>
Task<Installation> GetOrganizationInstallationForCurrent(string organization);
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="owner">The owner of the repo</param>
/// <param name="repo">The name of the repo</param>
Task<Installation> GetRepositoryInstallationForCurrent(string owner, string repo);
/// <summary>
/// Enables an authenticated GitHub App to find the repository's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-repository-installation</remarks>
/// <param name="repositoryId">The Id of the repository</param>
Task<Installation> GetRepositoryInstallationForCurrent(long repositoryId);
/// <summary>
/// Enables an authenticated GitHub App to find the users's installation information (requires GitHubApp auth).
/// </summary>
/// <remarks>https://developer.github.com/v3/apps/#find-user-installation</remarks>
/// <param name="user">The name of the user</param>
Task<Installation> GetUserInstallationForCurrent(string user);
} }
} }

View File

@@ -309,21 +309,79 @@ namespace Octokit
/// <summary> /// <summary>
/// Returns the <see cref="Uri"/> that returns all the installations of the authenticated application. /// Returns the <see cref="Uri"/> that returns all the installations of the authenticated application.
/// </summary> /// </summary>
/// <returns></returns>
public static Uri Installations() public static Uri Installations()
{ {
return "app/installations".FormatUri(); return "app/installations".FormatUri();
} }
/// <summary> /// <summary>
/// Returns the <see cref="Uri"/> that returns a single installation of the authenticated application. /// Returns the <see cref="Uri"/> that lists repositories accessible to the user for an installation..
/// </summary>
/// <param name="installationId">The id of the installation</param>
public static Uri UserInstallationRepositories(long installationId)
{
return "user/installations/{0}/repositories".FormatUri(installationId);
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns the repository's installation information.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static Uri RepoInstallation(string owner, string repo)
{
return "repos/{0}/{1}/installation".FormatUri(owner, repo);
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns the repository's installation information.
/// </summary>
/// <returns></returns>
public static Uri RepoInstallation(long repositoryId)
{
return "repositories/{0}/installation".FormatUri(repositoryId);
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns the organization's installation information.
/// </summary>
public static Uri OrganizationInstallation(string organization)
{
return "orgs/{0}/installation".FormatUri(organization); ;
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns the user's installation information.
/// </summary>
public static Uri UserInstallation(string username)
{
return "users/{0}/installation".FormatUri(username);
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns a single installation of the authenticated application.
/// </summary>
public static Uri Installation(long installationId) public static Uri Installation(long installationId)
{ {
return "app/installations/{0}".FormatUri(installationId); return "app/installations/{0}".FormatUri(installationId);
} }
/// <summary>
/// Returns the <see cref="Uri"/> that returns all the installations in repositories the user has explicit permission to access
/// </summary>
public static Uri UserInstallations()
{
return "user/installations".FormatUri();
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns all the repositores
/// </summary>
/// <returns></returns>
public static Uri InstallationRepositories()
{
return "installation/repositories".FormatUri();
}
/// <summary> /// <summary>
/// Returns the <see cref="Uri"/> that returns all of the issues across all the authenticated users visible /// Returns the <see cref="Uri"/> that returns all of the issues across all the authenticated users visible
/// repositories including owned repositories, member repositories, and organization repositories: /// repositories including owned repositories, member repositories, and organization repositories:

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class InstallationsResponse
{
public InstallationsResponse()
{
}
public InstallationsResponse(int totalCount, IReadOnlyList<Installation> installations)
{
TotalCount = totalCount;
Installations = installations;
}
/// <summary>
/// The total number of check suites that match the request filter
/// </summary>
public int TotalCount { get; protected set; }
/// <summary>
/// The retrieved check suites
/// </summary>
public IReadOnlyList<Installation> Installations { get; protected set; }
internal string DebuggerDisplay => string.Format(CultureInfo.CurrentCulture, "TotalCount: {0}, Installations: {1}", TotalCount, Installations.Count);
}
}

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RepositoriesResponse
{
public RepositoriesResponse()
{
}
public RepositoriesResponse(int totalCount, IReadOnlyList<Repository> repositories)
{
TotalCount = totalCount;
Repositories = repositories;
}
/// <summary>
/// The total number of check suites that match the request filter
/// </summary>
public int TotalCount { get; protected set; }
/// <summary>
/// The retrieved check suites
/// </summary>
public IReadOnlyList<Repository> Repositories { get; protected set; }
internal string DebuggerDisplay => string.Format(CultureInfo.CurrentCulture, "TotalCount: {0}, Repositories: {1}", TotalCount, Repositories.Count);
}
}