github.com/google/go-github/v52@v52.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/en/rest/actions/required-workflows?apiVersion=2022-11-28#list-required-workflows 71 func (s *ActionsService) ListOrgRequiredWorkflows(ctx context.Context, org string, opts *ListOptions) (*OrgRequiredWorkflows, *Response, error) { 72 url := fmt.Sprintf("orgs/%v/actions/required_workflows", org) 73 u, err := addOptions(url, opts) 74 if err != nil { 75 return nil, nil, err 76 } 77 78 req, err := s.client.NewRequest("GET", u, nil) 79 if err != nil { 80 return nil, nil, err 81 } 82 83 requiredWorkflows := new(OrgRequiredWorkflows) 84 resp, err := s.client.Do(ctx, req, &requiredWorkflows) 85 if err != nil { 86 return nil, resp, err 87 } 88 89 return requiredWorkflows, resp, nil 90 } 91 92 // CreateRequiredWorkflow creates the required workflow in an org. 93 // 94 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#create-a-required-workflow 95 func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) { 96 url := fmt.Sprintf("orgs/%v/actions/required_workflows", org) 97 req, err := s.client.NewRequest("PUT", url, createRequiredWorkflowOptions) 98 if err != nil { 99 return nil, nil, err 100 } 101 102 orgRequiredWorkflow := new(OrgRequiredWorkflow) 103 resp, err := s.client.Do(ctx, req, orgRequiredWorkflow) 104 if err != nil { 105 return nil, resp, err 106 } 107 108 return orgRequiredWorkflow, resp, nil 109 } 110 111 // GetRequiredWorkflowByID get the RequiredWorkflows for an org by its ID. 112 // 113 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#list-required-workflows 114 func (s *ActionsService) GetRequiredWorkflowByID(ctx context.Context, owner string, requiredWorkflowID int64) (*OrgRequiredWorkflow, *Response, error) { 115 u := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", owner, requiredWorkflowID) 116 117 req, err := s.client.NewRequest("GET", u, nil) 118 if err != nil { 119 return nil, nil, err 120 } 121 122 requiredWorkflow := new(OrgRequiredWorkflow) 123 resp, err := s.client.Do(ctx, req, &requiredWorkflow) 124 if err != nil { 125 return nil, resp, err 126 } 127 128 return requiredWorkflow, resp, nil 129 } 130 131 // UpdateRequiredWorkflow updates a required workflow in an org. 132 // 133 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#update-a-required-workflow 134 func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) { 135 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID) 136 req, err := s.client.NewRequest("PATCH", url, updateRequiredWorkflowOptions) 137 if err != nil { 138 return nil, nil, err 139 } 140 141 orgRequiredWorkflow := new(OrgRequiredWorkflow) 142 resp, err := s.client.Do(ctx, req, orgRequiredWorkflow) 143 if err != nil { 144 return nil, resp, err 145 } 146 147 return orgRequiredWorkflow, resp, nil 148 } 149 150 // DeleteRequiredWorkflow deletes a required workflow in an org. 151 // 152 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#update-a-required-workflow 153 func (s *ActionsService) DeleteRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64) (*Response, error) { 154 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID) 155 req, err := s.client.NewRequest("DELETE", url, nil) 156 if err != nil { 157 return nil, err 158 } 159 return s.client.Do(ctx, req, nil) 160 } 161 162 // ListRequiredWorkflowSelectedRepos lists the Repositories selected for a workflow. 163 // 164 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#list-selected-repositories-for-a-required-workflow 165 func (s *ActionsService) ListRequiredWorkflowSelectedRepos(ctx context.Context, org string, requiredWorkflowID int64, opts *ListOptions) (*RequiredWorkflowSelectedRepos, *Response, error) { 166 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories", org, requiredWorkflowID) 167 u, err := addOptions(url, opts) 168 if err != nil { 169 return nil, nil, err 170 } 171 req, err := s.client.NewRequest("GET", u, nil) 172 if err != nil { 173 return nil, nil, err 174 } 175 176 requiredWorkflowRepos := new(RequiredWorkflowSelectedRepos) 177 resp, err := s.client.Do(ctx, req, &requiredWorkflowRepos) 178 if err != nil { 179 return nil, resp, err 180 } 181 182 return requiredWorkflowRepos, resp, nil 183 } 184 185 // SetRequiredWorkflowSelectedRepos sets the Repositories selected for a workflow. 186 // 187 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#sets-repositories-for-a-required-workflow 188 func (s *ActionsService) SetRequiredWorkflowSelectedRepos(ctx context.Context, org string, requiredWorkflowID int64, ids SelectedRepoIDs) (*Response, error) { 189 type repoIDs struct { 190 SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"` 191 } 192 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories", org, requiredWorkflowID) 193 req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids}) 194 if err != nil { 195 return nil, err 196 } 197 198 return s.client.Do(ctx, req, nil) 199 } 200 201 // AddRepoToRequiredWorkflow adds the Repository to a required workflow. 202 // 203 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#add-a-repository-to-a-required-workflow 204 func (s *ActionsService) AddRepoToRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID, repoID int64) (*Response, error) { 205 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories/%v", org, requiredWorkflowID, repoID) 206 req, err := s.client.NewRequest("PUT", url, nil) 207 if err != nil { 208 return nil, err 209 } 210 return s.client.Do(ctx, req, nil) 211 } 212 213 // RemoveRepoFromRequiredWorkflow removes the Repository from a required workflow. 214 // 215 // GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#add-a-repository-to-a-required-workflow 216 func (s *ActionsService) RemoveRepoFromRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID, repoID int64) (*Response, error) { 217 url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v/repositories/%v", org, requiredWorkflowID, repoID) 218 req, err := s.client.NewRequest("DELETE", url, nil) 219 if err != nil { 220 return nil, err 221 } 222 return s.client.Do(ctx, req, nil) 223 } 224 225 // ListRepoRequiredWorkflows lists the RequiredWorkflows for a repo. 226 // 227 // Github API docs:https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#list-repository-required-workflows 228 func (s *ActionsService) ListRepoRequiredWorkflows(ctx context.Context, owner, repo string, opts *ListOptions) (*RepoRequiredWorkflows, *Response, error) { 229 url := fmt.Sprintf("repos/%v/%v/actions/required_workflows", owner, repo) 230 u, err := addOptions(url, opts) 231 if err != nil { 232 return nil, nil, err 233 } 234 235 req, err := s.client.NewRequest("GET", u, nil) 236 if err != nil { 237 return nil, nil, err 238 } 239 240 requiredWorkflows := new(RepoRequiredWorkflows) 241 resp, err := s.client.Do(ctx, req, &requiredWorkflows) 242 if err != nil { 243 return nil, resp, err 244 } 245 246 return requiredWorkflows, resp, nil 247 }