orgs_users_blocking.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // ListBlockedUsers lists all the users blocked by an organization.
  11. //
  12. // GitHub API docs: https://developer.github.com/v3/orgs/blocking/#list-blocked-users
  13. func (s *OrganizationsService) ListBlockedUsers(ctx context.Context, org string, opt *ListOptions) ([]*User, *Response, error) {
  14. u := fmt.Sprintf("orgs/%v/blocks", org)
  15. u, err := addOptions(u, opt)
  16. if err != nil {
  17. return nil, nil, err
  18. }
  19. req, err := s.client.NewRequest("GET", u, nil)
  20. if err != nil {
  21. return nil, nil, err
  22. }
  23. // TODO: remove custom Accept header when this API fully launches.
  24. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  25. var blockedUsers []*User
  26. resp, err := s.client.Do(ctx, req, &blockedUsers)
  27. if err != nil {
  28. return nil, resp, err
  29. }
  30. return blockedUsers, resp, nil
  31. }
  32. // IsBlocked reports whether specified user is blocked from an organization.
  33. //
  34. // GitHub API docs: https://developer.github.com/v3/orgs/blocking/#check-whether-a-user-is-blocked-from-an-organization
  35. func (s *OrganizationsService) IsBlocked(ctx context.Context, org string, user string) (bool, *Response, error) {
  36. u := fmt.Sprintf("orgs/%v/blocks/%v", org, user)
  37. req, err := s.client.NewRequest("GET", u, nil)
  38. if err != nil {
  39. return false, nil, err
  40. }
  41. // TODO: remove custom Accept header when this API fully launches.
  42. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  43. resp, err := s.client.Do(ctx, req, nil)
  44. isBlocked, err := parseBoolResponse(err)
  45. return isBlocked, resp, err
  46. }
  47. // BlockUser blocks specified user from an organization.
  48. //
  49. // GitHub API docs: https://developer.github.com/v3/orgs/blocking/#block-a-user
  50. func (s *OrganizationsService) BlockUser(ctx context.Context, org string, user string) (*Response, error) {
  51. u := fmt.Sprintf("orgs/%v/blocks/%v", org, user)
  52. req, err := s.client.NewRequest("PUT", u, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. // TODO: remove custom Accept header when this API fully launches.
  57. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  58. return s.client.Do(ctx, req, nil)
  59. }
  60. // UnblockUser unblocks specified user from an organization.
  61. //
  62. // GitHub API docs: https://developer.github.com/v3/orgs/blocking/#unblock-a-user
  63. func (s *OrganizationsService) UnblockUser(ctx context.Context, org string, user string) (*Response, error) {
  64. u := fmt.Sprintf("orgs/%v/blocks/%v", org, user)
  65. req, err := s.client.NewRequest("DELETE", u, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // TODO: remove custom Accept header when this API fully launches.
  70. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  71. return s.client.Do(ctx, req, nil)
  72. }