-
Notifications
You must be signed in to change notification settings - Fork 3
Dev #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Dev #12
Changes from all commits
9e148a4
3ad9d82
db7d83f
b3c425b
9338b6c
058a8bd
eac04fd
c3267c0
e8c3e35
b40a7e3
118c511
8e6c3f9
3e192f8
c53fbc0
8bf7301
f507210
9568899
d496b79
2f94656
23b00f9
0ab9617
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,46 +7,188 @@ import ( | |
| "fmt" | ||
| "io/ioutil" | ||
| "log" | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/go-github-metrics-1/pkg/sdkstats" | ||
| "github.com/learnoperators/go-github-metrics/pkg/sdkstats" | ||
|
|
||
| "github.com/google/go-github/github" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| type helmVersionParser struct{} | ||
| type baseVersionParser struct { | ||
| searchQ string | ||
| searchLatest string | ||
| } | ||
| type gomodVersionParser struct { | ||
| searchQ string | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, would be great have in the README an overview. As for example, describing the purpose of it. After I checked the code shows that we are looking for getting metrics over projects published in GitHub which were done using Operator-SDK. Am I right? The starts, commits and etc are nor over operator-sdk repo/project. Am I right? |
||
| } | ||
| type unknownVersionParser struct{} | ||
|
|
||
| func main() { | ||
| var token string | ||
| flag.StringVar(&token, "token", "", "GitHub API token") | ||
| flag.Parse() | ||
| args := flag.Args() | ||
| if len(token) == 0 { | ||
| log.Fatal("GITHUB API TOKEN MUST BE ENTERED \n Usage: './main --token=YOURTOKEN'") | ||
| } | ||
| ctx := context.Background() | ||
| ts := oauth2.StaticTokenSource( | ||
| &oauth2.Token{AccessToken: args[0]}, | ||
| &oauth2.Token{AccessToken: token}, | ||
| ) | ||
| tc := oauth2.NewClient(ctx, ts) | ||
| client := github.NewClient(tc) | ||
| client := sdkstats.Client{Client: github.NewClient(tc)} | ||
|
|
||
| var queries []string | ||
|
|
||
| queries = append(queries, "filename:Dockerfile quay.io/operator-framework/helm-operator") | ||
| queries = append(queries, "filename:Dockerfile quay.io/operator-framework/ansible-operator") | ||
| queries = append(queries, "filename:go.mod github.com/operator-framework/operator-sdk") | ||
| queries = append(queries, "filename:Gopkg.toml github.com/operator-framework/operator-sdk") | ||
| queries := []sdkstats.RepoMetadataQuery{ | ||
| /*sdkstats.RepoMetadataQuery{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the code is not useful, so could be removed. WDYT? |
||
| ProjectType: "helm", | ||
| Queries: []string{"filename:Dockerfile quay.io/operator-framework/helm-operator"}, | ||
| VersionParser: &baseVersionParser{ | ||
| searchQ: "quay.io/operator-framework/helm-operator", | ||
| }, | ||
| }, | ||
| sdkstats.RepoMetadataQuery{ | ||
| ProjectType: "ansible", | ||
| Queries: []string{"filename:Dockerfile quay.io/operator-framework/ansible-operator"}, | ||
| VersionParser: &baseVersionParser{ | ||
| searchQ: "quay.io/operator-framework/ansible-operator", | ||
| }, | ||
| },*/ | ||
| sdkstats.RepoMetadataQuery{ | ||
| ProjectType: "go.mod", | ||
| Queries: []string{ | ||
| "filename:go.mod github.com/operator-framework/operator-sdk", | ||
| }, | ||
| VersionParser: &gomodVersionParser{ | ||
| //searchQ: "replace github.com/operator-framework/operator-sdk => github.com/operator-framework/operator-sdk v", | ||
| searchQ: "github.com/operator-framework/operator-sdk", | ||
| }, | ||
| }, | ||
| /*sdkstats.RepoMetadataQuery{ | ||
| ProjectType: "gopkg.toml", | ||
| Queries: []string{"filename:Gopkg.toml github.com/operator-framework/operator-sdk"}, | ||
| VersionParser: &unknownVersionParser{}, | ||
| },*/ | ||
| } | ||
| // GetStats function for Query String from 'queries', These Strings are specific to Operator-SDK patterns. | ||
| collectStats := []sdkstats.RepoMetadata{} | ||
|
|
||
| for _, r := range queries { | ||
| stats, err := sdkstats.GetStats(client, r) | ||
| if len(stats) == 0 { | ||
| log.Fatal("GITHUB API is down at time, Please try after few minutes") | ||
| } else { | ||
| fileName := stats[0].ProjectType + ".json" | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| stats, err := client.GetStats(ctx, r) | ||
|
|
||
| fmt.Println("Total count for ", r.ProjectType, ":", len(stats)) | ||
| if err != nil { | ||
| fmt.Printf("Failed to get stats for queries %v: %v\n", r.Queries, err) | ||
| } | ||
| collectStats = append(collectStats, stats...) | ||
| } | ||
| fileName := "Search_Results.json" | ||
| file, err := json.MarshalIndent(collectStats, "", " ") | ||
| err = ioutil.WriteFile(fileName, file, 0644) | ||
| if err != nil { | ||
| fmt.Printf("Error encountered while writing results to file %v\n", err) | ||
| fmt.Println(collectStats) | ||
| os.Exit(1) | ||
| } | ||
| fmt.Println("Results are written in Search_Results.json") | ||
| } | ||
|
|
||
| // Parse the given Code result to search Text Matches for Version number. | ||
| func (p baseVersionParser) ParseVersion(codeResults github.CodeResult) (string, error) { | ||
| baseImageRegex := regexp.QuoteMeta(p.searchQ) | ||
| versionRegex := `(:([^s]+))\n` | ||
| re := regexp.MustCompile(baseImageRegex + versionRegex) | ||
| for _, r := range codeResults.TextMatches { | ||
| matches := re.FindStringSubmatch(r.GetFragment()) | ||
| if matches != nil { | ||
| if matches[1] == "" { | ||
| return "latest", nil | ||
| } else { | ||
| return strings.Trim(matches[2], "\n"), nil | ||
| } | ||
| } | ||
| } | ||
| return "unknown", nil | ||
| } | ||
|
|
||
| // Parse the given Code result to search Text Matches for Version number. | ||
| /*func (p baseVersionParser) ParseVersion(codeResults github.CodeResult) (string, error) { | ||
| var version string | ||
| var s, v []string | ||
|
|
||
| for _, r := range codeResults.TextMatches { | ||
| if strings.Contains(r.GetFragment(), p.searchQ) { | ||
| value := r.GetFragment() | ||
| s = strings.Split(value, "\n") | ||
| stLoop: | ||
| for _, st := range s { | ||
| if strings.Contains(st, p.searchQ) { | ||
| v = strings.Split(st, ":") | ||
| if len(v) == 0 { | ||
| version = "unknown" | ||
| } else if len(v) == 1 { | ||
| version = "latest" | ||
| } else { | ||
| version = v[1] | ||
| } | ||
| break stLoop | ||
| } | ||
| } | ||
| fmt.Println("Total count: ", len(stats)) | ||
| file, _ := json.MarshalIndent(stats, "", " ") | ||
| _ = ioutil.WriteFile(fileName, file, 0644) | ||
| fmt.Println("Results are written in ", fileName) | ||
| } | ||
| } | ||
| if version == "" { | ||
| version = "unknown" | ||
| } | ||
| return version, nil | ||
| } | ||
|
|
||
| // Parse the given Code result to search Text Matches for Version number. | ||
| func (p gomodVersionParser) ParseVersion(codeResults github.CodeResult) (string, error) { | ||
| var version string | ||
| var s, v []string | ||
|
|
||
| for _, r := range codeResults.TextMatches { | ||
| if strings.Contains(r.GetFragment(), p.searchQ) { | ||
| value := r.GetFragment() | ||
| s = strings.Split(value, "\n") | ||
| stLoop: | ||
| for _, st := range s { | ||
| if strings.Contains(st, p.searchQ) { | ||
| v = strings.Split(st, " v") | ||
| if len(v) == 0 { | ||
| version = "unknown" | ||
| } else { | ||
| version = v[1] | ||
| } | ||
| break stLoop | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return version, nil | ||
| }*/ | ||
|
|
||
| // Parse the given Code result to search Text Matches for Version number. | ||
| func (p gomodVersionParser) ParseVersion(codeResults github.CodeResult) (string, error) { | ||
| baseImageRegex := regexp.QuoteMeta(p.searchQ) | ||
| versionRegex := `([\s]([^s]+)$)` | ||
| re := regexp.MustCompile(baseImageRegex + versionRegex) | ||
| for _, r := range codeResults.TextMatches { | ||
| matches := re.FindStringSubmatch(r.GetFragment()) | ||
| if matches != nil { | ||
| if matches[1] == "" { | ||
| return "latest", nil | ||
| } else { | ||
| return strings.Trim(matches[2], "\n"), nil | ||
| } | ||
| } | ||
| } | ||
| return "unknown", nil | ||
| } | ||
|
|
||
| // Parse the given Code result to search Text Matches for Version number. | ||
| func (p unknownVersionParser) ParseVersion(codeResults github.CodeResult) (string, error) { | ||
| return "unknown", nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module github.com/learnoperators/go-github-metrics | ||
|
|
||
| go 1.13 | ||
|
|
||
| require ( | ||
| github.com/google/go-github v17.0.0+incompatible | ||
| github.com/google/go-querystring v1.0.0 // indirect | ||
| golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | ||
| github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
| github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= | ||
| github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= | ||
| github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= | ||
| github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||
| golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
| golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= | ||
| golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
| golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= | ||
| golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= | ||
| golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
| google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a good practice, we should have basic info in the README in order to allow any person knows how to configure using the project. It could be another PR, however, how we can review this one without the knowledge to test it locally?