Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list options support for environments (#2258)
Fixes: #2257 .
- Loading branch information
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters