Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions example/listenvironments/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 (
"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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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