Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
79 lines (72 loc) · 1.86 KB
/
Copy pathgithub.go
File metadata and controls
79 lines (72 loc) · 1.86 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sort"
"strconv"
"time"
"gosuda.org/website/view/home"
)
// fetchGitHubStats lists the org's public, non-fork repositories.
// GITHUB_TOKEN is optional; it only raises the API rate limit.
func fetchGitHubStats(org string) (*home.GitHubStats, error) {
client := &http.Client{Timeout: 15 * time.Second}
stats := &home.GitHubStats{}
for page := 1; ; page++ {
req, err := http.NewRequest("GET", "https://api.github.com/orgs/"+org+"/repos?type=public&per_page=100&page="+strconv.Itoa(page), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
var repos []struct {
Name string `json:"name"`
Description string `json:"description"`
HTMLURL string `json:"html_url"`
Language string `json:"language"`
Stars int `json:"stargazers_count"`
Fork bool `json:"fork"`
Archived bool `json:"archived"`
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("github api: %s", resp.Status)
}
err = json.NewDecoder(resp.Body).Decode(&repos)
resp.Body.Close()
if err != nil {
return nil, err
}
for _, r := range repos {
if r.Fork {
continue
}
stats.RepoCount++
stats.StarCount += r.Stars
if !r.Archived {
stats.Repos = append(stats.Repos, home.Repo{
Name: r.Name,
Description: r.Description,
URL: r.HTMLURL,
Language: r.Language,
Stars: r.Stars,
})
}
}
if len(repos) < 100 {
break
}
}
sort.SliceStable(stats.Repos, func(i, j int) bool {
return stats.Repos[i].Stars > stats.Repos[j].Stars
})
return stats, nil
}