/
main.go
54 lines (43 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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/v46/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))
}