Skip to content

Commit 3b96fb3

Browse files
authored
Implement Custom Properties (#2986)
Fixes: #2965.
1 parent 14dccc2 commit 3b96fb3

File tree

4 files changed

+657
-0
lines changed

4 files changed

+657
-0
lines changed

github/github-accessors.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/orgs_properties.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
// CustomProperty represents an organization custom property object.
14+
type CustomProperty struct {
15+
// PropertyName is required for most endpoints except when calling CreateOrUpdateCustomProperty;
16+
// where this is sent in the path and thus can be omitted.
17+
PropertyName *string `json:"property_name,omitempty"`
18+
// Possible values for ValueType are: string, single_select
19+
ValueType string `json:"value_type"`
20+
Required *bool `json:"required,omitempty"`
21+
DefaultValue *string `json:"default_value,omitempty"`
22+
Description *string `json:"description,omitempty"`
23+
AllowedValues []string `json:"allowed_values,omitempty"`
24+
}
25+
26+
// RepoCustomPropertyValue represents a repository custom property value.
27+
type RepoCustomPropertyValue struct {
28+
RepositoryID int64 `json:"repository_id"`
29+
RepositoryName string `json:"repository_name"`
30+
RepositoryFullName string `json:"repository_full_name"`
31+
Properties []*CustomPropertyValue `json:"properties"`
32+
}
33+
34+
// CustomPropertyValue represents a custom property value.
35+
type CustomPropertyValue struct {
36+
PropertyName string `json:"property_name"`
37+
Value *string `json:"value,omitempty"`
38+
}
39+
40+
// GetAllCustomProperties gets all custom properties that are defined for the specified organization.
41+
//
42+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#get-all-custom-properties-for-an-organization
43+
//
44+
//meta:operation GET /orgs/{org}/properties/schema
45+
func (s *OrganizationsService) GetAllCustomProperties(ctx context.Context, org string) ([]*CustomProperty, *Response, error) {
46+
u := fmt.Sprintf("orgs/%v/properties/schema", org)
47+
48+
req, err := s.client.NewRequest("GET", u, nil)
49+
if err != nil {
50+
return nil, nil, err
51+
}
52+
53+
var customProperties []*CustomProperty
54+
resp, err := s.client.Do(ctx, req, &customProperties)
55+
if err != nil {
56+
return nil, resp, err
57+
}
58+
59+
return customProperties, resp, nil
60+
}
61+
62+
// CreateOrUpdateCustomProperties creates new or updates existing custom properties that are defined for the specified organization.
63+
//
64+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#create-or-update-custom-properties-for-an-organization
65+
//
66+
//meta:operation PATCH /orgs/{org}/properties/schema
67+
func (s *OrganizationsService) CreateOrUpdateCustomProperties(ctx context.Context, org string, properties []*CustomProperty) ([]*CustomProperty, *Response, error) {
68+
u := fmt.Sprintf("orgs/%v/properties/schema", org)
69+
70+
params := struct {
71+
Properties []*CustomProperty `json:"properties"`
72+
}{
73+
Properties: properties,
74+
}
75+
76+
req, err := s.client.NewRequest("PATCH", u, params)
77+
if err != nil {
78+
return nil, nil, err
79+
}
80+
81+
var customProperties []*CustomProperty
82+
resp, err := s.client.Do(ctx, req, &customProperties)
83+
if err != nil {
84+
return nil, resp, err
85+
}
86+
87+
return customProperties, resp, nil
88+
}
89+
90+
// GetCustomProperty gets a custom property that is defined for the specified organization.
91+
//
92+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#get-a-custom-property-for-an-organization
93+
//
94+
//meta:operation GET /orgs/{org}/properties/schema/{custom_property_name}
95+
func (s *OrganizationsService) GetCustomProperty(ctx context.Context, org, name string) (*CustomProperty, *Response, error) {
96+
u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, name)
97+
98+
req, err := s.client.NewRequest("GET", u, nil)
99+
if err != nil {
100+
return nil, nil, err
101+
}
102+
103+
var customProperty *CustomProperty
104+
resp, err := s.client.Do(ctx, req, &customProperty)
105+
if err != nil {
106+
return nil, resp, err
107+
}
108+
109+
return customProperty, resp, nil
110+
}
111+
112+
// CreateOrUpdateCustomProperty creates a new or updates an existing custom property that is defined for the specified organization.
113+
//
114+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#create-or-update-a-custom-property-for-an-organization
115+
//
116+
//meta:operation PUT /orgs/{org}/properties/schema/{custom_property_name}
117+
func (s *OrganizationsService) CreateOrUpdateCustomProperty(ctx context.Context, org, customPropertyName string, property *CustomProperty) (*CustomProperty, *Response, error) {
118+
u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName)
119+
120+
req, err := s.client.NewRequest("PUT", u, property)
121+
if err != nil {
122+
return nil, nil, err
123+
}
124+
125+
var customProperty *CustomProperty
126+
resp, err := s.client.Do(ctx, req, &customProperty)
127+
if err != nil {
128+
return nil, resp, err
129+
}
130+
131+
return customProperty, resp, nil
132+
}
133+
134+
// RemoveCustomProperty removes a custom property that is defined for the specified organization.
135+
//
136+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#remove-a-custom-property-for-an-organization
137+
//
138+
//meta:operation DELETE /orgs/{org}/properties/schema/{custom_property_name}
139+
func (s *OrganizationsService) RemoveCustomProperty(ctx context.Context, org, customPropertyName string) (*Response, error) {
140+
u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, customPropertyName)
141+
142+
req, err := s.client.NewRequest("DELETE", u, nil)
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
return s.client.Do(ctx, req, nil)
148+
}
149+
150+
// ListCustomPropertyValues lists all custom property values for repositories in the specified organization.
151+
//
152+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#list-custom-property-values-for-organization-repositories
153+
//
154+
//meta:operation GET /orgs/{org}/properties/values
155+
func (s *OrganizationsService) ListCustomPropertyValues(ctx context.Context, org string, opts *ListOptions) ([]*RepoCustomPropertyValue, *Response, error) {
156+
u := fmt.Sprintf("orgs/%v/properties/values", org)
157+
u, err := addOptions(u, opts)
158+
if err != nil {
159+
return nil, nil, err
160+
}
161+
162+
req, err := s.client.NewRequest("GET", u, nil)
163+
if err != nil {
164+
return nil, nil, err
165+
}
166+
167+
var repoCustomPropertyValues []*RepoCustomPropertyValue
168+
resp, err := s.client.Do(ctx, req, &repoCustomPropertyValues)
169+
if err != nil {
170+
return nil, resp, err
171+
}
172+
173+
return repoCustomPropertyValues, resp, nil
174+
}
175+
176+
// CreateOrUpdateRepoCustomPropertyValues creates new or updates existing custom property values across multiple repositories for the specified organization.
177+
//
178+
// GitHub API docs: https://docs.github.com/rest/orgs/properties#create-or-update-custom-property-values-for-organization-repositories
179+
//
180+
//meta:operation PATCH /orgs/{org}/properties/values
181+
func (s *OrganizationsService) CreateOrUpdateRepoCustomPropertyValues(ctx context.Context, org string, repoNames []string, properties []*CustomProperty) (*Response, error) {
182+
u := fmt.Sprintf("orgs/%v/properties/values", org)
183+
184+
params := struct {
185+
RepositoryNames []string `json:"repository_names"`
186+
Properties []*CustomProperty `json:"properties"`
187+
}{
188+
RepositoryNames: repoNames,
189+
Properties: properties,
190+
}
191+
192+
req, err := s.client.NewRequest("PATCH", u, params)
193+
if err != nil {
194+
return nil, err
195+
}
196+
197+
return s.client.Do(ctx, req, nil)
198+
}

0 commit comments

Comments
 (0)