-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain.go
More file actions
198 lines (176 loc) · 5.1 KB
/
main.go
File metadata and controls
198 lines (176 loc) · 5.1 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
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
package main
import (
"context"
"find-gh-poc/pkg/extract"
"find-gh-poc/pkg/githubclient"
"find-gh-poc/pkg/readme"
"find-gh-poc/pkg/search"
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"github.com/schollz/progressbar/v3"
)
func run(ctx context.Context, client *githubclient.GitHubClient, query string, outputFile string) error {
fmt.Println("Fetching repositories...")
repos, err := search.SearchRepositories(ctx, client, query)
if err != nil {
return fmt.Errorf("failed to fetch repositories: %w", err)
}
if len(repos) == 0 {
return fmt.Errorf("no repositories found for query: %s", query)
}
fmt.Printf("Found %d repositories\n", len(repos))
bar := progressbar.NewOptions(len(repos),
progressbar.OptionSetDescription("Processing repositories..."),
progressbar.OptionSetItsString("repositories"),
progressbar.OptionShowIts(),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
)
cveExtractor := extract.NewCVEExtractor()
allRepoCVEs := make(map[string]map[string]bool) // repoURL -> cveID -> true
const batchSize = 10
for i := 0; i < len(repos); i += batchSize {
end := i + batchSize
if end > len(repos) {
end = len(repos)
}
batch := repos[i:end]
// If we have a full batch (10 repos), use batch processing
if len(batch) == batchSize {
for _, repo := range batch {
if _, exists := allRepoCVEs[repo.Url]; !exists {
allRepoCVEs[repo.Url] = make(map[string]bool)
}
repoCVEs := cveExtractor.ExtractCVEsFromRepository(repo)
for _, cve := range repoCVEs {
allRepoCVEs[repo.Url][cve] = true
}
}
batchReadmes, err := readme.FetchReadmeBatch(ctx, client, batch)
if err != nil {
fmt.Printf("Warning: Failed to fetch README batch: %v\n", err)
} else {
for _, repo := range batch {
readme, exists := batchReadmes[repo.Url]
if !exists {
readme = ""
}
repoCVEs := cveExtractor.ExtractCVEsFromReadme(readme)
for _, cve := range repoCVEs {
allRepoCVEs[repo.Url][cve] = true
}
}
}
} else {
// Handle remaining repositories individually
for _, repo := range batch {
if _, exists := allRepoCVEs[repo.Url]; !exists {
allRepoCVEs[repo.Url] = make(map[string]bool)
}
repoCVEs := cveExtractor.ExtractCVEsFromRepository(repo)
for _, cve := range repoCVEs {
allRepoCVEs[repo.Url][cve] = true
}
individualReadme, err := readme.FetchSingleReadme(ctx, client, repo)
if err != nil {
fmt.Printf("Warning: Failed to fetch README for %s: %v\n", repo.Url, err)
} else {
repoCVEs := cveExtractor.ExtractCVEsFromReadme(individualReadme)
for _, cve := range repoCVEs {
allRepoCVEs[repo.Url][cve] = true
}
}
}
}
bar.Add(len(batch))
}
results := make(map[string][]string)
for repoURL, cveIDs := range allRepoCVEs {
for cveID := range cveIDs {
if _, exists := results[cveID]; !exists {
results[cveID] = make([]string, 0)
}
results[cveID] = append(results[cveID], repoURL)
}
}
output, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("couldn't create output file: %w", err)
}
defer output.Close()
for cveID, repoURLs := range results {
for _, repoURL := range repoURLs {
_, _ = io.WriteString(output, cveID+" - "+repoURL+"\n")
}
}
return nil
}
func main() {
token := flag.String("token-string", "", "Github token")
tokenFile := flag.String("token-file", "", "File to read Github token from")
query := flag.String("query-string", "", "GraphQL search query")
queryFile := flag.String("query-file", "", "File to read GraphQL search query from")
outputFile := flag.String("o", "", "Output file name")
flag.Parse()
if (*token == "" && *tokenFile == "") || *outputFile == "" {
fmt.Println("Token and output file must be specified!")
os.Exit(1)
}
if *query == "" && *queryFile == "" {
fmt.Println("Query must be specified!")
os.Exit(1)
}
githubToken := ""
if *tokenFile != "" {
file, err := os.Open(*tokenFile)
if err != nil {
fmt.Println("Couldn't open file to read token!")
os.Exit(1)
}
defer file.Close()
tokenData, err := io.ReadAll(file)
if err != nil {
fmt.Println("Couldn't read from token file!")
os.Exit(1)
}
githubToken = string(tokenData)
} else {
githubToken = *token
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
<-signalChannel
fmt.Println("\nProgram interrupted, exiting...")
cancel()
}()
searchQuery := *query
if searchQuery == "" {
file, err := os.Open(*queryFile)
if err != nil {
fmt.Println("Couldn't open file to read query!")
os.Exit(1)
}
defer file.Close()
queryData, err := io.ReadAll(file)
if err != nil {
fmt.Println("Couldn't read from query file!")
os.Exit(1)
}
searchQuery = strings.Trim(string(queryData), " \n\r\t")
}
searchQuery += " in:readme in:description in:name"
client := githubclient.NewGitHubClient(githubToken)
err := run(ctx, client, searchQuery, *outputFile)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}