-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgithub.go
102 lines (82 loc) · 2.02 KB
/
github.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
package main
import (
"encoding/json"
"log"
"time"
)
type PushEventCommit struct {
Key string `json:"sha"`
Url string
Files []GithubCommitFile
}
type PushEvent struct {
Key json.Number `json:"push_id"`
Commits []PushEventCommit
}
type GithubEvent struct {
Key string `json:"id"`
Type string
Date string `json:"created_at"`
Payload json.RawMessage
}
type GithubCommitFile struct {
Url string `json:"raw_url"`
Content string
}
type GithubCommit struct {
Files []GithubCommitFile
}
func (p *PushEvent) Download() {
_, exists := conf.keys[string(p.Key)]
if exists {
return
}
for i := range p.Commits {
var ghc GithubCommit
data := getGithub(p.Commits[i].Url)
err := json.Unmarshal(data, &ghc)
if err != nil {
log.Printf("[-] Could not parse commit %s: %s\n.", p.Commits[i].Key, err.Error())
continue
}
for j := range ghc.Files {
f := ghc.Files[j]
f.Content = string(get(f.Url))
p.Commits[i].Files = append(p.Commits[i].Files, f)
}
}
conf.keys[string(p.Key)] = time.Now()
}
func scrapeGithubEvents(c chan<- *ProcessItem) {
var events []*GithubEvent
log.Println("[+] Checking for new Github events.")
resp := getGithub("https://api.github.com/events")
err := json.Unmarshal(resp, &events)
if err != nil {
log.Println("[-] Could not parse list of events.")
log.Printf("[-] %s.\n", err.Error())
log.Println(string(resp))
return
}
log.Printf("[+] Processing %d events.\n", len(events))
for i := range events {
if events[i].Type == "PushEvent" {
var pe PushEvent
err := json.Unmarshal(events[i].Payload, &pe)
if err != nil {
log.Printf("[-] Could not parse payload for %s\n", events[i].Key)
log.Printf("[-] %s.\n", err.Error())
log.Println(string(events[i].Payload))
continue
}
pe.Download()
for i := range pe.Commits {
for j := range pe.Commits[i].Files {
f := pe.Commits[i].Files[j]
item := &ProcessItem{Source: "GithubCommit", Location: f.Url, Key: string(pe.Key), Content: f.Content}
c <- item
}
}
}
}
}