-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
219 lines (183 loc) · 5.09 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"github.com/google/go-github/v32/github"
"github.com/pmatseykanets/gh-tools/auth"
gh "github.com/pmatseykanets/gh-tools/github"
"github.com/pmatseykanets/gh-tools/terminal"
"github.com/pmatseykanets/gh-tools/version"
"golang.org/x/oauth2"
)
func usage() {
usage := `Manage notification subscriptions across GitHub repositories
Usage: gh-watch [flags] [owner][/repo]
owner Repository owner (user or organization)
repo Repository name
Flags:
-help Print this information and exit
-no-repo= The pattern to reject repository names
-repo= The pattern to match repository names
-token Prompt for an Access Token
-unwatch Unsubscribe from repository notifications
-version Print the version and exit
-watch Subscribe to repository notifications
`
fmt.Println(usage)
}
func main() {
if err := run(context.Background()); err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
}
type config struct {
owner string
repo string
repoRegexp *regexp.Regexp
token bool // Propmt for an access token.
noRepoRegexp *regexp.Regexp // The pattern to reject repository names.
watch bool // Subscribe to repository notifications.
unwatch bool // Unsubscribe from repository notifications.
}
type subscriber struct {
gh *github.Client
config config
stdout io.WriteCloser
stderr io.WriteCloser
}
func readConfig() (config, error) {
if len(os.Args) == 0 {
usage()
os.Exit(1)
}
config := config{}
var (
showVersion, showHelp bool
repo, noRepo string
err error
)
flag.BoolVar(&showHelp, "help", showHelp, "Print this information and exit")
flag.StringVar(&noRepo, "no-repo", "", "The pattern to reject repository names")
flag.StringVar(&repo, "repo", "", "The pattern to match repository names")
flag.BoolVar(&config.token, "token", config.token, "Prompt for Access Token")
flag.BoolVar(&config.unwatch, "unwatch", config.unwatch, "Unsubscribe from repository notifications")
flag.BoolVar(&showVersion, "version", showVersion, "Print version and exit")
flag.BoolVar(&config.watch, "watch", config.watch, "Subscribe to repository notifications")
flag.Usage = usage
flag.Parse()
if showHelp {
usage()
os.Exit(0)
}
if showVersion {
fmt.Printf("gh-watch version %s\n", version.Version)
os.Exit(0)
}
parts := strings.Split(flag.Arg(0), "/")
nparts := len(parts)
if nparts > 0 {
config.owner = parts[0]
}
if nparts > 1 {
config.repo = parts[1]
}
if nparts > 2 {
return config, fmt.Errorf("invalid owner or repository name %s", flag.Arg(0))
}
if config.owner == "" {
return config, fmt.Errorf("owner is required")
}
if repo != "" {
config.repoRegexp, err = regexp.Compile(repo)
if err != nil {
return config, fmt.Errorf("invalid name pattern: %s: %s", repo, err)
}
}
if noRepo != "" {
if config.noRepoRegexp, err = regexp.Compile(noRepo); err != nil {
return config, fmt.Errorf("invalid no-repo pattern: %s", err)
}
}
return config, nil
}
func run(ctx context.Context) error {
var err error
subscriber := &subscriber{
stdout: os.Stdout,
stderr: os.Stderr,
}
subscriber.config, err = readConfig()
if err != nil {
return err
}
var token string
if subscriber.config.token {
token, _ = terminal.PasswordPrompt("Access Token: ")
} else {
token = auth.GetToken()
}
if token == "" {
return fmt.Errorf("access token is required")
}
subscriber.gh = github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)))
return subscriber.run(ctx)
}
func subscriptionStatus(sub *github.Subscription) string {
if sub == nil {
return "not watching"
}
if sub.GetIgnored() {
return "ignoring"
}
return "watching"
}
func (w *subscriber) run(ctx context.Context) error {
repos, err := gh.NewRepoFinder(w.gh).Find(ctx, gh.RepoFilter{
Owner: w.config.owner,
Repo: w.config.repo,
RepoRegexp: w.config.repoRegexp,
})
if err != nil {
return err
}
for _, repo := range repos {
fmt.Fprint(w.stdout, repo.GetFullName())
// Get the current subscription for the repo.
sub, _, err := w.gh.Activity.GetRepositorySubscription(ctx, w.config.owner, repo.GetName())
if err != nil {
fmt.Fprintln(w.stdout)
return err
}
// List the current subscription status.
fmt.Fprint(w.stdout, " ", subscriptionStatus(sub))
switch {
case w.config.watch && !sub.GetSubscribed():
sub, _, err = w.gh.Activity.SetRepositorySubscription(ctx, w.config.owner, repo.GetName(), &github.Subscription{
Subscribed: github.Bool(true),
})
if err != nil {
fmt.Fprintln(w.stdout)
return err
}
fmt.Fprint(w.stdout, " -> ", subscriptionStatus(sub))
case w.config.unwatch && sub.GetSubscribed():
_, err = w.gh.Activity.DeleteRepositorySubscription(ctx, w.config.owner, repo.GetName())
if err != nil {
fmt.Fprintln(w.stdout)
return err
}
sub = nil
fmt.Fprint(w.stdout, " -> ", subscriptionStatus(sub))
}
fmt.Fprintln(w.stdout)
}
return nil
}