github.com/google/go-github/v60@v60.0.0/github/actions_required_workflows.go (about)

     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  // OrgRequiredWorkflow represents a required workflow object at the org level.
    14  type OrgRequiredWorkflow struct {
    15  	ID                      *int64      `json:"id,omitempty"`
    16  	Name                    *string     `json:"name,omitempty"`
    17  	Path                    *string     `json:"path,omitempty"`
    18  	Scope                   *string     `json:"scope,omitempty"`
    19  	Ref                     *string     `json:"ref,omitempty"`
    20  	State                   *string     `json:"state,omitempty"`
    21  	SelectedRepositoriesURL *string     `json:"selected_repositories_url,omitempty"`
    22  	CreatedAt               *Timestamp  `json:"created_at,omitempty"`
    23  	UpdatedAt               *Timestamp  `json:"updated_at,omitempty"`
    24  	Repository              *Repository `json:"repository,omitempty"`
    25  }
    26  
    27  // OrgRequiredWorkflows represents the required workflows for the org.
    28  type OrgRequiredWorkflows struct {
    29  	TotalCount        *int                   `json:"total_count,omitempty"`
    30  	RequiredWorkflows []*OrgRequiredWorkflow `json:"required_workflows,omitempty"`
    31  }
    32  
    33  // CreateUpdateRequiredWorkflowOptions represents the input object used to create or update required workflows.
    34  type CreateUpdateRequiredWorkflowOptions struct {
    35  	WorkflowFilePath      *string          `json:"workflow_file_path,omitempty"`
    36  	RepositoryID          *int64           `json:"repository_id,omitempty"`
    37  	Scope                 *string          `json:"scope,omitempty"`
    38  	SelectedRepositoryIDs *SelectedRepoIDs `json:"selected_repository_ids,omitempty"`
    39  }
    40  
    41  // RequiredWorkflowSelectedRepos represents the repos that a required workflow is applied to.
    42  type RequiredWorkflowSelectedRepos struct {
    43  	TotalCount   *int          `json:"total_count,omitempty"`
    44  	Repositories []*Repository `json:"repositories,omitempty"`
    45  }
    46  
    47  // RepoRequiredWorkflow represents a required workflow object at the repo level.
    48  type RepoRequiredWorkflow struct {
    49  	ID               *int64      `json:"id,omitempty"`
    50  	NodeID           *string     `json:"node_id,omitempty"`
    51  	Name             *string     `json:"name,omitempty"`
    52  	Path             *string     `json:"path,omitempty"`
    53  	State            *string     `json:"state,omitempty"`
    54  	URL              *string     `json:"url,omitempty"`
    55  	HTMLURL          *string     `json:"html_url,omitempty"`
    56  	BadgeURL         *string     `json:"badge_url,omitempty"`
    57  	CreatedAt        *Timestamp  `json:"created_at,omitempty"`
    58  	UpdatedAt        *Timestamp  `json:"updated_at,omitempty"`
    59  	SourceRepository *Repository `json:"source_repository,omitempty"`
    60  }
    61  
    62  // RepoRequiredWorkflows represents the required workflows for a repo.
    63  type RepoRequiredWorkflows struct {
    64  	TotalCount        *int                    `json:"total_count,omitempty"`
    65  	RequiredWorkflows []*RepoRequiredWorkflow `json:"required_workflows,omitempty"`
    66  }
    67  
    68  // ListOrgRequiredWorkflows lists the RequiredWorkflows for an org.
    69  //
    70  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
    71  //
    72  //meta:operation GET /orgs/{org}/actions/required_workflows
    73  func (s *ActionsService) ListOrgRequiredWorkflows(ctx context.Context, org string, opts *ListOptions) (*OrgRequiredWorkflows, *Response, error) {
    74  	url := fmt.Sprintf("orgs/%v/actions/required_workflows", org)
    75  	u, err := addOptions(url, opts)
    76  	if err != nil {
    77  		return nil, nil, err
    78  	}
    79  
    80  	req, err := s.client.NewRequest("GET", u, nil)
    81  	if err != nil {
    82  		return nil, nil, err
    83  	}
    84  
    85  	requiredWorkflows := new(OrgRequiredWorkflows)
    86  	resp, err := s.client.Do(ctx, req, &requiredWorkflows)
    87  	if err != nil {
    88  		return nil, resp, err
    89  	}
    90  
    91  	return requiredWorkflows, resp, nil
    92  }
    93  
    94  // CreateRequiredWorkflow creates the required workflow in an org.
    95  //
    96  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
    97  //
    98  //meta:operation POST /orgs/{org}/actions/required_workflows
    99  func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
   100  	url := fmt.Sprintf("orgs/%v/actions/required_workflows", org)
   101  	req, err := s.client.NewRequest("POST", url, createRequiredWorkflowOptions)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  
   106  	orgRequiredWorkflow := new(OrgRequiredWorkflow)
   107  	resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
   108  	if err != nil {
   109  		return nil, resp, err
   110  	}
   111  
   112  	return orgRequiredWorkflow, resp, nil
   113  }
   114  
   115  // GetRequiredWorkflowByID get the RequiredWorkflows for an org by its ID.
   116  //
   117  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   118  //
   119  //meta:operation GET /orgs/{org}/actions/required_workflows/{workflow_id}
   120  func (s *ActionsService) GetRequiredWorkflowByID(ctx context.Context, owner string, requiredWorkflowID int64) (*OrgRequiredWorkflow, *Response, error) {
   121  	u := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", owner, requiredWorkflowID)
   122  
   123  	req, err := s.client.NewRequest("GET", u, nil)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	requiredWorkflow := new(OrgRequiredWorkflow)
   129  	resp, err := s.client.Do(ctx, req, &requiredWorkflow)
   130  	if err != nil {
   131  		return nil, resp, err
   132  	}
   133  
   134  	return requiredWorkflow, resp, nil
   135  }
   136  
   137  // UpdateRequiredWorkflow updates a required workflow in an org.
   138  //
   139  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   140  //
   141  //meta:operation PATCH /orgs/{org}/actions/required_workflows/{workflow_id}
   142  func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
   143  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID)
   144  	req, err := s.client.NewRequest("PATCH", url, updateRequiredWorkflowOptions)
   145  	if err != nil {
   146  		return nil, nil, err
   147  	}
   148  
   149  	orgRequiredWorkflow := new(OrgRequiredWorkflow)
   150  	resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
   151  	if err != nil {
   152  		return nil, resp, err
   153  	}
   154  
   155  	return orgRequiredWorkflow, resp, nil
   156  }
   157  
   158  // DeleteRequiredWorkflow deletes a required workflow in an org.
   159  //
   160  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   161  //
   162  //meta:operation DELETE /orgs/{org}/actions/required_workflows/{workflow_id}
   163  func (s *ActionsService) DeleteRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64) (*Response, error) {
   164  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID)
   165  	req, err := s.client.NewRequest("DELETE", url, nil)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	return s.client.Do(ctx, req, nil)
   170  }
   171  
   172  // ListRequiredWorkflowSelectedRepos lists the Repositories selected for a workflow.
   173  //
   174  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   175  //
   176  //meta:operation GET /orgs/{org}/actions/required_workflows/{workflow_id}/repositories
   177  func (s *ActionsService) ListRequiredWorkflowSelectedRepos(ctx context.Context, org string, requiredWorkflowID int64, opts *ListOptions) (*RequiredWorkflowSelectedRepos, *Response, error) {
   178  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories", org, requiredWorkflowID)
   179  	u, err := addOptions(url, opts)
   180  	if err != nil {
   181  		return nil, nil, err
   182  	}
   183  	req, err := s.client.NewRequest("GET", u, nil)
   184  	if err != nil {
   185  		return nil, nil, err
   186  	}
   187  
   188  	requiredWorkflowRepos := new(RequiredWorkflowSelectedRepos)
   189  	resp, err := s.client.Do(ctx, req, &requiredWorkflowRepos)
   190  	if err != nil {
   191  		return nil, resp, err
   192  	}
   193  
   194  	return requiredWorkflowRepos, resp, nil
   195  }
   196  
   197  // SetRequiredWorkflowSelectedRepos sets the Repositories selected for a workflow.
   198  //
   199  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   200  //
   201  //meta:operation PUT /orgs/{org}/actions/required_workflows/{workflow_id}/repositories
   202  func (s *ActionsService) SetRequiredWorkflowSelectedRepos(ctx context.Context, org string, requiredWorkflowID int64, ids SelectedRepoIDs) (*Response, error) {
   203  	type repoIDs struct {
   204  		SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"`
   205  	}
   206  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories", org, requiredWorkflowID)
   207  	req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids})
   208  	if err != nil {
   209  		return nil, err
   210  	}
   211  
   212  	return s.client.Do(ctx, req, nil)
   213  }
   214  
   215  // AddRepoToRequiredWorkflow adds the Repository to a required workflow.
   216  //
   217  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   218  //
   219  //meta:operation PUT /orgs/{org}/actions/required_workflows/{workflow_id}/repositories/{repository_id}
   220  func (s *ActionsService) AddRepoToRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID, repoID int64) (*Response, error) {
   221  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories/%v", org, requiredWorkflowID, repoID)
   222  	req, err := s.client.NewRequest("PUT", url, nil)
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  	return s.client.Do(ctx, req, nil)
   227  }
   228  
   229  // RemoveRepoFromRequiredWorkflow removes the Repository from a required workflow.
   230  //
   231  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   232  //
   233  //meta:operation DELETE /orgs/{org}/actions/required_workflows/{workflow_id}/repositories/{repository_id}
   234  func (s *ActionsService) RemoveRepoFromRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID, repoID int64) (*Response, error) {
   235  	url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories/%v", org, requiredWorkflowID, repoID)
   236  	req, err := s.client.NewRequest("DELETE", url, nil)
   237  	if err != nil {
   238  		return nil, err
   239  	}
   240  	return s.client.Do(ctx, req, nil)
   241  }
   242  
   243  // ListRepoRequiredWorkflows lists the RequiredWorkflows for a repo.
   244  //
   245  // GitHub API docs: https://docs.github.com/actions/using-workflows/required-workflows
   246  //
   247  //meta:operation GET /repos/{owner}/{repo}/actions/required_workflows
   248  func (s *ActionsService) ListRepoRequiredWorkflows(ctx context.Context, owner, repo string, opts *ListOptions) (*RepoRequiredWorkflows, *Response, error) {
   249  	url := fmt.Sprintf("repos/%v/%v/actions/required_workflows", owner, repo)
   250  	u, err := addOptions(url, opts)
   251  	if err != nil {
   252  		return nil, nil, err
   253  	}
   254  
   255  	req, err := s.client.NewRequest("GET", u, nil)
   256  	if err != nil {
   257  		return nil, nil, err
   258  	}
   259  
   260  	requiredWorkflows := new(RepoRequiredWorkflows)
   261  	resp, err := s.client.Do(ctx, req, &requiredWorkflows)
   262  	if err != nil {
   263  		return nil, resp, err
   264  	}
   265  
   266  	return requiredWorkflows, resp, nil
   267  }