using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Diagnostics.CodeAnalysis; #if NET_45 using System.Collections.Generic; #endif 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) { Status = new CommitStatusClient(apiConnection); Hooks = new RepositoryHooksClient(apiConnection); Forks = new RepositoryForksClient(apiConnection); Collaborator = new RepoCollaboratorsClient(apiConnection); Statistics = new StatisticsClient(apiConnection); Deployment = new DeploymentsClient(apiConnection); PullRequest = new PullRequestsClient(apiConnection); Comment = new RepositoryCommentsClient(apiConnection); Commit = new RepositoryCommitsClient(apiConnection); Release = new ReleasesClient(apiConnection); DeployKeys = new RepositoryDeployKeysClient(apiConnection); Merging = new MergingClient(apiConnection); Content = new RepositoryContentsClient(apiConnection); Page = new RepositoryPagesClient(apiConnection); Invitation = new RepositoryInvitationsClient(apiConnection); Branch = new RepositoryBranchesClient(apiConnection); Traffic = new RepositoryTrafficClient(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"); 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 repository /// 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).ConfigureAwait(false); } catch (ApiValidationException e) { string errorMessage = e.ApiError.FirstErrorMessageSafe(); if (string.Equals( "name already exists on this account", errorMessage, StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(organizationLogin)) { throw new RepositoryExistsException(newRepository.Name, e); } var baseAddress = Connection.BaseAddress.Host != GitHubClient.GitHubApiUrl.Host ? Connection.BaseAddress : new Uri("https://github.com/"); throw new RepositoryExistsException( organizationLogin, newRepository.Name, baseAddress, e); } if (string.Equals( "please upgrade your plan to create a new private repository.", errorMessage, StringComparison.OrdinalIgnoreCase)) { throw new PrivateRepositoryQuotaExceededException(e); } if (string.Equals( "name can't be private. You are over your quota.", errorMessage, StringComparison.OrdinalIgnoreCase)) { throw new PrivateRepositoryQuotaExceededException(e); } if (errorMessage != null && errorMessage.EndsWith("is an unknown gitignore template.", StringComparison.OrdinalIgnoreCase)) { throw new InvalidGitIgnoreTemplateException(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"); return ApiConnection.Delete(ApiUrls.Repository(owner, name)); } /// /// 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 Id of the repository /// Thrown when a general API error occurs. public Task Delete(long repositoryId) { return ApiConnection.Delete(ApiUrls.Repository(repositoryId)); } /// /// Gets the specified branch. /// /// /// See the API documentation for more details /// /// The Id of the repository /// The name of the branch [Obsolete("Please use RepositoriesClient.Branch.Get() instead. This method will be removed in a future version")] public Task GetBranch(long repositoryId, string branchName) { Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); return Branch.Get(repositoryId, branchName); } /// /// 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); } /// /// Updates the specified repository with the values given in /// /// The Id of the repository /// New values to update the repository with /// The updated public Task Edit(long repositoryId, RepositoryUpdate update) { Ensure.ArgumentNotNull(update, "update"); return ApiConnection.Patch(ApiUrls.Repository(repositoryId), update); } /// /// Edit the specified branch with the values given in /// /// The owner of the repository /// The name of the repository /// The name of the branch /// New values to update the branch with /// The updated [Obsolete("This existing implementation will cease to work when the Branch Protection API preview period ends. Please use the RepositoryBranchesClient methods instead.")] public Task EditBranch(string owner, string name, string branch, BranchUpdate update) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return Branch.Edit(owner, name, branch, update); } /// /// Edit the specified branch with the values given in /// /// The Id of the repository /// The name of the branch /// New values to update the branch with /// The updated [Obsolete("This existing implementation will cease to work when the Branch Protection API preview period ends. Please use the RepositoryBranchesClient methods instead.")] public Task EditBranch(long repositoryId, string branch, BranchUpdate update) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return Branch.Edit(repositoryId, branch, 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"); return ApiConnection.Get(ApiUrls.Repository(owner, name)); } /// /// Gets the specified repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository /// Thrown when a general API error occurs. /// A public Task Get(long repositoryId) { return ApiConnection.Get(ApiUrls.Repository(repositoryId)); } /// /// Gets all public repositories. /// /// /// 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> GetAllPublic() { return ApiConnection.GetAll(ApiUrls.AllPublicRepositories()); } /// /// Gets all public repositories since the integer Id of the last Repository that you've seen. /// /// /// See the API documentation for more information. /// The default page size on GitHub.com is 30. /// /// Search parameters of the last repository seen /// Thrown if the client is not authenticated. /// Thrown when a general API error occurs. /// A of . public Task> GetAllPublic(PublicRepositoryRequest request) { Ensure.ArgumentNotNull(request, "request"); var url = ApiUrls.AllPublicRepositories(request.Since); return ApiConnection.GetAll(url); } /// /// 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 GetAllForCurrent(ApiOptions.None); } /// /// Gets all repositories owned by the current user. /// /// /// See the API documentation for more information. /// /// Options for changing the API response /// Thrown if the client is not authenticated. /// Thrown when a general API error occurs. /// A of . public Task> GetAllForCurrent(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Repositories(), options); } /// /// Gets all repositories owned by the current user. /// /// /// See the API documentation for more information. /// The default page size on GitHub.com is 30. /// /// Search parameters to filter results on /// Thrown if the client is not authenticated. /// Thrown when a general API error occurs. /// A of . public Task> GetAllForCurrent(RepositoryRequest request) { Ensure.ArgumentNotNull(request, "request"); return GetAllForCurrent(request, ApiOptions.None); } public Task> GetAllForCurrent(RepositoryRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, "request"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Repositories(), request.ToParametersDictionary(), options); } /// /// Gets all repositories owned by the specified user. /// /// /// See the API documentation for more information. /// The default page size on GitHub.com is 30. /// /// The account name to search for /// Thrown when a general API error occurs. /// A of . public Task> GetAllForUser(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); return GetAllForUser(login, ApiOptions.None); } /// /// Gets all repositories owned by the specified user. /// /// /// See the API documentation for more information. /// /// The account name to search for /// Options for changing the API response /// Thrown when a general API error occurs. /// A of . public Task> GetAllForUser(string login, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Repositories(login), options); } /// /// 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 GetAllForOrg(organization, ApiOptions.None); } /// /// Gets all repositories owned by the specified organization. /// /// /// See the API documentation for more information. /// /// The organization name to search for /// Options for changing the API response /// Thrown when a general API error occurs. /// A of . public Task> GetAllForOrg(string organization, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(organization, "organization"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.OrganizationRepositories(organization), options); } /// /// A client for GitHub's Repository Branches API. /// /// /// See the Branches API documentation for more details /// [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] public IRepositoryBranchesClient Branch { get; private set; } /// /// 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 Status { get; private set; } /// /// A client for GitHub's Repository Hooks API. /// /// See Hooks API documentation for more information. public IRepositoryHooksClient Hooks { get; private set; } /// /// A client for GitHub's Repository Forks API. /// /// See Forks API documentation for more information. public IRepositoryForksClient Forks { get; private set; } /// /// A client for GitHub's Repo Collaborators. /// /// /// See the Collaborators API documentation for more details /// public IRepoCollaboratorsClient Collaborator { 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 Commit { get; private set; } /// /// Access GitHub's Releases API. /// /// /// Refer to the API documentation for more information: https://developer.github.com/v3/repos/releases/ /// public IReleasesClient Release { get; private set; } /// /// Client for GitHub's Repository Merging API /// /// /// See the Merging API documentation for more details /// public IMergingClient Merging { 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 Comment { get; private set; } /// /// Client for managing deploy keys in a repository. /// /// /// See the Repository Deploy Keys API documentation for more information. /// public IRepositoryDeployKeysClient DeployKeys { get; private set; } /// /// Client for managing the contents of a repository. /// /// /// See the Repository Contents API documentation for more information. /// public IRepositoryContentsClient Content { 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 [Obsolete("Please use RepositoriesClient.Branch.GetAll() instead. This method will be removed in a future version")] public Task> GetAllBranches(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return Branch.GetAll(owner, name); } /// /// Gets all the branches for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository [Obsolete("Please use RepositoriesClient.Branch.GetAll() instead. This method will be removed in a future version")] public Task> GetAllBranches(long repositoryId) { return Branch.GetAll(repositoryId); } /// /// 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 /// Options for changing the API response [Obsolete("Please use RepositoriesClient.Branch.GetAll() instead. This method will be removed in a future version")] public Task> GetAllBranches(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return Branch.GetAll(owner, name, options); } /// /// Gets all the branches for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// Options for changing the API response [Obsolete("Please use RepositoriesClient.Branch.GetAll() instead. This method will be removed in a future version")] public Task> GetAllBranches(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return Branch.GetAll(repositoryId, options); } /// /// 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) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAllContributors(owner, name, false); } /// /// Gets all contributors for the specified repository. Does not include anonymous contributors. /// /// /// See the API documentation for more details /// /// The Id of the repository /// All contributors of the repository. public Task> GetAllContributors(long repositoryId) { return GetAllContributors(repositoryId, false); } /// /// 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 /// Options for changing the API response /// All contributors of the repository. public Task> GetAllContributors(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return GetAllContributors(owner, name, false, options); } /// /// Gets all contributors for the specified repository. Does not include anonymous contributors. /// /// /// See the API documentation for more details /// /// The Id of the repository /// Options for changing the API response /// All contributors of the repository. public Task> GetAllContributors(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return GetAllContributors(repositoryId, false, options); } /// /// 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"); return GetAllContributors(owner, name, includeAnonymous, ApiOptions.None); } /// /// Gets all contributors for the specified repository. With the option to include anonymous contributors. /// /// /// See the API documentation for more details /// /// The Id of the repository /// True if anonymous contributors should be included in result; Otherwise false /// All contributors of the repository. public Task> GetAllContributors(long repositoryId, bool includeAnonymous) { return GetAllContributors(repositoryId, includeAnonymous, ApiOptions.None); } /// /// 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 /// Options for changing the API response /// All contributors of the repository. public Task> GetAllContributors(string owner, string name, bool includeAnonymous, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); var parameters = new Dictionary(); if (includeAnonymous) parameters.Add("anon", "1"); return ApiConnection.GetAll(ApiUrls.RepositoryContributors(owner, name), parameters, options); } /// /// Gets all contributors for the specified repository. With the option to include anonymous contributors. /// /// /// See the API documentation for more details /// /// The Id of the repository /// True if anonymous contributors should be included in result; Otherwise false /// Options for changing the API response /// All contributors of the repository. public Task> GetAllContributors(long repositoryId, bool includeAnonymous, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); var parameters = new Dictionary(); if (includeAnonymous) parameters.Add("anon", "1"); return ApiConnection.GetAll(ApiUrls.RepositoryContributors(repositoryId), parameters, options); } /// /// 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 async Task> GetAllLanguages(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); var endpoint = ApiUrls.RepositoryLanguages(owner, name); var data = await ApiConnection.Get>(endpoint).ConfigureAwait(false); return new ReadOnlyCollection( data.Select(kvp => new RepositoryLanguage(kvp.Key, kvp.Value)).ToList()); } /// /// Gets all languages for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// All languages used in the repository and the number of bytes of each language. public async Task> GetAllLanguages(long repositoryId) { var endpoint = ApiUrls.RepositoryLanguages(repositoryId); var data = await ApiConnection.Get>(endpoint).ConfigureAwait(false); return new ReadOnlyCollection( data.Select(kvp => new RepositoryLanguage(kvp.Key, kvp.Value)).ToList()); } /// /// 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 GetAllTeams(owner, name, ApiOptions.None); } /// /// Gets all teams for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// All s associated with the repository public Task> GetAllTeams(long repositoryId) { return GetAllTeams(repositoryId, ApiOptions.None); } /// /// Gets all teams for the specified repository. /// /// /// See the API documentation for more details /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// All s associated with the repository public Task> GetAllTeams(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryTeams(owner, name), options); } /// /// Gets all teams for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// Options for changing the API response /// All s associated with the repository public Task> GetAllTeams(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryTeams(repositoryId), options); } /// /// 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 repositories tags. public Task> GetAllTags(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAllTags(owner, name, ApiOptions.None); } /// /// Gets all tags for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// All of the repositories tags. public Task> GetAllTags(long repositoryId) { return GetAllTags(repositoryId, ApiOptions.None); } /// /// Gets all tags for the specified repository. /// /// /// See the API documentation for more details /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// All of the repositories tags. public Task> GetAllTags(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryTags(owner, name), options); } /// /// Gets all tags for the specified repository. /// /// /// See the API documentation for more details /// /// The Id of the repository /// Options for changing the API response /// All of the repositories tags. public Task> GetAllTags(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryTags(repositoryId), options); } /// /// 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 [Obsolete("Please use RepositoriesClient.Branch.Get() instead. This method will be removed in a future version")] public Task GetBranch(string owner, string name, string branchName) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); return Branch.Get(owner, name, branchName); } /// /// A client for GitHub's Repository Pages API. /// /// /// See the Repository Pages API documentation for more information. /// public IRepositoryPagesClient Page { get; private set; } /// /// A client for GitHub's Repository Invitations API. /// /// /// See the Repository Invitations API documentation for more information. /// public IRepositoryInvitationsClient Invitation { get; private set; } /// /// Access GitHub's Repository Traffic API /// /// /// Refer to the API documentation for more information: https://developer.github.com/v3/repos/traffic/ /// public IRepositoryTrafficClient Traffic { get; private set; } } }