-
Notifications
You must be signed in to change notification settings - Fork 46
/
example_gh_test.go
157 lines (148 loc) · 3.64 KB
/
example_gh_test.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package gh
import (
"fmt"
"log"
"os"
"time"
"github.com/cli/go-gh/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
)
// Execute 'gh issue list -R cli/cli', and print the output.
func ExampleExec() {
args := []string{"issue", "list", "-R", "cli/cli"}
stdOut, stdErr, err := Exec(args...)
if err != nil {
log.Fatal(err)
}
fmt.Println(stdOut.String())
fmt.Println(stdErr.String())
}
// Get tags from cli/cli repository using REST API.
func ExampleRESTClient_simple() {
client, err := RESTClient(nil)
if err != nil {
log.Fatal(err)
}
response := []struct{ Name string }{}
err = client.Get("repos/cli/cli/tags", &response)
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}
// Get tags from cli/cli repository using REST API.
// Specifying host, auth token, headers and logging to stdout.
func ExampleRESTClient_advanced() {
opts := api.ClientOptions{
Host: "github.com",
AuthToken: "xxxxxxxxxx", // Replace with valid auth token.
Headers: map[string]string{"Time-Zone": "America/Los_Angeles"},
Log: os.Stdout,
}
client, err := RESTClient(&opts)
if err != nil {
log.Fatal(err)
}
response := []struct{ Name string }{}
err = client.Get("repos/cli/cli/tags", &response)
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}
// Query tags from cli/cli repository using GQL API.
func ExampleGQLClient_simple() {
client, err := GQLClient(nil)
if err != nil {
log.Fatal(err)
}
var query struct {
Repository struct {
Refs struct {
Nodes []struct {
Name string
}
} `graphql:"refs(refPrefix: $refPrefix, last: $last)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"refPrefix": graphql.String("refs/tags/"),
"last": graphql.Int(30),
"owner": graphql.String("cli"),
"name": graphql.String("cli"),
}
err = client.Query("RepositoryTags", &query, variables)
if err != nil {
log.Fatal(err)
}
fmt.Println(query)
}
// Query tags from cli/cli repository using GQL API.
// Enable caching and request timeout.
func ExampleGQLClient_advanced() {
opts := api.ClientOptions{
EnableCache: true,
Timeout: 5 * time.Second,
}
client, err := GQLClient(&opts)
if err != nil {
log.Fatal(err)
}
var query struct {
Repository struct {
Refs struct {
Nodes []struct {
Name string
}
} `graphql:"refs(refPrefix: $refPrefix, last: $last)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"refPrefix": graphql.String("refs/tags/"),
"last": graphql.Int(30),
"owner": graphql.String("cli"),
"name": graphql.String("cli"),
}
err = client.Query("RepositoryTags", &query, variables)
if err != nil {
log.Fatal(err)
}
fmt.Println(query)
}
// Get repository for the current directory.
func ExampleCurrentRepository() {
repo, err := CurrentRepository()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s/%s/%s\n", repo.Host(), repo.Owner(), repo.Name())
}
// Example of using SkipResolution option to change a http.Client
// into a api.RESTClient.
func ExampleSkipResolution() {
host := "github.com"
httpOpts := api.ClientOptions{
Host: host,
}
httpClient, err := HTTPClient(&httpOpts)
if err != nil {
log.Fatal(err)
}
// Use SkipResolution as our http.Client does the handling of
// options and headers.
restOpts := api.ClientOptions{
SkipResolution: true,
Host: host,
Transport: httpClient.Transport,
}
restClient, err := RESTClient(&restOpts)
if err != nil {
log.Fatal(err)
}
response := []struct{ Name string }{}
err = restClient.Get("repos/cli/cli/tags", &response)
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}