github.com/google/go-github/v53@v53.2.0/github/actions_runners.go (about) 1 // Copyright 2020 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 // RunnerApplicationDownload represents a binary for the self-hosted runner application that can be downloaded. 14 type RunnerApplicationDownload struct { 15 OS *string `json:"os,omitempty"` 16 Architecture *string `json:"architecture,omitempty"` 17 DownloadURL *string `json:"download_url,omitempty"` 18 Filename *string `json:"filename,omitempty"` 19 TempDownloadToken *string `json:"temp_download_token,omitempty"` 20 SHA256Checksum *string `json:"sha256_checksum,omitempty"` 21 } 22 23 // ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled. 24 type ActionsEnabledOnOrgRepos struct { 25 TotalCount int `json:"total_count"` 26 Repositories []*Repository `json:"repositories"` 27 } 28 29 // ListRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run. 30 // 31 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-runner-applications-for-a-repository 32 func (s *ActionsService) ListRunnerApplicationDownloads(ctx context.Context, owner, repo string) ([]*RunnerApplicationDownload, *Response, error) { 33 u := fmt.Sprintf("repos/%v/%v/actions/runners/downloads", owner, repo) 34 req, err := s.client.NewRequest("GET", u, nil) 35 if err != nil { 36 return nil, nil, err 37 } 38 39 var rads []*RunnerApplicationDownload 40 resp, err := s.client.Do(ctx, req, &rads) 41 if err != nil { 42 return nil, resp, err 43 } 44 45 return rads, resp, nil 46 } 47 48 // GenerateJITConfigRequest specifies body parameters to GenerateRepoJITConfig. 49 type GenerateJITConfigRequest struct { 50 Name string `json:"name"` 51 RunnerGroupID int64 `json:"runner_group_id"` 52 WorkFolder *string `json:"work_folder,omitempty"` 53 54 // Labels represents the names of the custom labels to add to the runner. 55 // Minimum items: 1. Maximum items: 100. 56 Labels []string `json:"labels"` 57 } 58 59 // JITRunnerConfig represents encoded JIT configuration that can be used to bootstrap a self-hosted runner. 60 type JITRunnerConfig struct { 61 EncodedJITConfig *string `json:"encoded_jit_config,omitempty"` 62 } 63 64 // GenerateOrgJITConfig generate a just-in-time configuration for an organization. 65 // 66 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-configuration-for-a-just-in-time-runner-for-an-organization 67 func (s *ActionsService) GenerateOrgJITConfig(ctx context.Context, owner string, request *GenerateJITConfigRequest) (*JITRunnerConfig, *Response, error) { 68 u := fmt.Sprintf("orgs/%v/actions/runners/generate-jitconfig", owner) 69 req, err := s.client.NewRequest("POST", u, request) 70 if err != nil { 71 return nil, nil, err 72 } 73 74 jitConfig := new(JITRunnerConfig) 75 resp, err := s.client.Do(ctx, req, jitConfig) 76 if err != nil { 77 return nil, resp, err 78 } 79 80 return jitConfig, resp, nil 81 } 82 83 // GenerateRepoJITConfig generates a just-in-time configuration for a repository. 84 // 85 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-configuration-for-a-just-in-time-runner-for-a-repository 86 func (s *ActionsService) GenerateRepoJITConfig(ctx context.Context, owner, repo string, request *GenerateJITConfigRequest) (*JITRunnerConfig, *Response, error) { 87 u := fmt.Sprintf("repos/%v/%v/actions/runners/generate-jitconfig", owner, repo) 88 req, err := s.client.NewRequest("POST", u, request) 89 if err != nil { 90 return nil, nil, err 91 } 92 93 jitConfig := new(JITRunnerConfig) 94 resp, err := s.client.Do(ctx, req, jitConfig) 95 if err != nil { 96 return nil, resp, err 97 } 98 99 return jitConfig, resp, nil 100 } 101 102 // RegistrationToken represents a token that can be used to add a self-hosted runner to a repository. 103 type RegistrationToken struct { 104 Token *string `json:"token,omitempty"` 105 ExpiresAt *Timestamp `json:"expires_at,omitempty"` 106 } 107 108 // CreateRegistrationToken creates a token that can be used to add a self-hosted runner. 109 // 110 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-a-repository 111 func (s *ActionsService) CreateRegistrationToken(ctx context.Context, owner, repo string) (*RegistrationToken, *Response, error) { 112 u := fmt.Sprintf("repos/%v/%v/actions/runners/registration-token", owner, repo) 113 114 req, err := s.client.NewRequest("POST", u, nil) 115 if err != nil { 116 return nil, nil, err 117 } 118 119 registrationToken := new(RegistrationToken) 120 resp, err := s.client.Do(ctx, req, registrationToken) 121 if err != nil { 122 return nil, resp, err 123 } 124 125 return registrationToken, resp, nil 126 } 127 128 // Runner represents a self-hosted runner registered with a repository. 129 type Runner struct { 130 ID *int64 `json:"id,omitempty"` 131 Name *string `json:"name,omitempty"` 132 OS *string `json:"os,omitempty"` 133 Status *string `json:"status,omitempty"` 134 Busy *bool `json:"busy,omitempty"` 135 Labels []*RunnerLabels `json:"labels,omitempty"` 136 } 137 138 // RunnerLabels represents a collection of labels attached to each runner. 139 type RunnerLabels struct { 140 ID *int64 `json:"id,omitempty"` 141 Name *string `json:"name,omitempty"` 142 Type *string `json:"type,omitempty"` 143 } 144 145 // Runners represents a collection of self-hosted runners for a repository. 146 type Runners struct { 147 TotalCount int `json:"total_count"` 148 Runners []*Runner `json:"runners"` 149 } 150 151 // ListRunners lists all the self-hosted runners for a repository. 152 // 153 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-self-hosted-runners-for-a-repository 154 func (s *ActionsService) ListRunners(ctx context.Context, owner, repo string, opts *ListOptions) (*Runners, *Response, error) { 155 u := fmt.Sprintf("repos/%v/%v/actions/runners", owner, repo) 156 u, err := addOptions(u, opts) 157 if err != nil { 158 return nil, nil, err 159 } 160 161 req, err := s.client.NewRequest("GET", u, nil) 162 if err != nil { 163 return nil, nil, err 164 } 165 166 runners := &Runners{} 167 resp, err := s.client.Do(ctx, req, &runners) 168 if err != nil { 169 return nil, resp, err 170 } 171 172 return runners, resp, nil 173 } 174 175 // GetRunner gets a specific self-hosted runner for a repository using its runner ID. 176 // 177 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#get-a-self-hosted-runner-for-a-repository 178 func (s *ActionsService) GetRunner(ctx context.Context, owner, repo string, runnerID int64) (*Runner, *Response, error) { 179 u := fmt.Sprintf("repos/%v/%v/actions/runners/%v", owner, repo, runnerID) 180 req, err := s.client.NewRequest("GET", u, nil) 181 if err != nil { 182 return nil, nil, err 183 } 184 185 runner := new(Runner) 186 resp, err := s.client.Do(ctx, req, runner) 187 if err != nil { 188 return nil, resp, err 189 } 190 191 return runner, resp, nil 192 } 193 194 // RemoveToken represents a token that can be used to remove a self-hosted runner from a repository. 195 type RemoveToken struct { 196 Token *string `json:"token,omitempty"` 197 ExpiresAt *Timestamp `json:"expires_at,omitempty"` 198 } 199 200 // CreateRemoveToken creates a token that can be used to remove a self-hosted runner from a repository. 201 // 202 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-remove-token-for-a-repository 203 func (s *ActionsService) CreateRemoveToken(ctx context.Context, owner, repo string) (*RemoveToken, *Response, error) { 204 u := fmt.Sprintf("repos/%v/%v/actions/runners/remove-token", owner, repo) 205 206 req, err := s.client.NewRequest("POST", u, nil) 207 if err != nil { 208 return nil, nil, err 209 } 210 211 removeToken := new(RemoveToken) 212 resp, err := s.client.Do(ctx, req, removeToken) 213 if err != nil { 214 return nil, resp, err 215 } 216 217 return removeToken, resp, nil 218 } 219 220 // RemoveRunner forces the removal of a self-hosted runner in a repository using the runner id. 221 // 222 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#delete-a-self-hosted-runner-from-a-repository 223 func (s *ActionsService) RemoveRunner(ctx context.Context, owner, repo string, runnerID int64) (*Response, error) { 224 u := fmt.Sprintf("repos/%v/%v/actions/runners/%v", owner, repo, runnerID) 225 226 req, err := s.client.NewRequest("DELETE", u, nil) 227 if err != nil { 228 return nil, err 229 } 230 231 return s.client.Do(ctx, req, nil) 232 } 233 234 // ListOrganizationRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run. 235 // 236 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-runner-applications-for-an-organization 237 func (s *ActionsService) ListOrganizationRunnerApplicationDownloads(ctx context.Context, owner string) ([]*RunnerApplicationDownload, *Response, error) { 238 u := fmt.Sprintf("orgs/%v/actions/runners/downloads", owner) 239 req, err := s.client.NewRequest("GET", u, nil) 240 if err != nil { 241 return nil, nil, err 242 } 243 244 var rads []*RunnerApplicationDownload 245 resp, err := s.client.Do(ctx, req, &rads) 246 if err != nil { 247 return nil, resp, err 248 } 249 250 return rads, resp, nil 251 } 252 253 // CreateOrganizationRegistrationToken creates a token that can be used to add a self-hosted runner to an organization. 254 // 255 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-an-organization 256 func (s *ActionsService) CreateOrganizationRegistrationToken(ctx context.Context, owner string) (*RegistrationToken, *Response, error) { 257 u := fmt.Sprintf("orgs/%v/actions/runners/registration-token", owner) 258 259 req, err := s.client.NewRequest("POST", u, nil) 260 if err != nil { 261 return nil, nil, err 262 } 263 264 registrationToken := new(RegistrationToken) 265 resp, err := s.client.Do(ctx, req, registrationToken) 266 if err != nil { 267 return nil, resp, err 268 } 269 270 return registrationToken, resp, nil 271 } 272 273 // ListOrganizationRunners lists all the self-hosted runners for an organization. 274 // 275 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-self-hosted-runners-for-an-organization 276 func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner string, opts *ListOptions) (*Runners, *Response, error) { 277 u := fmt.Sprintf("orgs/%v/actions/runners", owner) 278 u, err := addOptions(u, opts) 279 if err != nil { 280 return nil, nil, err 281 } 282 283 req, err := s.client.NewRequest("GET", u, nil) 284 if err != nil { 285 return nil, nil, err 286 } 287 288 runners := &Runners{} 289 resp, err := s.client.Do(ctx, req, &runners) 290 if err != nil { 291 return nil, resp, err 292 } 293 294 return runners, resp, nil 295 } 296 297 // ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization. 298 // 299 // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-repositories-enabled-for-github-actions-in-an-organization 300 func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) { 301 u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) 302 u, err := addOptions(u, opts) 303 if err != nil { 304 return nil, nil, err 305 } 306 307 req, err := s.client.NewRequest("GET", u, nil) 308 if err != nil { 309 return nil, nil, err 310 } 311 312 repos := &ActionsEnabledOnOrgRepos{} 313 resp, err := s.client.Do(ctx, req, repos) 314 if err != nil { 315 return nil, resp, err 316 } 317 318 return repos, resp, nil 319 } 320 321 // SetEnabledReposInOrg replaces the list of selected repositories that are enabled for GitHub Actions in an organization.. 322 // 323 // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-selected-repositories-enabled-for-github-actions-in-an-organization 324 func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) { 325 u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) 326 327 req, err := s.client.NewRequest("PUT", u, struct { 328 IDs []int64 `json:"selected_repository_ids"` 329 }{IDs: repositoryIDs}) 330 if err != nil { 331 return nil, err 332 } 333 334 resp, err := s.client.Do(ctx, req, nil) 335 if err != nil { 336 return resp, err 337 } 338 339 return resp, nil 340 } 341 342 // AddEnabledReposInOrg adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization. 343 // 344 // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#enable-a-selected-repository-for-github-actions-in-an-organization 345 func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { 346 u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) 347 348 req, err := s.client.NewRequest("PUT", u, nil) 349 if err != nil { 350 return nil, err 351 } 352 353 resp, err := s.client.Do(ctx, req, nil) 354 if err != nil { 355 return resp, err 356 } 357 358 return resp, nil 359 } 360 361 // RemoveEnabledRepoInOrg removes a single repository from the list of enabled repos for GitHub Actions in an organization. 362 // 363 // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#disable-a-selected-repository-for-github-actions-in-an-organization 364 func (s *ActionsService) RemoveEnabledRepoInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { 365 u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) 366 367 req, err := s.client.NewRequest("DELETE", u, nil) 368 if err != nil { 369 return nil, err 370 } 371 372 resp, err := s.client.Do(ctx, req, nil) 373 if err != nil { 374 return resp, err 375 } 376 377 return resp, nil 378 } 379 380 // GetOrganizationRunner gets a specific self-hosted runner for an organization using its runner ID. 381 // 382 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#get-a-self-hosted-runner-for-an-organization 383 func (s *ActionsService) GetOrganizationRunner(ctx context.Context, owner string, runnerID int64) (*Runner, *Response, error) { 384 u := fmt.Sprintf("orgs/%v/actions/runners/%v", owner, runnerID) 385 req, err := s.client.NewRequest("GET", u, nil) 386 if err != nil { 387 return nil, nil, err 388 } 389 390 runner := new(Runner) 391 resp, err := s.client.Do(ctx, req, runner) 392 if err != nil { 393 return nil, resp, err 394 } 395 396 return runner, resp, nil 397 } 398 399 // CreateOrganizationRemoveToken creates a token that can be used to remove a self-hosted runner from an organization. 400 // 401 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-remove-token-for-an-organization 402 func (s *ActionsService) CreateOrganizationRemoveToken(ctx context.Context, owner string) (*RemoveToken, *Response, error) { 403 u := fmt.Sprintf("orgs/%v/actions/runners/remove-token", owner) 404 405 req, err := s.client.NewRequest("POST", u, nil) 406 if err != nil { 407 return nil, nil, err 408 } 409 410 removeToken := new(RemoveToken) 411 resp, err := s.client.Do(ctx, req, removeToken) 412 if err != nil { 413 return nil, resp, err 414 } 415 416 return removeToken, resp, nil 417 } 418 419 // RemoveOrganizationRunner forces the removal of a self-hosted runner from an organization using the runner id. 420 // 421 // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#delete-a-self-hosted-runner-from-an-organization 422 func (s *ActionsService) RemoveOrganizationRunner(ctx context.Context, owner string, runnerID int64) (*Response, error) { 423 u := fmt.Sprintf("orgs/%v/actions/runners/%v", owner, runnerID) 424 425 req, err := s.client.NewRequest("DELETE", u, nil) 426 if err != nil { 427 return nil, err 428 } 429 430 return s.client.Do(ctx, req, nil) 431 }