using System;
#if NET_45
using System.Collections.Generic;
#endif
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
namespace Octokit
{
///
/// A client for GitHub's Repositories API.
///
///
/// See the Repositories API documentation for more details.
///
public class RepositoriesClient : ApiClient, IRepositoriesClient
{
///
/// Initializes a new GitHub Repos API client.
///
/// An API connection
public RepositoriesClient(IApiConnection apiConnection) : base(apiConnection)
{
CommitStatus = new CommitStatusClient(apiConnection);
RepoCollaborators = new RepoCollaboratorsClient(apiConnection);
Statistics = new StatisticsClient(apiConnection);
Deployment = new DeploymentsClient(apiConnection);
PullRequest = new PullRequestsClient(apiConnection);
RepositoryComments = new RepositoryCommentsClient(apiConnection);
Commits = new RepositoryCommitsClient(apiConnection);
}
///
/// Creates a new repository for the current user.
///
///
/// See the API documentation for more information.
///
/// A instance describing the new repository to create
/// Thrown when a general API error occurs.
/// A instance for the created repository.
public Task Create(NewRepository newRepository)
{
Ensure.ArgumentNotNull(newRepository, "newRepository");
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");
return Create(ApiUrls.Repositories(), null, newRepository);
}
///
/// Creates a new repository in the specified organization.
///
///
/// See the API documentation for more information.
///
/// Login of the organization in which to create the repostiory
/// A instance describing the new repository to create
/// Thrown when a general API error occurs.
/// A instance for the created repository
public Task Create(string organizationLogin, NewRepository newRepository)
{
Ensure.ArgumentNotNull(organizationLogin, "organizationLogin");
Ensure.ArgumentNotNull(newRepository, "newRepository");
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");
return Create(ApiUrls.OrganizationRepositories(organizationLogin), organizationLogin, newRepository);
}
async Task Create(Uri url, string organizationLogin, NewRepository newRepository)
{
try
{
return await ApiConnection.Post(url, newRepository);
}
catch (ApiValidationException e)
{
string errorMessage = e.ApiError.FirstErrorMessageSafe();
if (String.Equals(
"name already exists on this account",
errorMessage,
StringComparison.OrdinalIgnoreCase))
{
string owner = organizationLogin ?? Connection.Credentials.Login;
var baseAddress = Connection.BaseAddress.Host != GitHubClient.GitHubApiUrl.Host
? Connection.BaseAddress
: new Uri("https://github.com/");
throw new RepositoryExistsException(
owner,
newRepository.Name,
organizationLogin != null,
baseAddress, e);
}
if (String.Equals(
"name can't be private. You are over your quota.",
errorMessage,
StringComparison.OrdinalIgnoreCase))
{
throw new PrivateRepositoryQuotaExceededException(e);
}
throw;
}
}
///
/// Deletes the specified repository.
///
///
/// See the API documentation for more information.
/// Deleting a repository requires admin access. If OAuth is used, the `delete_repo` scope is required.
///
/// The owner of the repository
/// The name of the repository
/// Thrown when a general API error occurs.
public Task Delete(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}".FormatUri(owner, name);
return ApiConnection.Delete(endpoint);
}
///
/// Updates the specified repository with the values given in
///
/// The owner of the repository
/// The name of the repository
/// New values to update the repository with
/// The updated
public Task Edit(string owner, string name, RepositoryUpdate update)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(update, "update");
return ApiConnection.Patch(ApiUrls.Repository(owner, name), update);
}
///
/// Gets the specified repository.
///
///
/// See the API documentation for more information.
///
/// The owner of the repository
/// The name of the repository
/// Thrown when a general API error occurs.
/// A
public Task Get(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}".FormatUri(owner, name);
return ApiConnection.Get(endpoint);
}
///
/// Gets all repositories owned by the current user.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown if the client is not authenticated.
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForCurrent()
{
return ApiConnection.GetAll(ApiUrls.Repositories());
}
///
/// Gets all repositories owned by the specified user.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForUser(string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
return ApiConnection.GetAll(ApiUrls.Repositories(login));
}
///
/// Gets all repositories owned by the specified organization.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForOrg(string organization)
{
Ensure.ArgumentNotNullOrEmptyString(organization, "organization");
return ApiConnection.GetAll(ApiUrls.OrganizationRepositories(organization));
}
///
/// Gets the preferred README for the specified repository.
///
///
/// See the API documentation for more information.
///
/// The owner of the repository
/// The name of the repository
/// Thrown when a general API error occurs.
///
public async Task GetReadme(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}/readme".FormatUri(owner, name);
var readmeInfo = await ApiConnection.Get(endpoint, null).ConfigureAwait(false);
return new Readme(readmeInfo, ApiConnection);
}
///
/// Gets the perferred README's HTML for the specified repository.
///
///
/// See the API documentation for more information.
///
/// The owner of the repository
/// The name of the repository
/// Thrown when a general API error occurs.
///
public Task GetReadmeHtml(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}/readme".FormatUri(owner, name);
return ApiConnection.GetHtml(endpoint, null);
}
///
/// A client for GitHub's Commit Status API.
///
///
/// See the Commit Status API documentation for more
/// details. Also check out the blog post
/// that announced this feature.
///
public ICommitStatusClient CommitStatus { get; private set; }
///
/// A client for GitHub's Repo Collaborators.
///
///
/// See the Collaborators API documentation for more details
///
public IRepoCollaboratorsClient RepoCollaborators { get; private set; }
///
/// Client for GitHub's Repository Deployments API
///
///
/// See the Collaborators API documentation for more details
///
public IDeploymentsClient Deployment { get; private set; }
///
/// Client for GitHub's Repository Statistics API
///
///
/// See the Statistics API documentation for more details
///
public IStatisticsClient Statistics { get; private set; }
///
/// Client for GitHub's Repository Commits API
///
///
/// See the Commits API documentation for more details
///
public IRepositoryCommitsClient Commits { get; private set; }
///
/// Client for managing pull requests.
///
///
/// See the Pull Requests API documentation for more details
///
public IPullRequestsClient PullRequest { get; private set; }
///
/// Client for managing commit comments in a repository.
///
///
/// See the Repository Comments API documentation for more information.
///
public IRepositoryCommentsClient RepositoryComments { get; private set; }
///
/// Gets all the branches for the specified repository.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// Thrown when a general API error occurs.
/// All es of the repository
public Task> GetAllBranches(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = ApiUrls.RepoBranches(owner, name);
return ApiConnection.GetAll(endpoint);
}
///
/// Gets all contributors for the specified repository. Does not include anonymous contributors.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// All contributors of the repository.
public Task> GetAllContributors(string owner, string name)
{
return GetAllContributors(owner, name, false);
}
///
/// Gets all contributors for the specified repository. With the option to include anonymous contributors.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// True if anonymous contributors should be included in result; Otherwise false
/// All contributors of the repository.
public Task> GetAllContributors(string owner, string name, bool includeAnonymous)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var parameters = new Dictionary();
if (includeAnonymous)
parameters.Add("anon", "1");
return ApiConnection.GetAll(ApiUrls.RepositoryContributors(owner, name), parameters);
}
///
/// Gets all languages for the specified repository.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// All languages used in the repository and the number of bytes of each language.
public Task> GetAllLanguages(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection
.Get>(ApiUrls.RepositoryLanguages(owner, name))
.ContinueWith>(t =>
new ReadOnlyCollection(
t.Result.Select(kvp => new RepositoryLanguage(kvp.Key, kvp.Value)).ToList()
),
TaskContinuationOptions.OnlyOnRanToCompletion);
}
///
/// Gets all teams for the specified repository.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// All s associated with the repository
public Task> GetAllTeams(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll(ApiUrls.RepositoryTeams(owner, name));
}
///
/// Gets all tags for the specified repository.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// All of the repositorys tags.
public Task> GetAllTags(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll(ApiUrls.RepositoryTags(owner, name));
}
///
/// Gets the specified branch.
///
///
/// See the API documentation for more details
///
/// The owner of the repository
/// The name of the repository
/// The name of the branch
/// The specified
public Task GetBranch(string owner, string repositoryName, string branchName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName");
Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName");
return ApiConnection.Get(ApiUrls.RepoBranch(owner, repositoryName, branchName));
}
}
}