-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (108 loc) · 2.49 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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/google/go-github/v45/github"
"golang.org/x/mod/modfile"
"golang.org/x/oauth2"
)
var (
githubToken = os.Getenv("GITHUB_ACCESS_TOKEN")
owner = os.Getenv("GITHUB_TARGET_OWNER")
repo = os.Getenv("GITHUB_TARGET_REPO")
)
func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, "go.mod", nil)
if err != nil {
log.Fatalln(err)
}
s, err := fc.GetContent()
if err != nil {
log.Fatalln(err)
}
f, err := modfile.Parse("go.mod", []byte(s), nil)
if err != nil {
log.Fatalln(err)
}
for _, req := range f.Require {
if req.Indirect {
continue
}
if !strings.HasPrefix(req.Mod.Path, "github.com/") {
continue
}
func() {
path := strings.TrimPrefix(req.Mod.Path, "github.com/")
reqOwner := strings.Split(path, "/")[0]
reqRepo := strings.Split(path, "/")[1]
url, _, err := client.Repositories.GetArchiveLink(ctx, reqOwner, reqRepo, github.Zipball, nil, true)
if err != nil {
log.Println(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
zipFileName := fmt.Sprintf("%s-%s.zip", reqOwner, reqRepo)
out, err := os.Create(zipFileName)
if err != nil {
log.Println(err)
return
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Println(err)
return
}
b, err := exec.Command("zipinfo", "-1", zipFileName).Output()
if err != nil {
log.Println(err)
return
}
dirName := strings.Split(string(b), "/")[0]
if err = exec.Command("unzip", "-o", zipFileName).Run(); err != nil {
log.Println(err)
return
}
if err = exec.Command("cd", dirName).Run(); err != nil {
log.Println(err)
return
}
fmt.Printf("## %s/%s\n", reqOwner, reqRepo)
b, err = exec.Command("goreportcard-cli").CombinedOutput()
if err != nil {
log.Println(err)
return
}
fmt.Println(string(b))
if err = exec.Command("cd", "..").Run(); err != nil {
log.Println(err)
return
}
if err = exec.Command("rm", "-rf", dirName).Run(); err != nil {
log.Println(err)
return
}
if err = exec.Command("rm", zipFileName).Run(); err != nil {
log.Println(err)
return
}
}()
}
}