...

Source file src/github.com/google/go-github/v55/github/actions_runners.go

Documentation: github.com/google/go-github/v55/github

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

View as plain text