-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
350 lines (326 loc) · 9.82 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// The issue-backup stores a backup of GitHub issues in JSON format.
//
// Usage:
//
// issue-backup [OPTION]...
//
// Flags:
//
// -owner string
// owner name (GitHub user or organization)
// -q suppress non-error messages
// -repo string
// repository name
// -token string
// GitHub OAuth personal access token
//
// Example:
//
// issue-backup -owner USER -repo REPO -token ACCESS_TOKEN
//
// To create a personal access token on GitHub visit https://github.com/settings/tokens
//
// If the environment variable ISSUE_BACKUP_GITHUB_TOKEN is set, the access
// token will be read from there.
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/google/go-github/v32/github"
"github.com/mewkiz/pkg/jsonutil"
"github.com/mewkiz/pkg/term"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
var (
// dbg is a logger with the "issue-backup:" prefix which logs debug messages
// to standard error.
dbg = log.New(os.Stderr, term.CyanBold("issue-backup:")+" ", 0)
// warn is a logger with the "issue-backup:" prefix which logs warning
// messages to standard error.
warn = log.New(os.Stderr, term.RedBold("issue-backup:")+" ", 0)
)
const issueBackupTokenEnvName = "ISSUE_BACKUP_GITHUB_TOKEN"
const use = `
Usage:
issue-backup [OPTION]...
Flags:
`
const example = `
Example:
issue-backup -owner USER -repo REPO -token ACCESS_TOKEN
To create a personal access token on GitHub visit https://github.com/settings/tokens
If the environment variable ` + issueBackupTokenEnvName + ` is set, the access token will be read from there.
`
func usage() {
fmt.Fprintln(os.Stderr, use[1:])
flag.PrintDefaults()
fmt.Fprint(os.Stderr, example)
}
func main() {
// Parse command line arguments.
var (
// Owner name (GitHub user or organization).
ownerName string
// Suppress non-error messages.
quiet bool
// Repository name.
repoName string
// GitHub OAuth personal access token.
token string
)
flag.StringVar(&ownerName, "owner", "", "owner name (GitHub user or organization)")
flag.BoolVar(&quiet, "q", false, "suppress non-error messages")
flag.StringVar(&repoName, "repo", "", "repository name")
flag.StringVar(&token, "token", "", "GitHub OAuth personal access token")
flag.Usage = usage
flag.Parse()
// Sanity check of command line flags.
if len(ownerName) == 0 {
log.Println("owner name not specified; see -owner flag")
flag.Usage()
os.Exit(1)
}
if len(repoName) == 0 {
log.Println("repository name not specified; see -repo flag")
flag.Usage()
os.Exit(1)
}
if envToken, ok := os.LookupEnv(issueBackupTokenEnvName); ok {
dbg.Printf("using OAuth token from %s environment variable", issueBackupTokenEnvName)
token = envToken
}
if len(token) == 0 {
warn.Printf("OAuth token not specified; use -token flag or set %s environment variable", issueBackupTokenEnvName)
}
// Mute debug messages if `-q` is set.
if quiet {
dbg.SetOutput(ioutil.Discard)
}
// Locate forks with divergent commits.
if err := backupIssues(ownerName, repoName, token); err != nil {
log.Fatalf("%+v", err)
}
}
// backupIssues creates a backup of all issues of the given owner/repo.
func backupIssues(ownerName, repoName, token string) error {
c := newClient(token)
// Get issues.
issues, err := c.getIssues(ownerName, repoName)
if err != nil {
return errors.WithStack(err)
}
for _, issue := range issues {
dbg.Printf("issue #%d", issue.GetNumber())
if err := jsonutil.Write(os.Stdout, issue); err != nil {
return errors.WithStack(err)
}
fmt.Println()
if issue.GetComments() > 0 {
dbg.Printf("%d comments of issue #%d", issue.GetComments(), issue.GetNumber())
comments, err := c.getIssueComments(ownerName, repoName, issue.GetNumber())
if err != nil {
return errors.WithStack(err)
}
if err := jsonutil.Write(os.Stdout, comments); err != nil {
return errors.WithStack(err)
}
fmt.Println()
}
}
// Get pull requests.
pullRequests, err := c.getPullRequests(ownerName, repoName)
if err != nil {
return errors.WithStack(err)
}
for _, pullRequest := range pullRequests {
dbg.Printf("pull request #%d", pullRequest.GetNumber())
if err := jsonutil.Write(os.Stdout, pullRequest); err != nil {
return errors.WithStack(err)
}
fmt.Println()
if pullRequest.GetComments() > 0 {
dbg.Printf("%d comments of pull request #%d", pullRequest.GetComments(), pullRequest.GetNumber())
comments, err := c.getPullRequestComments(ownerName, repoName, pullRequest.GetNumber())
if err != nil {
return errors.WithStack(err)
}
if err := jsonutil.Write(os.Stdout, comments); err != nil {
return errors.WithStack(err)
}
fmt.Println()
}
}
return nil
}
// Client is an OAuth authenticated GitHub client.
type Client struct {
ctx context.Context
client *github.Client
}
// newClient returns a GitHub client authenticated with the given OAuth token.
func newClient(token string) *Client {
ctx := context.Background()
var tc *http.Client
// Use personal OAuth access token if specified.
if len(token) > 0 {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc = oauth2.NewClient(ctx, ts)
}
client := github.NewClient(tc)
return &Client{
ctx: ctx,
client: client,
}
}
// --- [ issues ] --------------------------------------------------------------
// getIssues returns the issues of the given owner/repo.
func (c *Client) getIssues(ownerName, repoName string) ([]*github.Issue, error) {
opt := &github.IssueListByRepoOptions{
State: "all",
ListOptions: github.ListOptions{
PerPage: 100,
},
}
// get commits from all pages.
var allIssues []*github.Issue
page := 1
for {
issues, resp, err := c.client.Issues.ListByRepo(c.ctx, ownerName, repoName, opt)
if err != nil {
for waitForRateLimitReset(err) {
// try again after rate limit resets.
issues, resp, err = c.client.Issues.ListByRepo(c.ctx, ownerName, repoName, opt)
}
if err != nil {
warn.Printf("unable to get issues of %s:%s (page %d); %v", ownerName, repoName, page, err)
break // return partial results
}
}
allIssues = append(allIssues, issues...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
page++
}
return allIssues, nil
}
// getIssueComments returns the comments for the specified issue number of the
// given owner/repo.
func (c *Client) getIssueComments(ownerName, repoName string, issueNumber int) ([]*github.IssueComment, error) {
opt := &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
// get commits from all pages.
var allComments []*github.IssueComment
page := 1
for {
comments, resp, err := c.client.Issues.ListComments(c.ctx, ownerName, repoName, issueNumber, opt)
if err != nil {
for waitForRateLimitReset(err) {
// try again after rate limit resets.
comments, resp, err = c.client.Issues.ListComments(c.ctx, ownerName, repoName, issueNumber, opt)
}
if err != nil {
warn.Printf("unable to get comments of %s:%s for issue #%d (page %d); %v", ownerName, repoName, issueNumber, page, err)
break // return partial results
}
}
allComments = append(allComments, comments...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
page++
}
return allComments, nil
}
// --- [ pull requests ] -------------------------------------------------------
// getPullRequests returns the pull requests of the given owner/repo.
func (c *Client) getPullRequests(ownerName, repoName string) ([]*github.PullRequest, error) {
opt := &github.PullRequestListOptions{
State: "all",
ListOptions: github.ListOptions{
PerPage: 100,
},
}
// get commits from all pages.
var allPullRequests []*github.PullRequest
page := 1
for {
pullRequests, resp, err := c.client.PullRequests.List(c.ctx, ownerName, repoName, opt)
if err != nil {
for waitForRateLimitReset(err) {
// try again after rate limit resets.
pullRequests, resp, err = c.client.PullRequests.List(c.ctx, ownerName, repoName, opt)
}
if err != nil {
warn.Printf("unable to get pull requests of %s:%s (page %d); %v", ownerName, repoName, page, err)
break // return partial results
}
}
allPullRequests = append(allPullRequests, pullRequests...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
page++
}
return allPullRequests, nil
}
// getPullRequestComments returns the comments for the specified pull request number of the
// given owner/repo.
func (c *Client) getPullRequestComments(ownerName, repoName string, pullRequestNumber int) ([]*github.PullRequestComment, error) {
opt := &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
// get commits from all pages.
var allComments []*github.PullRequestComment
page := 1
for {
comments, resp, err := c.client.PullRequests.ListComments(c.ctx, ownerName, repoName, pullRequestNumber, opt)
if err != nil {
for waitForRateLimitReset(err) {
// try again after rate limit resets.
comments, resp, err = c.client.PullRequests.ListComments(c.ctx, ownerName, repoName, pullRequestNumber, opt)
}
if err != nil {
warn.Printf("unable to get comments of %s:%s for pull request #%d (page %d); %v", ownerName, repoName, pullRequestNumber, page, err)
break // return partial results
}
}
allComments = append(allComments, comments...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
page++
}
return allComments, nil
}
// ### [ Helper functions ] ####################################################
// waitForRateLimitReset waits until the rate limit resets. The boolean return
// value indicates whether the given error is a GitHub rate limit error.
func waitForRateLimitReset(err error) bool {
e, ok := err.(*github.RateLimitError)
if !ok {
return false
}
delta := time.Until(e.Rate.Reset.Time)
dbg.Printf("rate limit hit; sleeping for %v before retrying", delta)
time.Sleep(delta)
return true
}