Skip to content

Commit 35923ca

Browse files
committed
adding script to find popular, but unused projects
Issue: gofrs#14
1 parent 139fce5 commit 35923ca

File tree

180 files changed

+61045
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+61045
-0
lines changed

LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2015, Tim Heckman
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of linode-netint nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ If you feel there's a project that would benefit from our help, please open an
1515
issue above. The issue itself will have some questions that you can answer to
1616
help us discover more about the project. From there we can begin to evaluate the
1717
project, and our ability to properly support it.
18+
19+
We've also started writing a script that tries to find projects which are highly
20+
used, but also lacking recent updates.

cmd/finder/main.go

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2018 The Gofrs. All rights reserved.
2+
// Use of this source code is governed by the BSD 3-Clause
3+
// license that can be found in the LICENSE file.
4+
5+
// finder is a command line tool (CLI) used to find stale projects
6+
// on Github (those without recent commits, issues, etc) and rank
7+
// them by their godoc import counts.
8+
//
9+
// Godoc.org import counts are public and computed by godoc.org as
10+
// it indexes the public Go repositories.
11+
package main // import "github.com/gofrs/help-requests/cmd/finder
12+
13+
import (
14+
"context"
15+
"errors"
16+
"flag"
17+
"fmt"
18+
"math"
19+
"net/http"
20+
"os"
21+
"sort"
22+
"strconv"
23+
"strings"
24+
"text/tabwriter"
25+
"time"
26+
27+
"github.com/google/go-github/github"
28+
"golang.org/x/net/html"
29+
"golang.org/x/oauth2"
30+
)
31+
32+
var (
33+
flagCount = flag.Int("count", 25, "How many (Github) projects to lookup")
34+
)
35+
36+
func main() {
37+
flag.Parse()
38+
39+
ghClient, err := createGithubClient(context.Background())
40+
if err != nil {
41+
fmt.Fprintf(os.Stderr, "ERROR: problem creating github client: %v", err)
42+
}
43+
44+
opts := github.ListOptions{
45+
PerPage: *flagCount,
46+
Page: 1,
47+
}
48+
49+
query := "stars:>100 pushed:<2018-01-01 language:Go"
50+
repoRes, res, err := ghClient.Search.Repositories(context.Background(), query, &github.SearchOptions{
51+
Sort: "stars",
52+
Order: "desc",
53+
ListOptions: opts,
54+
})
55+
if err != nil {
56+
fmt.Fprintf(os.Stderr, "ERROR: problem reading github repositories: %v", err)
57+
}
58+
res.Close = true
59+
60+
type row struct {
61+
text string
62+
importCount int
63+
}
64+
var rows []row
65+
for i := range repoRes.Repositories {
66+
repo := repoRes.Repositories[i]
67+
68+
cleanName := strings.Replace(*repo.HTMLURL, `https://`, "", 1)
69+
70+
// TODO(adam): goroutines + sync.WaitGroup
71+
importers, err := scrapeGodocImports(cleanName)
72+
if err != nil {
73+
fmt.Fprintf(os.Stderr, "ERROR: problem grabbing %s godoc importers: %v\n", cleanName, err)
74+
}
75+
76+
days := int(math.Abs(float64(repo.PushedAt.Sub(time.Now()).Hours()) / 24.0))
77+
line := fmt.Sprintf("%s\t%d\t%d\t%d\n", cleanName, *repo.StargazersCount, days, importers)
78+
rows = append(rows, row{
79+
text: line,
80+
importCount: importers,
81+
})
82+
}
83+
sort.Slice(rows, func(i, j int) bool { return rows[i].importCount < rows[j].importCount })
84+
85+
// Write (sorted) output
86+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
87+
fmt.Fprintf(w, "name\tstars\tlast commit (days)\timporters\n")
88+
defer w.Flush()
89+
for i := range rows {
90+
// we're going to write the rows in reverse
91+
// this will output them in desc order
92+
fmt.Fprintf(w, rows[len(rows)-i-1].text)
93+
}
94+
}
95+
96+
func createGithubClient(ctx context.Context) (*github.Client, error) {
97+
v := os.Getenv("GITHUB_TOKEN")
98+
if v == "" {
99+
return nil, errors.New("environment variable GITHUB_TOKEN is required")
100+
}
101+
ts := oauth2.StaticTokenSource(&oauth2.Token{
102+
AccessToken: v,
103+
})
104+
tc := oauth2.NewClient(ctx, ts)
105+
return github.NewClient(tc), nil
106+
}
107+
108+
func scrapeGodocImports(importPath string) (int, error) {
109+
req, err := http.NewRequest("GET", "https://godoc.org/"+importPath, nil)
110+
if err != nil {
111+
return -1, fmt.Errorf("problem loading godoc.org: %v", err)
112+
}
113+
req.Header.Set("User-Agent", "Gofrs popstalerepo bot")
114+
115+
resp, err := http.DefaultClient.Do(req)
116+
if err != nil {
117+
return -1, fmt.Errorf("problem loading %s: %v", req.URL, err)
118+
}
119+
defer resp.Body.Close()
120+
121+
// recursive search, from /x/net/html docs
122+
var f func(n *html.Node) (int, error)
123+
f = func(n *html.Node) (int, error) {
124+
if n.Type == html.ElementNode && n.Data == "a" {
125+
for _, a := range n.Attr {
126+
// TODO(adam): we should try and refresh importers
127+
// when running into errors.
128+
if a.Key == "href" && strings.Contains(a.Val, "?importers") {
129+
parts := strings.Fields(n.FirstChild.Data)
130+
n, err := strconv.Atoi(parts[0])
131+
if err != nil {
132+
return -1, fmt.Errorf("couldn't parse %q: %v", parts[0], err)
133+
}
134+
return n, nil
135+
}
136+
}
137+
}
138+
for c := n.FirstChild; c != nil; c = c.NextSibling {
139+
n, err := f(c)
140+
if err == nil && n > 0 {
141+
return n, err
142+
}
143+
}
144+
return -1, errors.New(`didn't find <a href="?importers">`)
145+
}
146+
147+
doc, err := html.Parse(resp.Body)
148+
if err != nil {
149+
return -1, fmt.Errorf("couldn't parse html: %v", err)
150+
}
151+
return f(doc)
152+
}

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/gofrs/help-requests
2+
3+
require (
4+
github.com/golang/protobuf v1.2.0 // indirect
5+
github.com/google/go-github v17.0.0+incompatible
6+
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
7+
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3
8+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
9+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
10+
google.golang.org/appengine v1.1.0 // indirect
11+
)

go.sum

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
2+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3+
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
4+
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
5+
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0=
6+
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
7+
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI=
8+
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
9+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
10+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
11+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
12+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13+
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
14+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=

vendor/github.com/golang/protobuf/AUTHORS

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/golang/protobuf/CONTRIBUTORS

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/golang/protobuf/LICENSE

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)