github.com/google/go-github/v70@v70.0.0/github/orgs_members.go (about)

     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/rest/orgs/members#list-organization-members
    75  // GitHub API docs: https://docs.github.com/rest/orgs/members#list-public-organization-members
    76  //
    77  //meta:operation GET /orgs/{org}/members
    78  //meta:operation GET /orgs/{org}/public_members
    79  func (s *OrganizationsService) ListMembers(ctx context.Context, org string, opts *ListMembersOptions) ([]*User, *Response, error) {
    80  	var u string
    81  	if opts != nil && opts.PublicOnly {
    82  		u = fmt.Sprintf("orgs/%v/public_members", org)
    83  	} else {
    84  		u = fmt.Sprintf("orgs/%v/members", org)
    85  	}
    86  	u, err := addOptions(u, opts)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	req, err := s.client.NewRequest("GET", u, nil)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	var members []*User
    97  	resp, err := s.client.Do(ctx, req, &members)
    98  	if err != nil {
    99  		return nil, resp, err
   100  	}
   101  
   102  	return members, resp, nil
   103  }
   104  
   105  // IsMember checks if a user is a member of an organization.
   106  //
   107  // GitHub API docs: https://docs.github.com/rest/orgs/members#check-organization-membership-for-a-user
   108  //
   109  //meta:operation GET /orgs/{org}/members/{username}
   110  func (s *OrganizationsService) IsMember(ctx context.Context, org, user string) (bool, *Response, error) {
   111  	u := fmt.Sprintf("orgs/%v/members/%v", org, user)
   112  	req, err := s.client.NewRequest("GET", u, nil)
   113  	if err != nil {
   114  		return false, nil, err
   115  	}
   116  
   117  	resp, err := s.client.Do(ctx, req, nil)
   118  	member, err := parseBoolResponse(err)
   119  	return member, resp, err
   120  }
   121  
   122  // IsPublicMember checks if a user is a public member of an organization.
   123  //
   124  // GitHub API docs: https://docs.github.com/rest/orgs/members#check-public-organization-membership-for-a-user
   125  //
   126  //meta:operation GET /orgs/{org}/public_members/{username}
   127  func (s *OrganizationsService) IsPublicMember(ctx context.Context, org, user string) (bool, *Response, error) {
   128  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   129  	req, err := s.client.NewRequest("GET", u, nil)
   130  	if err != nil {
   131  		return false, nil, err
   132  	}
   133  
   134  	resp, err := s.client.Do(ctx, req, nil)
   135  	member, err := parseBoolResponse(err)
   136  	return member, resp, err
   137  }
   138  
   139  // RemoveMember removes a user from all teams of an organization.
   140  //
   141  // GitHub API docs: https://docs.github.com/rest/orgs/members#remove-an-organization-member
   142  //
   143  //meta:operation DELETE /orgs/{org}/members/{username}
   144  func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user string) (*Response, error) {
   145  	u := fmt.Sprintf("orgs/%v/members/%v", org, user)
   146  	req, err := s.client.NewRequest("DELETE", u, nil)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	return s.client.Do(ctx, req, nil)
   152  }
   153  
   154  // CancelInvite cancels an organization invitation.
   155  //
   156  // GitHub API docs: https://docs.github.com/rest/orgs/members#cancel-an-organization-invitation
   157  //
   158  //meta:operation DELETE /orgs/{org}/invitations/{invitation_id}
   159  func (s *OrganizationsService) CancelInvite(ctx context.Context, org string, invitationID int64) (*Response, error) {
   160  	u := fmt.Sprintf("orgs/%v/invitations/%v", org, invitationID)
   161  	req, err := s.client.NewRequest("DELETE", u, nil)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  	return s.client.Do(ctx, req, nil)
   166  }
   167  
   168  // PublicizeMembership publicizes a user's membership in an organization. (A
   169  // user cannot publicize the membership for another user.)
   170  //
   171  // GitHub API docs: https://docs.github.com/rest/orgs/members#set-public-organization-membership-for-the-authenticated-user
   172  //
   173  //meta:operation PUT /orgs/{org}/public_members/{username}
   174  func (s *OrganizationsService) PublicizeMembership(ctx context.Context, org, user string) (*Response, error) {
   175  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   176  	req, err := s.client.NewRequest("PUT", u, nil)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	return s.client.Do(ctx, req, nil)
   182  }
   183  
   184  // ConcealMembership conceals a user's membership in an organization.
   185  //
   186  // GitHub API docs: https://docs.github.com/rest/orgs/members#remove-public-organization-membership-for-the-authenticated-user
   187  //
   188  //meta:operation DELETE /orgs/{org}/public_members/{username}
   189  func (s *OrganizationsService) ConcealMembership(ctx context.Context, org, user string) (*Response, error) {
   190  	u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
   191  	req, err := s.client.NewRequest("DELETE", u, nil)
   192  	if err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return s.client.Do(ctx, req, nil)
   197  }
   198  
   199  // ListOrgMembershipsOptions specifies optional parameters to the
   200  // OrganizationsService.ListOrgMemberships method.
   201  type ListOrgMembershipsOptions struct {
   202  	// Filter memberships to include only those with the specified state.
   203  	// Possible values are: "active", "pending".
   204  	State string `url:"state,omitempty"`
   205  
   206  	ListOptions
   207  }
   208  
   209  // ListOrgMemberships lists the organization memberships for the authenticated user.
   210  //
   211  // GitHub API docs: https://docs.github.com/rest/orgs/members#list-organization-memberships-for-the-authenticated-user
   212  //
   213  //meta:operation GET /user/memberships/orgs
   214  func (s *OrganizationsService) ListOrgMemberships(ctx context.Context, opts *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
   215  	u := "user/memberships/orgs"
   216  	u, err := addOptions(u, opts)
   217  	if err != nil {
   218  		return nil, nil, err
   219  	}
   220  
   221  	req, err := s.client.NewRequest("GET", u, nil)
   222  	if err != nil {
   223  		return nil, nil, err
   224  	}
   225  
   226  	var memberships []*Membership
   227  	resp, err := s.client.Do(ctx, req, &memberships)
   228  	if err != nil {
   229  		return nil, resp, err
   230  	}
   231  
   232  	return memberships, resp, nil
   233  }
   234  
   235  // GetOrgMembership gets the membership for a user in a specified organization.
   236  // Passing an empty string for user will get the membership for the
   237  // authenticated user.
   238  //
   239  // GitHub API docs: https://docs.github.com/rest/orgs/members#get-an-organization-membership-for-the-authenticated-user
   240  // GitHub API docs: https://docs.github.com/rest/orgs/members#get-organization-membership-for-a-user
   241  //
   242  //meta:operation GET /orgs/{org}/memberships/{username}
   243  //meta:operation GET /user/memberships/orgs/{org}
   244  func (s *OrganizationsService) GetOrgMembership(ctx context.Context, user, org string) (*Membership, *Response, error) {
   245  	var u string
   246  	if user != "" {
   247  		u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   248  	} else {
   249  		u = fmt.Sprintf("user/memberships/orgs/%v", org)
   250  	}
   251  
   252  	req, err := s.client.NewRequest("GET", u, nil)
   253  	if err != nil {
   254  		return nil, nil, err
   255  	}
   256  
   257  	membership := new(Membership)
   258  	resp, err := s.client.Do(ctx, req, membership)
   259  	if err != nil {
   260  		return nil, resp, err
   261  	}
   262  
   263  	return membership, resp, nil
   264  }
   265  
   266  // EditOrgMembership edits the membership for user in specified organization.
   267  // Passing an empty string for user will edit the membership for the
   268  // authenticated user.
   269  //
   270  // GitHub API docs: https://docs.github.com/rest/orgs/members#set-organization-membership-for-a-user
   271  // GitHub API docs: https://docs.github.com/rest/orgs/members#update-an-organization-membership-for-the-authenticated-user
   272  //
   273  //meta:operation PUT /orgs/{org}/memberships/{username}
   274  //meta:operation PATCH /user/memberships/orgs/{org}
   275  func (s *OrganizationsService) EditOrgMembership(ctx context.Context, user, org string, membership *Membership) (*Membership, *Response, error) {
   276  	var u, method string
   277  	if user != "" {
   278  		u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   279  		method = "PUT"
   280  	} else {
   281  		u = fmt.Sprintf("user/memberships/orgs/%v", org)
   282  		method = "PATCH"
   283  	}
   284  
   285  	req, err := s.client.NewRequest(method, u, membership)
   286  	if err != nil {
   287  		return nil, nil, err
   288  	}
   289  
   290  	m := new(Membership)
   291  	resp, err := s.client.Do(ctx, req, m)
   292  	if err != nil {
   293  		return nil, resp, err
   294  	}
   295  
   296  	return m, resp, nil
   297  }
   298  
   299  // RemoveOrgMembership removes user from the specified organization. If the
   300  // user has been invited to the organization, this will cancel their invitation.
   301  //
   302  // GitHub API docs: https://docs.github.com/rest/orgs/members#remove-organization-membership-for-a-user
   303  //
   304  //meta:operation DELETE /orgs/{org}/memberships/{username}
   305  func (s *OrganizationsService) RemoveOrgMembership(ctx context.Context, user, org string) (*Response, error) {
   306  	u := fmt.Sprintf("orgs/%v/memberships/%v", org, user)
   307  	req, err := s.client.NewRequest("DELETE", u, nil)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  
   312  	return s.client.Do(ctx, req, nil)
   313  }
   314  
   315  // ListPendingOrgInvitations returns a list of pending invitations.
   316  //
   317  // GitHub API docs: https://docs.github.com/rest/orgs/members#list-pending-organization-invitations
   318  //
   319  //meta:operation GET /orgs/{org}/invitations
   320  func (s *OrganizationsService) ListPendingOrgInvitations(ctx context.Context, org string, opts *ListOptions) ([]*Invitation, *Response, error) {
   321  	u := fmt.Sprintf("orgs/%v/invitations", org)
   322  	u, err := addOptions(u, opts)
   323  	if err != nil {
   324  		return nil, nil, err
   325  	}
   326  
   327  	req, err := s.client.NewRequest("GET", u, nil)
   328  	if err != nil {
   329  		return nil, nil, err
   330  	}
   331  
   332  	var pendingInvitations []*Invitation
   333  	resp, err := s.client.Do(ctx, req, &pendingInvitations)
   334  	if err != nil {
   335  		return nil, resp, err
   336  	}
   337  
   338  	return pendingInvitations, resp, nil
   339  }
   340  
   341  // CreateOrgInvitationOptions specifies the parameters to the OrganizationService.Invite
   342  // method.
   343  type CreateOrgInvitationOptions struct {
   344  	// GitHub user ID for the person you are inviting. Not required if you provide Email.
   345  	InviteeID *int64 `json:"invitee_id,omitempty"`
   346  	// Email address of the person you are inviting, which can be an existing GitHub user.
   347  	// Not required if you provide InviteeID
   348  	Email *string `json:"email,omitempty"`
   349  	// Specify role for new member. Can be one of:
   350  	// * admin - Organization owners with full administrative rights to the
   351  	// 	 organization and complete access to all repositories and teams.
   352  	// * direct_member - Non-owner organization members with ability to see
   353  	//   other members and join teams by invitation.
   354  	// * billing_manager - Non-owner organization members with ability to
   355  	//   manage the billing settings of your organization.
   356  	// Default is "direct_member".
   357  	Role   *string `json:"role,omitempty"`
   358  	TeamID []int64 `json:"team_ids,omitempty"`
   359  }
   360  
   361  // CreateOrgInvitation invites people to an organization by using their GitHub user ID or their email address.
   362  // In order to create invitations in an organization,
   363  // the authenticated user must be an organization owner.
   364  //
   365  // GitHub API docs: https://docs.github.com/rest/orgs/members#create-an-organization-invitation
   366  //
   367  //meta:operation POST /orgs/{org}/invitations
   368  func (s *OrganizationsService) CreateOrgInvitation(ctx context.Context, org string, opts *CreateOrgInvitationOptions) (*Invitation, *Response, error) {
   369  	u := fmt.Sprintf("orgs/%v/invitations", org)
   370  
   371  	req, err := s.client.NewRequest("POST", u, opts)
   372  	if err != nil {
   373  		return nil, nil, err
   374  	}
   375  
   376  	var invitation *Invitation
   377  	resp, err := s.client.Do(ctx, req, &invitation)
   378  	if err != nil {
   379  		return nil, resp, err
   380  	}
   381  
   382  	return invitation, resp, nil
   383  }
   384  
   385  // ListOrgInvitationTeams lists all teams associated with an invitation. In order to see invitations in an organization,
   386  // the authenticated user must be an organization owner.
   387  //
   388  // GitHub API docs: https://docs.github.com/rest/orgs/members#list-organization-invitation-teams
   389  //
   390  //meta:operation GET /orgs/{org}/invitations/{invitation_id}/teams
   391  func (s *OrganizationsService) ListOrgInvitationTeams(ctx context.Context, org, invitationID string, opts *ListOptions) ([]*Team, *Response, error) {
   392  	u := fmt.Sprintf("orgs/%v/invitations/%v/teams", org, invitationID)
   393  	u, err := addOptions(u, opts)
   394  	if err != nil {
   395  		return nil, nil, err
   396  	}
   397  
   398  	req, err := s.client.NewRequest("GET", u, nil)
   399  	if err != nil {
   400  		return nil, nil, err
   401  	}
   402  
   403  	var orgInvitationTeams []*Team
   404  	resp, err := s.client.Do(ctx, req, &orgInvitationTeams)
   405  	if err != nil {
   406  		return nil, resp, err
   407  	}
   408  
   409  	return orgInvitationTeams, resp, nil
   410  }
   411  
   412  // ListFailedOrgInvitations returns a list of failed invitations.
   413  //
   414  // GitHub API docs: https://docs.github.com/rest/orgs/members#list-failed-organization-invitations
   415  //
   416  //meta:operation GET /orgs/{org}/failed_invitations
   417  func (s *OrganizationsService) ListFailedOrgInvitations(ctx context.Context, org string, opts *ListOptions) ([]*Invitation, *Response, error) {
   418  	u := fmt.Sprintf("orgs/%v/failed_invitations", org)
   419  	u, err := addOptions(u, opts)
   420  	if err != nil {
   421  		return nil, nil, err
   422  	}
   423  
   424  	req, err := s.client.NewRequest("GET", u, nil)
   425  	if err != nil {
   426  		return nil, nil, err
   427  	}
   428  
   429  	var failedInvitations []*Invitation
   430  	resp, err := s.client.Do(ctx, req, &failedInvitations)
   431  	if err != nil {
   432  		return nil, resp, err
   433  	}
   434  
   435  	return failedInvitations, resp, nil
   436  }