diff --git a/github/repos_properties.go b/github/repos_properties.go new file mode 100644 index 00000000000..5a8626c4530 --- /dev/null +++ b/github/repos_properties.go @@ -0,0 +1,33 @@ +// 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" +) + +// GetAllCustomPropertyValues gets all custom property values that are set for a repository. +// +// GitHub API docs: https://docs.github.com/rest/repos/custom-properties#get-all-custom-property-values-for-a-repository +// +//meta:operation GET /repos/{owner}/{repo}/properties/values +func (s *RepositoriesService) GetAllCustomPropertyValues(ctx context.Context, org, repo string) ([]*CustomPropertyValue, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/properties/values", org, repo) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var customPropertyValues []*CustomPropertyValue + resp, err := s.client.Do(ctx, req, &customPropertyValues) + if err != nil { + return nil, resp, err + } + + return customPropertyValues, resp, nil +} diff --git a/github/repos_properties_test.go b/github/repos_properties_test.go new file mode 100644 index 00000000000..ee231a138c6 --- /dev/null +++ b/github/repos_properties_test.go @@ -0,0 +1,65 @@ +// 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 TestRepositoriesService_GetAllCustomPropertyValues(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/properties/values", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[ + { + "property_name": "environment", + "value": "production" + }, + { + "property_name": "service", + "value": "web" + } + ]`) + }) + + ctx := context.Background() + customPropertyValues, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, "o", "r") + if err != nil { + t.Errorf("Repositories.GetAllCustomPropertyValues returned error: %v", err) + } + + want := []*CustomPropertyValue{ + { + PropertyName: "environment", + Value: String("production"), + }, + { + PropertyName: "service", + Value: String("web"), + }, + } + + if !cmp.Equal(customPropertyValues, want) { + t.Errorf("Repositories.GetAllCustomPropertyValues returned %+v, want %+v", customPropertyValues, want) + } + + const methodName = "GetAllCustomPropertyValues" + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.GetAllCustomPropertyValues(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +}