-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (115 loc) · 3.36 KB
/
main.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
137
138
139
140
141
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/cli/go-gh"
)
type RepoStats struct {
FullName string `json:"full_name"`
StargazersCount int `json:"stargazers_count"`
ForksCount int `json:"forks_count"`
OpenIssuesCount int `json:"open_issues_count"`
WatchersCount int `json:"watchers_count"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
}
func getContributorsFromPage(owner, repo string, pageNum int) []interface{} {
ghArgs := []string{"api", fmt.Sprintf("repos/%s/%s/contributors?page=%d", owner, repo, pageNum)}
stdOut, _, err := gh.Exec(ghArgs...)
if err != nil {
fmt.Printf("Error fetching contributors: %s\n", err)
return nil
}
var contributors []interface{}
if err := json.Unmarshal(stdOut.Bytes(), &contributors); err != nil {
fmt.Printf("Error parsing JSON: %s\n", err)
return nil
}
return contributors
}
func getCommitsFromPage(owner, repo string, pageNum int) []interface{} {
ghArgs := []string{"api", fmt.Sprintf("repos/%s/%s/commits?page=%d", owner, repo, pageNum)}
stdOut, _, err := gh.Exec(ghArgs...)
if err != nil {
fmt.Printf("Error fetching commits: %s\n", err)
return nil
}
var commits []interface{}
if err := json.Unmarshal(stdOut.Bytes(), &commits); err != nil {
fmt.Printf("Error parsing JSON: %s\n", err)
return nil
}
return commits
}
func fetchContributorsCount(owner, repo string) int {
pageNum := 1
totalContributors := 0
for {
contributors := getContributorsFromPage(owner, repo, pageNum)
if len(contributors) == 0 {
break
}
totalContributors += len(contributors)
pageNum++
}
return totalContributors
}
func fetchTotalCommits(owner, repo string) int {
pageNum := 1
totalCommits := 0
for {
commits := getCommitsFromPage(owner, repo, pageNum)
if len(commits) == 0 {
break
}
totalCommits += len(commits)
pageNum++
}
return totalCommits
}
func main() {
fmt.Printf("Debug: Received args: %v\n", os.Args)
var args []string
var owner, repo string
if len(os.Args) == 2 {
args = strings.Split(os.Args[1], " ")
} else {
args = os.Args[1:]
}
for i, arg := range args {
if arg == "-owner" && i+1 < len(args) {
owner = args[i+1]
} else if arg == "-repo" && i+1 < len(args) {
repo = args[i+1]
}
}
if owner == "" || repo == "" {
fmt.Println("Usage: gh repo-stats -owner <owner> -repo <repo>")
os.Exit(1)
}
fmt.Printf("Debug: Owner: %s, Repo: %s\n", owner, repo)
ghArgs := []string{"api", fmt.Sprintf("repos/%s/%s", owner, repo)}
stdOut, _, err := gh.Exec(ghArgs...)
if err != nil {
fmt.Printf("Error fetching repository data: %s\n", err)
return
}
var stats RepoStats
if err := json.Unmarshal(stdOut.Bytes(), &stats); err != nil {
fmt.Printf("Error parsing JSON: %s\n", err)
return
}
contributorsCount := fetchContributorsCount(owner, repo)
totalCommits := fetchTotalCommits(owner, repo)
fmt.Printf("Repository: %s\n", stats.FullName)
fmt.Printf("🌟 Stars: %d\n", stats.StargazersCount)
fmt.Printf("🍴 Forks: %d\n", stats.ForksCount)
fmt.Printf("🔓 Open Issues: %d\n", stats.OpenIssuesCount)
fmt.Printf("👀 Watchers: %d\n", stats.WatchersCount)
fmt.Printf("🔖 Default Branch: %s\n", stats.DefaultBranch)
fmt.Printf("📦 Archived: %t\n", stats.Archived)
fmt.Printf("👥 Contributors: %d\n", contributorsCount)
fmt.Printf("📌 Total Commits: %d\n", totalCommits)
}