-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgh-stars.go
130 lines (107 loc) · 3.13 KB
/
gh-stars.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 (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"github.com/augustohp/gh-stars/datetime"
"github.com/google/go-github/github"
flag "github.com/spf13/pflag"
"golang.org/x/oauth2"
)
type option struct {
token string ""
limit int
insecure bool
version bool
help bool
}
const Version = "1.1.0"
var opt = &option{}
func init() {
flag.BoolVarP(&opt.version, "version", "v", false, "Displays version information")
flag.BoolVarP(&opt.help, "help", "h", false, "Displays this help message")
flag.BoolVarP(&opt.insecure, "insecure", "i", false, "Disables HTTPS certificate verification")
flag.StringVarP(&opt.token, "token", "t", "", "A GitHub Personal API Access Token")
flag.IntVarP(&opt.limit, "limit", "l", 0, "Limit the amount of starts retrieved")
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "https://github.com/augustohp/gh-stars")
os.Exit(0)
}
func main() {
flag.Parse()
if opt.help {
usage()
}
if opt.version {
fmt.Fprintf(os.Stderr, "%s %s\n", os.Args[0], Version)
os.Exit(0)
}
if opt.token == "" && os.Getenv("GHSTARS_GITHUB_TOKEN") == "" {
fmt.Fprintln(os.Stderr, "Error: Please, provide a GITHUB API token.")
os.Exit(2)
}
if opt.token == "" && os.Getenv("GHSTARS_GITHUB_TOKEN") != "" {
opt.token = os.Getenv("GHSTARS_GITHUB_TOKEN")
}
run()
}
func run() {
ctx := context.Background()
gh, err := newGithubClient(ctx, opt.token)
authenticatedUser, _, err := gh.Users.Get(ctx, "")
if err != nil {
log.Fatalf("Error listing authenticathed user from GitHub: %v", err)
}
stars, err := listLastStars(ctx, gh, authenticatedUser.GetLogin(), opt.limit)
if err != nil {
log.Fatalf("Error listing starred activity of %s: %v", authenticatedUser.GetLogin(), err)
}
lastWeek := datetime.BeginningOfWeek()
for _, star := range stars {
repository := star.GetRepository()
owner := repository.GetOwner()
starredAt := star.GetStarredAt()
if starredAt.Before(lastWeek) {
continue
}
fmt.Fprintf(os.Stdout, "%s/%s: %s\n", owner.GetLogin(), repository.GetName(), repository.GetDescription())
}
}
func listLastStars(ctx context.Context, gh *github.Client, username string, limit int) ([]*github.StarredRepository, error) {
listStarredOptions := &github.ActivityListStarredOptions{
Sort: "created",
Direction: "desc",
ListOptions: github.ListOptions{
PerPage: limit,
},
}
starredRepositories, _, err := gh.Activity.ListStarred(ctx, username, listStarredOptions)
if err != nil {
return nil, err
}
return starredRepositories, nil
}
func newHttpClient() *http.Client {
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: opt.insecure},
}
return &http.Client{Transport: t}
}
func newGithubClient(ctx context.Context, token string) (*github.Client, error) {
ctx = context.WithValue(ctx, oauth2.HTTPClient, newHttpClient())
t := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
c := oauth2.NewClient(ctx, t)
client := github.NewClient(c)
var err error
return client, err
}