using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Organization Outside Collaborators API. /// /// /// See the Orgs API documentation for more information. /// public class OrganizationOutsideCollaboratorsClient : ApiClient, IOrganizationOutsideCollaboratorsClient { /// /// Initializes a new Organization Outside Collaborators API client. /// /// An API connection public OrganizationOutsideCollaboratorsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// List all users who are outside collaborators of an organization. An outside collaborator is a user that /// is not a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The users [ManualRoute("GET", "/orgs/{org}/outside_collaborators")] public Task> GetAll(string org) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); return GetAll(org, ApiOptions.None); } /// /// List all users who are outside collaborators of an organization. An outside collaborator is a user that /// is not a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// Options for changing the API response /// The users [ManualRoute("GET", "/orgs/{org}/outside_collaborators")] public Task> GetAll(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.OutsideCollaborators(org), null, options); } /// /// List all users who are outside collaborators of an organization. An outside collaborator is a user that /// is not a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The filter to use when getting the users, /// The users [ManualRoute("GET", "/orgs/{org}/outside_collaborators")] public Task> GetAll(string org, OrganizationMembersFilter filter) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); return GetAll(org, filter, ApiOptions.None); } /// /// List all users who are outside collaborators of an organization. An outside collaborator is a user that /// is not a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The filter to use when getting the users, /// Options for changing the API response /// The users [ManualRoute("GET", "/orgs/{org}/outside_collaborators")] public Task> GetAll(string org, OrganizationMembersFilter filter, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.OutsideCollaborators(org, filter), null, options); } /// /// Removes a user as an outside collaborator from the organization, this will remove them from all repositories /// within the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The login of the user /// [ManualRoute("DELETE", "/orgs/{org}/outside_collaborators/{username}")] public async Task Delete(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var statusCode = await Connection.Delete(ApiUrls.OutsideCollaborator(org, user)).ConfigureAwait(false); if (statusCode != HttpStatusCode.NoContent && statusCode != (HttpStatusCode)422) { throw new ApiException("Invalid Status Code returned. Expected a 204 or a 422", statusCode); } return statusCode == HttpStatusCode.NoContent; } catch (ApiException ex) { if (ex.StatusCode == (HttpStatusCode)422) { throw new UserIsOrganizationMemberException(ex.HttpResponse); } throw; } } /// /// Converts an organization member to an outside collaborator, /// when an organization member is converted to an outside collaborator, /// they'll only have access to the repositories that their current team membership allows. /// The user will no longer be a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// [ManualRoute("PUT", "/orgs/{org}/outside_collaborators/{username}")] public async Task ConvertFromMember(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); try { var statusCode = await Connection.Put(ApiUrls.OutsideCollaborator(org, user)); if (statusCode != HttpStatusCode.NoContent && statusCode != HttpStatusCode.Forbidden) { throw new ApiException("Invalid Status Code returned. Expected a 204 or a 403", statusCode); } return statusCode == HttpStatusCode.NoContent; } catch (ForbiddenException fex) { if (string.Equals( "Cannot convert the last owner to an outside collaborator", fex.Message, StringComparison.OrdinalIgnoreCase)) { throw new UserIsLastOwnerOfOrganizationException(fex.HttpResponse); } throw; } catch (NotFoundException nfex) { if (string.Equals( $"{user} is not a member of the {org} organization.", nfex.Message, StringComparison.OrdinalIgnoreCase)) { throw new UserIsNotMemberOfOrganizationException(nfex.HttpResponse); } throw; } } } }