-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.go
89 lines (69 loc) · 1.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
package scouter
import (
"context"
"log"
"net/http"
"github.com/google/go-github/github"
)
const (
UserMaxPerPage = 100
SearchMaxPerPage = 1000
)
func GetGithubUser(tc *http.Client, username string) (*github.User, error) {
client := github.NewClient(tc)
user, resp, err := client.Users.Get(context.Background(), username)
log.Print(resp)
return user, err
}
func FetchGithubUsers(tc *http.Client) ([]*github.User, error) {
client := github.NewClient(tc)
opt := &github.UserListOptions{
ListOptions: github.ListOptions{
Page: 0,
PerPage: UserMaxPerPage,
},
}
users, resp, err := client.Users.ListAll(context.Background(), opt)
log.Println(resp)
return users, err
}
// https://developer.github.com/v3/search/#search-users
// location:Taiwan&sort=joined&order=asc
func SearchGithubUsers(tc *http.Client, page int, query, sort, order string) (*github.UsersSearchResult, error) {
client := github.NewClient(tc)
opt := &github.SearchOptions{
Sort: sort,
Order: order,
TextMatch: false,
ListOptions: github.ListOptions{
Page: page,
PerPage: SearchMaxPerPage,
},
}
result, resp, err := client.Search.Users(context.Background(), query, opt)
log.Println(resp)
return result, err
}
func CountGithubUsers(tc *http.Client, query string) (int, error) {
client := github.NewClient(tc)
opt := &github.SearchOptions{}
result, resp, err := client.Search.Users(context.Background(), query, opt)
log.Println(resp)
return result.GetTotal(), err
}
func UpsertGithubUsers(tc *http.Client, users []github.User) error {
for _, user := range users {
detailUser, err := GetGithubUser(tc, *user.Login)
if err != nil {
return err
}
u := User{
ID: detailUser.GetID(),
User: detailUser,
}
if err := UpsertUser(u); err != nil {
return err
}
}
return nil
}