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