-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
139 lines (115 loc) · 2.8 KB
/
github.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
package main
import (
"context"
"errors"
"os"
"path/filepath"
"time"
"github.com/google/go-github/v52/github"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
)
const (
tokenName = "GITHUB_TOKEN"
)
var (
errTokenNotFound = errors.New("github token is not found")
)
type repository struct {
Name string `json:"name"`
FullName string `json:"fullname"`
Description string `json:"description"`
Owner string `json:"owner"`
Stars int `json:"stars"`
URL string `json:"url"`
DefaultBranch string `json:"default_branch"`
PushedAt time.Time `json:"pushed_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func getTokenFromHub() (string, error) {
// try to read hub config file.
cfg := filepath.Join(params.homedir, ".config", "hub")
f, err := os.Open(cfg)
if err != nil {
return "", err
}
defer f.Close()
d := yaml.NewDecoder(f)
m := new(map[string][]map[string]string)
if err := d.Decode(m); err != nil {
return "", err
}
items, ok := (*m)["github.com"]
if !ok || len(items) == 0 {
return "", errTokenNotFound
}
for _, v := range items {
if tkn, ok := v["oauth_token"]; ok {
return tkn, nil
}
}
return "", errTokenNotFound
}
func getToken() (string, error) {
tkn := os.Getenv(tokenName)
if tkn != "" {
return tkn, nil
}
return getTokenFromHub()
}
type listOptions struct {
user string
token string
}
type listOption func(*listOptions)
func withUser(user string) listOption {
return func(o *listOptions) {
o.user = user
}
}
func withToken(token string) listOption {
return func(o *listOptions) {
o.token = token
}
}
func listGithubRepositories(opts ...listOption) ([]repository, error) {
opt := &listOptions{}
for _, f := range opts {
f(opt)
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: opt.token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
lstopt := new(github.RepositoryListOptions)
lstopt.Page = 1
lstopt.PerPage = 100
repositories := make([]repository, 0)
for {
repos, res, err := client.Repositories.List(ctx, opt.user, lstopt)
if err != nil {
return nil, err
}
for _, repo := range repos {
r := repository{
Name: repo.GetName(),
FullName: repo.GetFullName(),
Description: repo.GetDescription(),
Owner: repo.GetOwner().GetLogin(),
Stars: repo.GetStargazersCount(),
PushedAt: repo.GetPushedAt().UTC(),
URL: repo.GetHTMLURL(),
DefaultBranch: repo.GetDefaultBranch(),
CreatedAt: repo.GetCreatedAt().UTC(),
UpdatedAt: repo.GetUpdatedAt().UTC(),
}
repositories = append(repositories, r)
}
if res.NextPage == 0 {
break
}
lstopt.Page = res.NextPage
}
return repositories, nil
}