...

Source file src/github.com/google/go-github/v33/github/orgs_members.go

Documentation: github.com/google/go-github/v33/github

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  )
    12  
    13  // Membership represents the status of a user's membership in an organization or team.
    14  type Membership struct {
    15  	URL *string `json:"url,omitempty"`
    16  
    17  	// State is the user's status within the organization or team.
    18  	// Possible values are: "active", "pending"
    19  	State *string `json:"state,omitempty"`
    20  
    21  	// Role identifies the user's role within the organization or team.
    22  	// Possible values for organization membership:
    23  	//     member - non-owner organization member
    24  	//     admin - organization owner
    25  	//
    26  	// Possible values for team membership are:
    27  	//     member - a normal member of the team
    28  	//     maintainer - a team maintainer. Able to add/remove other team
    29  	//                  members, promote other team members to team
    30  	//                  maintainer, and edit the team’s name and description
    31  	Role *string `json:"role,omitempty"`
    32  
    33  	// For organization membership, the API URL of the organization.
    34  	OrganizationURL *string `json:"organization_url,omitempty"`
    35  
    36  	// For organization membership, the organization the membership is for.
    37  	Organization *Organization `json:"organization,omitempty"`
    38  
    39  	// For organization membership, the user the membership is for.
    40  	User *User `json:"user,omitempty"`
    41  }
    42  
    43  func (m Membership) String() string {
    44  	return Stringify(m)
    45  }
    46  
    47  // ListMembersOptions specifies optional parameters to the
    48  // OrganizationsService.ListMembers method.
    49  type ListMembersOptions struct {
    50  	// If true (or if the authenticated user is not an owner of the
    51  	// organization), list only publicly visible members.
    52  	PublicOnly bool `url:"-"`
    53  
    54  	// Filter members returned in the list. Possible values are:
    55  	// 2fa_disabled, all. Default is "all".
    56  	Filter string `url:"filter,omitempty"`
    57  
    58  	// Role filters members returned by their role in the organization.
    59  	// Possible values are:
    60  	//     all - all members of the organization, regardless of role
    61  	//     admin - organization owners
    62  	//     member - non-owner organization members
    63  	//
    64  	// Default is "all".
    65  	Role string `url:"role,omitempty"`
    66  
    67  	ListOptions
    68  }
    69  
    70  // ListMembers lists the members for an organization. If the authenticated
    71  // user is an owner of the organization, this will return both concealed and
    72  // public members, otherwise it will only return public members.
    73  //
    74  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organization-members
    75  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-public-organization-members
    76  func (s *OrganizationsService) ListMembers(ctx context.Context, org string, opts *ListMembersOptions) ([]*User, *Response, error) {
    77  	var u string
    78  	if opts != nil && opts.PublicOnly {
    79  		u = fmt.Sprintf("orgs/%v/public_members", org)
    80  	} else {
    81  		u = fmt.Sprintf("orgs/%v/members", org)
    82  	}
    83  	u, err := addOptions(u, opts)
    84  	if err != nil {
    85  		return nil, nil, err
    86  	}
    87  
    88  	req, err := s.client.NewRequest("GET", u, nil)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  
    93  	var members []*User
    94  	resp, err := s.client.Do(ctx, req, &members)
    95  	if err != nil {
    96  		return nil, resp, err
    97  	}
    98  
    99  	return members, resp, nil
   100  }
   101  
   102  // IsMember checks if a user is a member of an organization.
   103  //
   104  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#check-organization-membership-for-a-user
   105  func (s *OrganizationsService) IsMember(ctx context.Context, org, user string) (bool, *Response, error) {
   106  	u := fmt.Sprintf("orgs/%v/members/%v", org, user)
   107  	req, err := s.client.NewRequest("GET", u, nil)
   108  	if err != nil {
   109  		return false, nil, err
   110  	}
   111  
   112  	resp, err := s.client.Do(ctx, req, nil)
   113  	member, err := parseBoolResponse(err)
   114  	return member, resp, err
   115  }
   116  
   117  // IsPublicMember checks if a user is a public member of an organization.
   118  //
   119  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#check-public-organization-membership-for-a-user
   120  func (s *OrganizationsService) IsPublicMember(ctx context.Context, org, user string) (bool, *Response, error) {
   121  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   122  	req, err := s.client.NewRequest("GET", u, nil)
   123  	if err != nil {
   124  		return false, nil, err
   125  	}
   126  
   127  	resp, err := s.client.Do(ctx, req, nil)
   128  	member, err := parseBoolResponse(err)
   129  	return member, resp, err
   130  }
   131  
   132  // RemoveMember removes a user from all teams of an organization.
   133  //
   134  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#remove-an-organization-member
   135  func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user string) (*Response, error) {
   136  	u := fmt.Sprintf("orgs/%v/members/%v", org, user)
   137  	req, err := s.client.NewRequest("DELETE", u, nil)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	return s.client.Do(ctx, req, nil)
   143  }
   144  
   145  // PublicizeMembership publicizes a user's membership in an organization. (A
   146  // user cannot publicize the membership for another user.)
   147  //
   148  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#set-public-organization-membership-for-the-authenticated-user
   149  func (s *OrganizationsService) PublicizeMembership(ctx context.Context, org, user string) (*Response, error) {
   150  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   151  	req, err := s.client.NewRequest("PUT", u, nil)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	return s.client.Do(ctx, req, nil)
   157  }
   158  
   159  // ConcealMembership conceals a user's membership in an organization.
   160  //
   161  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#remove-public-organization-membership-for-the-authenticated-user
   162  func (s *OrganizationsService) ConcealMembership(ctx context.Context, org, user string) (*Response, error) {
   163  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   164  	req, err := s.client.NewRequest("DELETE", u, nil)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	return s.client.Do(ctx, req, nil)
   170  }
   171  
   172  // ListOrgMembershipsOptions specifies optional parameters to the
   173  // OrganizationsService.ListOrgMemberships method.
   174  type ListOrgMembershipsOptions struct {
   175  	// Filter memberships to include only those with the specified state.
   176  	// Possible values are: "active", "pending".
   177  	State string `url:"state,omitempty"`
   178  
   179  	ListOptions
   180  }
   181  
   182  // ListOrgMemberships lists the organization memberships for the authenticated user.
   183  //
   184  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organization-memberships-for-the-authenticated-user
   185  func (s *OrganizationsService) ListOrgMemberships(ctx context.Context, opts *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
   186  	u := "user/memberships/orgs"
   187  	u, err := addOptions(u, opts)
   188  	if err != nil {
   189  		return nil, nil, err
   190  	}
   191  
   192  	req, err := s.client.NewRequest("GET", u, nil)
   193  	if err != nil {
   194  		return nil, nil, err
   195  	}
   196  
   197  	var memberships []*Membership
   198  	resp, err := s.client.Do(ctx, req, &memberships)
   199  	if err != nil {
   200  		return nil, resp, err
   201  	}
   202  
   203  	return memberships, resp, nil
   204  }
   205  
   206  // GetOrgMembership gets the membership for a user in a specified organization.
   207  // Passing an empty string for user will get the membership for the
   208  // authenticated user.
   209  //
   210  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#get-an-organization-membership-for-the-authenticated-user
   211  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#get-organization-membership-for-a-user
   212  func (s *OrganizationsService) GetOrgMembership(ctx context.Context, user, org string) (*Membership, *Response, error) {
   213  	var u string
   214  	if user != "" {
   215  		u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   216  	} else {
   217  		u = fmt.Sprintf("user/memberships/orgs/%v", org)
   218  	}
   219  
   220  	req, err := s.client.NewRequest("GET", u, nil)
   221  	if err != nil {
   222  		return nil, nil, err
   223  	}
   224  
   225  	membership := new(Membership)
   226  	resp, err := s.client.Do(ctx, req, membership)
   227  	if err != nil {
   228  		return nil, resp, err
   229  	}
   230  
   231  	return membership, resp, nil
   232  }
   233  
   234  // EditOrgMembership edits the membership for user in specified organization.
   235  // Passing an empty string for user will edit the membership for the
   236  // authenticated user.
   237  //
   238  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#update-an-organization-membership-for-the-authenticated-user
   239  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#set-organization-membership-for-a-user
   240  func (s *OrganizationsService) EditOrgMembership(ctx context.Context, user, org string, membership *Membership) (*Membership, *Response, error) {
   241  	var u, method string
   242  	if user != "" {
   243  		u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   244  		method = "PUT"
   245  	} else {
   246  		u = fmt.Sprintf("user/memberships/orgs/%v", org)
   247  		method = "PATCH"
   248  	}
   249  
   250  	req, err := s.client.NewRequest(method, u, membership)
   251  	if err != nil {
   252  		return nil, nil, err
   253  	}
   254  
   255  	m := new(Membership)
   256  	resp, err := s.client.Do(ctx, req, m)
   257  	if err != nil {
   258  		return nil, resp, err
   259  	}
   260  
   261  	return m, resp, nil
   262  }
   263  
   264  // RemoveOrgMembership removes user from the specified organization. If the
   265  // user has been invited to the organization, this will cancel their invitation.
   266  //
   267  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#remove-organization-membership-for-a-user
   268  func (s *OrganizationsService) RemoveOrgMembership(ctx context.Context, user, org string) (*Response, error) {
   269  	u := fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   270  	req, err := s.client.NewRequest("DELETE", u, nil)
   271  	if err != nil {
   272  		return nil, err
   273  	}
   274  
   275  	return s.client.Do(ctx, req, nil)
   276  }
   277  
   278  // ListPendingOrgInvitations returns a list of pending invitations.
   279  //
   280  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-pending-organization-invitations
   281  func (s *OrganizationsService) ListPendingOrgInvitations(ctx context.Context, org string, opts *ListOptions) ([]*Invitation, *Response, error) {
   282  	u := fmt.Sprintf("orgs/%v/invitations", org)
   283  	u, err := addOptions(u, opts)
   284  	if err != nil {
   285  		return nil, nil, err
   286  	}
   287  
   288  	req, err := s.client.NewRequest("GET", u, nil)
   289  	if err != nil {
   290  		return nil, nil, err
   291  	}
   292  
   293  	var pendingInvitations []*Invitation
   294  	resp, err := s.client.Do(ctx, req, &pendingInvitations)
   295  	if err != nil {
   296  		return nil, resp, err
   297  	}
   298  	return pendingInvitations, resp, nil
   299  }
   300  
   301  // CreateOrgInvitationOptions specifies the parameters to the OrganizationService.Invite
   302  // method.
   303  type CreateOrgInvitationOptions struct {
   304  	// GitHub user ID for the person you are inviting. Not required if you provide Email.
   305  	InviteeID *int64 `json:"invitee_id,omitempty"`
   306  	// Email address of the person you are inviting, which can be an existing GitHub user.
   307  	// Not required if you provide InviteeID
   308  	Email *string `json:"email,omitempty"`
   309  	// Specify role for new member. Can be one of:
   310  	// * admin - Organization owners with full administrative rights to the
   311  	// 	 organization and complete access to all repositories and teams.
   312  	// * direct_member - Non-owner organization members with ability to see
   313  	//   other members and join teams by invitation.
   314  	// * billing_manager - Non-owner organization members with ability to
   315  	//   manage the billing settings of your organization.
   316  	// Default is "direct_member".
   317  	Role   *string `json:"role"`
   318  	TeamID []int64 `json:"team_ids"`
   319  }
   320  
   321  // CreateOrgInvitation invites people to an organization by using their GitHub user ID or their email address.
   322  // In order to create invitations in an organization,
   323  // the authenticated user must be an organization owner.
   324  //
   325  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#create-an-organization-invitation
   326  func (s *OrganizationsService) CreateOrgInvitation(ctx context.Context, org string, opts *CreateOrgInvitationOptions) (*Invitation, *Response, error) {
   327  	u := fmt.Sprintf("orgs/%v/invitations", org)
   328  
   329  	req, err := s.client.NewRequest("POST", u, opts)
   330  	if err != nil {
   331  		return nil, nil, err
   332  	}
   333  
   334  	var invitation *Invitation
   335  	resp, err := s.client.Do(ctx, req, &invitation)
   336  	if err != nil {
   337  		return nil, resp, err
   338  	}
   339  	return invitation, resp, nil
   340  }
   341  
   342  // ListOrgInvitationTeams lists all teams associated with an invitation. In order to see invitations in an organization,
   343  // the authenticated user must be an organization owner.
   344  //
   345  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organization-invitation-teams
   346  func (s *OrganizationsService) ListOrgInvitationTeams(ctx context.Context, org, invitationID string, opts *ListOptions) ([]*Team, *Response, error) {
   347  	u := fmt.Sprintf("orgs/%v/invitations/%v/teams", org, invitationID)
   348  	u, err := addOptions(u, opts)
   349  	if err != nil {
   350  		return nil, nil, err
   351  	}
   352  
   353  	req, err := s.client.NewRequest("GET", u, nil)
   354  	if err != nil {
   355  		return nil, nil, err
   356  	}
   357  
   358  	var orgInvitationTeams []*Team
   359  	resp, err := s.client.Do(ctx, req, &orgInvitationTeams)
   360  	if err != nil {
   361  		return nil, resp, err
   362  	}
   363  	return orgInvitationTeams, resp, nil
   364  }
   365  

View as plain text