Debian Codegraph @

github/orgs_users_blocking.go

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