Debian Codegraph @

example/listenvironments/main.go

raw
1 // Copyright 2022 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // listenvironments is an example of how to use ListEnvironments method with EnvironmentListOptions.
7 // It's runnable with the following command:
8 //
9 // export GITHUB_TOKEN=your_token
10 // export GITHUB_REPOSITORY_OWNER=your_owner
11 // export GITHUB_REPOSITORY_NAME=your_repo
12 // go run .
13 package main
14
15 import (
16 "context"
17 "fmt"
18 "log"
19 "os"
20
21 "github.com/google/go-github/v60/github"
22 )
23
24 func main() {
25 token := os.Getenv("GITHUB_AUTH_TOKEN")
26 repo := os.Getenv("GITHUB_REPOSITORY_NAME")
27 owner := os.Getenv("GITHUB_REPOSITORY_OWNER")
28
29 if token == "" {
30 log.Fatal("Unauthorized: No token present")
31 }
32
33 ctx := context.Background()
34 client := github.NewClient(nil).WithAuthToken(token)
35
36 expectedPageSize := 2
37
38 opts := &github.EnvironmentListOptions{ListOptions: github.ListOptions{PerPage: expectedPageSize}}
39 envResponse, _, err := client.Repositories.ListEnvironments(ctx, owner, repo, opts)
40 if err != nil {
41 log.Fatal(err)
42 }
43
44 if len(envResponse.Environments) != expectedPageSize {
45 log.Fatal("Unexpected number of environments returned")
46 }
47
48 // The number of environments here should be equal to expectedPageSize
49 fmt.Printf("%d environments returned\n", len(envResponse.Environments))
50 }