Skip to content

Commit

Permalink
Add GetAllCustomPropertyValues for repositories (google#3020)
Browse files Browse the repository at this point in the history
Fixes: google#3014.
  • Loading branch information
liaodaniel authored and gmlewis committed Dec 19, 2023
1 parent 25e042b commit 6e03d4e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
33 changes: 33 additions & 0 deletions github/repos_properties.go
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions github/repos_properties_test.go
Original file line number Diff line number Diff line change
@@ -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
})
}

0 comments on commit 6e03d4e

Please sign in to comment.