Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 0030f0b

Browse files
committed
add repo analysis
1 parent 73a33cb commit 0030f0b

3 files changed

Lines changed: 81 additions & 36 deletions

File tree

api.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
6+
"github.com/kataras/iris/v12"
57
)
68

79
func GetUser(username string) (data map[string]interface{}, err error) {
@@ -24,6 +26,13 @@ func GetLanguages(username string, repo string) (data map[string]interface{}, er
2426
return data, err
2527
}
2628

29+
func getLicense(license interface{}) string {
30+
if license != nil {
31+
return license.(map[string]interface{})["spdx_id"].(string)
32+
}
33+
return "Empty"
34+
}
35+
2736
func GetUserExist(username string) bool {
2837
res, err := GetUser(username)
2938
if err != nil {
@@ -84,3 +93,58 @@ func CollectLanguages(username string, repos []interface{}) (data map[string]flo
8493

8594
return data, nil
8695
}
96+
97+
func AnalysisUser(username string) (data iris.Map, err error, code int) {
98+
if GetUserExist(username) {
99+
res, err := GetUser(username)
100+
if err != nil {
101+
return nil, err, iris.StatusInternalServerError
102+
}
103+
repos, err := iterRepos(username)
104+
if err != nil {
105+
return nil, err, iris.StatusInternalServerError
106+
}
107+
languages, err := CollectLanguages(username, repos)
108+
if err != nil {
109+
return nil, err, iris.StatusInternalServerError
110+
}
111+
return iris.Map{
112+
"username": username,
113+
"location": res["location"],
114+
"org": res["type"] != "User",
115+
"repos": res["public_repos"],
116+
"follower": ScaleConvert(res["followers"].(float64), true),
117+
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
118+
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
119+
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
120+
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
121+
"languages": languages,
122+
}, nil, iris.StatusOK
123+
}
124+
return nil, errors.New("user not found"), iris.StatusNotFound
125+
}
126+
127+
func AnalysisRepo(username string, repo string) (data iris.Map, err error, code int) {
128+
if GetRepoExist(username, repo) {
129+
res, err := GetRepo(username, repo)
130+
if err != nil {
131+
return nil, err, iris.StatusInternalServerError
132+
}
133+
languages, err := GetLanguages(username, repo)
134+
if err != nil {
135+
return nil, err, iris.StatusInternalServerError
136+
}
137+
return iris.Map{
138+
"username": username,
139+
"repo": repo,
140+
"size": SizeConvert(res["size"].(float64), 1),
141+
"stars": ScaleConvert(res["stargazers_count"].(float64), true),
142+
"forks": ScaleConvert(res["forks_count"].(float64), true),
143+
"watchers": ScaleConvert(res["watchers_count"].(float64), true),
144+
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
145+
"license": getLicense(res["license"]),
146+
"languages": languages,
147+
}, nil, iris.StatusOK
148+
}
149+
return nil, errors.New("repo not found"), iris.StatusNotFound
150+
}

main.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
func main() {
99
app := iris.Default()
1010
{
11-
app.Get("/user/{username:string}", AnalyseUser)
12-
app.Get("/repo/{username:string}/{repo:string}", AnalyseRepo)
11+
app.Get("/user/{username:string}", UserAPI)
12+
app.Get("/repo/{username:string}/{repo:string}", RepoAPI)
1313
}
1414

1515
logger.SetLevel(logrus.DebugLevel)
@@ -21,41 +21,22 @@ func main() {
2121
app.Listen(":8080")
2222
}
2323

24-
func AnalyseUser(ctx iris.Context) {
24+
func UserAPI(ctx iris.Context) {
2525
username := ctx.Params().Get("username")
26-
if GetUserExist(username) {
27-
res, err := GetUser(username)
28-
if err != nil {
29-
ThrowError(ctx, iris.StatusInternalServerError, err.Error())
30-
return
31-
}
32-
repos, err := iterRepos(username)
33-
if err != nil {
34-
ThrowError(ctx, iris.StatusInternalServerError, err.Error())
35-
return
36-
}
37-
langs, err := CollectLanguages(username, repos)
38-
if err != nil {
39-
ThrowError(ctx, iris.StatusInternalServerError, err.Error())
40-
return
41-
}
42-
ctx.JSON(iris.Map{
43-
"username": username,
44-
"location": res["location"],
45-
"org": res["type"] != "User",
46-
"repos": res["public_repos"],
47-
"follower": ScaleConvert(res["followers"].(float64), true),
48-
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
49-
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
50-
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
51-
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
52-
"languages": langs,
53-
})
54-
return
26+
data, err, code := AnalysisUser(username)
27+
if err != nil {
28+
ThrowError(ctx, err.Error(), code)
29+
} else {
30+
ctx.JSON(data)
5531
}
56-
ThrowError(ctx, iris.StatusNotFound, "User not found.")
5732
}
5833

59-
func AnalyseRepo(ctx iris.Context) {
60-
return
34+
func RepoAPI(ctx iris.Context) {
35+
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
36+
data, err, code := AnalysisRepo(username, repo)
37+
if err != nil {
38+
ThrowError(ctx, err.Error(), code)
39+
} else {
40+
ctx.JSON(data)
41+
}
6142
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func Get(uri string, ptr interface{}) (err error) {
7575
return nil
7676
}
7777

78-
func ThrowError(ctx iris.Context, code int, message string) {
78+
func ThrowError(ctx iris.Context, message string, code int) {
7979
ctx.StatusCode(code)
8080
ctx.JSON(iris.Map{
8181
"message": message,

0 commit comments

Comments
 (0)