-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
110 lines (85 loc) · 2.06 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/stacklok/frizbee/pkg/replacer"
"log"
"os"
"path/filepath"
"sync"
"github.com/google/go-github/v62/github"
fzconfig "github.com/stacklok/frizbee/pkg/utils/config"
)
func main() {
config := ParseArgs()
log.Printf("Configuration:\n%s", config)
client := github.NewClient(nil)
ctx := context.Background()
done := make(chan bool)
downloaded := make(chan string)
downloader := NewRepositoryDownloader(client, config)
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
var analyzed []Analysis
for {
select {
case repo := <-downloaded:
analysis, err := AnalyseRepository(config, repo)
if err != nil {
log.Printf("[ERROR] analysing repository: %v", err)
// TODO: https://github.com/stacklok/frizbee/issues/77
continue
}
analyzed = append(analyzed, analysis)
case <-done:
resultFile, err := os.Create(config.ResultFile)
if err != nil {
log.Fatalf("creating output file: %v", err)
}
err = json.NewEncoder(resultFile).Encode(analyzed)
if err != nil {
log.Fatalf("encoding output file: %v", err)
}
return
}
}
}()
go func() {
defer wg.Done()
err := downloader.Download(ctx, downloaded)
if err != nil {
log.Fatalf("downloading: %v", err)
}
done <- true
}()
wg.Wait()
}
func AnalyseRepository(config Config, repo string) (Analysis, error) {
analysis := NewAnalysis(repo)
repoPath := filepath.Join(config.DownloadDir, repo, ".github", "workflows")
// Create a new Frizbee instance
r := replacer.NewGitHubActionsReplacer(&fzconfig.Config{})
actions, err := r.ListPath(repoPath)
if err != nil {
return analysis, fmt.Errorf("listing actions: %w", err)
}
for _, action := range actions.Entities {
if len(action.Ref) == 40 && isHex(action.Ref) {
analysis.CountPinned()
} else {
analysis.CountUnpinned()
}
}
return analysis, nil
}
func isHex(s string) bool {
for _, r := range s {
if !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'f')) {
return false
}
}
return true
}