forked from thecodearchive/GitTimeMachine
/
main.go
146 lines (123 loc) · 3.32 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
package main
import (
"database/sql"
"io/ioutil"
"log"
"path/filepath"
"github.com/google/go-github/github"
"gopkg.in/yaml.v2"
)
func firstFetch(dataDir string, repositories []string,
reposInsert *sql.Stmt, GitHubClient *github.Client) {
for _, main := range repositories {
log.Printf("Doing the startup fetch of %s...", main)
repo, err := OpenRepository(dataDir, main)
if err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
err = repo.Fetch(main, true)
if err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
if _, err := reposInsert.Exec(main, main); err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
forks, err := getForks(main, GitHubClient)
if err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
for i, fork := range forks {
log.Printf("[%d / %d] %s", i+1, len(forks), fork)
if err := repo.Fetch(fork, false); err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
if _, err := reposInsert.Exec(fork, main); err != nil {
log.Println("[!] Startup fetch failure:", err)
continue
}
}
repo.Close()
}
}
func monitorRepoChanges(reposSelect *sql.Stmt, changedRepos chan [2]string,
GitHubClient *github.Client) {
firehose := make(chan github.Event, 30)
go gitHubFirehose(firehose, GitHubClient)
for e := range firehose {
if *e.Type == "PushEvent" {
name := *e.Repo.Name
var mainName string
err := reposSelect.QueryRow(name).Scan(&mainName)
switch {
case err == sql.ErrNoRows:
// not a monitored repo
case err != nil:
log.Println("[!] Name lookup failure", err)
default:
changedRepos <- [...]string{name, mainName}
if len(changedRepos) > cap(changedRepos)/10*9 {
log.Println("[!] Queue is filling up:", len(changedRepos))
}
}
}
}
}
type Config struct {
Repositories []map[string]string `yaml:"Repositories"`
DataDir string `yaml:"DataDir"`
UserAgent string `yaml:"UserAgent"`
GitHubID string `yaml:"GitHubID"`
GitHubSecret string `yaml:"GitHubSecret"`
QueueSize int `yaml:"QueueSize"`
}
func main() {
configText, err := ioutil.ReadFile("config.yml")
if err != nil {
log.Fatal(err)
}
var C Config
err = yaml.Unmarshal(configText, &C)
if err != nil {
log.Fatal(err)
}
reposDB, reposInsert, reposSelect, err := OpenReposDb(
filepath.Join(C.DataDir, "repos.sqlite"))
if err != nil {
log.Fatal(err)
}
defer reposDB.Close()
t := &github.UnauthenticatedRateLimitedTransport{
ClientID: C.GitHubID,
ClientSecret: C.GitHubSecret,
}
GitHubClient := github.NewClient(t.Client())
GitHubClient.UserAgent = C.UserAgent
var repositories []string
for _, e := range C.Repositories {
for entryType, entry := range e {
switch entryType {
case "repo":
repositories = append(repositories, entry)
case "owner":
result, err := getUserRepos(entry, GitHubClient)
if err != nil {
log.Fatal(err)
}
repositories = append(repositories, result...)
default:
log.Fatal("unknown Repositories type")
}
}
}
changedRepos := make(chan [2]string, C.QueueSize)
go monitorRepoChanges(reposSelect, changedRepos, GitHubClient)
go firstFetch(C.DataDir, repositories, reposInsert, GitHubClient)
for changedRepo := range changedRepos {
log.Println(changedRepo)
}
}