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