-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghshurcool.go
100 lines (85 loc) · 2.57 KB
/
ghshurcool.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
package ghshurcool
import (
"context"
"fmt"
"os"
"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)
type Repository struct {
Name string `graphql:"name"`
ID string `graphql:"id"`
StarGazers struct {
TotalCount int `graphql:"totalCount"`
} `graphql:"stargazers"`
} // @name Node
type RepositoryEdge struct {
Cursor string `graphql:"cursor"`
Node Repository `graphql:"node"`
} // @name RepositoryEdge
type PageInfo struct {
EndCursor string `graphql:"endCursor"`
HasNextPage bool `graphql:"hasNextPage"`
HasPreviousPage bool `graphql:"hasPreviousPage"`
StartCursor string `graphql:"startCursor"`
} // @name PageInfo
type RepositoryConnection struct {
RepositoryConnection struct {
Edges []struct {
RepositoryEdge RepositoryEdge `graphql:"node"`
} `graphql:"edges"`
Nodes []struct {
Node Repository `graphql:"node"`
} `graphql:"nodes"`
PageInfo PageInfo `graphql:"pageInfo"`
} `graphql:"repositoryConnection(first: 10, after: $cursor)"`
} // @name RepositoryConnection
var repos struct {
Search struct {
Edges []struct {
Node struct {
SearchedRepository struct {
Name string `graphql:"name"`
ID string `graphql:"id"`
StarGazers struct {
TotalCount int `graphql:"totalCount"`
} `graphql:"stargazers"`
} `graphql:"... on Repository"`
}
}
} `graphql:"search(query: $query, type:REPOSITORY, first: $first)"`
}
var variables = map[string]any{}
//lint:ignore U1000 called by main
func FetchRepos(owner string) ([]string, []string, []int, error) {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient("https://api.github.com/graphql", httpClient)
// update the variables
variables["query"] = graphql.String(fmt.Sprintf("owner:%s", owner))
variables["first"] = graphql.Int(100)
// execute the query
err := client.Query(context.Background(), &repos, variables)
if err != nil {
return nil, nil, nil, err
}
// // uncomment these lines to see the size of the returned data
// buf := bytes.Buffer{}
// enc := gob.NewEncoder(&buf)
// err = enc.Encode(repos)
// if err != nil {
// return nil, nil, nil, err
// }
// fmt.Printf("Size of shurcool: %d\n", buf.Len())
var names []string
var stars []int
var ids []string
for _, edge := range repos.Search.Edges {
names = append(names, edge.Node.SearchedRepository.Name)
stars = append(stars, edge.Node.SearchedRepository.StarGazers.TotalCount)
ids = append(ids, edge.Node.SearchedRepository.ID)
}
return ids, names, stars, nil
}