github.com/google/go-github/v68@v68.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 interface{} `json:"value"` 49 } 50 51 // UnmarshalJSON implements the json.Unmarshaler interface. 52 // This helps us handle the fact that Value can be either a string, []string, or nil. 53 func (cpv *CustomPropertyValue) UnmarshalJSON(data []byte) error { 54 type aliasCustomPropertyValue CustomPropertyValue 55 aux := &struct { 56 *aliasCustomPropertyValue 57 }{ 58 aliasCustomPropertyValue: (*aliasCustomPropertyValue)(cpv), 59 } 60 if err := json.Unmarshal(data, &aux); err != nil { 61 return err 62 } 63 64 switch v := aux.Value.(type) { 65 case nil: 66 cpv.Value = nil 67 case string: 68 cpv.Value = v 69 case []interface{}: 70 strSlice := make([]string, len(v)) 71 for i, item := range v { 72 str, ok := item.(string) 73 if !ok { 74 return errors.New("non-string value in string array") 75 } 76 strSlice[i] = str 77 } 78 cpv.Value = strSlice 79 default: 80 return fmt.Errorf("unexpected value type: %T", v) 81 } 82 return nil 83 } 84 85 // GetAllCustomProperties gets all custom properties that are defined for the specified organization. 86 // 87 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#get-all-custom-properties-for-an-organization 88 // 89 //meta:operation GET /orgs/{org}/properties/schema 90 func (s *OrganizationsService) GetAllCustomProperties(ctx context.Context, org string) ([]*CustomProperty, *Response, error) { 91 u := fmt.Sprintf("orgs/%v/properties/schema", org) 92 93 req, err := s.client.NewRequest("GET", u, nil) 94 if err != nil { 95 return nil, nil, err 96 } 97 98 var customProperties []*CustomProperty 99 resp, err := s.client.Do(ctx, req, &customProperties) 100 if err != nil { 101 return nil, resp, err 102 } 103 104 return customProperties, resp, nil 105 } 106 107 // CreateOrUpdateCustomProperties creates new or updates existing custom properties that are defined for the specified organization. 108 // 109 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-custom-properties-for-an-organization 110 // 111 //meta:operation PATCH /orgs/{org}/properties/schema 112 func (s *OrganizationsService) CreateOrUpdateCustomProperties(ctx context.Context, org string, properties []*CustomProperty) ([]*CustomProperty, *Response, error) { 113 u := fmt.Sprintf("orgs/%v/properties/schema", org) 114 115 params := struct { 116 Properties []*CustomProperty `json:"properties"` 117 }{ 118 Properties: properties, 119 } 120 121 req, err := s.client.NewRequest("PATCH", u, params) 122 if err != nil { 123 return nil, nil, err 124 } 125 126 var customProperties []*CustomProperty 127 resp, err := s.client.Do(ctx, req, &customProperties) 128 if err != nil { 129 return nil, resp, err 130 } 131 132 return customProperties, resp, nil 133 } 134 135 // GetCustomProperty gets a custom property that is defined for the specified organization. 136 // 137 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#get-a-custom-property-for-an-organization 138 // 139 //meta:operation GET /orgs/{org}/properties/schema/{custom_property_name} 140 func (s *OrganizationsService) GetCustomProperty(ctx context.Context, org, name string) (*CustomProperty, *Response, error) { 141 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, name) 142 143 req, err := s.client.NewRequest("GET", u, nil) 144 if err != nil { 145 return nil, nil, err 146 } 147 148 var customProperty *CustomProperty 149 resp, err := s.client.Do(ctx, req, &customProperty) 150 if err != nil { 151 return nil, resp, err 152 } 153 154 return customProperty, resp, nil 155 } 156 157 // CreateOrUpdateCustomProperty creates a new or updates an existing custom property that is defined for the specified organization. 158 // 159 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-a-custom-property-for-an-organization 160 // 161 //meta:operation PUT /orgs/{org}/properties/schema/{custom_property_name} 162 func (s *OrganizationsService) CreateOrUpdateCustomProperty(ctx context.Context, org, customPropertyName string, property *CustomProperty) (*CustomProperty, *Response, error) { 163 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName) 164 165 req, err := s.client.NewRequest("PUT", u, property) 166 if err != nil { 167 return nil, nil, err 168 } 169 170 var customProperty *CustomProperty 171 resp, err := s.client.Do(ctx, req, &customProperty) 172 if err != nil { 173 return nil, resp, err 174 } 175 176 return customProperty, resp, nil 177 } 178 179 // RemoveCustomProperty removes a custom property that is defined for the specified organization. 180 // 181 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#remove-a-custom-property-for-an-organization 182 // 183 //meta:operation DELETE /orgs/{org}/properties/schema/{custom_property_name} 184 func (s *OrganizationsService) RemoveCustomProperty(ctx context.Context, org, customPropertyName string) (*Response, error) { 185 u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName) 186 187 req, err := s.client.NewRequest("DELETE", u, nil) 188 if err != nil { 189 return nil, err 190 } 191 192 return s.client.Do(ctx, req, nil) 193 } 194 195 // ListCustomPropertyValues lists all custom property values for repositories in the specified organization. 196 // 197 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#list-custom-property-values-for-organization-repositories 198 // 199 //meta:operation GET /orgs/{org}/properties/values 200 func (s *OrganizationsService) ListCustomPropertyValues(ctx context.Context, org string, opts *ListOptions) ([]*RepoCustomPropertyValue, *Response, error) { 201 u := fmt.Sprintf("orgs/%v/properties/values", org) 202 u, err := addOptions(u, opts) 203 if err != nil { 204 return nil, nil, err 205 } 206 207 req, err := s.client.NewRequest("GET", u, nil) 208 if err != nil { 209 return nil, nil, err 210 } 211 212 var repoCustomPropertyValues []*RepoCustomPropertyValue 213 resp, err := s.client.Do(ctx, req, &repoCustomPropertyValues) 214 if err != nil { 215 return nil, resp, err 216 } 217 218 return repoCustomPropertyValues, resp, nil 219 } 220 221 // CreateOrUpdateRepoCustomPropertyValues creates new or updates existing custom property values across multiple repositories for the specified organization. 222 // 223 // GitHub API docs: https://docs.github.com/rest/orgs/custom-properties#create-or-update-custom-property-values-for-organization-repositories 224 // 225 //meta:operation PATCH /orgs/{org}/properties/values 226 func (s *OrganizationsService) CreateOrUpdateRepoCustomPropertyValues(ctx context.Context, org string, repoNames []string, properties []*CustomPropertyValue) (*Response, error) { 227 u := fmt.Sprintf("orgs/%v/properties/values", org) 228 229 params := struct { 230 RepositoryNames []string `json:"repository_names"` 231 Properties []*CustomPropertyValue `json:"properties"` 232 }{ 233 RepositoryNames: repoNames, 234 Properties: properties, 235 } 236 237 req, err := s.client.NewRequest("PATCH", u, params) 238 if err != nil { 239 return nil, err 240 } 241 242 return s.client.Do(ctx, req, nil) 243 }