|
| 1 | +// Copyright 2025 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 | + "net/http" |
| 12 | +) |
| 13 | + |
| 14 | +// GetCodeSecurityConfigurations lists all code security configurations available in an enterprise. |
| 15 | +// |
| 16 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#get-code-security-configurations-for-an-enterprise |
| 17 | +// |
| 18 | +//meta:operation GET /enterprises/{enterprise}/code-security/configurations |
| 19 | +func (s *EnterpriseService) GetCodeSecurityConfigurations(ctx context.Context, enterprise string) ([]*CodeSecurityConfiguration, *Response, error) { |
| 20 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations", enterprise) |
| 21 | + |
| 22 | + req, err := s.client.NewRequest("GET", u, nil) |
| 23 | + if err != nil { |
| 24 | + return nil, nil, err |
| 25 | + } |
| 26 | + |
| 27 | + var configurations []*CodeSecurityConfiguration |
| 28 | + resp, err := s.client.Do(ctx, req, &configurations) |
| 29 | + if err != nil { |
| 30 | + return nil, resp, err |
| 31 | + } |
| 32 | + return configurations, resp, nil |
| 33 | +} |
| 34 | + |
| 35 | +// CreateCodeSecurityConfiguration creates a code security configuration in an enterprise. |
| 36 | +// |
| 37 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#create-a-code-security-configuration-for-an-enterprise |
| 38 | +// |
| 39 | +//meta:operation POST /enterprises/{enterprise}/code-security/configurations |
| 40 | +func (s *EnterpriseService) CreateCodeSecurityConfiguration(ctx context.Context, enterprise string, c *CodeSecurityConfiguration) (*CodeSecurityConfiguration, *Response, error) { |
| 41 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations", enterprise) |
| 42 | + |
| 43 | + req, err := s.client.NewRequest("POST", u, c) |
| 44 | + if err != nil { |
| 45 | + return nil, nil, err |
| 46 | + } |
| 47 | + |
| 48 | + var configuration *CodeSecurityConfiguration |
| 49 | + resp, err := s.client.Do(ctx, req, &configuration) |
| 50 | + if err != nil { |
| 51 | + return nil, resp, err |
| 52 | + } |
| 53 | + return configuration, resp, nil |
| 54 | +} |
| 55 | + |
| 56 | +// GetDefaultCodeSecurityConfigurations lists the default code security configurations for an enterprise. |
| 57 | +// |
| 58 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#get-default-code-security-configurations-for-an-enterprise |
| 59 | +// |
| 60 | +//meta:operation GET /enterprises/{enterprise}/code-security/configurations/defaults |
| 61 | +func (s *EnterpriseService) GetDefaultCodeSecurityConfigurations(ctx context.Context, enterprise string) ([]*CodeSecurityConfigurationWithDefaultForNewRepos, *Response, error) { |
| 62 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/defaults", enterprise) |
| 63 | + |
| 64 | + req, err := s.client.NewRequest("GET", u, nil) |
| 65 | + if err != nil { |
| 66 | + return nil, nil, err |
| 67 | + } |
| 68 | + |
| 69 | + var configurations []*CodeSecurityConfigurationWithDefaultForNewRepos |
| 70 | + resp, err := s.client.Do(ctx, req, &configurations) |
| 71 | + if err != nil { |
| 72 | + return nil, resp, err |
| 73 | + } |
| 74 | + return configurations, resp, nil |
| 75 | +} |
| 76 | + |
| 77 | +// GetCodeSecurityConfiguration gets a code security configuration available in an enterprise. |
| 78 | +// |
| 79 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#retrieve-a-code-security-configuration-of-an-enterprise |
| 80 | +// |
| 81 | +//meta:operation GET /enterprises/{enterprise}/code-security/configurations/{configuration_id} |
| 82 | +func (s *EnterpriseService) GetCodeSecurityConfiguration(ctx context.Context, enterprise string, id int64) (*CodeSecurityConfiguration, *Response, error) { |
| 83 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v", enterprise, id) |
| 84 | + |
| 85 | + req, err := s.client.NewRequest("GET", u, nil) |
| 86 | + if err != nil { |
| 87 | + return nil, nil, err |
| 88 | + } |
| 89 | + |
| 90 | + var configuration *CodeSecurityConfiguration |
| 91 | + resp, err := s.client.Do(ctx, req, &configuration) |
| 92 | + if err != nil { |
| 93 | + return nil, resp, err |
| 94 | + } |
| 95 | + return configuration, resp, nil |
| 96 | +} |
| 97 | + |
| 98 | +// UpdateCodeSecurityConfiguration updates a code security configuration in an enterprise. |
| 99 | +// |
| 100 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#update-a-custom-code-security-configuration-for-an-enterprise |
| 101 | +// |
| 102 | +//meta:operation PATCH /enterprises/{enterprise}/code-security/configurations/{configuration_id} |
| 103 | +func (s *EnterpriseService) UpdateCodeSecurityConfiguration(ctx context.Context, enterprise string, id int64, c *CodeSecurityConfiguration) (*CodeSecurityConfiguration, *Response, error) { |
| 104 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v", enterprise, id) |
| 105 | + |
| 106 | + req, err := s.client.NewRequest("PATCH", u, c) |
| 107 | + if err != nil { |
| 108 | + return nil, nil, err |
| 109 | + } |
| 110 | + |
| 111 | + var configuration *CodeSecurityConfiguration |
| 112 | + resp, err := s.client.Do(ctx, req, &configuration) |
| 113 | + if err != nil { |
| 114 | + return nil, resp, err |
| 115 | + } |
| 116 | + return configuration, resp, nil |
| 117 | +} |
| 118 | + |
| 119 | +// DeleteCodeSecurityConfiguration deletes a code security configuration from an enterprise. |
| 120 | +// |
| 121 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#delete-a-code-security-configuration-for-an-enterprise |
| 122 | +// |
| 123 | +//meta:operation DELETE /enterprises/{enterprise}/code-security/configurations/{configuration_id} |
| 124 | +func (s *EnterpriseService) DeleteCodeSecurityConfiguration(ctx context.Context, enterprise string, id int64) (*Response, error) { |
| 125 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v", enterprise, id) |
| 126 | + |
| 127 | + req, err := s.client.NewRequest("DELETE", u, nil) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + resp, err := s.client.Do(ctx, req, nil) |
| 132 | + if err != nil { |
| 133 | + return resp, err |
| 134 | + } |
| 135 | + return resp, nil |
| 136 | +} |
| 137 | + |
| 138 | +// AttachCodeSecurityConfigurationToRepositories attaches an enterprise code security configuration to repositories. |
| 139 | +// |
| 140 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#attach-an-enterprise-configuration-to-repositories |
| 141 | +// |
| 142 | +//meta:operation POST /enterprises/{enterprise}/code-security/configurations/{configuration_id}/attach |
| 143 | +func (s *EnterpriseService) AttachCodeSecurityConfigurationToRepositories(ctx context.Context, enterprise string, id int64, scope string) (*Response, error) { |
| 144 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v/attach", enterprise, id) |
| 145 | + type scopeType struct { |
| 146 | + Scope string `json:"scope"` |
| 147 | + } |
| 148 | + |
| 149 | + req, err := s.client.NewRequest("POST", u, scopeType{Scope: scope}) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + resp, err := s.client.Do(ctx, req, nil) |
| 154 | + if err != nil && resp.StatusCode != http.StatusAccepted { // StatusAccepted(202) is the expected status code as job is queued for processing |
| 155 | + return resp, err |
| 156 | + } |
| 157 | + return resp, nil |
| 158 | +} |
| 159 | + |
| 160 | +// SetDefaultCodeSecurityConfiguration sets a code security configuration as a default for an enterprise. |
| 161 | +// |
| 162 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#set-a-code-security-configuration-as-a-default-for-an-enterprise |
| 163 | +// |
| 164 | +//meta:operation PUT /enterprises/{enterprise}/code-security/configurations/{configuration_id}/defaults |
| 165 | +func (s *EnterpriseService) SetDefaultCodeSecurityConfiguration(ctx context.Context, enterprise string, id int64, newReposParam string) (*CodeSecurityConfigurationWithDefaultForNewRepos, *Response, error) { |
| 166 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v/defaults", enterprise, id) |
| 167 | + type configParam struct { |
| 168 | + DefaultForNewRepos string `json:"default_for_new_repos"` |
| 169 | + } |
| 170 | + |
| 171 | + req, err := s.client.NewRequest("PUT", u, configParam{DefaultForNewRepos: newReposParam}) |
| 172 | + if err != nil { |
| 173 | + return nil, nil, err |
| 174 | + } |
| 175 | + var c *CodeSecurityConfigurationWithDefaultForNewRepos |
| 176 | + resp, err := s.client.Do(ctx, req, &c) |
| 177 | + if err != nil { |
| 178 | + return nil, resp, err |
| 179 | + } |
| 180 | + return c, resp, nil |
| 181 | +} |
| 182 | + |
| 183 | +// GetRepositoriesForCodeSecurityConfiguration lists the repositories associated with an enterprise code security configuration. |
| 184 | +// |
| 185 | +// GitHub API docs: https://docs.github.com/rest/code-security/configurations#get-repositories-associated-with-an-enterprise-code-security-configuration |
| 186 | +// |
| 187 | +//meta:operation GET /enterprises/{enterprise}/code-security/configurations/{configuration_id}/repositories |
| 188 | +func (s *EnterpriseService) GetRepositoriesForCodeSecurityConfiguration(ctx context.Context, enterprise string, id int64) ([]*RepositoryAttachment, *Response, error) { |
| 189 | + u := fmt.Sprintf("enterprises/%v/code-security/configurations/%v/repositories", enterprise, id) |
| 190 | + |
| 191 | + req, err := s.client.NewRequest("GET", u, nil) |
| 192 | + if err != nil { |
| 193 | + return nil, nil, err |
| 194 | + } |
| 195 | + var attachments []*RepositoryAttachment |
| 196 | + resp, err := s.client.Do(ctx, req, &attachments) |
| 197 | + if err != nil { |
| 198 | + return nil, resp, err |
| 199 | + } |
| 200 | + return attachments, resp, nil |
| 201 | +} |
0 commit comments