/
client.go
57 lines (47 loc) · 1.38 KB
/
client.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
package utils
import (
"context"
"github.com/google/go-github/v45/github"
"golang.org/x/oauth2"
)
// GithubClient creates a Github Client from the provided OAuth token
func (c *Config) GithubClient() *github.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.AuthToken})
tokenClient := oauth2.NewClient(context.Background(), tokenSource)
return github.NewClient(tokenClient)
}
// GetUser finds the current username
func (c *Config) GetUser() (string, error) {
client := c.GithubClient()
user, _, err := client.Users.Get(context.Background(), "")
if err != nil {
return "", err
}
return *user.Login, nil
}
// FilteredRepos loads a list of repositories and removes ones that match a filter
func FilteredRepos(config Config) ([]*github.Repository, error) {
filter, err := NewFilter(config)
if err != nil {
return []*github.Repository{}, err
}
client := config.GithubClient()
var repos []*github.Repository
options := &github.RepositoryListOptions{ListOptions: github.ListOptions{PerPage: 100}}
for {
repoPage, resp, err := client.Repositories.List(context.Background(), "", options)
if err != nil {
return []*github.Repository{}, err
}
for _, repo := range repoPage {
if !filter.Match(*repo.FullName) {
repos = append(repos, repo)
}
}
if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}
return repos, nil
}