diff --git a/example/listenvironments/main.go b/example/listenvironments/main.go new file mode 100644 index 0000000000..11f9d8023c --- /dev/null +++ b/example/listenvironments/main.go @@ -0,0 +1,53 @@ +// Copyright 2022 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. + +// listenvironments is an example of how to use ListEnvironments method with EnvironmentListOptions. +// It's runnable with the following command: +// export GITHUB_TOKEN=your_token +// export GITHUB_REPOSITORY_OWNER=your_owner +// export GITHUB_REPOSITORY_NAME=your_repo +// go run . +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/google/go-github/v42/github" + "golang.org/x/oauth2" +) + +func main() { + token := os.Getenv("GITHUB_AUTH_TOKEN") + repo := os.Getenv("GITHUB_REPOSITORY_NAME") + owner := os.Getenv("GITHUB_REPOSITORY_OWNER") + + if token == "" { + log.Fatal("Unauthorized: No token present") + } + + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) + + ctx := context.Background() + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + expectedPageSize := 2 + + opts := &github.EnvironmentListOptions{ListOptions: github.ListOptions{PerPage: expectedPageSize}} + envResponse, _, err := client.Repositories.ListEnvironments(ctx, owner, repo, opts) + if err != nil { + log.Fatal(err) + } + + if len(envResponse.Environments) != expectedPageSize { + log.Fatal("Unexpected number of environments returned") + } + + // The number of environments here should be equal to expectedPageSize + fmt.Printf("%d environments returned\n", len(envResponse.Environments)) +} diff --git a/github/repos_environments.go b/github/repos_environments.go index 25cb005e8d..5dd454289c 100644 --- a/github/repos_environments.go +++ b/github/repos_environments.go @@ -63,6 +63,12 @@ type RequiredReviewer struct { Reviewer interface{} `json:"reviewer,omitempty"` } +// EnvironmentListOptions specifies the optional parameters to the +// RepositoriesService.ListEnvironments method. +type EnvironmentListOptions struct { + ListOptions +} + // UnmarshalJSON implements the json.Unmarshaler interface. // This helps us handle the fact that RequiredReviewer can have either a User or Team type reviewer field. func (r *RequiredReviewer) UnmarshalJSON(data []byte) error { @@ -99,8 +105,12 @@ func (r *RequiredReviewer) UnmarshalJSON(data []byte) error { // ListEnvironments lists all environments for a repository. // // GitHub API docs: https://docs.github.com/en/rest/reference/repos#get-all-environments -func (s *RepositoriesService) ListEnvironments(ctx context.Context, owner, repo string) (*EnvResponse, *Response, error) { +func (s *RepositoriesService) ListEnvironments(ctx context.Context, owner, repo string, opts *EnvironmentListOptions) (*EnvResponse, *Response, error) { u := fmt.Sprintf("repos/%s/%s/environments", owner, repo) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } req, err := s.client.NewRequest("GET", u, nil) if err != nil { diff --git a/github/repos_environments_test.go b/github/repos_environments_test.go index 18a30ad690..59b3f8b468 100644 --- a/github/repos_environments_test.go +++ b/github/repos_environments_test.go @@ -108,8 +108,14 @@ func TestRepositoriesService_ListEnvironments(t *testing.T) { fmt.Fprint(w, `{"total_count":1, "environments":[{"id":1}, {"id": 2}]}`) }) + opt := &EnvironmentListOptions{ + ListOptions: ListOptions{ + Page: 2, + PerPage: 2, + }, + } ctx := context.Background() - environments, _, err := client.Repositories.ListEnvironments(ctx, "o", "r") + environments, _, err := client.Repositories.ListEnvironments(ctx, "o", "r", opt) if err != nil { t.Errorf("Repositories.ListEnvironments returned error: %v", err) } @@ -120,12 +126,12 @@ func TestRepositoriesService_ListEnvironments(t *testing.T) { const methodName = "ListEnvironments" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Repositories.ListEnvironments(ctx, "\n", "\n") + _, _, err = client.Repositories.ListEnvironments(ctx, "\n", "\n", opt) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Repositories.ListEnvironments(ctx, "o", "r") + got, resp, err := client.Repositories.ListEnvironments(ctx, "o", "r", opt) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) }