Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add CreateOrUpdateCustomProperty function
  • Loading branch information
liaodaniel committed Nov 4, 2023
commit ecc77c200e0bf66bb9d9ebace873d11791fbdbae
25 changes: 25 additions & 0 deletions github/orgs_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

package github

import (
"context"
"fmt"
)

// CustomProperty represents the organization custom property object.
type CustomProperty struct {
PropertyName *string `json:"property_name,omitempty"`
Expand All @@ -15,3 +20,23 @@ type CustomProperty struct {
Description *string `json:"description,omitempty"`
AllowedValues []string `json:"allowed_values,omitempty"`
}

// CreateOrUpdateCustomProperty creates a new or updates an existing custom property that is defined for the specified organization.
//
// GitHub API docs: https://docs.github.com/en/rest/orgs/properties#create-or-update-a-custom-property-for-an-organization
func (s *OrganizationsService) CreateOrUpdateCustomProperty(ctx context.Context, org, name string, property *CustomProperty) (*CustomProperty, *Response, error) {
u := fmt.Sprintf("orgs/%v/properties/schema/%v", org, name)

req, err := s.client.NewRequest("PUT", u, property)
if err != nil {
return nil, nil, err
}

var customProperty *CustomProperty
resp, err := s.client.Do(ctx, req, &customProperty)
if err != nil {
return nil, resp, err
}

return customProperty, resp, nil
}
69 changes: 69 additions & 0 deletions github/orgs_properties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestOrganizationsService_CreateOrUpdateCustomProperty(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{
"property_name": "name",
"value_type": "single_select",
"required": true,
"default_value": "production",
"description": "Prod or dev environment",
"allowed_values":[
"production",
"development"
]
}`)
})

ctx := context.Background()
property, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, "o", "name", &CustomProperty{
ValueType: "single_select",
Required: Bool(true),
DefaultValue: String("production"),
Description: String("Prod or dev environment"),
AllowedValues: []string{"production", "development"},
})
if err != nil {
t.Errorf("Organizations.CreateOrUpdateCustomProperty returned error: %v", err)
}

want := &CustomProperty{
PropertyName: String("name"),
ValueType: "single_select",
Required: Bool(true),
DefaultValue: String("production"),
Description: String("Prod or dev environment"),
AllowedValues: []string{"production", "development"},
}
if !cmp.Equal(property, want) {
t.Errorf("Organizations.CreateOrUpdateCustomProperty returned %+v, want %+v", property, want)
}

const methodName = "CreateOrUpdateCustomProperty"

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, "o", "name", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}