From b07ef07faf5fb16e93882fab7c21e0a38e26358d Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 14:59:11 +0000 Subject: [PATCH 1/6] Tests for EnvironmentListOptions This commit updates existing tests for ListEnvironments to ensure that they will be compatible with the EnvironmentListOptions implementation. --- github/repos_environments_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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) } From bda70c028a7c92367937bfc1314a06c20d2813ae Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 15:01:26 +0000 Subject: [PATCH 2/6] EnvironmentListOptions implementation This commit implements EnvironmentListOptions for the ListEnvironments method by adding a new parameter and using the addOptions function. Additionally I've created a new struct called EnvironmentListOptions and embedded ListOptions with the idea that this could be expanded one day. --- github/repos_environments.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 { From 32b107b4ac81fcb011916fab83c995c80d1d56dd Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 15:02:11 +0000 Subject: [PATCH 3/6] ListEnvironments example This commit adds an example to show how to work with ListEnvironments and it's new opts parameter. --- example/listenvironments/main.go | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 example/listenvironments/main.go diff --git a/example/listenvironments/main.go b/example/listenvironments/main.go new file mode 100644 index 0000000000..8ba752d1b9 --- /dev/null +++ b/example/listenvironments/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/google/go-github/v42/github" + "golang.org/x/oauth2" +) + +// 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 . +func main() { + + var client *github.Client + var ctx = context.Background() + + 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}) + 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) + os.Exit(1) + } + + if len(envResponse.Environments) != expectedPageSize { + log.Fatal("Unexpected number of environments returned") + os.Exit(1) + } + + // The number of environments here should be equal to expectedPageSize + fmt.Printf("%d environments returned\n", len(envResponse.Environments)) +} From d92496fb75dfb0de279106896c0db1f2171a40b1 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 15:45:58 +0000 Subject: [PATCH 4/6] Address PR comments Made some minor change to this example that include removing os.Exit(), fixing some unnecessary variable declarations and godoc improvements. --- example/listenvironments/main.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/example/listenvironments/main.go b/example/listenvironments/main.go index 8ba752d1b9..c10f8c7681 100644 --- a/example/listenvironments/main.go +++ b/example/listenvironments/main.go @@ -1,3 +1,14 @@ +// Copyright 2015 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 ( @@ -10,14 +21,8 @@ import ( "golang.org/x/oauth2" ) -// 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 . func main() { - var client *github.Client - var ctx = context.Background() - token := os.Getenv("GITHUB_AUTH_TOKEN") repo := os.Getenv("GITHUB_REPOSITORY_NAME") owner := os.Getenv("GITHUB_REPOSITORY_OWNER") @@ -27,8 +32,10 @@ func main() { } ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) + + ctx := context.Background() tc := oauth2.NewClient(ctx, ts) - client = github.NewClient(tc) + client := github.NewClient(tc) expectedPageSize := 2 @@ -36,12 +43,10 @@ func main() { envResponse, _, err := client.Repositories.ListEnvironments(ctx, owner, repo, opts) if err != nil { log.Fatal(err) - os.Exit(1) } if len(envResponse.Environments) != expectedPageSize { log.Fatal("Unexpected number of environments returned") - os.Exit(1) } // The number of environments here should be equal to expectedPageSize From 316ee55e4cdcfeebf15616cfcb7784cc9a58d950 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 15:55:42 +0000 Subject: [PATCH 5/6] Resolved a golangci-lint issue "unnecessary leading newline (whitespace)" --- example/listenvironments/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/example/listenvironments/main.go b/example/listenvironments/main.go index c10f8c7681..f17151a275 100644 --- a/example/listenvironments/main.go +++ b/example/listenvironments/main.go @@ -22,7 +22,6 @@ import ( ) func main() { - token := os.Getenv("GITHUB_AUTH_TOKEN") repo := os.Getenv("GITHUB_REPOSITORY_NAME") owner := os.Getenv("GITHUB_REPOSITORY_OWNER") From ee239cd8339bd1a3dbb7708edd2bcbf6a52f4517 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Mon, 17 Jan 2022 15:59:17 +0000 Subject: [PATCH 6/6] Update example/listenvironments/main.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- example/listenvironments/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/listenvironments/main.go b/example/listenvironments/main.go index f17151a275..11f9d8023c 100644 --- a/example/listenvironments/main.go +++ b/example/listenvironments/main.go @@ -1,4 +1,4 @@ -// Copyright 2015 The go-github AUTHORS. All rights reserved. +// 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.