-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgh.go
92 lines (83 loc) · 1.72 KB
/
gh.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
package gh
import (
"context"
"fmt"
"github.com/google/go-github/v58/github"
"github.com/k1LoW/go-github-client/v58/factory"
)
type Gh struct {
client *github.Client
}
func New() (*Gh, error) {
client, err := factory.NewGithubClient()
if err != nil {
return nil, err
}
return &Gh{
client: client,
}, nil
}
func (g *Gh) Client() *github.Client {
return g.client
}
func (g *Gh) Repositories(ctx context.Context, owner string) ([]string, error) {
repos := []string{}
u, _, err := g.client.Users.Get(ctx, owner)
if err != nil {
return nil, err
}
if u.GetType() == "User" {
// User
page := 1
for {
rs, res, err := g.client.Repositories.List(ctx, owner, &github.RepositoryListOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
return nil, err
}
for _, r := range rs {
repos = append(repos, *r.Name)
}
if res.NextPage == 0 {
break
}
page = res.NextPage
}
} else {
// Organization
page := 1
for {
rs, res, err := g.client.Repositories.ListByOrg(ctx, owner, &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
return nil, err
}
for _, r := range rs {
repos = append(repos, *r.Name)
}
if res.NextPage == 0 {
break
}
page = res.NextPage
}
}
return repos, nil
}
func (g *Gh) ContentURL(ctx context.Context, owner, repo, path string) (string, error) {
fc, _, _, err := g.client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{})
if err != nil {
return "", err
}
if fc == nil {
return "", fmt.Errorf("%s is not file", path)
}
return fc.GetHTMLURL(), nil
}