-
Notifications
You must be signed in to change notification settings - Fork 48
/
example_gh_test.go
300 lines (285 loc) · 7.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package gh_test
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"time"
gh "github.com/cli/go-gh/v2"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/cli/go-gh/v2/pkg/repository"
"github.com/cli/go-gh/v2/pkg/tableprinter"
"github.com/cli/go-gh/v2/pkg/term"
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 := gh.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 ExampleDefaultRESTClient() {
client, err := api.DefaultRESTClient()
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() {
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 := api.NewRESTClient(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)
}
// Get release asset from cli/cli repository using REST API.
func ExampleRESTClient_request() {
opts := api.ClientOptions{
Headers: map[string]string{"Accept": "application/octet-stream"},
}
client, err := api.NewRESTClient(opts)
if err != nil {
log.Fatal(err)
}
// URL to cli/cli release v2.14.2 checksums.txt
assetURL := "repos/cli/cli/releases/assets/71589494"
response, err := client.Request(http.MethodGet, assetURL, nil)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
f, err := os.CreateTemp("", "*_checksums.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = io.Copy(f, response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Asset downloaded to %s\n", f.Name())
}
// Get releases from cli/cli repository using REST API with paginated results.
func ExampleRESTClient_pagination() {
var linkRE = regexp.MustCompile(`<([^>]+)>;\s*rel="([^"]+)"`)
findNextPage := func(response *http.Response) (string, bool) {
for _, m := range linkRE.FindAllStringSubmatch(response.Header.Get("Link"), -1) {
if len(m) > 2 && m[2] == "next" {
return m[1], true
}
}
return "", false
}
client, err := api.DefaultRESTClient()
if err != nil {
log.Fatal(err)
}
requestPath := "repos/cli/cli/releases"
page := 1
for {
response, err := client.Request(http.MethodGet, requestPath, nil)
if err != nil {
log.Fatal(err)
}
data := []struct{ Name string }{}
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&data)
if err != nil {
log.Fatal(err)
}
if err := response.Body.Close(); err != nil {
log.Fatal(err)
}
fmt.Printf("Page: %d\n", page)
fmt.Println(data)
var hasNextPage bool
if requestPath, hasNextPage = findNextPage(response); !hasNextPage {
break
}
page++
}
}
// Query tags from cli/cli repository using GraphQL API.
func ExampleDefaultGraphQLClient() {
client, err := api.DefaultGraphQLClient()
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 GraphQL API.
// Enable caching and request timeout.
func ExampleGraphQLClient() {
opts := api.ClientOptions{
EnableCache: true,
Timeout: 5 * time.Second,
}
client, err := api.NewGraphQLClient(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)
}
// Add a star to the cli/go-gh repository using the GraphQL API.
func ExampleGraphQLClient_mutate() {
client, err := api.DefaultGraphQLClient()
if err != nil {
log.Fatal(err)
}
var mutation struct {
AddStar struct {
Starrable struct {
Repository struct {
StargazerCount int
} `graphql:"... on Repository"`
Gist struct {
StargazerCount int
} `graphql:"... on Gist"`
}
} `graphql:"addStar(input: $input)"`
}
// Note that the shurcooL/githubv4 package has defined input structs generated from the
// GraphQL schema that can be used instead of writing your own.
type AddStarInput struct {
StarrableID string `json:"starrableId"`
}
variables := map[string]interface{}{
"input": AddStarInput{
StarrableID: "R_kgDOF_MgQQ",
},
}
err = client.Mutate("AddStar", &mutation, variables)
if err != nil {
log.Fatal(err)
}
fmt.Println(mutation.AddStar.Starrable.Repository.StargazerCount)
}
// Query releases from cli/cli repository using GraphQL API with paginated results.
func ExampleGraphQLClient_pagination() {
client, err := api.DefaultGraphQLClient()
if err != nil {
log.Fatal(err)
}
var query struct {
Repository struct {
Releases struct {
Nodes []struct {
Name string
}
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"releases(first: 30, after: $endCursor)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String("cli"),
"name": graphql.String("cli"),
"endCursor": (*graphql.String)(nil),
}
page := 1
for {
if err := client.Query("RepositoryReleases", &query, variables); err != nil {
log.Fatal(err)
}
fmt.Printf("Page: %d\n", page)
fmt.Println(query.Repository.Releases.Nodes)
if !query.Repository.Releases.PageInfo.HasNextPage {
break
}
variables["endCursor"] = graphql.String(query.Repository.Releases.PageInfo.EndCursor)
page++
}
}
// Get repository for the current directory.
func ExampleCurrent() {
repo, err := repository.Current()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s/%s/%s\n", repo.Host, repo.Owner, repo.Name)
}
// Print tabular data to a terminal or in machine-readable format for scripts.
func ExampleTablePrinter() {
terminal := term.FromEnv()
termWidth, _, _ := terminal.Size()
t := tableprinter.New(terminal.Out(), terminal.IsTerminalOutput(), termWidth)
red := func(s string) string {
return "\x1b[31m" + s + "\x1b[m"
}
// add a field that will render with color and will not be auto-truncated
t.AddField("1", tableprinter.WithColor(red), tableprinter.WithTruncate(nil))
t.AddField("hello")
t.EndRow()
t.AddField("2")
t.AddField("world")
t.EndRow()
if err := t.Render(); err != nil {
log.Fatal(err)
}
}