From 7fa1faa5c46de777cab11130ca7fb808c1885709 Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Wed, 8 Jun 2016 21:46:25 +1000 Subject: [PATCH] integration helper context classes now work against github.com or github enterprise, by using the IConnection used to create the context, to delete it in Dispose() update CreateContext extension methods to pass in the IConnection --- Octokit.Tests.Integration/EnterpriseHelper.cs | 42 +++---------------- Octokit.Tests.Integration/Helper.cs | 39 ++++++++++++----- .../Helpers/EnterpriseUserContext.cs | 6 ++- .../Helpers/GithubClientExtensions.cs | 14 +++---- .../ObservableGithubClientExtensions.cs | 12 +++--- .../Helpers/PublicKeyContext.cs | 6 ++- .../Helpers/RepositoryContext.cs | 7 +++- .../Helpers/TeamContext.cs | 8 ++-- 8 files changed, 64 insertions(+), 70 deletions(-) diff --git a/Octokit.Tests.Integration/EnterpriseHelper.cs b/Octokit.Tests.Integration/EnterpriseHelper.cs index baf16c9a..deae6e0e 100644 --- a/Octokit.Tests.Integration/EnterpriseHelper.cs +++ b/Octokit.Tests.Integration/EnterpriseHelper.cs @@ -108,50 +108,18 @@ namespace Octokit.Tests.Integration get { return Environment.GetEnvironmentVariable("OCTOKIT_GHE_CLIENTSECRET"); } } - public static void DeleteRepo(Repository repository) - { - if (repository != null) - DeleteRepo(repository.Owner.Login, repository.Name); - } - - public static void DeleteRepo(string owner, string name) - { - var api = GetAuthenticatedClient(); - try - { - api.Repository.Delete(owner, name).Wait(TimeSpan.FromSeconds(15)); - } - catch { } - } - - public static void DeleteTeam(Team team) - { - if (team != null) - DeleteTeam(team.Id); - } - - public static void DeleteTeam(int teamId) - { - var api = GetAuthenticatedClient(); - try - { - api.Organization.Team.Delete(teamId).Wait(TimeSpan.FromSeconds(15)); - } - catch { } - } - - public static void DeleteUser(User user) + public static void DeleteUser(IConnection connection, User user) { if (user != null) - DeleteUser(user.Login); + DeleteUser(connection, user.Login); } - public static void DeleteUser(string username) + public static void DeleteUser(IConnection connection, string username) { - var api = GetAuthenticatedClient(); try { - api.User.Administration.Delete(username).Wait(TimeSpan.FromSeconds(15)); + var client = new GitHubClient(connection); + client.User.Administration.Delete(username).Wait(TimeSpan.FromSeconds(15)); } catch { } } diff --git a/Octokit.Tests.Integration/Helper.cs b/Octokit.Tests.Integration/Helper.cs index 52eaeb8c..f383d377 100644 --- a/Octokit.Tests.Integration/Helper.cs +++ b/Octokit.Tests.Integration/Helper.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using Octokit.Reactive; namespace Octokit.Tests.Integration { @@ -110,34 +111,50 @@ namespace Octokit.Tests.Integration get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTSECRET"); } } - public static void DeleteRepo(Repository repository) + public static void DeleteRepo(IConnection connection, Repository repository) { if (repository != null) - DeleteRepo(repository.Owner.Login, repository.Name); + DeleteRepo(connection, repository.Owner.Login, repository.Name); } - public static void DeleteRepo(string owner, string name) + public static void DeleteRepo(IConnection connection, string owner, string name) { - var api = GetAuthenticatedClient(); try { - api.Repository.Delete(owner, name).Wait(TimeSpan.FromSeconds(15)); + var client = new GitHubClient(connection); + client.Repository.Delete(owner, name).Wait(TimeSpan.FromSeconds(15)); } catch { } } - public static void DeleteKey(PublicKey key) + public static void DeleteTeam(IConnection connection, Team team) { - if (key != null) - DeleteKey(key.Id); + if (team != null) + DeleteTeam(connection, team.Id); } - public static void DeleteKey(int keyId) + public static void DeleteTeam(IConnection connection, int teamId) { - var api = GetAuthenticatedClient(); try { - api.User.Keys.Delete(keyId).Wait(TimeSpan.FromSeconds(15)); + var client = new GitHubClient(connection); + client.Organization.Team.Delete(teamId).Wait(TimeSpan.FromSeconds(15)); + } + catch { } + } + + public static void DeleteKey(IConnection connection, PublicKey key) + { + if (key != null) + DeleteKey(connection, key.Id); + } + + public static void DeleteKey(IConnection connection, int keyId) + { + try + { + var client = new GitHubClient(connection); + client.User.Keys.Delete(keyId).Wait(TimeSpan.FromSeconds(15)); } catch { } } diff --git a/Octokit.Tests.Integration/Helpers/EnterpriseUserContext.cs b/Octokit.Tests.Integration/Helpers/EnterpriseUserContext.cs index 496fb52d..177a8c69 100644 --- a/Octokit.Tests.Integration/Helpers/EnterpriseUserContext.cs +++ b/Octokit.Tests.Integration/Helpers/EnterpriseUserContext.cs @@ -8,14 +8,16 @@ namespace Octokit.Tests.Integration.Helpers { internal sealed class EnterpriseUserContext : IDisposable { - internal EnterpriseUserContext(User user) + internal EnterpriseUserContext(IConnection connection, User user) { + _connection = connection; User = user; UserId = user.Id; UserLogin = user.Login; UserEmail = user.Email; } + private IConnection _connection; internal int UserId { get; private set; } internal string UserLogin { get; private set; } internal string UserEmail { get; private set; } @@ -24,7 +26,7 @@ namespace Octokit.Tests.Integration.Helpers public void Dispose() { - EnterpriseHelper.DeleteUser(User); + EnterpriseHelper.DeleteUser(_connection, User.Login); } } } diff --git a/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs b/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs index 5c892c43..18604004 100644 --- a/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs +++ b/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs @@ -9,35 +9,35 @@ namespace Octokit.Tests.Integration.Helpers var repoName = Helper.MakeNameWithTimestamp(repositoryName); var repo = await client.Repository.Create(new NewRepository(repoName) { AutoInit = true }); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateRepositoryContext(this IGitHubClient client, string organizationLogin, NewRepository newRepository) { var repo = await client.Repository.Create(organizationLogin, newRepository); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateRepositoryContext(this IGitHubClient client, NewRepository newRepository) { var repo = await client.Repository.Create(newRepository); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateTeamContext(this IGitHubClient client, string organization, NewTeam newTeam) { var team = await client.Organization.Team.Create(organization, newTeam); - return new TeamContext(team); + return new TeamContext(client.Connection, team); } - internal static async Task CreateUserContext(this IGitHubClient client, NewUser newUser) + internal static async Task CreateEnterpriseUserContext(this IGitHubClient client, NewUser newUser) { var user = await client.User.Administration.Create(newUser); - return new EnterpriseUserContext(user); + return new EnterpriseUserContext(client.Connection, user); } internal static async Task CreatePublicKeyContext(this IGitHubClient client) @@ -48,7 +48,7 @@ namespace Octokit.Tests.Integration.Helpers var key = await client.User.Keys.Create(new NewPublicKey(keyTitle, keyData)); - return new PublicKeyContext(key); + return new PublicKeyContext(client.Connection, key); } } } \ No newline at end of file diff --git a/Octokit.Tests.Integration/Helpers/ObservableGithubClientExtensions.cs b/Octokit.Tests.Integration/Helpers/ObservableGithubClientExtensions.cs index 4a864c3d..12ab85f2 100644 --- a/Octokit.Tests.Integration/Helpers/ObservableGithubClientExtensions.cs +++ b/Octokit.Tests.Integration/Helpers/ObservableGithubClientExtensions.cs @@ -11,35 +11,35 @@ namespace Octokit.Tests.Integration.Helpers var repoName = Helper.MakeNameWithTimestamp(repositoryName); var repo = await client.Repository.Create(new NewRepository(repoName) { AutoInit = true }); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateRepositoryContext(this IObservableGitHubClient client, string organizationLogin, NewRepository newRepository) { var repo = await client.Repository.Create(organizationLogin, newRepository); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateRepositoryContext(this IObservableGitHubClient client, NewRepository newRepository) { var repo = await client.Repository.Create(newRepository); - return new RepositoryContext(repo); + return new RepositoryContext(client.Connection, repo); } internal static async Task CreateEnterpriseTeamContext(this IObservableGitHubClient client, string organization, NewTeam newTeam) { var team = await client.Organization.Team.Create(organization, newTeam); - return new TeamContext(team); + return new TeamContext(client.Connection, team); } internal static async Task CreateEnterpriseUserContext(this IObservableGitHubClient client, NewUser newUser) { var user = await client.User.Administration.Create(newUser); - return new EnterpriseUserContext(user); + return new EnterpriseUserContext(client.Connection, user); } internal static async Task CreatePublicKeyContext(this IObservableGitHubClient client) @@ -50,7 +50,7 @@ namespace Octokit.Tests.Integration.Helpers var key = await client.User.Keys.Create(new NewPublicKey(keyTitle, keyData)); - return new PublicKeyContext(key); + return new PublicKeyContext(client.Connection, key); } } } \ No newline at end of file diff --git a/Octokit.Tests.Integration/Helpers/PublicKeyContext.cs b/Octokit.Tests.Integration/Helpers/PublicKeyContext.cs index 30fbaac7..bf4b2d32 100644 --- a/Octokit.Tests.Integration/Helpers/PublicKeyContext.cs +++ b/Octokit.Tests.Integration/Helpers/PublicKeyContext.cs @@ -8,14 +8,16 @@ namespace Octokit.Tests.Integration.Helpers { internal sealed class PublicKeyContext : IDisposable { - internal PublicKeyContext(PublicKey key) + internal PublicKeyContext(IConnection connection, PublicKey key) { + _connection = connection; Key = key; KeyId = key.Id; KeyTitle = key.Title; KeyData = key.Key; } + private IConnection _connection; internal int KeyId { get; private set; } internal string KeyTitle { get; private set; } internal string KeyData { get; private set; } @@ -24,7 +26,7 @@ namespace Octokit.Tests.Integration.Helpers public void Dispose() { - Helper.DeleteKey(Key); + Helper.DeleteKey(_connection, Key); } } } diff --git a/Octokit.Tests.Integration/Helpers/RepositoryContext.cs b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs index d16afd36..509cba7a 100644 --- a/Octokit.Tests.Integration/Helpers/RepositoryContext.cs +++ b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs @@ -3,18 +3,21 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Octokit.Reactive; namespace Octokit.Tests.Integration.Helpers { internal sealed class RepositoryContext : IDisposable { - internal RepositoryContext(Repository repo) + internal RepositoryContext(IConnection connection, Repository repo) { + _connection = connection; Repository = repo; RepositoryOwner = repo.Owner.Login; RepositoryName = repo.Name; } + private IConnection _connection; internal string RepositoryOwner { get; private set; } internal string RepositoryName { get; private set; } @@ -22,7 +25,7 @@ namespace Octokit.Tests.Integration.Helpers public void Dispose() { - Helper.DeleteRepo(Repository); + Helper.DeleteRepo(_connection, Repository); } } } diff --git a/Octokit.Tests.Integration/Helpers/TeamContext.cs b/Octokit.Tests.Integration/Helpers/TeamContext.cs index 967aebb2..07f20525 100644 --- a/Octokit.Tests.Integration/Helpers/TeamContext.cs +++ b/Octokit.Tests.Integration/Helpers/TeamContext.cs @@ -8,21 +8,23 @@ namespace Octokit.Tests.Integration.Helpers { internal sealed class TeamContext : IDisposable { - internal TeamContext(Team team) + internal TeamContext(IConnection connection, Team team) { + _connection = connection; Team = team; TeamId = team.Id; TeamName = team.Name; } + private IConnection _connection; internal int TeamId { get; private set; } internal string TeamName { get; private set; } - + internal Team Team { get; private set; } public void Dispose() { - EnterpriseHelper.DeleteTeam(Team); + Helper.DeleteTeam(_connection, Team); } } }