This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
130 lines (102 loc) · 3.87 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
package main
import (
"fmt"
"net/url"
"os"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/Ullaakut/astronomer/pkg/context"
"github.com/Ullaakut/astronomer/pkg/gql"
"github.com/Ullaakut/astronomer/pkg/signature"
"github.com/Ullaakut/astronomer/pkg/trust"
"github.com/Ullaakut/disgo"
"github.com/Ullaakut/disgo/style"
)
func parseArguments() error {
viper.SetEnvPrefix("astronomer")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
pflag.BoolP("verbose", "v", false, "Show extra logs (including comparative reports)")
pflag.BoolP("all", "a", false, "Force astronomer to scall every stargazer of the repository (overrides --stars)")
pflag.UintP("stars", "s", 1000, "Maxmimum amount of stars to scan, if fast mode is enabled")
pflag.StringP("cachedir", "c", "./data", "Set the directory in which to store cache data")
viper.AutomaticEnv()
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
return err
}
if viper.GetBool("help") || len(pflag.Args()) == 0 {
disgo.Infoln("Missing required repository argument")
pflag.Usage()
os.Exit(0)
}
return nil
}
func main() {
err := parseArguments()
if err != nil {
disgo.Errorln(style.Failure(style.SymbolCross, err))
os.Exit(1)
}
disgo.SetTerminalOptions(disgo.WithColors(true), disgo.WithDebug(viper.GetBool("verbose")))
repository := pflag.Arg(0)
// Split repository into repo owner & repo name.
repoInfo := strings.Split(repository, "/")
if len(repoInfo) != 2 {
disgo.Errorln(style.Failure(style.SymbolCross, " invalid repository %q: should be of the form \"repoOwner/repoName\"", repository))
os.Exit(1)
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
disgo.Errorln(style.Failure(style.SymbolCross, " missing github access token. Please set one in your GITHUB_TOKEN environment variable, with \"repo\" rights."))
os.Exit(1)
}
ctx := &context.Context{
RepoOwner: repoInfo[0],
RepoName: repoInfo[1],
GithubToken: token,
Stars: viper.GetUint("stars"),
CacheDirectoryPath: viper.GetString("cachedir"),
ScanAll: viper.GetBool("all"),
Verbose: viper.GetBool("verbose"),
}
if err := detectFakeStars(ctx); err != nil {
disgo.Errorln(style.Failure(style.SymbolCross, " ", err))
os.Exit(1)
}
}
func detectFakeStars(ctx *context.Context) error {
disgo.Infof("Beginning fetching process for repository %s/%s\n", ctx.RepoOwner, ctx.RepoName)
cursors, totalUsers, err := gql.FetchStargazers(ctx)
if err != nil {
return fmt.Errorf("failed to query stargazer data: %s", err)
}
if totalUsers < 1000 {
disgo.Infoln(style.Important("This repository appears to have a low amount of stargazers. Trust calculations might not be accurate."))
}
// For now, we only fetch contributions since 2013. It will be configurable later on
// once the algorithm is more accurate and more data has been fetched.
if !ctx.ScanAll && totalUsers > ctx.Stars {
disgo.Infof("Fetching contributions for %d users up to year %d\n", ctx.Stars, 2013)
} else {
disgo.Infof("Fetching contributions for %d users up to year %d\n", totalUsers, 2013)
}
users, err := gql.FetchContributions(ctx, cursors, 2013)
if err != nil {
return fmt.Errorf("failed to query stargazer data: %s", err)
}
report, err := trust.Compute(ctx, users)
if err != nil {
return fmt.Errorf("unable to compute trust report: %v", err)
}
trust.Render(report, true)
err = signature.SendReport(ctx, report)
if err != nil {
return fmt.Errorf("unable to send trust report: %v", err)
}
disgo.Infof("\n%s Analysis successful. %d users computed.\n", style.Success(style.SymbolCheck), len(users))
badgeEndpoint := url.QueryEscape(fmt.Sprintf("https://astronomer.ullaakut.eu/shields?owner=%s&name=%s", ctx.RepoOwner, ctx.RepoName))
disgo.Infof("GitHub badge available at https://img.shields.io/endpoint.svg?url=%s\n", badgeEndpoint)
return nil
}