using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Collaborators on a Repository. /// /// /// See the Collaborators API documentation for more details. /// public class RepoCollaboratorsClient : ApiClient, IRepoCollaboratorsClient { /// /// Initializes a new GitHub Repo Collaborators API client. /// /// An API connection. public RepoCollaboratorsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Gets all the collaborators on a 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. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators")] public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetAll(owner, name, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators")] public Task> GetAll(long repositoryId) { return GetAll(repositoryId, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// Thrown when a general API error occurs. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators")] public Task> GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(options, nameof(options)); return GetAll(owner, name, new RepositoryCollaboratorListRequest(), options); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Options for changing the API response /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators")] public Task> GetAll(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(options)); return GetAll(repositoryId, new RepositoryCollaboratorListRequest(), options); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Used to request and filter a list of repository collaborators /// Thrown when a general API error occurs. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators")] public Task> GetAll(string owner, string name, RepositoryCollaboratorListRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(request, nameof(request)); return GetAll(owner, name, request, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Used to request and filter a list of repository collaborators /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators")] public Task> GetAll(long repositoryId, RepositoryCollaboratorListRequest request) { Ensure.ArgumentNotNull(request, nameof(request)); return GetAll(repositoryId, request, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Used to request and filter a list of repository collaborators /// Options for changing the API response /// Thrown when a general API error occurs. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators")] public Task> GetAll(string owner, string name, RepositoryCollaboratorListRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepoCollaborators(owner, name), request.ToParametersDictionary(), options); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Used to request and filter a list of repository collaborators /// Options for changing the API response /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators")] public Task> GetAll(long repositoryId, RepositoryCollaboratorListRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepoCollaborators(repositoryId), request.ToParametersDictionary(), options); } /// /// Checks if a user is a collaborator on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the prospective collaborator /// Thrown when a general API error occurs. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators/{username}")] public async Task IsCollaborator(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var response = await Connection.Get(ApiUrls.RepoCollaborator(owner, name, user), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Checks if a user is a collaborator on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the prospective collaborator /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators/{username}")] public async Task IsCollaborator(long repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var response = await Connection.Get(ApiUrls.RepoCollaborator(repositoryId, user), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Review a user's permission level in a repository /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the collaborator to check permission for /// Thrown when a general API error occurs. [ManualRoute("GET", "/repos/{owner}/{repo}/collaborators/{username}/permission")] public Task ReviewPermission(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection .Get(ApiUrls.RepoCollaboratorPermission(owner, name, user), null, AcceptHeaders.OrganizationMembershipPreview); } /// /// Review a user's permission level in a repository /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the collaborator to check permission for /// Thrown when a general API error occurs. [ManualRoute("GET", "/repository/{id}/collaborators/{username}/permission")] public Task ReviewPermission(long repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection .Get(ApiUrls.RepoCollaboratorPermission(repositoryId, user), null, AcceptHeaders.OrganizationMembershipPreview); } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the new collaborator /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repos/{owner}/{repo}/collaborators/{username}")] public Task Add(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Put(ApiUrls.RepoCollaborator(owner, name, user)); } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the new collaborator /// The permission to set. Only valid on organization-owned repositories. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repos/{owner}/{repo}/collaborators/{username}")] public async Task Add(string owner, string name, string user, CollaboratorRequest permission) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var response = await Connection.Put(ApiUrls.RepoCollaborator(owner, name, user), permission).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the new collaborator /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repository/{id}/collaborators/{username}")] public Task Add(long repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Put(ApiUrls.RepoCollaborator(repositoryId, user)); } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the new collaborator /// The permission to set. Only valid on organization-owned repositories. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repository/{id}/collaborators/{username}")] public async Task Add(long repositoryId, string user, CollaboratorRequest permission) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var response = await Connection.Put(ApiUrls.RepoCollaborator(repositoryId, user), permission).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Invites a new collaborator to the repo /// /// /// See the API documentation for more information. /// /// The owner of the repository. /// The name of the repository. /// The name of the user to invite. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repos/{owner}/{repo}/collaborators/{username}")] public Task Invite(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Put(ApiUrls.RepoCollaborator(owner, name, user), new object()); } /// /// Invites a new collaborator to the repo /// /// /// See the API documentation for more information. /// /// The owner of the repository. /// The name of the repository. /// The name of the user to invite. /// The permission to set. Only valid on organization-owned repositories. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repos/{owner}/{repo}/collaborators/{username}")] public Task Invite(string owner, string name, string user, CollaboratorRequest permission) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); Ensure.ArgumentNotNull(permission, nameof(permission)); return ApiConnection.Put(ApiUrls.RepoCollaborator(owner, name, user), permission); } /// /// Invites a new collaborator to the repo /// /// /// See the API documentation for more information. /// /// The id of the repository. /// The name of the user to invite. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repository/{id}/collaborators/{username}")] public Task Invite(long repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Put(ApiUrls.RepoCollaborator(repositoryId, user), new object()); } /// /// Invites a new collaborator to the repo /// /// /// See the API documentation for more information. /// /// The id of the repository. /// The name of the user to invite. /// The permission to set. Only valid on organization-owned repositories. /// Thrown when a general API error occurs. [ManualRoute("PUT", "/repository/{id}/collaborators/{username}")] public Task Invite(long repositoryId, string user, CollaboratorRequest permission) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); Ensure.ArgumentNotNull(permission, nameof(permission)); return ApiConnection.Put(ApiUrls.RepoCollaborator(repositoryId, user), permission); } /// /// Deletes a collaborator from the repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the deleted collaborator /// Thrown when a general API error occurs. [ManualRoute("DELETE", "/repos/{owner}/{repo}/collaborators/{username}")] public Task Delete(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Delete(ApiUrls.RepoCollaborator(owner, name, user)); } /// /// Deletes a collaborator from the repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the deleted collaborator /// Thrown when a general API error occurs. [ManualRoute("DELETE", "/repository/{id}/collaborators/{username}")] public Task Delete(long repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Delete(ApiUrls.RepoCollaborator(repositoryId, user)); } } }