|
| 1 | +// Copyright 2023 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 | +// ListOrganizations represents the response from the list orgs endpoints. |
| 14 | +type ListOrganizations struct { |
| 15 | + TotalCount *int `json:"total_count,omitempty"` |
| 16 | + Organizations []*Organization `json:"organizations"` |
| 17 | +} |
| 18 | + |
| 19 | +// EnterpriseRunnerGroup represents a self-hosted runner group configured in an enterprise. |
| 20 | +type EnterpriseRunnerGroup struct { |
| 21 | + ID *int64 `json:"id,omitempty"` |
| 22 | + Name *string `json:"name,omitempty"` |
| 23 | + Visibility *string `json:"visibility,omitempty"` |
| 24 | + Default *bool `json:"default,omitempty"` |
| 25 | + SelectedOrganizationsURL *string `json:"selected_organizations_url,omitempty"` |
| 26 | + RunnersURL *string `json:"runners_url,omitempty"` |
| 27 | + Inherited *bool `json:"inherited,omitempty"` |
| 28 | + AllowsPublicRepositories *bool `json:"allows_public_repositories,omitempty"` |
| 29 | + RestrictedToWorkflows *bool `json:"restricted_to_workflows,omitempty"` |
| 30 | + SelectedWorkflows []string `json:"selected_workflows,omitempty"` |
| 31 | + WorkflowRestrictionsReadOnly *bool `json:"workflow_restrictions_read_only,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// EnterpriseRunnerGroups represents a collection of self-hosted runner groups configured for an enterprise. |
| 35 | +type EnterpriseRunnerGroups struct { |
| 36 | + TotalCount *int `json:"total_count,omitempty"` |
| 37 | + RunnerGroups []*EnterpriseRunnerGroup `json:"runner_groups"` |
| 38 | +} |
| 39 | + |
| 40 | +// CreateEnterpriseRunnerGroupRequest represents a request to create a Runner group for an enterprise. |
| 41 | +type CreateEnterpriseRunnerGroupRequest struct { |
| 42 | + Name *string `json:"name,omitempty"` |
| 43 | + Visibility *string `json:"visibility,omitempty"` |
| 44 | + // List of organization IDs that can access the runner group. |
| 45 | + SelectedOrganizationIDs []int64 `json:"selected_organization_ids,omitempty"` |
| 46 | + // Runners represent a list of runner IDs to add to the runner group. |
| 47 | + Runners []int64 `json:"runners,omitempty"` |
| 48 | + // If set to True, public repos can use this runner group |
| 49 | + AllowsPublicRepositories *bool `json:"allows_public_repositories,omitempty"` |
| 50 | + // If true, the runner group will be restricted to running only the workflows specified in the SelectedWorkflows slice. |
| 51 | + RestrictedToWorkflows *bool `json:"restricted_to_workflows,omitempty"` |
| 52 | + // List of workflows the runner group should be allowed to run. This setting will be ignored unless RestrictedToWorkflows is set to true. |
| 53 | + SelectedWorkflows []string `json:"selected_workflows,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// UpdateEnterpriseRunnerGroupRequest represents a request to update a Runner group for an enterprise. |
| 57 | +type UpdateEnterpriseRunnerGroupRequest struct { |
| 58 | + Name *string `json:"name,omitempty"` |
| 59 | + Visibility *string `json:"visibility,omitempty"` |
| 60 | + AllowsPublicRepositories *bool `json:"allows_public_repositories,omitempty"` |
| 61 | + RestrictedToWorkflows *bool `json:"restricted_to_workflows,omitempty"` |
| 62 | + SelectedWorkflows []string `json:"selected_workflows,omitempty"` |
| 63 | +} |
| 64 | + |
| 65 | +// SetOrgAccessRunnerGroupRequest represents a request to replace the list of organizations |
| 66 | +// that can access a self-hosted runner group configured in an enterprise. |
| 67 | +type SetOrgAccessRunnerGroupRequest struct { |
| 68 | + // Updated list of organization IDs that should be given access to the runner group. |
| 69 | + SelectedOrganizationIDs []int64 `json:"selected_organization_ids"` |
| 70 | +} |
| 71 | + |
| 72 | +// ListEnterpriseRunnerGroupOptions extend ListOptions to have the optional parameters VisibleToOrganization. |
| 73 | +type ListEnterpriseRunnerGroupOptions struct { |
| 74 | + ListOptions |
| 75 | + |
| 76 | + // Only return runner groups that are allowed to be used by this organization. |
| 77 | + VisibleToOrganization string `url:"visible_to_organization,omitempty"` |
| 78 | +} |
| 79 | + |
| 80 | +// ListRunnerGroups lists all self-hosted runner groups configured in an enterprise. |
| 81 | +// |
| 82 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#list-self-hosted-runner-groups-for-an-enterprise |
| 83 | +func (s *EnterpriseService) ListRunnerGroups(ctx context.Context, enterprise string, opts *ListEnterpriseRunnerGroupOptions) (*EnterpriseRunnerGroups, *Response, error) { |
| 84 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups", enterprise) |
| 85 | + u, err := addOptions(u, opts) |
| 86 | + if err != nil { |
| 87 | + return nil, nil, err |
| 88 | + } |
| 89 | + |
| 90 | + req, err := s.client.NewRequest("GET", u, nil) |
| 91 | + if err != nil { |
| 92 | + return nil, nil, err |
| 93 | + } |
| 94 | + |
| 95 | + groups := &EnterpriseRunnerGroups{} |
| 96 | + resp, err := s.client.Do(ctx, req, &groups) |
| 97 | + if err != nil { |
| 98 | + return nil, resp, err |
| 99 | + } |
| 100 | + |
| 101 | + return groups, resp, nil |
| 102 | +} |
| 103 | + |
| 104 | +// GetEnterpriseRunnerGroup gets a specific self-hosted runner group for an enterprise using its RunnerGroup ID. |
| 105 | +// |
| 106 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#get-a-self-hosted-runner-group-for-an-enterprise |
| 107 | +func (s *EnterpriseService) GetEnterpriseRunnerGroup(ctx context.Context, enterprise string, groupID int64) (*EnterpriseRunnerGroup, *Response, error) { |
| 108 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v", enterprise, groupID) |
| 109 | + req, err := s.client.NewRequest("GET", u, nil) |
| 110 | + if err != nil { |
| 111 | + return nil, nil, err |
| 112 | + } |
| 113 | + |
| 114 | + runnerGroup := new(EnterpriseRunnerGroup) |
| 115 | + resp, err := s.client.Do(ctx, req, runnerGroup) |
| 116 | + if err != nil { |
| 117 | + return nil, resp, err |
| 118 | + } |
| 119 | + |
| 120 | + return runnerGroup, resp, nil |
| 121 | +} |
| 122 | + |
| 123 | +// DeleteEnterpriseRunnerGroup deletes a self-hosted runner group from an enterprise. |
| 124 | +// |
| 125 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#delete-a-self-hosted-runner-group-from-an-enterprise |
| 126 | +func (s *EnterpriseService) DeleteEnterpriseRunnerGroup(ctx context.Context, enterprise string, groupID int64) (*Response, error) { |
| 127 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v", enterprise, groupID) |
| 128 | + |
| 129 | + req, err := s.client.NewRequest("DELETE", u, nil) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + return s.client.Do(ctx, req, nil) |
| 135 | +} |
| 136 | + |
| 137 | +// CreateEnterpriseRunnerGroup creates a new self-hosted runner group for an enterprise. |
| 138 | +// |
| 139 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#create-a-self-hosted-runner-group-for-an-enterprise |
| 140 | +func (s *EnterpriseService) CreateEnterpriseRunnerGroup(ctx context.Context, enterprise string, createReq CreateEnterpriseRunnerGroupRequest) (*EnterpriseRunnerGroup, *Response, error) { |
| 141 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups", enterprise) |
| 142 | + req, err := s.client.NewRequest("POST", u, createReq) |
| 143 | + if err != nil { |
| 144 | + return nil, nil, err |
| 145 | + } |
| 146 | + |
| 147 | + runnerGroup := new(EnterpriseRunnerGroup) |
| 148 | + resp, err := s.client.Do(ctx, req, runnerGroup) |
| 149 | + if err != nil { |
| 150 | + return nil, resp, err |
| 151 | + } |
| 152 | + |
| 153 | + return runnerGroup, resp, nil |
| 154 | +} |
| 155 | + |
| 156 | +// UpdateEnterpriseRunnerGroup updates a self-hosted runner group for an enterprise. |
| 157 | +// |
| 158 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#update-a-self-hosted-runner-group-for-an-enterprise |
| 159 | +func (s *EnterpriseService) UpdateEnterpriseRunnerGroup(ctx context.Context, enterprise string, groupID int64, updateReq UpdateEnterpriseRunnerGroupRequest) (*EnterpriseRunnerGroup, *Response, error) { |
| 160 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v", enterprise, groupID) |
| 161 | + req, err := s.client.NewRequest("PATCH", u, updateReq) |
| 162 | + if err != nil { |
| 163 | + return nil, nil, err |
| 164 | + } |
| 165 | + |
| 166 | + runnerGroup := new(EnterpriseRunnerGroup) |
| 167 | + resp, err := s.client.Do(ctx, req, runnerGroup) |
| 168 | + if err != nil { |
| 169 | + return nil, resp, err |
| 170 | + } |
| 171 | + |
| 172 | + return runnerGroup, resp, nil |
| 173 | +} |
| 174 | + |
| 175 | +// ListOrganizationAccessRunnerGroup lists the organizations with access to a self-hosted runner group configured in an enterprise. |
| 176 | +// |
| 177 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#list-organization-access-to-a-self-hosted-runner-group-in-an-enterprise |
| 178 | +func (s *EnterpriseService) ListOrganizationAccessRunnerGroup(ctx context.Context, enterprise string, groupID int64, opts *ListOptions) (*ListOrganizations, *Response, error) { |
| 179 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/organizations", enterprise, groupID) |
| 180 | + u, err := addOptions(u, opts) |
| 181 | + if err != nil { |
| 182 | + return nil, nil, err |
| 183 | + } |
| 184 | + |
| 185 | + req, err := s.client.NewRequest("GET", u, nil) |
| 186 | + if err != nil { |
| 187 | + return nil, nil, err |
| 188 | + } |
| 189 | + |
| 190 | + orgs := &ListOrganizations{} |
| 191 | + resp, err := s.client.Do(ctx, req, &orgs) |
| 192 | + if err != nil { |
| 193 | + return nil, resp, err |
| 194 | + } |
| 195 | + |
| 196 | + return orgs, resp, nil |
| 197 | +} |
| 198 | + |
| 199 | +// SetOrganizationAccessRunnerGroup replaces the list of organizations that have access to a self-hosted runner group configured in an enterprise |
| 200 | +// with a new List of organizations. |
| 201 | +// |
| 202 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#set-organization-access-for-a-self-hosted-runner-group-in-an-enterprise |
| 203 | +func (s *EnterpriseService) SetOrganizationAccessRunnerGroup(ctx context.Context, enterprise string, groupID int64, ids SetOrgAccessRunnerGroupRequest) (*Response, error) { |
| 204 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/organizations", enterprise, groupID) |
| 205 | + |
| 206 | + req, err := s.client.NewRequest("PUT", u, ids) |
| 207 | + if err != nil { |
| 208 | + return nil, err |
| 209 | + } |
| 210 | + |
| 211 | + return s.client.Do(ctx, req, nil) |
| 212 | +} |
| 213 | + |
| 214 | +// AddOrganizationAccessRunnerGroup adds an organization to the list of selected organizations that can access a self-hosted runner group. |
| 215 | +// The runner group must have visibility set to 'selected'. |
| 216 | +// |
| 217 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#add-organization-access-to-a-self-hosted-runner-group-in-an-enterprise |
| 218 | +func (s *EnterpriseService) AddOrganizationAccessRunnerGroup(ctx context.Context, enterprise string, groupID, orgID int64) (*Response, error) { |
| 219 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/organizations/%v", enterprise, groupID, orgID) |
| 220 | + |
| 221 | + req, err := s.client.NewRequest("PUT", u, nil) |
| 222 | + if err != nil { |
| 223 | + return nil, err |
| 224 | + } |
| 225 | + |
| 226 | + return s.client.Do(ctx, req, nil) |
| 227 | +} |
| 228 | + |
| 229 | +// RemoveOrganizationAccessRunnerGroup removes an organization from the list of selected organizations that can access a self-hosted runner group. |
| 230 | +// The runner group must have visibility set to 'selected'. |
| 231 | +// |
| 232 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#remove-organization-access-to-a-self-hosted-runner-group-in-an-enterprise |
| 233 | +func (s *EnterpriseService) RemoveOrganizationAccessRunnerGroup(ctx context.Context, enterprise string, groupID, orgID int64) (*Response, error) { |
| 234 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/organizations/%v", enterprise, groupID, orgID) |
| 235 | + |
| 236 | + req, err := s.client.NewRequest("DELETE", u, nil) |
| 237 | + if err != nil { |
| 238 | + return nil, err |
| 239 | + } |
| 240 | + |
| 241 | + return s.client.Do(ctx, req, nil) |
| 242 | +} |
| 243 | + |
| 244 | +// ListRunnerGroupRunners lists self-hosted runners that are in a specific enterprise group. |
| 245 | +// |
| 246 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#list-self-hosted-runners-in-a-group-for-an-enterprise |
| 247 | +func (s *EnterpriseService) ListRunnerGroupRunners(ctx context.Context, enterprise string, groupID int64, opts *ListOptions) (*Runners, *Response, error) { |
| 248 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/runners", enterprise, groupID) |
| 249 | + u, err := addOptions(u, opts) |
| 250 | + if err != nil { |
| 251 | + return nil, nil, err |
| 252 | + } |
| 253 | + |
| 254 | + req, err := s.client.NewRequest("GET", u, nil) |
| 255 | + if err != nil { |
| 256 | + return nil, nil, err |
| 257 | + } |
| 258 | + |
| 259 | + runners := &Runners{} |
| 260 | + resp, err := s.client.Do(ctx, req, &runners) |
| 261 | + if err != nil { |
| 262 | + return nil, resp, err |
| 263 | + } |
| 264 | + |
| 265 | + return runners, resp, nil |
| 266 | +} |
| 267 | + |
| 268 | +// SetRunnerGroupRunners replaces the list of self-hosted runners that are part of an enterprise runner group |
| 269 | +// with a new list of runners. |
| 270 | +// |
| 271 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#set-self-hosted-runners-in-a-group-for-an-enterprise |
| 272 | +func (s *EnterpriseService) SetRunnerGroupRunners(ctx context.Context, enterprise string, groupID int64, ids SetRunnerGroupRunnersRequest) (*Response, error) { |
| 273 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/runners", enterprise, groupID) |
| 274 | + |
| 275 | + req, err := s.client.NewRequest("PUT", u, ids) |
| 276 | + if err != nil { |
| 277 | + return nil, err |
| 278 | + } |
| 279 | + |
| 280 | + return s.client.Do(ctx, req, nil) |
| 281 | +} |
| 282 | + |
| 283 | +// AddRunnerGroupRunners adds a self-hosted runner to a runner group configured in an enterprise. |
| 284 | +// |
| 285 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#add-a-self-hosted-runner-to-a-group-for-an-enterprise |
| 286 | +func (s *EnterpriseService) AddRunnerGroupRunners(ctx context.Context, enterprise string, groupID, runnerID int64) (*Response, error) { |
| 287 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/runners/%v", enterprise, groupID, runnerID) |
| 288 | + |
| 289 | + req, err := s.client.NewRequest("PUT", u, nil) |
| 290 | + if err != nil { |
| 291 | + return nil, err |
| 292 | + } |
| 293 | + |
| 294 | + return s.client.Do(ctx, req, nil) |
| 295 | +} |
| 296 | + |
| 297 | +// RemoveRunnerGroupRunners removes a self-hosted runner from a group configured in an enterprise. |
| 298 | +// The runner is then returned to the default group. |
| 299 | +// |
| 300 | +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runner-groups?apiVersion=2022-11-28#remove-a-self-hosted-runner-from-a-group-for-an-enterprise |
| 301 | +func (s *EnterpriseService) RemoveRunnerGroupRunners(ctx context.Context, enterprise string, groupID, runnerID int64) (*Response, error) { |
| 302 | + u := fmt.Sprintf("enterprises/%v/actions/runner-groups/%v/runners/%v", enterprise, groupID, runnerID) |
| 303 | + |
| 304 | + req, err := s.client.NewRequest("DELETE", u, nil) |
| 305 | + if err != nil { |
| 306 | + return nil, err |
| 307 | + } |
| 308 | + |
| 309 | + return s.client.Do(ctx, req, nil) |
| 310 | +} |
0 commit comments