generated from HariSekhon/Template-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·243 lines (198 loc) · 6.14 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
///bin/sh -c true; exec /usr/bin/env go run "$0" "$@"
// vim:ts=4:sts=4:sw=4:noet
//
// Author: Hari Sekhon
// Date: 2024-10-02 05:17:53 +0300 (Wed, 02 Oct 2024)
//
// https///github.com/HariSekhon/GitHub-Repos-MermaidJS-Gantt-Chart
//
// License: see accompanying Hari Sekhon LICENSE file
//
// If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
//
// https://www.linkedin.com/in/HariSekhon
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"sort"
"strings"
"time"
)
type GitHubRepo struct {
Name string `json:"name"`
Fork bool `json:"fork"`
CreatedAt time.Time `json:"created_at"`
PushedAt time.Time `json:"pushed_at"`
}
type GitHubCommit struct {
Commit struct {
Author struct {
Date time.Time `json:"date"`
} `json:"author"`
} `json:"commit"`
}
func main() {
helpFlag := flag.Bool("help", false, "Show help message")
flag.Parse()
if *helpFlag {
fmt.Println(`
Generates a Mermaid.js Gantt chart of a GitHub user's public repos active dates
using each repo's created and pushed dates
Arguments:
<github_username> GitHub username for which to fetch the repositories
Environment Variables:
GH_TOKEN GitHub token (preferred as it matches GitHub CLI environment variable)
GITHUB_TOKEN Fallback GitHub token (if GH_TOKEN is not set)
Usage: go run main.go <github_username>
`)
os.Exit(3)
}
if len(os.Args) < 2 {
log.Fatal("Usage: go run main.go <github_username>")
}
username := os.Args[1]
githubToken := os.Getenv("GH_TOKEN")
if githubToken == "" {
githubToken = os.Getenv("GITHUB_TOKEN")
}
if githubToken == "" {
log.Fatal("GitHub token not found in environment variables (GH_TOKEN or GITHUB_TOKEN)")
}
repos, err := fetchRepos(username, githubToken)
if err != nil {
log.Fatalf("Error fetching repos: %v\n", err)
}
log.Info("Generating Gantt Chart")
ganttChart := generateGanttChart(repos)
initFile := "init.mmd"
log.Info("Reading Gantt Chart Config from ", initFile)
ganttConfigBytes, err := ioutil.ReadFile(initFile)
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
ganttConfig := string(ganttConfigBytes)
filename := "gantt_chart.mmd"
log.Info("Writing to ", filename)
err = writeGanttChartToFile(ganttConfig + ganttChart, filename)
if err != nil {
log.Fatalf("Error writing Gantt chart to file: %v", err)
}
log.Info("Markdown file with Mermaid.js Gantt chart generated successfully")
}
func fetchRepos(username, token string) ([]GitHubRepo, error) {
var allRepos []GitHubRepo
page := 1
log.Info("Fetching public GitHub repos for user: ", username)
for {
url := fmt.Sprintf("https://api.github.com/users/%s/repos?per_page=100&page=%d", username, page)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "token " + token)
client := &http.Client{}
log.Info("Fetching GitHub repos page: ", page)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var repos []GitHubRepo
err = json.Unmarshal(body, &repos)
if err != nil {
return nil, err
}
if len(repos) == 0 {
break
}
// filter out forked repos
for _, repo := range repos {
if !repo.Fork {
allRepos = append(allRepos, repo)
}
}
if resp.Header.Get("Link") == "" || ! hasNextPage(resp.Header.Get("Link")) {
break
}
page++
}
return allRepos, nil
}
func hasNextPage(linkHeader string) bool {
// The Link header contains links to the next, previous, first, and last pages of results
// Example: <https://api.github.com/user/repos?page=2>; rel="next",
// <https://api.github.com/user/repos?page=34>; rel="last"
return strings.Contains(linkHeader, `rel="next"`)
}
func fetchFirstAndLastCommit(owner, repo, token string) (time.Time, time.Time, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits", owner, repo)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "token " + token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return time.Time{}, time.Time{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return time.Time{}, time.Time{}, err
}
var commits []GitHubCommit
err = json.Unmarshal(body, &commits)
if err != nil {
return time.Time{}, time.Time{}, err
}
// first commit is the last in the array (reverse chronological)
firstCommit := commits[len(commits)-1].Commit.Author.Date
// last commit is the first one in the array
lastCommit := commits[0].Commit.Author.Date
return firstCommit, lastCommit, nil
}
func generateGanttChart(repos []GitHubRepo) string {
// sort the repos by start date (CreatedAt)
sort.Slice(repos, func(i, j int) bool {
return repos[i].CreatedAt.Before(repos[j].CreatedAt)
})
ganttChart := "gantt\n dateFormat YYYY-MM-DD\n title Repositories Gantt Chart\n"
for _, repo := range repos {
taskType := "active"
if ! isWithinLastSixMonths(repo.PushedAt) {
taskType = "done"
}
ganttChart += fmt.Sprintf(" %s : %s, %s, %s\n",
repo.Name,
taskType,
repo.CreatedAt.Format("2006-01-02"),
repo.PushedAt.Format("2006-01-02"))
}
return ganttChart
}
func writeGanttChartToFile(ganttChart string, fileName string) error {
// create or overwrite the specified file
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("could not create file %s: %v", fileName, err)
}
defer file.Close()
_, err = file.WriteString(ganttChart)
if err != nil {
return fmt.Errorf("could not write to file %s: %v", fileName, err)
}
return nil
}
func isWithinLastSixMonths(date time.Time) bool {
now := time.Now()
// calculate the date that is 6 months ago from now
sixMonthsAgo := now.AddDate(0, -6, 0)
// check if the given date is after or equal to the date 6 months ago
return date.After(sixMonthsAgo)
}