Skip to content

Commit c06e8a9

Browse files
feat: Add support for Codespaces organizations APIs (#3892)
1 parent c65cb55 commit c06e8a9

File tree

2 files changed

+414
-0
lines changed

2 files changed

+414
-0
lines changed

github/codespaces_orgs.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright 2025 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+
// CodespacesOrgAccessControlRequest represent request for SetOrgAccessControl.
14+
type CodespacesOrgAccessControlRequest struct {
15+
// Visibility represent which users can access codespaces in the organization.
16+
// Can be one of: disabled, selected_members, all_members, all_members_and_outside_collaborators.
17+
Visibility string `json:"visibility"`
18+
// SelectedUsernames represent the usernames of the organization members who should have access to codespaces in the organization.
19+
// Required when visibility is selected_members.
20+
SelectedUsernames []string `json:"selected_usernames,omitzero"`
21+
}
22+
23+
// ListInOrg lists the codespaces associated to a specified organization.
24+
//
25+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#list-codespaces-for-the-organization
26+
//
27+
//meta:operation GET /orgs/{org}/codespaces
28+
func (s *CodespacesService) ListInOrg(ctx context.Context, org string, opts *ListOptions) (*ListCodespaces, *Response, error) {
29+
u := fmt.Sprintf("orgs/%v/codespaces", org)
30+
u, err := addOptions(u, opts)
31+
if err != nil {
32+
return nil, nil, err
33+
}
34+
35+
req, err := s.client.NewRequest("GET", u, nil)
36+
if err != nil {
37+
return nil, nil, err
38+
}
39+
40+
var codespaces *ListCodespaces
41+
resp, err := s.client.Do(ctx, req, &codespaces)
42+
if err != nil {
43+
return nil, resp, err
44+
}
45+
46+
return codespaces, resp, nil
47+
}
48+
49+
// SetOrgAccessControl sets which users can access codespaces in an organization.
50+
//
51+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#manage-access-control-for-organization-codespaces
52+
//
53+
//meta:operation PUT /orgs/{org}/codespaces/access
54+
func (s *CodespacesService) SetOrgAccessControl(ctx context.Context, org string, request CodespacesOrgAccessControlRequest) (*Response, error) {
55+
u := fmt.Sprintf("orgs/%v/codespaces/access", org)
56+
req, err := s.client.NewRequest("PUT", u, request)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
resp, err := s.client.Do(ctx, req, nil)
62+
if err != nil {
63+
return resp, err
64+
}
65+
66+
return resp, nil
67+
}
68+
69+
// AddUsersToOrgAccess adds users to Codespaces access for an organization.
70+
//
71+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#add-users-to-codespaces-access-for-an-organization
72+
//
73+
//meta:operation POST /orgs/{org}/codespaces/access/selected_users
74+
func (s *CodespacesService) AddUsersToOrgAccess(ctx context.Context, org string, usernames []string) (*Response, error) {
75+
u := fmt.Sprintf("orgs/%v/codespaces/access/selected_users", org)
76+
req, err := s.client.NewRequest("POST", u, map[string][]string{"selected_usernames": usernames})
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
resp, err := s.client.Do(ctx, req, nil)
82+
if err != nil {
83+
return resp, err
84+
}
85+
86+
return resp, nil
87+
}
88+
89+
// RemoveUsersFromOrgAccess removes users from Codespaces access for an organization.
90+
//
91+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#remove-users-from-codespaces-access-for-an-organization
92+
//
93+
//meta:operation DELETE /orgs/{org}/codespaces/access/selected_users
94+
func (s *CodespacesService) RemoveUsersFromOrgAccess(ctx context.Context, org string, usernames []string) (*Response, error) {
95+
u := fmt.Sprintf("orgs/%v/codespaces/access/selected_users", org)
96+
req, err := s.client.NewRequest("DELETE", u, map[string][]string{"selected_usernames": usernames})
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
resp, err := s.client.Do(ctx, req, nil)
102+
if err != nil {
103+
return resp, err
104+
}
105+
106+
return resp, nil
107+
}
108+
109+
// ListUserCodespacesInOrg lists the codespaces that a member of an organization has for repositories in that organization.
110+
//
111+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#list-codespaces-for-a-user-in-organization
112+
//
113+
//meta:operation GET /orgs/{org}/members/{username}/codespaces
114+
func (s *CodespacesService) ListUserCodespacesInOrg(ctx context.Context, org, username string, opts *ListOptions) (*ListCodespaces, *Response, error) {
115+
u := fmt.Sprintf("orgs/%v/members/%v/codespaces", org, username)
116+
u, err := addOptions(u, opts)
117+
if err != nil {
118+
return nil, nil, err
119+
}
120+
121+
req, err := s.client.NewRequest("GET", u, nil)
122+
if err != nil {
123+
return nil, nil, err
124+
}
125+
126+
var codespaces *ListCodespaces
127+
resp, err := s.client.Do(ctx, req, &codespaces)
128+
if err != nil {
129+
return nil, resp, err
130+
}
131+
132+
return codespaces, resp, nil
133+
}
134+
135+
// DeleteUserCodespaceInOrg deletes a user's codespace from the organization.
136+
//
137+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#delete-a-codespace-from-the-organization
138+
//
139+
//meta:operation DELETE /orgs/{org}/members/{username}/codespaces/{codespace_name}
140+
func (s *CodespacesService) DeleteUserCodespaceInOrg(ctx context.Context, org, username, codespaceName string) (*Response, error) {
141+
u := fmt.Sprintf("orgs/%v/members/%v/codespaces/%v", org, username, codespaceName)
142+
req, err := s.client.NewRequest("DELETE", u, nil)
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
resp, err := s.client.Do(ctx, req, nil)
148+
if err != nil {
149+
return resp, err
150+
}
151+
152+
return resp, nil
153+
}
154+
155+
// StopUserCodespaceInOrg stops a codespace for an organization user.
156+
//
157+
// GitHub API docs: https://docs.github.com/rest/codespaces/organizations#stop-a-codespace-for-an-organization-user
158+
//
159+
//meta:operation POST /orgs/{org}/members/{username}/codespaces/{codespace_name}/stop
160+
func (s *CodespacesService) StopUserCodespaceInOrg(ctx context.Context, org, username, codespaceName string) (*Response, error) {
161+
u := fmt.Sprintf("orgs/%v/members/%v/codespaces/%v/stop", org, username, codespaceName)
162+
req, err := s.client.NewRequest("POST", u, nil)
163+
if err != nil {
164+
return nil, err
165+
}
166+
167+
resp, err := s.client.Do(ctx, req, nil)
168+
if err != nil {
169+
return resp, err
170+
}
171+
172+
return resp, nil
173+
}

0 commit comments

Comments
 (0)