forked from yyle88/yyle88
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyyle88.go
136 lines (117 loc) · 4.08 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
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
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())
}
zaplog.SUG.Debugln(neatjsons.SxB(response.Body()))
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.Debugln(neatjsons.S(repos))
return repos, nil
}
type Organization struct {
Name string `json:"login"` // 组织名称
Link string `json:"url"` // 组织链接
ReposLink string `json:"repos_url"` // 组织链接
}
func GetOrganizations(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())
}
zaplog.SUG.Debugln(neatjsons.SxB(response.Body()))
zaplog.SUG.Debugln(neatjsons.S(organizations))
return organizations, nil
}
func GetOrganizationRepos(orgName 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("org", orgName).
SetResult(&repos).
Get("https://api.github.com/orgs/{org}/repos")
if err != nil {
return nil, erero.Wro(err)
}
if response.StatusCode() != http.StatusOK {
return nil, erero.New(response.Status())
}
zaplog.SUG.Debugln(neatjsons.SxB(response.Body()))
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 == orgName || b.Name == orgName {
return a.Name == orgName //当项目名称与组织名称相同时,说明是主项目,因此需要放在前面
} 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.Debugln(neatjsons.S(repos))
return repos, nil
}