forked from rsc/github
/
issue.go
1006 lines (874 loc) · 26.2 KB
/
issue.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Issue is a client for reading and updating issues in a GitHub project issue tracker.
usage: issue [-a] [-e] [-p owner/repo] <query>
Issue runs the query against the given project's issue tracker and
prints a table of matching issues, sorted by issue summary.
The default owner/repo is golang/go.
If multiple arguments are given as the query, issue joins them by
spaces to form a single issue search. These two commands are equivalent:
issue assignee:rsc author:robpike
issue "assignee:rsc author:robpike"
Searches are always limited to open issues.
If the query is a single number, issue prints that issue in detail,
including all comments.
Authentication
Issue expects to find a GitHub "personal access token" in
$HOME/.github-issue-token and will use that token to authenticate
to GitHub when reading or writing issue data.
A token can be created by visiting https://github.com/settings/tokens/new.
The token only needs the 'repo' scope checkbox, and optionally 'private_repo'
if you want to work with issue trackers for private repositories.
It does not need any other permissions.
The -token flag specifies an alternate file from which to read the token.
Acme Editor Integration
If the -a flag is specified, issue runs as a collection of acme windows
instead of a command-line tool. In this mode, the query is optional.
If no query is given, issue uses "state:open".
There are three kinds of acme windows: issue, issue creation, issue list,
search result, and milestone list.
The following text forms can be looked for (right clicked on)
and open a window (or navigate to an existing one).
nnnn issue #nnnn
#nnnn issue #nnnn
all the issue list
milestone(s) the milestone list
<milestone-name> the named milestone (e.g., Go1.5)
Executing "New" opens an issue creation window.
Executing "Search <query>" opens a new window showing the
results of that search.
Issue Window
An issue window, opened by loading an issue number,
displays full detail about an issue, a header followed by each comment.
For example:
Title: time: Duration should implement fmt.Formatter
State: closed
Assignee: robpike
Closed: 2015-01-08 05:20:00
Labels: release-none repo-main size-m
Milestone:
URL: https://github.com/golang/go/issues/8786
Reported by dsymonds (2014-09-21 23:02:50)
It'd be nice if http://play.golang.org/p/KCnUQOPyol
printed "[+3us]", which would require time.Duration
implementing fmt.Formatter to get the '+' flag.
Comment by rsc (2015-01-08 05:17:06)
time must not depend on fmt.
Executing "Get" reloads the issue data.
Executing "Put" updates an issue. It saves any changes to the issue header
and, if any text has been entered between the header and the "Reported by" line,
posts that text as a new comment. If both succeed, Put then reloads the issue data.
The "Closed" and "URL" headers cannot be changed.
Issue Creation Window
An issue creation window, opened by executing "New", is like an issue window
but displays only an empty issue template:
Title:
Assignee:
Labels:
Milestone:
<describe issue here>
Once the template has been completed (only the title is required), executing "Put"
creates the issue and converts the window into a issue window for the new issue.
Issue List Window
An issue list window displays a list of all open issue numbers and titles.
If the project has any open milestones, they are listed in a header line.
For example:
Milestones: Go1.4.1 Go1.5 Go1.5Maybe
9027 archive/tar: round-trip of Header misses values
8669 archive/zip: not possible to a start writing zip at offset other than zero
8359 archive/zip: not possible to specify deflate compression level
...
As in any window, right clicking on an issue number opens a window for that issue.
Search Result Window
A search result window, opened by executing "Search <query>", displays a list of issues
matching a search query. It shows the query in a header line. For example:
Search author:rsc
9131 bench: no documentation
599 cmd/5c, 5g, 8c, 8g: make 64-bit fields 64-bit aligned
6699 cmd/5l: use m to store div/mod denominator
4997 cmd/6a, cmd/8a: MOVL $x-8(SP) and LEAL x-8(SP) are different
...
Executing "Sort" in a search result window toggles between sorting by title
and sorting by decreasing issue number.
Bulk Edit Window
Executing "Bulk" in an issue list or search result window opens a new
bulk edit window applying to the displayed issues. If there is a non-empty
text selection in the issue list or search result list, the bulk edit window
is restricted to issues in the selection.
The bulk edit window consists of a metadata header followed by a list of issues, like:
State: open
Assignee:
Labels:
Milestone: Go1.4.3
10219 cmd/gc: internal compiler error: agen: unknown op
9711 net/http: Testing timeout on Go1.4.1
9576 runtime: crash in checkdead
9954 runtime: invalid heap pointer found in bss on openbsd/386
The metadata header shows only metadata shared by all the issues.
In the above example, all four issues are open and have milestone Go1.4.3,
but they have no common labels nor a common assignee.
The bulk edit applies to the issues listed in the window text; adding or removing
issue lines changes the set of issues affected by Get or Put operations.
Executing "Get" refreshes the metadata header and issue summaries.
Executing "Put" updates all the listed issues. It applies any changes made to
the metadata header and, if any text has been entered between the header
and the first issue line, posts that text as a comment. If all operations succeed,
Put then refreshes the window as Get does.
Milestone List Window
The milestone list window, opened by loading any of the names
"milestone", "Milestone", or "Milestones", displays the open project
milestones, sorted by due date, along with the number of open issues in each.
For example:
2015-01-15 Go1.4.1 1
2015-07-31 Go1.5 215
2015-07-31 Go1.5Maybe 5
Loading one of the listed milestone names opens a search for issues
in that milestone.
Alternate Editor Integration
The -e flag enables basic editing of issues with editors other than acme.
The editor invoked is $VISUAL if set, $EDITOR if set, or else ed.
Issue prepares a textual representation of issue data in a temporary file,
opens that file in the editor, waits for the editor to exit, and then applies any
changes from the file to the actual issues.
When <query> is a single number, issue -e edits a single issue.
See the ``Issue Window'' section above.
If the <query> is the text "new", issue -e creates a new issue.
See the ``Issue Creation Window'' section above.
Otherwise, for general queries, issue -e edits multiple issues in bulk.
See the ``Bulk Edit Window'' section above.
JSON Output
The -json flag causes issue to print the results in JSON format
using these data structures:
type Issue struct {
Number int
Ref string
Title string
State string
Assignee string
Closed time.Time
Labels []string
Milestone string
URL string
Reporter string
Created time.Time
Text string
Comments []*Comment
}
type Comment struct {
Author string
Time time.Time
Text string
}
If asked for a specific issue, the output is an Issue with Comments.
Otherwise, the result is an array of Issues without Comments.
*/
package main // import "rsc.io/github/issue"
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/google/go-github/v45/github"
"golang.org/x/oauth2"
)
var (
acmeFlag = flag.Bool("a", false, "open in new acme window")
editFlag = flag.Bool("e", false, "edit in system editor")
jsonFlag = flag.Bool("json", false, "write JSON output")
project = flag.String("p", "golang/go", "GitHub owner/repo name")
rawFlag = flag.Bool("raw", false, "do no processing of markdown")
tokenFile = flag.String("token", "", "read GitHub token personal access token from `file` (default $HOME/.github-issue-token)")
logHTTP = flag.Bool("loghttp", false, "log http requests")
)
func usage() {
fmt.Fprintf(os.Stderr, `usage: issue [-a] [-e] [-p owner/repo] <query>
If query is a single number, prints the full history for the issue.
Otherwise, prints a table of matching results.
`)
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
log.SetFlags(0)
log.SetPrefix("issue: ")
if flag.NArg() == 0 && !*acmeFlag {
usage()
}
if *jsonFlag && *acmeFlag {
log.Fatal("cannot use -a with -json")
}
if *jsonFlag && *editFlag {
log.Fatal("cannot use -e with -acme")
}
if *logHTTP {
http.DefaultTransport = newLogger(http.DefaultTransport)
}
f := strings.Split(*project, "/")
if len(f) != 2 {
log.Fatal("invalid form for -p argument: must be owner/repo, like golang/go")
}
loadAuth()
if *acmeFlag {
acmeMode()
}
q := strings.Join(flag.Args(), " ")
if *editFlag && q == "new" {
editIssue(*project, []byte(createTemplate), new(github.Issue))
return
}
n, _ := strconv.Atoi(q)
if n != 0 {
if *editFlag {
var buf bytes.Buffer
issue, err := showIssue(&buf, *project, n)
if err != nil {
log.Fatal(err)
}
editIssue(*project, buf.Bytes(), issue)
return
}
if _, err := showIssue(os.Stdout, *project, n); err != nil {
log.Fatal(err)
}
return
}
if *editFlag {
all, err := searchIssues(*project, q)
if err != nil {
log.Fatal(err)
}
if len(all) == 0 {
log.Fatal("no issues matched search")
}
sort.Sort(issuesByTitle(all))
bulkEditIssues(*project, all)
return
}
if err := showQuery(os.Stdout, *project, q); err != nil {
log.Fatal(err)
}
}
func showIssue(w io.Writer, project string, n int) (*github.Issue, error) {
issue, _, err := client.Issues.Get(context.TODO(), projectOwner(project), projectRepo(project), n)
if err != nil {
return nil, err
}
updateIssueCache(project, issue)
return issue, printIssue(w, project, issue)
}
const timeFormat = "2006-01-02 15:04:05"
func printIssue(w io.Writer, project string, issue *github.Issue) error {
if *jsonFlag {
showJSONIssue(w, project, issue)
return nil
}
fmt.Fprintf(w, "Title: %s\n", getString(issue.Title))
fmt.Fprintf(w, "State: %s\n", getString(issue.State))
fmt.Fprintf(w, "Assignee: %s\n", getUserLogin(issue.Assignee))
if issue.ClosedAt != nil {
fmt.Fprintf(w, "Closed: %s\n", getTime(issue.ClosedAt).Format(timeFormat))
}
fmt.Fprintf(w, "Labels: %s\n", strings.Join(getLabelNames(issue.Labels), " "))
fmt.Fprintf(w, "Milestone: %s\n", getMilestoneTitle(issue.Milestone))
fmt.Fprintf(w, "URL: https://github.com/%s/%s/issues/%d\n", projectOwner(project), projectRepo(project), getInt(issue.Number))
fmt.Fprintf(w, "\nReported by %s (%s)\n", getUserLogin(issue.User), getTime(issue.CreatedAt).Format(timeFormat))
if issue.Body != nil {
if *rawFlag {
fmt.Fprintf(w, "\n%s\n\n", *issue.Body)
} else {
text := strings.TrimSpace(*issue.Body)
if text != "" {
fmt.Fprintf(w, "\n\t%s\n", wrap(text, "\t"))
}
}
}
var output []string
for page := 1; ; {
list, resp, err := client.Issues.ListComments(context.TODO(), projectOwner(project), projectRepo(project), getInt(issue.Number), &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
for _, com := range list {
var buf bytes.Buffer
w := &buf
fmt.Fprintf(w, "%s\n", getTime(com.CreatedAt).Format(time.RFC3339))
fmt.Fprintf(w, "\nComment by %s (%s)\n", getUserLogin(com.User), getTime(com.CreatedAt).Format(timeFormat))
if com.Body != nil {
if *rawFlag {
fmt.Fprintf(w, "\n%s\n\n", *com.Body)
} else {
text := strings.TrimSpace(*com.Body)
if text != "" {
fmt.Fprintf(w, "\n\t%s\n", wrap(text, "\t"))
}
}
}
output = append(output, buf.String())
}
if err != nil {
return err
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
for page := 1; ; {
list, resp, err := client.Issues.ListIssueEvents(context.TODO(), projectOwner(project), projectRepo(project), getInt(issue.Number), &github.ListOptions{
Page: page,
PerPage: 100,
})
for _, ev := range list {
var buf bytes.Buffer
w := &buf
fmt.Fprintf(w, "%s\n", getTime(ev.CreatedAt).Format(time.RFC3339))
switch event := getString(ev.Event); event {
case "mentioned", "subscribed", "unsubscribed":
// ignore
default:
fmt.Fprintf(w, "\n* %s %s (%s)\n", getUserLogin(ev.Actor), event, getTime(ev.CreatedAt).Format(timeFormat))
case "closed", "referenced", "merged":
id := getString(ev.CommitID)
if id != "" {
if len(id) > 7 {
id = id[:7]
}
id = " in commit " + id
}
fmt.Fprintf(w, "\n* %s %s%s (%s)\n", getUserLogin(ev.Actor), event, id, getTime(ev.CreatedAt).Format(timeFormat))
if id != "" {
commit, _, err := client.Git.GetCommit(context.TODO(), projectOwner(project), projectRepo(project), *ev.CommitID)
if err == nil {
fmt.Fprintf(w, "\n\tAuthor: %s <%s> %s\n\tCommitter: %s <%s> %s\n\n\t%s\n",
getString(commit.Author.Name), getString(commit.Author.Email), getTime(commit.Author.Date).Format(timeFormat),
getString(commit.Committer.Name), getString(commit.Committer.Email), getTime(commit.Committer.Date).Format(timeFormat),
wrap(getString(commit.Message), "\t"))
}
}
case "assigned", "unassigned":
fmt.Fprintf(w, "\n* %s %s %s (%s)\n", getUserLogin(ev.Actor), event, getUserLogin(ev.Assignee), getTime(ev.CreatedAt).Format(timeFormat))
case "labeled", "unlabeled":
fmt.Fprintf(w, "\n* %s %s %s (%s)\n", getUserLogin(ev.Actor), event, getString(ev.Label.Name), getTime(ev.CreatedAt).Format(timeFormat))
case "milestoned", "demilestoned":
if event == "milestoned" {
event = "added to milestone"
} else {
event = "removed from milestone"
}
fmt.Fprintf(w, "\n* %s %s %s (%s)\n", getUserLogin(ev.Actor), event, getString(ev.Milestone.Title), getTime(ev.CreatedAt).Format(timeFormat))
case "renamed":
fmt.Fprintf(w, "\n* %s changed title (%s)\n - %s\n + %s\n", getUserLogin(ev.Actor), getTime(ev.CreatedAt).Format(timeFormat), getString(ev.Rename.From), getString(ev.Rename.To))
}
output = append(output, buf.String())
}
if err != nil {
return err
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
sort.Strings(output)
for _, s := range output {
i := strings.Index(s, "\n")
fmt.Fprintf(w, "%s", s[i+1:])
}
return nil
}
func showQuery(w io.Writer, project, q string) error {
all, err := searchIssues(project, q)
if err != nil {
return err
}
sort.Sort(issuesByTitle(all))
if *jsonFlag {
showJSONList(project, all)
return nil
}
for _, issue := range all {
fmt.Fprintf(w, "%v\t%v\n", getInt(issue.Number), getString(issue.Title))
}
return nil
}
type issuesByTitle []*github.Issue
func (x issuesByTitle) Len() int { return len(x) }
func (x issuesByTitle) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x issuesByTitle) Less(i, j int) bool {
if getString(x[i].Title) != getString(x[j].Title) {
return getString(x[i].Title) < getString(x[j].Title)
}
return getInt(x[i].Number) < getInt(x[j].Number)
}
func searchIssues(project, q string) ([]*github.Issue, error) {
if opt, ok := queryToListOptions(project, q); ok {
return listRepoIssues(project, opt)
}
var all []*github.Issue
for page := 1; ; {
// TODO(rsc): Rethink excluding pull requests.
x, resp, err := client.Search.Issues(context.TODO(), "type:issue state:open repo:"+project+" "+q, &github.SearchOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
for i := range x.Issues {
updateIssueCache(project, x.Issues[i])
all = append(all, x.Issues[i])
}
if err != nil {
return all, err
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
return all, nil
}
func queryToListOptions(project, q string) (opt github.IssueListByRepoOptions, ok bool) {
if strings.ContainsAny(q, `"'`) {
return
}
for _, f := range strings.Fields(q) {
i := strings.Index(f, ":")
if i < 0 {
return
}
key, val := f[:i], f[i+1:]
switch key {
default:
return
case "milestone":
if opt.Milestone != "" || val == "" {
return
}
id := findMilestone(ioutil.Discard, project, &val)
if id == nil {
return
}
opt.Milestone = fmt.Sprint(*id)
case "state":
if opt.State != "" || val == "" {
return
}
opt.State = val
case "assignee":
if opt.Assignee != "" || val == "" {
return
}
opt.Assignee = val
case "author":
if opt.Creator != "" || val == "" {
return
}
opt.Creator = val
case "mentions":
if opt.Mentioned != "" || val == "" {
return
}
opt.Mentioned = val
case "label":
if opt.Labels != nil || val == "" {
return
}
opt.Labels = strings.Split(val, ",")
case "sort":
if opt.Sort != "" || val == "" {
return
}
opt.Sort = val
case "updated":
if !opt.Since.IsZero() || !strings.HasPrefix(val, ">=") {
return
}
// TODO: Can set Since if we parse val[2:].
return
case "no":
switch val {
default:
return
case "milestone":
if opt.Milestone != "" {
return
}
opt.Milestone = "none"
}
}
}
return opt, true
}
func listRepoIssues(project string, opt github.IssueListByRepoOptions) ([]*github.Issue, error) {
var all []*github.Issue
for page := 1; ; {
xopt := opt
xopt.ListOptions = github.ListOptions{
Page: page,
PerPage: 100,
}
issues, resp, err := client.Issues.ListByRepo(context.TODO(), projectOwner(project), projectRepo(project), &xopt)
for i := range issues {
updateIssueCache(project, issues[i])
all = append(all, issues[i])
}
if err != nil {
return all, err
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
// Filter out pull requests, since we cannot say type:issue like in searchIssues.
// TODO(rsc): Rethink excluding pull requests.
save := all[:0]
for _, issue := range all {
if issue.PullRequestLinks == nil {
save = append(save, issue)
}
}
return save, nil
}
func loadMilestones(project string) ([]*github.Milestone, error) {
// NOTE(rsc): There appears to be no paging possible.
all, _, err := client.Issues.ListMilestones(context.TODO(), projectOwner(project), projectRepo(project), &github.MilestoneListOptions{
State: "open",
})
if err != nil {
return nil, err
}
if all == nil {
all = []*github.Milestone{}
}
return all, nil
}
func wrap(t string, prefix string) string {
var out strings.Builder
t = strings.Replace(t, "\r\n", "\n", -1)
max := 70
if *acmeFlag {
max = 100
}
doWrap := true
lines := strings.Split(t, "\n")
for i, line := range lines {
if i > 0 {
out.WriteByte('\n')
out.WriteString(prefix)
}
if line == "```" {
doWrap = !doWrap
}
s := line
if doWrap {
for len(s) > max {
i := strings.LastIndex(s[:max], " ")
if i < 0 {
i = strings.Index(s, " ")
if i == -1 {
i = len(s) - 1
}
}
i++
out.WriteString(s[:i])
out.WriteByte('\n')
out.WriteString(prefix)
s = s[i:]
}
}
out.WriteString(s)
}
return out.String()
}
var client *github.Client
// GitHub personal access token, from https://github.com/settings/applications.
var authToken string
func loadAuth() {
var data []byte
var err error
switch {
case lookExec("secret-tool") == nil:
// TODO(hank) This host argument should be parameterized.
cmd := exec.Command("secret-tool", "lookup", "host", "github.com", "application", "Issues")
data, err = cmd.Output()
if err == nil {
break
}
log.Printf("ran command: %v", cmd.Args)
log.Printf("error from `secret-tool`: %v", err)
fallthrough
default:
const short = ".github-issue-token"
filename := filepath.Clean(os.Getenv("HOME") + "/" + short)
shortFilename := filepath.Clean("$HOME/" + short)
if *tokenFile != "" {
filename = *tokenFile
shortFilename = *tokenFile
}
data, err = ioutil.ReadFile(filename)
if err != nil {
log.Fatal("reading token: ", err, "\n\n"+
"Please create a personal access token at https://github.com/settings/tokens/new\n"+
"and write it to ", shortFilename, " to use this program.\n"+
"The token only needs the repo scope, or private_repo if you want to\n"+
"view or edit issues for private repositories.\n"+
"The benefit of using a personal access token over using your GitHub\n"+
"password directly is that you can limit its use and revoke it at any time.\n\n")
}
fi, err := os.Stat(filename)
if err != nil {
log.Fatal(err)
}
if fi.Mode()&0077 != 0 {
log.Fatalf("reading token: %s mode is %#o, want %#o", shortFilename, fi.Mode()&0777, fi.Mode()&0700)
}
}
authToken = strings.TrimSpace(string(data))
t := &oauth2.Transport{
Source: &tokenSource{AccessToken: authToken},
}
client = github.NewClient(&http.Client{Transport: t})
}
func lookExec(n string) (err error) {
_, err = exec.LookPath(n)
return err
}
type tokenSource oauth2.Token
func (t *tokenSource) Token() (*oauth2.Token, error) {
return (*oauth2.Token)(t), nil
}
func getInt(x *int) int {
if x == nil {
return 0
}
return *x
}
func getString(x *string) string {
if x == nil {
return ""
}
return *x
}
func getUserLogin(x *github.User) string {
if x == nil || x.Login == nil {
return ""
}
return *x.Login
}
func getTime(x *time.Time) time.Time {
if x == nil {
return time.Time{}
}
return (*x).Local()
}
func getMilestoneTitle(x *github.Milestone) string {
if x == nil || x.Title == nil {
return ""
}
return *x.Title
}
func getLabelNames(x []*github.Label) []string {
var out []string
for _, lab := range x {
out = append(out, getString(lab.Name))
}
sort.Strings(out)
return out
}
type projectAndNumber struct {
project string
number int
}
var issueCache struct {
sync.Mutex
m map[projectAndNumber]*github.Issue
}
func updateIssueCache(project string, issue *github.Issue) {
n := getInt(issue.Number)
if n == 0 {
return
}
issueCache.Lock()
if issueCache.m == nil {
issueCache.m = make(map[projectAndNumber]*github.Issue)
}
issueCache.m[projectAndNumber{project, n}] = issue
issueCache.Unlock()
}
func bulkReadIssuesCached(project string, ids []int) ([]*github.Issue, error) {
var all []*github.Issue
issueCache.Lock()
for _, id := range ids {
all = append(all, issueCache.m[projectAndNumber{project, id}])
}
issueCache.Unlock()
var errbuf bytes.Buffer
for i, id := range ids {
if all[i] == nil {
issue, _, err := client.Issues.Get(context.TODO(), projectOwner(project), projectRepo(project), id)
if err != nil {
fmt.Fprintf(&errbuf, "reading #%d: %v\n", id, err)
continue
}
updateIssueCache(project, issue)
all[i] = issue
}
}
var err error
if errbuf.Len() > 0 {
err = fmt.Errorf("%s", strings.TrimSpace(errbuf.String()))
}
return all, err
}
// JSON output
// If you make changes to the structs, copy them back into the doc comment.
type Issue struct {
Number int
Ref string
Title string
State string
Assignee string
Closed time.Time
Labels []string
Milestone string
URL string
Reporter string
Created time.Time
Text string
Comments []*Comment
}
type Comment struct {
Author string
Time time.Time
Text string
}
func showJSONIssue(w io.Writer, project string, issue *github.Issue) {
data, err := json.MarshalIndent(toJSONWithComments(project, issue), "", "\t")
if err != nil {
log.Fatal(err)
}
data = append(data, '\n')
w.Write(data)
}
func showJSONList(project string, all []*github.Issue) {
j := []*Issue{} // non-nil for json
for _, issue := range all {
j = append(j, toJSON(project, issue))
}
data, err := json.MarshalIndent(j, "", "\t")
if err != nil {
log.Fatal(err)
}
data = append(data, '\n')
os.Stdout.Write(data)
}
func toJSON(project string, issue *github.Issue) *Issue {
j := &Issue{
Number: getInt(issue.Number),
Ref: fmt.Sprintf("%s/%s#%d\n", projectOwner(project), projectRepo(project), getInt(issue.Number)),
Title: getString(issue.Title),
State: getString(issue.State),
Assignee: getUserLogin(issue.Assignee),
Closed: getTime(issue.ClosedAt),
Labels: getLabelNames(issue.Labels),
Milestone: getMilestoneTitle(issue.Milestone),
URL: fmt.Sprintf("https://github.com/%s/%s/issues/%d\n", projectOwner(project), projectRepo(project), getInt(issue.Number)),
Reporter: getUserLogin(issue.User),
Created: getTime(issue.CreatedAt),
Text: getString(issue.Body),
Comments: []*Comment{},
}
if j.Labels == nil {
j.Labels = []string{}
}
return j
}
func toJSONWithComments(project string, issue *github.Issue) *Issue {
j := toJSON(project, issue)
for page := 1; ; {
list, resp, err := client.Issues.ListComments(context.TODO(), projectOwner(project), projectRepo(project), getInt(issue.Number), &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
log.Fatal(err)
}
for _, com := range list {
j.Comments = append(j.Comments, &Comment{
Author: getUserLogin(com.User),
Time: getTime(com.CreatedAt),
Text: getString(com.Body),
})
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
return j
}
func newLogger(t http.RoundTripper) http.RoundTripper {
return &loggingTransport{transport: t}
}
type loggingTransport struct {
transport http.RoundTripper
mu sync.Mutex
active []byte
}
func (t *loggingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
t.mu.Lock()
index := len(t.active)
start := time.Now()
fmt.Fprintf(os.Stderr, "HTTP: %s %s+ %s\n", timeFormat1(start), t.active, r.URL)
t.active = append(t.active, '|')
t.mu.Unlock()
resp, err := t.transport.RoundTrip(r)
last := r.URL.Path
if i := strings.LastIndex(last, "/"); i >= 0 {
last = last[i:]
}
display := last
if resp != nil {
display += " " + resp.Status
}
if err != nil {
display += " error: " + err.Error()
}
now := time.Now()
t.mu.Lock()
t.active[index] = '-'
fmt.Fprintf(os.Stderr, "HTTP: %s %s %s (%.3fs)\n", timeFormat1(now), t.active, display, now.Sub(start).Seconds())
t.active[index] = ' '
n := len(t.active)
for n%4 == 0 && n >= 4 && t.active[n-1] == ' ' && t.active[n-2] == ' ' && t.active[n-3] == ' ' && t.active[n-4] == ' ' {
t.active = t.active[:n-4]
n -= 4
}
t.mu.Unlock()