mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-06 07:16:09 +00:00
179 lines
6.2 KiB
C#
179 lines
6.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace Octokit.Tests.Integration
|
|
{
|
|
public static class Helper
|
|
{
|
|
static readonly Lazy<Credentials> _credentialsThunk = new Lazy<Credentials>(() =>
|
|
{
|
|
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME");
|
|
UserName = githubUsername;
|
|
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");
|
|
|
|
var githubToken = Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN");
|
|
|
|
if (githubToken != null)
|
|
return new Credentials(githubToken);
|
|
|
|
var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD");
|
|
|
|
if (githubUsername == null || githubPassword == null)
|
|
return null;
|
|
|
|
return new Credentials(githubUsername, githubPassword);
|
|
});
|
|
|
|
static readonly Lazy<Credentials> _oauthApplicationCredentials = new Lazy<Credentials>(() =>
|
|
{
|
|
var applicationClientId = ClientId;
|
|
var applicationClientSecret = ClientSecret;
|
|
|
|
if (applicationClientId == null || applicationClientSecret == null)
|
|
return null;
|
|
|
|
return new Credentials(applicationClientId, applicationClientSecret);
|
|
});
|
|
|
|
static readonly Lazy<Credentials> _basicAuthCredentials = new Lazy<Credentials>(() =>
|
|
{
|
|
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME");
|
|
UserName = githubUsername;
|
|
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");
|
|
|
|
var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD");
|
|
|
|
if (githubUsername == null || githubPassword == null)
|
|
return null;
|
|
|
|
return new Credentials(githubUsername, githubPassword);
|
|
});
|
|
|
|
static readonly Lazy<Uri> _gitHubEnterpriseUrl = new Lazy<Uri>(() =>
|
|
{
|
|
string uri = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBENTERPRISEURL");
|
|
|
|
if (uri != null)
|
|
return new Uri(uri);
|
|
|
|
return null;
|
|
});
|
|
|
|
static Helper()
|
|
{
|
|
// Force reading of environment variables.
|
|
// This wasn't happening if UserName/Organization were
|
|
// retrieved before Credentials.
|
|
Debug.WriteIf(Credentials == null, "No credentials specified.");
|
|
}
|
|
|
|
public static string UserName { get; private set; }
|
|
public static string Organization { get; private set; }
|
|
|
|
public static Credentials Credentials { get { return _credentialsThunk.Value; } }
|
|
|
|
public static Credentials ApplicationCredentials { get { return _oauthApplicationCredentials.Value; } }
|
|
|
|
public static Credentials BasicAuthCredentials { get { return _basicAuthCredentials.Value; } }
|
|
|
|
public static Uri GitHubEnterpriseUrl { get { return _gitHubEnterpriseUrl.Value; } }
|
|
|
|
public static bool IsUsingToken
|
|
{
|
|
get
|
|
{
|
|
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN"));
|
|
}
|
|
}
|
|
|
|
public static bool IsPaidAccount
|
|
{
|
|
get
|
|
{
|
|
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_PRIVATEREPOSITORIES"));
|
|
}
|
|
}
|
|
|
|
public static string ClientId
|
|
{
|
|
get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTID"); }
|
|
}
|
|
|
|
public static string ClientSecret
|
|
{
|
|
get { return Environment.GetEnvironmentVariable("OCTOKIT_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 string MakeNameWithTimestamp(string name)
|
|
{
|
|
return string.Concat(name, "-", DateTime.UtcNow.ToString("yyyyMMddhhmmssfff"));
|
|
}
|
|
|
|
public static Stream LoadFixture(string fileName)
|
|
{
|
|
var key = "Octokit.Tests.Integration.fixtures." + fileName;
|
|
var stream = typeof(Helper).Assembly.GetManifestResourceStream(key);
|
|
if (stream == null)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"The file '" + fileName + "' was not found as an embedded resource in the assembly. Failing the test...");
|
|
}
|
|
return stream;
|
|
}
|
|
|
|
public static IGitHubClient GetAuthenticatedClient()
|
|
{
|
|
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
|
|
{
|
|
Credentials = Credentials
|
|
};
|
|
}
|
|
|
|
public static IGitHubClient GetBasicAuthClient()
|
|
{
|
|
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
|
|
{
|
|
Credentials = BasicAuthCredentials
|
|
};
|
|
}
|
|
|
|
public static GitHubClient GetAuthenticatedApplicationClient()
|
|
{
|
|
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
|
|
{
|
|
Credentials = ApplicationCredentials
|
|
};
|
|
}
|
|
|
|
public static IGitHubClient GetAnonymousClient()
|
|
{
|
|
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl);
|
|
}
|
|
|
|
public static IGitHubClient GetBadCredentialsClient()
|
|
{
|
|
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
|
|
{
|
|
Credentials = new Credentials(Guid.NewGuid().ToString(), "bad-password")
|
|
};
|
|
}
|
|
}
|
|
}
|