...

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

Documentation: github.com/google/go-github/v33/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  }
    20  
    21  // ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled.
    22  type ActionsEnabledOnOrgRepos struct {
    23  	TotalCount   int           `json:"total_count"`
    24  	Repositories []*Repository `json:"repositories"`
    25  }
    26  
    27  // ListRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run.
    28  //
    29  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-runner-applications-for-a-repository
    30  func (s *ActionsService) ListRunnerApplicationDownloads(ctx context.Context, owner, repo string) ([]*RunnerApplicationDownload, *Response, error) {
    31  	u := fmt.Sprintf("repos/%v/%v/actions/runners/downloads", owner, repo)
    32  	req, err := s.client.NewRequest("GET", u, nil)
    33  	if err != nil {
    34  		return nil, nil, err
    35  	}
    36  
    37  	var rads []*RunnerApplicationDownload
    38  	resp, err := s.client.Do(ctx, req, &rads)
    39  	if err != nil {
    40  		return nil, resp, err
    41  	}
    42  
    43  	return rads, resp, nil
    44  }
    45  
    46  // RegistrationToken represents a token that can be used to add a self-hosted runner to a repository.
    47  type RegistrationToken struct {
    48  	Token     *string    `json:"token,omitempty"`
    49  	ExpiresAt *Timestamp `json:"expires_at,omitempty"`
    50  }
    51  
    52  // CreateRegistrationToken creates a token that can be used to add a self-hosted runner.
    53  //
    54  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#create-a-registration-token-for-a-repository
    55  func (s *ActionsService) CreateRegistrationToken(ctx context.Context, owner, repo string) (*RegistrationToken, *Response, error) {
    56  	u := fmt.Sprintf("repos/%v/%v/actions/runners/registration-token", owner, repo)
    57  
    58  	req, err := s.client.NewRequest("POST", u, nil)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	registrationToken := new(RegistrationToken)
    64  	resp, err := s.client.Do(ctx, req, registrationToken)
    65  	if err != nil {
    66  		return nil, resp, err
    67  	}
    68  
    69  	return registrationToken, resp, nil
    70  }
    71  
    72  // Runner represents a self-hosted runner registered with a repository.
    73  type Runner struct {
    74  	ID     *int64          `json:"id,omitempty"`
    75  	Name   *string         `json:"name,omitempty"`
    76  	OS     *string         `json:"os,omitempty"`
    77  	Status *string         `json:"status,omitempty"`
    78  	Busy   *bool           `json:"busy,omitempty"`
    79  	Labels []*RunnerLabels `json:"labels,omitempty"`
    80  }
    81  
    82  // RunnerLabels represents a collection of labels attached to each runner.
    83  type RunnerLabels struct {
    84  	ID   *int64  `json:"id,omitempty"`
    85  	Name *string `json:"name,omitempty"`
    86  	Type *string `json:"type,omitempty"`
    87  }
    88  
    89  // Runners represents a collection of self-hosted runners for a repository.
    90  type Runners struct {
    91  	TotalCount int       `json:"total_count"`
    92  	Runners    []*Runner `json:"runners"`
    93  }
    94  
    95  // ListRunners lists all the self-hosted runners for a repository.
    96  //
    97  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-self-hosted-runners-for-a-repository
    98  func (s *ActionsService) ListRunners(ctx context.Context, owner, repo string, opts *ListOptions) (*Runners, *Response, error) {
    99  	u := fmt.Sprintf("repos/%v/%v/actions/runners", owner, repo)
   100  	u, err := addOptions(u, opts)
   101  	if err != nil {
   102  		return nil, nil, err
   103  	}
   104  
   105  	req, err := s.client.NewRequest("GET", u, nil)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	runners := &Runners{}
   111  	resp, err := s.client.Do(ctx, req, &runners)
   112  	if err != nil {
   113  		return nil, resp, err
   114  	}
   115  
   116  	return runners, resp, nil
   117  }
   118  
   119  // GetRunner gets a specific self-hosted runner for a repository using its runner ID.
   120  //
   121  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-self-hosted-runner-for-a-repository
   122  func (s *ActionsService) GetRunner(ctx context.Context, owner, repo string, runnerID int64) (*Runner, *Response, error) {
   123  	u := fmt.Sprintf("repos/%v/%v/actions/runners/%v", owner, repo, runnerID)
   124  	req, err := s.client.NewRequest("GET", u, nil)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  
   129  	runner := new(Runner)
   130  	resp, err := s.client.Do(ctx, req, runner)
   131  	if err != nil {
   132  		return nil, resp, err
   133  	}
   134  
   135  	return runner, resp, nil
   136  }
   137  
   138  // RemoveToken represents a token that can be used to remove a self-hosted runner from a repository.
   139  type RemoveToken struct {
   140  	Token     *string    `json:"token,omitempty"`
   141  	ExpiresAt *Timestamp `json:"expires_at,omitempty"`
   142  }
   143  
   144  // CreateRemoveToken creates a token that can be used to remove a self-hosted runner from a repository.
   145  //
   146  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#create-a-remove-token-for-a-repository
   147  func (s *ActionsService) CreateRemoveToken(ctx context.Context, owner, repo string) (*RemoveToken, *Response, error) {
   148  	u := fmt.Sprintf("repos/%v/%v/actions/runners/remove-token", owner, repo)
   149  
   150  	req, err := s.client.NewRequest("POST", u, nil)
   151  	if err != nil {
   152  		return nil, nil, err
   153  	}
   154  
   155  	removeToken := new(RemoveToken)
   156  	resp, err := s.client.Do(ctx, req, removeToken)
   157  	if err != nil {
   158  		return nil, resp, err
   159  	}
   160  
   161  	return removeToken, resp, nil
   162  }
   163  
   164  // RemoveRunner forces the removal of a self-hosted runner in a repository using the runner id.
   165  //
   166  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#delete-a-self-hosted-runner-from-a-repository
   167  func (s *ActionsService) RemoveRunner(ctx context.Context, owner, repo string, runnerID int64) (*Response, error) {
   168  	u := fmt.Sprintf("repos/%v/%v/actions/runners/%v", owner, repo, runnerID)
   169  
   170  	req, err := s.client.NewRequest("DELETE", u, nil)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	return s.client.Do(ctx, req, nil)
   176  }
   177  
   178  // ListOrganizationRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run.
   179  //
   180  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-runner-applications-for-an-organization
   181  func (s *ActionsService) ListOrganizationRunnerApplicationDownloads(ctx context.Context, owner string) ([]*RunnerApplicationDownload, *Response, error) {
   182  	u := fmt.Sprintf("orgs/%v/actions/runners/downloads", owner)
   183  	req, err := s.client.NewRequest("GET", u, nil)
   184  	if err != nil {
   185  		return nil, nil, err
   186  	}
   187  
   188  	var rads []*RunnerApplicationDownload
   189  	resp, err := s.client.Do(ctx, req, &rads)
   190  	if err != nil {
   191  		return nil, resp, err
   192  	}
   193  
   194  	return rads, resp, nil
   195  }
   196  
   197  // CreateOrganizationRegistrationToken creates a token that can be used to add a self-hosted runner to an organization.
   198  //
   199  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#create-a-registration-token-for-an-organization
   200  func (s *ActionsService) CreateOrganizationRegistrationToken(ctx context.Context, owner string) (*RegistrationToken, *Response, error) {
   201  	u := fmt.Sprintf("orgs/%v/actions/runners/registration-token", owner)
   202  
   203  	req, err := s.client.NewRequest("POST", u, nil)
   204  	if err != nil {
   205  		return nil, nil, err
   206  	}
   207  
   208  	registrationToken := new(RegistrationToken)
   209  	resp, err := s.client.Do(ctx, req, registrationToken)
   210  	if err != nil {
   211  		return nil, resp, err
   212  	}
   213  
   214  	return registrationToken, resp, nil
   215  }
   216  
   217  // ListOrganizationRunners lists all the self-hosted runners for an organization.
   218  //
   219  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-self-hosted-runners-for-an-organization
   220  func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner string, opts *ListOptions) (*Runners, *Response, error) {
   221  	u := fmt.Sprintf("orgs/%v/actions/runners", owner)
   222  	u, err := addOptions(u, opts)
   223  	if err != nil {
   224  		return nil, nil, err
   225  	}
   226  
   227  	req, err := s.client.NewRequest("GET", u, nil)
   228  	if err != nil {
   229  		return nil, nil, err
   230  	}
   231  
   232  	runners := &Runners{}
   233  	resp, err := s.client.Do(ctx, req, &runners)
   234  	if err != nil {
   235  		return nil, resp, err
   236  	}
   237  
   238  	return runners, resp, nil
   239  }
   240  
   241  // ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization.
   242  //
   243  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-selected-repositories-enabled-for-github-actions-in-an-organization
   244  func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) {
   245  	u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner)
   246  	u, err := addOptions(u, opts)
   247  	if err != nil {
   248  		return nil, nil, err
   249  	}
   250  
   251  	req, err := s.client.NewRequest("GET", u, nil)
   252  	if err != nil {
   253  		return nil, nil, err
   254  	}
   255  
   256  	repos := &ActionsEnabledOnOrgRepos{}
   257  	resp, err := s.client.Do(ctx, req, repos)
   258  	if err != nil {
   259  		return nil, resp, err
   260  	}
   261  
   262  	return repos, resp, nil
   263  }
   264  
   265  // GetOrganizationRunner gets a specific self-hosted runner for an organization using its runner ID.
   266  //
   267  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-self-hosted-runner-for-an-organization
   268  func (s *ActionsService) GetOrganizationRunner(ctx context.Context, owner string, runnerID int64) (*Runner, *Response, error) {
   269  	u := fmt.Sprintf("orgs/%v/actions/runners/%v", owner, runnerID)
   270  	req, err := s.client.NewRequest("GET", u, nil)
   271  	if err != nil {
   272  		return nil, nil, err
   273  	}
   274  
   275  	runner := new(Runner)
   276  	resp, err := s.client.Do(ctx, req, runner)
   277  	if err != nil {
   278  		return nil, resp, err
   279  	}
   280  
   281  	return runner, resp, nil
   282  }
   283  
   284  // CreateOrganizationRemoveToken creates a token that can be used to remove a self-hosted runner from an organization.
   285  //
   286  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#create-a-remove-token-for-an-organization
   287  func (s *ActionsService) CreateOrganizationRemoveToken(ctx context.Context, owner string) (*RemoveToken, *Response, error) {
   288  	u := fmt.Sprintf("orgs/%v/actions/runners/remove-token", owner)
   289  
   290  	req, err := s.client.NewRequest("POST", u, nil)
   291  	if err != nil {
   292  		return nil, nil, err
   293  	}
   294  
   295  	removeToken := new(RemoveToken)
   296  	resp, err := s.client.Do(ctx, req, removeToken)
   297  	if err != nil {
   298  		return nil, resp, err
   299  	}
   300  
   301  	return removeToken, resp, nil
   302  }
   303  
   304  // RemoveOrganizationRunner forces the removal of a self-hosted runner from an organization using the runner id.
   305  //
   306  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#delete-a-self-hosted-runner-from-an-organization
   307  func (s *ActionsService) RemoveOrganizationRunner(ctx context.Context, owner string, runnerID int64) (*Response, error) {
   308  	u := fmt.Sprintf("orgs/%v/actions/runners/%v", owner, runnerID)
   309  
   310  	req, err := s.client.NewRequest("DELETE", u, nil)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  
   315  	return s.client.Do(ctx, req, nil)
   316  }
   317  

View as plain text