github.com/google/go-github/v74@v74.0.0/github/orgs_properties.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 "encoding/json" 11 "errors" 12 "fmt" 13 ) 14 15 // CustomProperty represents an organization custom property object. 16 type CustomProperty struct { 17 // PropertyName is required for most endpoints except when calling CreateOrUpdateCustomProperty; 18 // where this is sent in the path and thus can be omitted. 19 PropertyName *string `json:"property_name,omitempty"` 20 // SourceType is the source type of the property where it has been created. Can be one of: organization, enterprise. 21 SourceType *string `json:"source_type,omitempty"` 22 // The type of the value for the property. Can be one of: string, single_select, multi_select, true_false. 23 ValueType string `json:"value_type"` 24 // Whether the property is required. 25 Required *bool `json:"required,omitempty"` 26 // Default value of the property. 27 DefaultValue *string `json:"default_value,omitempty"` 28 // Short description of the property. 29 Description *string `json:"description,omitempty"` 30 // An ordered list of the allowed values of the property. The property can have up to 200 31 // allowed values. 32 AllowedValues []string `json:"allowed_values,omitempty"` 33 // Who can edit the values of the property. Can be one of: org_actors, org_and_repo_actors, nil (null). 34 ValuesEditableBy *string `json:"values_editable_by,omitempty"` 35 } 36 37 // RepoCustomPropertyValue represents a repository custom property value. 38 type RepoCustomPropertyValue struct { 39 RepositoryID int64 `json:"repository_id"` 40 RepositoryName string `json:"repository_name"` 41 RepositoryFullName string `json:"repository_full_name"` 42 Properties []*CustomPropertyValue `json:"properties"` 43 } 44 45 // CustomPropertyValue represents a custom property value. 46 type CustomPropertyValue struct { 47 PropertyName string `json:"property_name"` 48 Value any `json:"value"` 49 } 50 51 // ListCustomPropertyValuesOptions specifies the optional parameters to the 52 // OrganizationsService.ListCustomPropertyValues method. 53 type ListCustomPropertyValuesOptions struct { 54 RepositoryQuery string `url:"repository_query,omitempty"` 55 ListOptions 56 } 57 58 // UnmarshalJSON implements the json.Unmarshaler interface. 59 // This helps us handle the fact that Value can be either a string, []string, or nil. 60 func (cpv *CustomPropertyValue) UnmarshalJSON(data []byte) error { 61 type aliasCustomPropertyValue CustomPropertyValue 62 aux := &struct { 63 *aliasCustomPropertyValue 64 }{ 65 aliasCustomPropertyValue: (*aliasCustomPropertyValue)(cpv), 66 } 67 if err := json.Unmarshal(data, &aux); err != nil { 68 return err 69 } 70 71 switch v := aux.Value.(type) { 72 case nil: 73 cpv.Value = nil 74 case string: 75 cpv.Value = v 76 case []any: 77 strSlice := make([]string, len(v)) 78 for i, item := range v { 79 str, ok := item.(string) 80 if !ok { 81 return errors.New("non-string value in string array") 82 } 83 strSlice[i] = str 84 } 85 cpv.Value = strSlice 86 default: 87 return fmt.Errorf("unexpected value type: %T", v) 88 } 89 return nil 90 } 91 92 // GetAllCustomProperties gets all custom properties that are defined for the specified organization. 93 // 94 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#get-all-custom-properties-for-an-organization 95 // 96 //meta:operation GET /orgs/{org}/properties/schema 97 func (s *OrganizationsService) GetAllCustomProperties(ctx context.Context, org string) ([]*CustomProperty, *Response, error) { 98 u := fmt.Sprintf("orgs/%v/properties/schema", org) 99 100 req, err := s.client.NewRequest("GET", u, nil) 101 if err != nil { 102 return nil, nil, err 103 } 104 105 var customProperties []*CustomProperty 106 resp, err := s.client.Do(ctx, req, &customProperties) 107 if err != nil { 108 return nil, resp, err 109 } 110 111 return customProperties, resp, nil 112 } 113 114 // CreateOrUpdateCustomProperties creates new or updates existing custom properties that are defined for the specified organization. 115 // 116 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-custom-properties-for-an-organization 117 // 118 //meta:operation PATCH /orgs/{org}/properties/schema 119 func (s *OrganizationsService) CreateOrUpdateCustomProperties(ctx context.Context, org string, properties []*CustomProperty) ([]*CustomProperty, *Response, error) { 120 u := fmt.Sprintf("orgs/%v/properties/schema", org) 121 122 params := struct { 123 Properties []*CustomProperty `json:"properties"` 124 }{ 125 Properties: properties, 126 } 127 128 req, err := s.client.NewRequest("PATCH", u, params) 129 if err != nil { 130 return nil, nil, err 131 } 132 133 var customProperties []*CustomProperty 134 resp, err := s.client.Do(ctx, req, &customProperties) 135 if err != nil { 136 return nil, resp, err 137 } 138 139 return customProperties, resp, nil 140 } 141 142 // GetCustomProperty gets a custom property that is defined for the specified organization. 143 // 144 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#get-a-custom-property-for-an-organization 145 // 146 //meta:operation GET /orgs/{org}/properties/schema/{custom_property_name} 147 func (s *OrganizationsService) GetCustomProperty(ctx context.Context, org, name string) (*CustomProperty, *Response, error) { 148 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, name) 149 150 req, err := s.client.NewRequest("GET", u, nil) 151 if err != nil { 152 return nil, nil, err 153 } 154 155 var customProperty *CustomProperty 156 resp, err := s.client.Do(ctx, req, &customProperty) 157 if err != nil { 158 return nil, resp, err 159 } 160 161 return customProperty, resp, nil 162 } 163 164 // CreateOrUpdateCustomProperty creates a new or updates an existing custom property that is defined for the specified organization. 165 // 166 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-a-custom-property-for-an-organization 167 // 168 //meta:operation PUT /orgs/{org}/properties/schema/{custom_property_name} 169 func (s *OrganizationsService) CreateOrUpdateCustomProperty(ctx context.Context, org, customPropertyName string, property *CustomProperty) (*CustomProperty, *Response, error) { 170 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName) 171 172 req, err := s.client.NewRequest("PUT", u, property) 173 if err != nil { 174 return nil, nil, err 175 } 176 177 var customProperty *CustomProperty 178 resp, err := s.client.Do(ctx, req, &customProperty) 179 if err != nil { 180 return nil, resp, err 181 } 182 183 return customProperty, resp, nil 184 } 185 186 // RemoveCustomProperty removes a custom property that is defined for the specified organization. 187 // 188 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#remove-a-custom-property-for-an-organization 189 // 190 //meta:operation DELETE /orgs/{org}/properties/schema/{custom_property_name} 191 func (s *OrganizationsService) RemoveCustomProperty(ctx context.Context, org, customPropertyName string) (*Response, error) { 192 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName) 193 194 req, err := s.client.NewRequest("DELETE", u, nil) 195 if err != nil { 196 return nil, err 197 } 198 199 return s.client.Do(ctx, req, nil) 200 } 201 202 // ListCustomPropertyValues lists all custom property values for repositories in the specified organization. 203 // 204 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#list-custom-property-values-for-organization-repositories 205 // 206 //meta:operation GET /orgs/{org}/properties/values 207 func (s *OrganizationsService) ListCustomPropertyValues(ctx context.Context, org string, opts *ListCustomPropertyValuesOptions) ([]*RepoCustomPropertyValue, *Response, error) { 208 u := fmt.Sprintf("orgs/%v/properties/values", org) 209 u, err := addOptions(u, opts) 210 if err != nil { 211 return nil, nil, err 212 } 213 214 req, err := s.client.NewRequest("GET", u, nil) 215 if err != nil { 216 return nil, nil, err 217 } 218 219 var repoCustomPropertyValues []*RepoCustomPropertyValue 220 resp, err := s.client.Do(ctx, req, &repoCustomPropertyValues) 221 if err != nil { 222 return nil, resp, err 223 } 224 225 return repoCustomPropertyValues, resp, nil 226 } 227 228 // CreateOrUpdateRepoCustomPropertyValues creates new or updates existing custom property values across multiple repositories for the specified organization. 229 // 230 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-custom-property-values-for-organization-repositories 231 // 232 //meta:operation PATCH /orgs/{org}/properties/values 233 func (s *OrganizationsService) CreateOrUpdateRepoCustomPropertyValues(ctx context.Context, org string, repoNames []string, properties []*CustomPropertyValue) (*Response, error) { 234 u := fmt.Sprintf("orgs/%v/properties/values", org) 235 236 params := struct { 237 RepositoryNames []string `json:"repository_names"` 238 Properties []*CustomPropertyValue `json:"properties"` 239 }{ 240 RepositoryNames: repoNames, 241 Properties: properties, 242 } 243 244 req, err := s.client.NewRequest("PATCH", u, params) 245 if err != nil { 246 return nil, err 247 } 248 249 return s.client.Do(ctx, req, nil) 250 }