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