using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using Octokit.Internal; namespace Octokit { /// /// Filter members in the list /// /// /// see https://developer.github.com/v3/orgs/members/#members-list for details /// public enum OrganizationMembersFilter { /// /// All members the authenticated user can see. /// [Parameter(Value = "all")] All, /// /// Members without two-factor authentication enabled /// [Parameter(Value = "2fa_disabled")] TwoFactorAuthenticationDisabled } public enum OrganizationMembersRole { [Parameter(Value = "all")] All, [Parameter(Value = "admin")] Admin, [Parameter(Value = "member")] Member } /// /// A client for GitHub's Organization Members API. /// /// /// See the Orgs API documentation for more information. /// public class OrganizationMembersClient : ApiClient, IOrganizationMembersClient { /// /// Initializes a new Organization Members API client. /// /// An API connection public OrganizationMembersClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The users public Task> GetAll(string org) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); return GetAll(org, ApiOptions.None); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// Options for changing the API response /// The users public Task> GetAll(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Members(org), options); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The filter to use when getting the users, /// The users public Task> GetAll(string org, OrganizationMembersFilter filter) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); return GetAll(org, filter, ApiOptions.None); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// 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 public Task> GetAll(string org, OrganizationMembersFilter filter, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Members(org, filter), options); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The role filter to use when getting the users, /// The users public Task> GetAll(string org, OrganizationMembersRole role) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); return GetAll(org, role, ApiOptions.None); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The role filter to use when getting the users, /// Options for changing the API response /// The users public Task> GetAll(string org, OrganizationMembersRole role, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Members(org, role), options); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The filter to use when getting the users, /// The role filter to use when getting the users, /// The users public Task> GetAll(string org, OrganizationMembersFilter filter, OrganizationMembersRole role) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); return GetAll(org, filter, role, ApiOptions.None); } /// /// /// List all users who are members of an organization. A member is a user that /// belongs to at least 1 team in the organization. /// /// /// If the authenticated user is also an owner of this organization then both /// concealed and public member will be returned. /// /// /// If the requester is not an owner of the organization the query will be redirected /// to the public members list. /// /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The filter to use when getting the users, /// The role filter to use when getting the users, /// Options for changing the API response /// The users public Task> GetAll(string org, OrganizationMembersFilter filter, OrganizationMembersRole role, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Members(org, filter, role), options); } /// /// List all users who have publicized their membership of the organization. /// /// http://developer.github.com/v3/orgs/members/#public-members-list /// The login for the organization /// public Task> GetAllPublic(string org) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); return GetAllPublic(org, ApiOptions.None); } /// /// List all users who have publicized their membership of the organization. /// /// http://developer.github.com/v3/orgs/members/#public-members-list /// The login for the organization /// Options for changing the API response /// public Task> GetAllPublic(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.PublicMembers(org), options); } /// /// Check if a user is, publicly or privately, a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// public async Task CheckMember(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); try { var response = await Connection.Get(ApiUrls.CheckMember(org, user), null, null).ConfigureAwait(false); var statusCode = response.HttpResponse.StatusCode; if (statusCode != HttpStatusCode.NotFound && statusCode != HttpStatusCode.NoContent && statusCode != HttpStatusCode.Found) { throw new ApiException("Invalid Status Code returned. Expected a 204, a 302 or a 404", statusCode); } return statusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Check is a user is publicly a member of the organization. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// public async Task CheckMemberPublic(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); try { var response = await Connection.Get(ApiUrls.CheckMemberPublic(org, user), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Removes a user from the organization, this will also remove them from all teams /// within the organization and they will no longer have any access to the organization's /// repositories. /// /// /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// public Task Delete(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Delete("orgs/{0}/members/{1}".FormatUri(org, user)); } /// /// Make the authenticated user's organization membership public. /// /// /// This method requires authentication. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// public async Task Publicize(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); try { var requestData = new { }; var response = await Connection.Put(ApiUrls.OrganizationMembership(org, user), requestData).ConfigureAwait(false); if (response.HttpResponse.StatusCode != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204", response.HttpResponse.StatusCode); } return response.HttpResponse.StatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Make the authenticated user's organization membership private. /// /// /// This method requries authentication. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// public Task Conceal(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, "org"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Delete(ApiUrls.OrganizationMembership(org, user)); } } }