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 } public enum MembershipRole { [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 [ManualRoute("GET", "/orgs/{org}/members")] public Task> GetAll(string org) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(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 [ManualRoute("GET", "/orgs/{org}/members")] public Task> GetAll(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(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 [ManualRoute("GET", "/orgs/{org}/members")] public Task> GetAll(string org, OrganizationMembersFilter filter) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(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 [ManualRoute("GET", "/orgs/{org}/members")] public Task> GetAll(string org, OrganizationMembersFilter filter, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(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 [ManualRoute("GET", "/orgs/{org}/members?role={1}")] public Task> GetAll(string org, OrganizationMembersRole role) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(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 [ManualRoute("GET", "/orgs/{org}/members?role={1}")] public Task> GetAll(string org, OrganizationMembersRole role, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(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 [ManualRoute("GET", "/orgs/{org}/members?filter={1}&role={2}")] public Task> GetAll(string org, OrganizationMembersFilter filter, OrganizationMembersRole role) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(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 [ManualRoute("GET", "/orgs/{org}/members?filter={1}&role={2}")] public Task> GetAll(string org, OrganizationMembersFilter filter, OrganizationMembersRole role, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(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 /// [ManualRoute("GET", "/orgs/{org}/public_members")] public Task> GetAllPublic(string org) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(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 /// [ManualRoute("GET", "/orgs/{org}/public_members")] public Task> GetAllPublic(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(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 /// [ManualRoute("GET", "/orgs/{org}/members/{username}")] public async Task CheckMember(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(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 /// [ManualRoute("GET", "/orgs/{org}/public_members/{username}")] public async Task CheckMemberPublic(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(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 /// [ManualRoute("DELETE", "/orgs/{org}/members/{username}")] public Task Delete(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(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 /// [ManualRoute("PUT", "/orgs/{org}/public_members/{username}")] public async Task Publicize(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(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 requires authentication. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// [ManualRoute("DELETE", "/orgs/{org}/public_members/{username}")] public Task Conceal(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Delete(ApiUrls.OrganizationMembership(org, user)); } /// /// Get a user's membership with an organization. /// /// /// This method requires authentication. /// The authenticated user must be an organization member. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// [ManualRoute("GET", "/orgs/{org}/memberships/{username}")] public Task GetOrganizationMembership(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Get(ApiUrls.OrganizationMemberships(org, user)); } /// /// Add a user to the organization or update the user's role withing the organization. /// /// /// This method requires authentication. /// The authenticated user must be an organization owner. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// An instance describing the /// changes to make to the user's organization membership /// [ManualRoute("PUT", "/orgs/{org}/memberships/{username}")] public Task AddOrUpdateOrganizationMembership(string org, string user, OrganizationMembershipUpdate addOrUpdateRequest) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); Ensure.ArgumentNotNull(addOrUpdateRequest, nameof(addOrUpdateRequest)); return ApiConnection.Put(ApiUrls.OrganizationMemberships(org, user), addOrUpdateRequest); } /// /// Remove a user's membership with an organization. /// /// /// This method requires authentication. /// The authenticated user must be an organization owner. /// See the API documentation /// for more information. /// /// The login for the organization /// The login for the user /// [ManualRoute("DELETE", "/orgs/{org}/memberships/{username}")] public Task RemoveOrganizationMembership(string org, string user) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNullOrEmptyString(user, nameof(user)); return ApiConnection.Delete(ApiUrls.OrganizationMemberships(org, user)); } /// /// List all pending invitations for the organization. /// /// /// See the API Documentation /// for more information. /// /// The login for the organization /// [ManualRoute("GET", "/orgs/{org}/invitations")] public Task> GetAllPendingInvitations(string org) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); return GetAllPendingInvitations(org, ApiOptions.None); } /// /// List all pending invitations for the organization. /// /// /// See the API Documentation /// for more information. /// /// The login for the organization /// Options to change API behaviour /// [ManualRoute("GET", "/orgs/{org}/invitations")] public Task> GetAllPendingInvitations(string org, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(org, nameof(org)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.OrganizationPendingInvititations(org), null, AcceptHeaders.OrganizationMembershipPreview, options); } } }