forked from yyle88/yyle88
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yyle88.go
94 lines (80 loc) · 2.57 KB
/
yyle88.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
package yyle88
import (
"net/http"
"os"
"strings"
"time"
restyv2 "github.com/go-resty/resty/v2"
"github.com/yyle88/erero"
"github.com/yyle88/neatjson/neatjsons"
"github.com/yyle88/sortslice"
"github.com/yyle88/zaplog"
)
type Repo struct {
Name string `json:"name"`
Link string `json:"html_url"`
Desc string `json:"description"`
Stargazers int `json:"stargazers_count"`
PushedAt time.Time `json:"pushed_at"`
}
func GetGithubRepos(username string) ([]*Repo, error) {
var repos []*Repo
// 从环境变量读取 GitHub Token
githubToken := os.Getenv("GITHUB_TOKEN")
// 使用 Token 添加 Authorization 请求头
request := restyv2.New().SetTimeout(time.Minute).R()
if githubToken != "" {
request = request.SetHeader("Authorization", "token "+githubToken)
}
response, err := request.
SetPathParam("username", username).
SetResult(&repos).
Get("https://api.github.com/users/{username}/repos")
if err != nil {
return nil, erero.Wro(err)
}
if response.StatusCode() != http.StatusOK {
return nil, erero.New(response.Status())
}
sortslice.SortVStable(repos, func(a, b *Repo) bool {
if strings.HasPrefix(a.Name, ".") || strings.HasPrefix(b.Name, ".") {
return !strings.HasPrefix(a.Name, ".")
} else if a.Name == username || b.Name == username {
return a.Name != username //当是主页项目时把它排在最后面,避免排的太靠前占据重要的位置
} else if a.Stargazers > b.Stargazers {
return true //星多者排前面
} else if a.Stargazers < b.Stargazers {
return false //星少者排后面
} else {
return a.PushedAt.After(b.PushedAt) //同样星星时最近有更新的排前面
}
})
zaplog.SUG.Info(neatjsons.S(repos))
return repos, nil
}
type Organization struct {
Name string `json:"login"` // 组织名称
Link string `json:"html_url"` // 组织链接
}
func GetGithubOrganizations(username string) ([]*Organization, error) {
var organizations []*Organization
// 从环境变量读取 GitHub Token
githubToken := os.Getenv("GITHUB_TOKEN")
// 使用 Token 添加 Authorization 请求头
request := restyv2.New().SetTimeout(time.Minute).R()
if githubToken != "" {
request = request.SetHeader("Authorization", "Bearer "+githubToken)
}
// 请求获取用户的组织信息
response, err := request.
SetPathParam("username", username).
SetResult(&organizations).
Get("https://api.github.com/users/{username}/orgs")
if err != nil {
return nil, erero.Wro(err)
}
if response.StatusCode() != http.StatusOK {
return nil, erero.New(response.Status())
}
return organizations, nil
}