github.com/google/go-github/v49@v49.1.0/github/orgs_users_blocking.go (about) 1 // Copyright 2017 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 // ListBlockedUsers lists all the users blocked by an organization. 14 // 15 // GitHub API docs: https://docs.github.com/en/rest/orgs/blocking#list-users-blocked-by-an-organization 16 func (s *OrganizationsService) ListBlockedUsers(ctx context.Context, org string, opts *ListOptions) ([]*User, *Response, error) { 17 u := fmt.Sprintf("orgs/%v/blocks", org) 18 u, err := addOptions(u, opts) 19 if err != nil { 20 return nil, nil, err 21 } 22 23 req, err := s.client.NewRequest("GET", u, nil) 24 if err != nil { 25 return nil, nil, err 26 } 27 28 // TODO: remove custom Accept header when this API fully launches. 29 req.Header.Set("Accept", mediaTypeBlockUsersPreview) 30 31 var blockedUsers []*User 32 resp, err := s.client.Do(ctx, req, &blockedUsers) 33 if err != nil { 34 return nil, resp, err 35 } 36 37 return blockedUsers, resp, nil 38 } 39 40 // IsBlocked reports whether specified user is blocked from an organization. 41 // 42 // GitHub API docs: https://docs.github.com/en/rest/orgs/blocking#check-if-a-user-is-blocked-by-an-organization 43 func (s *OrganizationsService) IsBlocked(ctx context.Context, org string, user string) (bool, *Response, error) { 44 u := fmt.Sprintf("orgs/%v/blocks/%v", org, user) 45 46 req, err := s.client.NewRequest("GET", u, nil) 47 if err != nil { 48 return false, nil, err 49 } 50 51 // TODO: remove custom Accept header when this API fully launches. 52 req.Header.Set("Accept", mediaTypeBlockUsersPreview) 53 54 resp, err := s.client.Do(ctx, req, nil) 55 isBlocked, err := parseBoolResponse(err) 56 return isBlocked, resp, err 57 } 58 59 // BlockUser blocks specified user from an organization. 60 // 61 // GitHub API docs: https://docs.github.com/en/rest/orgs/blocking#block-a-user-from-an-organization 62 func (s *OrganizationsService) BlockUser(ctx context.Context, org string, user string) (*Response, error) { 63 u := fmt.Sprintf("orgs/%v/blocks/%v", org, user) 64 65 req, err := s.client.NewRequest("PUT", u, nil) 66 if err != nil { 67 return nil, err 68 } 69 70 // TODO: remove custom Accept header when this API fully launches. 71 req.Header.Set("Accept", mediaTypeBlockUsersPreview) 72 73 return s.client.Do(ctx, req, nil) 74 } 75 76 // UnblockUser unblocks specified user from an organization. 77 // 78 // GitHub API docs: https://docs.github.com/en/rest/orgs/blocking#unblock-a-user-from-an-organization 79 func (s *OrganizationsService) UnblockUser(ctx context.Context, org string, user string) (*Response, error) { 80 u := fmt.Sprintf("orgs/%v/blocks/%v", org, user) 81 82 req, err := s.client.NewRequest("DELETE", u, nil) 83 if err != nil { 84 return nil, err 85 } 86 87 // TODO: remove custom Accept header when this API fully launches. 88 req.Header.Set("Accept", mediaTypeBlockUsersPreview) 89 90 return s.client.Do(ctx, req, nil) 91 }