2017-02-07 21:33:23 +00:00
|
|
|
|
// Copyright 2013 The go-github AUTHORS. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package github
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
|
|
// Membership represents the status of a user's membership in an organization or team.
|
|
|
|
|
type Membership struct {
|
|
|
|
|
URL *string `json:"url,omitempty"`
|
|
|
|
|
|
|
|
|
|
// State is the user's status within the organization or team.
|
|
|
|
|
// Possible values are: "active", "pending"
|
|
|
|
|
State *string `json:"state,omitempty"`
|
|
|
|
|
|
|
|
|
|
// Role identifies the user's role within the organization or team.
|
|
|
|
|
// Possible values for organization membership:
|
|
|
|
|
// member - non-owner organization member
|
|
|
|
|
// admin - organization owner
|
|
|
|
|
//
|
|
|
|
|
// Possible values for team membership are:
|
|
|
|
|
// member - a normal member of the team
|
|
|
|
|
// maintainer - a team maintainer. Able to add/remove other team
|
|
|
|
|
// members, promote other team members to team
|
|
|
|
|
// maintainer, and edit the team’s name and description
|
|
|
|
|
Role *string `json:"role,omitempty"`
|
|
|
|
|
|
|
|
|
|
// For organization membership, the API URL of the organization.
|
|
|
|
|
OrganizationURL *string `json:"organization_url,omitempty"`
|
|
|
|
|
|
|
|
|
|
// For organization membership, the organization the membership is for.
|
|
|
|
|
Organization *Organization `json:"organization,omitempty"`
|
|
|
|
|
|
|
|
|
|
// For organization membership, the user the membership is for.
|
|
|
|
|
User *User `json:"user,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m Membership) String() string {
|
|
|
|
|
return Stringify(m)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListMembersOptions specifies optional parameters to the
|
|
|
|
|
// OrganizationsService.ListMembers method.
|
|
|
|
|
type ListMembersOptions struct {
|
|
|
|
|
// If true (or if the authenticated user is not an owner of the
|
|
|
|
|
// organization), list only publicly visible members.
|
|
|
|
|
PublicOnly bool `url:"-"`
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// Filter members returned in the list. Possible values are:
|
|
|
|
|
// 2fa_disabled, all. Default is "all".
|
2017-02-07 21:33:23 +00:00
|
|
|
|
Filter string `url:"filter,omitempty"`
|
|
|
|
|
|
|
|
|
|
// Role filters members returned by their role in the organization.
|
|
|
|
|
// Possible values are:
|
|
|
|
|
// all - all members of the organization, regardless of role
|
|
|
|
|
// admin - organization owners
|
|
|
|
|
// member - non-organization members
|
|
|
|
|
//
|
|
|
|
|
// Default is "all".
|
|
|
|
|
Role string `url:"role,omitempty"`
|
|
|
|
|
|
|
|
|
|
ListOptions
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// ListMembers lists the members for an organization. If the authenticated
|
2017-02-07 21:33:23 +00:00
|
|
|
|
// user is an owner of the organization, this will return both concealed and
|
|
|
|
|
// public members, otherwise it will only return public members.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#members-list
|
|
|
|
|
func (s *OrganizationsService) ListMembers(ctx context.Context, org string, opt *ListMembersOptions) ([]*User, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
var u string
|
|
|
|
|
if opt != nil && opt.PublicOnly {
|
|
|
|
|
u = fmt.Sprintf("orgs/%v/public_members", org)
|
|
|
|
|
} else {
|
|
|
|
|
u = fmt.Sprintf("orgs/%v/members", org)
|
|
|
|
|
}
|
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
var members []*User
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &members)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return members, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsMember checks if a user is a member of an organization.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#check-membership
|
|
|
|
|
func (s *OrganizationsService) IsMember(ctx context.Context, org, user string) (bool, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/members/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
member, err := parseBoolResponse(err)
|
|
|
|
|
return member, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsPublicMember checks if a user is a public member of an organization.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#check-public-membership
|
|
|
|
|
func (s *OrganizationsService) IsPublicMember(ctx context.Context, org, user string) (bool, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
member, err := parseBoolResponse(err)
|
|
|
|
|
return member, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveMember removes a user from all teams of an organization.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-a-member
|
|
|
|
|
func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/members/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PublicizeMembership publicizes a user's membership in an organization. (A
|
|
|
|
|
// user cannot publicize the membership for another user.)
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#publicize-a-users-membership
|
|
|
|
|
func (s *OrganizationsService) PublicizeMembership(ctx context.Context, org, user string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("PUT", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConcealMembership conceals a user's membership in an organization.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#conceal-a-users-membership
|
|
|
|
|
func (s *OrganizationsService) ConcealMembership(ctx context.Context, org, user string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListOrgMembershipsOptions specifies optional parameters to the
|
|
|
|
|
// OrganizationsService.ListOrgMemberships method.
|
|
|
|
|
type ListOrgMembershipsOptions struct {
|
|
|
|
|
// Filter memberships to include only those with the specified state.
|
|
|
|
|
// Possible values are: "active", "pending".
|
|
|
|
|
State string `url:"state,omitempty"`
|
|
|
|
|
|
|
|
|
|
ListOptions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListOrgMemberships lists the organization memberships for the authenticated user.
|
|
|
|
|
//
|
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#list-your-organization-memberships
|
2017-04-11 15:10:46 +00:00
|
|
|
|
func (s *OrganizationsService) ListOrgMemberships(ctx context.Context, opt *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := "user/memberships/orgs"
|
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var memberships []*Membership
|
2017-04-11 15:10:46 +00:00
|
|
|
|
resp, err := s.client.Do(ctx, req, &memberships)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return memberships, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetOrgMembership gets the membership for a user in a specified organization.
|
|
|
|
|
// Passing an empty string for user will get the membership for the
|
|
|
|
|
// authenticated user.
|
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// GitHub API docs:
|
|
|
|
|
// https://developer.github.com/v3/orgs/members/#get-organization-membership
|
|
|
|
|
// https://developer.github.com/v3/orgs/members/#get-your-organization-membership
|
|
|
|
|
func (s *OrganizationsService) GetOrgMembership(ctx context.Context, user, org string) (*Membership, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
var u string
|
|
|
|
|
if user != "" {
|
|
|
|
|
u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
|
|
|
|
|
} else {
|
|
|
|
|
u = fmt.Sprintf("user/memberships/orgs/%v", org)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
membership := new(Membership)
|
2017-04-11 15:10:46 +00:00
|
|
|
|
resp, err := s.client.Do(ctx, req, membership)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return membership, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EditOrgMembership edits the membership for user in specified organization.
|
|
|
|
|
// Passing an empty string for user will edit the membership for the
|
|
|
|
|
// authenticated user.
|
|
|
|
|
//
|
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#add-or-update-organization-membership
|
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#edit-your-organization-membership
|
2017-04-11 15:10:46 +00:00
|
|
|
|
func (s *OrganizationsService) EditOrgMembership(ctx context.Context, user, org string, membership *Membership) (*Membership, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
var u, method string
|
|
|
|
|
if user != "" {
|
|
|
|
|
u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
|
|
|
|
|
method = "PUT"
|
|
|
|
|
} else {
|
|
|
|
|
u = fmt.Sprintf("user/memberships/orgs/%v", org)
|
|
|
|
|
method = "PATCH"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(method, u, membership)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := new(Membership)
|
2017-04-11 15:10:46 +00:00
|
|
|
|
resp, err := s.client.Do(ctx, req, m)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return m, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
// RemoveOrgMembership removes user from the specified organization. If the
|
2017-02-07 21:33:23 +00:00
|
|
|
|
// user has been invited to the organization, this will cancel their invitation.
|
|
|
|
|
//
|
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-organization-membership
|
2017-04-11 15:10:46 +00:00
|
|
|
|
func (s *OrganizationsService) RemoveOrgMembership(ctx context.Context, user, org string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
|
u := fmt.Sprintf("orgs/%v/memberships/%v", org, user)
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListPendingOrgInvitations returns a list of pending invitations.
|
|
|
|
|
//
|
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/orgs/members/#list-pending-organization-invitations
|
|
|
|
|
func (s *OrganizationsService) ListPendingOrgInvitations(ctx context.Context, org int, opt *ListOptions) ([]*Invitation, *Response, error) {
|
|
|
|
|
u := fmt.Sprintf("orgs/%v/invitations", org)
|
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pendingInvitations []*Invitation
|
|
|
|
|
resp, err := s.client.Do(ctx, req, &pendingInvitations)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, resp, err
|
|
|
|
|
}
|
|
|
|
|
return pendingInvitations, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
|
}
|