Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list options support for environments #2258

Merged
merged 6 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions 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
chelnak marked this conversation as resolved.
Show resolved Hide resolved

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))
}
12 changes: 11 additions & 1 deletion github/repos_environments.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions github/repos_environments_test.go
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down