-
Notifications
You must be signed in to change notification settings - Fork 19
/
acme.go
635 lines (582 loc) · 13 KB
/
acme.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
// Copyright 2013 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.
// Originally code.google.com/p/rsc/cmd/issue/acme.go.
package main
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"9fans.net/go/acme"
"9fans.net/go/plumb"
"github.com/google/go-github/v62/github"
)
func (w *awin) project() string {
p := w.prefix
p = strings.TrimPrefix(p, "/issue/")
i := strings.Index(p, "/")
if i >= 0 {
j := strings.Index(p[i+1:], "/")
if j >= 0 {
p = p[:i+1+j]
}
}
return p
}
func acmeMode() {
var dummy awin
dummy.prefix = "/issue/" + *project + "/"
if flag.NArg() > 0 {
// TODO(rsc): Without -a flag, the query is conatenated into one query.
// Decide which behavior should be used, and use it consistently.
// TODO(rsc): Block this look from doing the multiline selection mode?
for _, arg := range flag.Args() {
if dummy.Look(arg) {
continue
}
if arg == "new" {
dummy.createIssue()
continue
}
dummy.newSearch(dummy.prefix, "search", arg)
}
} else {
dummy.Look("all")
}
go dummy.plumbserve()
select {}
}
func (w *awin) plumbserve() {
fid, err := plumb.Open("githubissue", 0)
if err != nil {
w.Err(fmt.Sprintf("plumb: %v", err))
return
}
r := bufio.NewReader(fid)
for {
var m plumb.Message
if err := m.Recv(r); err != nil {
w.Err(fmt.Sprintf("plumb recv: %v", err))
return
}
if m.Type != "text" {
w.Err(fmt.Sprintf("plumb recv: unexpected type: %s\n", m.Type))
continue
}
if m.Dst != "githubissue" {
w.Err(fmt.Sprintf("plumb recv: unexpected dst: %s\n", m.Dst))
continue
}
// TODO use m.Dir
data := string(m.Data)
var project, what string
if strings.HasPrefix(data, "/issue/") {
project = data[len("/issue/"):]
i := strings.LastIndex(project, "/")
if i < 0 {
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
continue
}
project, what = project[:i], project[i+1:]
} else {
i := strings.Index(data, "#")
if i < 0 {
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
continue
}
project, what = data[:i], data[i+1:]
}
if strings.Count(project, "/") != 1 {
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
continue
}
var plummy awin
plummy.prefix = "/issue/" + project + "/"
if !plummy.Look(what) {
w.Err(fmt.Sprintf("plumb recv: can't look %s%s", plummy.prefix, what))
}
}
}
const (
modeSingle = 1 + iota
modeQuery
modeCreate
modeMilestone
modeBulk
)
type awin struct {
*acme.Win
prefix string
mode int
query string
id int
github *github.Issue
title string
sortByNumber bool // otherwise sort by title
}
var all struct {
sync.Mutex
m map[*acme.Win]*awin
}
func (w *awin) exit() {
all.Lock()
defer all.Unlock()
if all.m[w.Win] == w {
delete(all.m, w.Win)
}
if len(all.m) == 0 {
os.Exit(0)
}
}
func (w *awin) new(prefix, title string) *awin {
all.Lock()
defer all.Unlock()
if all.m == nil {
all.m = make(map[*acme.Win]*awin)
}
w1 := new(awin)
w1.title = title
var err error
w1.Win, err = acme.New()
if err != nil {
log.Printf("creating acme window: %v", err)
time.Sleep(10 * time.Millisecond)
w1.Win, err = acme.New()
if err != nil {
log.Fatalf("creating acme window again: %v", err)
}
}
w1.prefix = prefix
w1.SetErrorPrefix(w1.prefix)
w1.Name(w1.prefix + title)
all.m[w1.Win] = w1
return w1
}
func (w *awin) show(title string) bool {
return acme.Show(w.prefix+title) != nil
}
var numRE = regexp.MustCompile(`(?m)^#[0-9]+\t`)
var repoHashRE = regexp.MustCompile(`\A([A-Za-z0-9_]+/[A-Za-z0-9_]+)#(all|[0-9]+)\z`)
var milecache struct {
sync.Mutex
list map[string][]*github.Milestone
}
func cachedMilestones(project string) []*github.Milestone {
milecache.Lock()
if milecache.list == nil {
milecache.list = make(map[string][]*github.Milestone)
}
if milecache.list[project] == nil {
milecache.list[project], _ = loadMilestones(project)
}
list := milecache.list[project]
milecache.Unlock()
return list
}
func (w *awin) Look(text string) bool {
ids := readBulkIDs([]byte(text))
if len(ids) > 0 {
for _, id := range ids {
text := fmt.Sprint(id)
if w.show(text) {
continue
}
w.newIssue(w.prefix, text, id)
}
return true
}
if text == "all" {
if w.show("all") {
return true
}
w.newSearch(w.prefix, "all", "")
return true
}
if text == "Milestone" || text == "Milestones" || text == "milestone" {
if w.show("milestone") {
return true
}
w.newMilestoneList()
return true
}
list := cachedMilestones(w.project())
for _, m := range list {
if getString(m.Title) == text {
if w.show(text) {
return true
}
w.newSearch(w.prefix, text, "milestone:"+text)
return true
}
}
if n, _ := strconv.Atoi(strings.TrimPrefix(text, "#")); 0 < n && n < 1000000 {
text = strings.TrimPrefix(text, "#")
if w.show(text) {
return true
}
w.newIssue(w.prefix, text, n)
return true
}
if m := repoHashRE.FindStringSubmatch(text); m != nil {
project := m[1]
what := m[2]
prefix := "/issue/" + project + "/"
if acme.Show(prefix+what) != nil {
return true
}
if what == "all" {
w.newSearch(prefix, what, "")
return true
}
if n, _ := strconv.Atoi(what); 0 < n && n < 1000000 {
w.newIssue(prefix, what, n)
return true
}
return false
}
if m := numRE.FindAllString(text, -1); m != nil {
for _, s := range m {
w.Look(strings.TrimSpace(strings.TrimPrefix(s, "#")))
}
return true
}
return false
}
func (w *awin) setMilestone(milestone, text string) {
var buf bytes.Buffer
id := findMilestone(&buf, w.project(), &milestone)
if buf.Len() > 0 {
w.Err(strings.TrimSpace(buf.String()))
}
if id == nil {
return
}
milestoneID := *id
stop := w.Blink()
defer stop()
if w.mode == modeSingle {
w.setMilestone1(milestoneID, w.id)
w.load()
return
}
if n, _ := strconv.Atoi(strings.TrimPrefix(text, "#")); 0 < n && n < 100000 {
w.setMilestone1(milestoneID, n)
return
}
if m := numRE.FindAllString(text, -1); m != nil {
for _, s := range m {
n, _ := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(s, "#")))
if 0 < n && n < 100000 {
w.setMilestone1(milestoneID, n)
}
}
return
}
}
func (w *awin) setMilestone1(milestoneID, n int) {
var edit github.IssueRequest
edit.Milestone = &milestoneID
_, _, err := client.Issues.Edit(context.TODO(), projectOwner(w.project()), projectRepo(w.project()), n, &edit)
if err != nil {
w.Err(fmt.Sprintf("Error changing issue #%d: %v", n, err))
}
}
func (w *awin) createIssue() {
w = w.new(w.prefix, "new")
w.mode = modeCreate
w.Ctl("cleartag")
w.Fprintf("tag", " Put Search ")
go w.load()
go w.loop()
}
func (w *awin) newIssue(prefix, title string, id int) {
w = w.new(prefix, title)
w.mode = modeSingle
w.id = id
w.Ctl("cleartag")
w.Fprintf("tag", " Get Put Look ")
go w.load()
go w.loop()
}
func (w *awin) newBulkEdit(body []byte) {
w = w.new(w.prefix, "bulk-edit/")
w.mode = modeBulk
w.query = ""
w.Ctl("cleartag")
w.Fprintf("tag", " New Get Sort Search ")
w.Write("body", append([]byte("Loading...\n\n"), body...))
go w.load()
go w.loop()
}
func (w *awin) newMilestoneList() {
w = w.new(w.prefix, "milestone")
w.mode = modeMilestone
w.query = ""
w.Ctl("cleartag")
w.Fprintf("tag", " New Get Sort Search ")
w.Write("body", []byte("Loading..."))
go w.load()
go w.loop()
}
func (w *awin) newSearch(prefix, title, query string) {
w = w.new(prefix, title)
w.mode = modeQuery
w.query = query
w.Ctl("cleartag")
w.Fprintf("tag", " New Get Bulk Sort Search ")
w.Write("body", []byte("Loading..."))
go w.load()
go w.loop()
}
var createTemplate = `Title:
Assignee:
Labels:
Milestone:
<describe issue here>
`
func (w *awin) load() {
switch w.mode {
case modeCreate:
w.Clear()
w.Write("body", []byte(createTemplate))
w.Ctl("clean")
case modeSingle:
var buf bytes.Buffer
stop := w.Blink()
issue, err := showIssue(&buf, w.project(), w.id)
stop()
w.Clear()
if err != nil {
w.Write("body", []byte(err.Error()))
break
}
w.Write("body", buf.Bytes())
w.Ctl("clean")
w.github = issue
case modeMilestone:
stop := w.Blink()
milestones, err := loadMilestones(w.project())
milecache.Lock()
if milecache.list == nil {
milecache.list = make(map[string][]*github.Milestone)
}
milecache.list[w.project()] = milestones
milecache.Unlock()
stop()
w.Clear()
if err != nil {
w.Fprintf("body", "Error loading milestones: %v\n", err)
break
}
var buf bytes.Buffer
for _, m := range milestones {
fmt.Fprintf(&buf, "%s\t%s\t%d\n", getTime(m.DueOn).Format("2006-01-02"), getString(m.Title), getInt(m.OpenIssues))
}
w.PrintTabbed(buf.String())
w.Ctl("clean")
case modeQuery:
var buf bytes.Buffer
stop := w.Blink()
err := showQuery(&buf, w.project(), w.query)
if w.title == "all" {
cachedMilestones(w.project())
}
stop()
w.Clear()
if err != nil {
w.Write("body", []byte(err.Error()))
break
}
if w.title == "all" {
var names []string
for _, m := range cachedMilestones(w.project()) {
names = append(names, getString(m.Title))
}
if len(names) > 0 {
w.Fprintf("body", "Milestones: %s\n\n", strings.Join(names, " "))
}
}
if w.title == "search" {
w.Fprintf("body", "Search %s\n\n", w.query)
}
w.PrintTabbed(buf.String())
w.Ctl("clean")
case modeBulk:
stop := w.Blink()
body, err := w.ReadAll("body")
if err != nil {
w.Err(fmt.Sprintf("%v", err))
stop()
break
}
base, original, err := bulkEditStartFromText(w.project(), body)
stop()
if err != nil {
w.Err(fmt.Sprintf("%v", err))
break
}
w.Clear()
w.PrintTabbed(string(original))
w.Ctl("clean")
w.github = base
}
w.Addr("0")
w.Ctl("dot=addr")
w.Ctl("show")
}
func diff(line, field, old string) *string {
old = strings.TrimSpace(old)
line = strings.TrimSpace(strings.TrimPrefix(line, field))
if old == line {
return nil
}
return &line
}
func (w *awin) put() {
stop := w.Blink()
defer stop()
switch w.mode {
case modeSingle, modeCreate:
old := w.github
if w.mode == modeCreate {
old = new(github.Issue)
}
data, err := w.ReadAll("body")
if err != nil {
w.Err(fmt.Sprintf("Put: %v", err))
return
}
issue, _, err := writeIssue(w.project(), old, data, false)
if err != nil {
w.Err(err.Error())
return
}
if w.mode == modeCreate {
w.mode = modeSingle
w.id = getInt(issue.Number)
w.title = fmt.Sprint(w.id)
w.Name(w.prefix + w.title)
w.github = issue
}
w.load()
case modeBulk:
data, err := w.ReadAll("body")
if err != nil {
w.Err(fmt.Sprintf("Put: %v", err))
return
}
ids, err := bulkWriteIssue(w.project(), w.github, data, func(s string) { w.Err("Put: " + s) })
if err != nil {
errText := strings.Replace(err.Error(), "\n", "\t\n", -1)
if len(ids) > 0 {
w.Err(fmt.Sprintf("updated %d issue%s with errors:\n\t%v", len(ids), suffix(len(ids)), errText))
break
}
w.Err(fmt.Sprintf("%s", errText))
break
}
w.Err(fmt.Sprintf("updated %d issue%s", len(ids), suffix(len(ids))))
case modeMilestone:
w.Err("cannot Put milestone list")
case modeQuery:
w.Err("cannot Put issue list")
}
}
func (w *awin) sort() {
if err := w.Addr("0/^[0-9]/,"); err != nil {
w.Err("nothing to sort")
}
var less func(string, string) bool
if w.sortByNumber {
less = func(x, y string) bool { return lineNumber(x) > lineNumber(y) }
} else {
less = func(x, y string) bool { return skipField(x) < skipField(y) }
}
if err := w.Sort(less); err != nil {
w.Err(err.Error())
}
w.Addr("0")
w.Ctl("dot=addr")
w.Ctl("show")
}
func lineNumber(s string) int {
n := 0
for j := 0; j < len(s) && '0' <= s[j] && s[j] <= '9'; j++ {
n = n*10 + int(s[j]-'0')
}
return n
}
func skipField(s string) string {
i := strings.Index(s, "\t")
if i < 0 {
return s
}
for i < len(s) && s[i+1] == '\t' {
i++
}
return s[i:]
}
func (w *awin) Execute(cmd string) bool {
switch cmd {
case "Get":
w.load()
return true
case "Put":
w.put()
return true
case "Del":
w.Ctl("del")
return true
case "New":
w.createIssue()
return true
case "Sort":
if w.mode != modeQuery {
w.Err("can only sort issue list windows")
break
}
w.sortByNumber = !w.sortByNumber
w.sort()
return true
case "Bulk":
// TODO(rsc): If Bulk has an argument, treat as search query and use results?
if w.mode != modeQuery {
w.Err("can only start bulk edit in issue list windows")
return true
}
text := w.Selection()
if text == "" {
data, err := w.ReadAll("body")
if err != nil {
w.Err(fmt.Sprintf("%v", err))
return true
}
text = string(data)
}
w.newBulkEdit([]byte(text))
return true
}
if strings.HasPrefix(cmd, "Search ") {
w.newSearch(w.prefix, "search", strings.TrimSpace(strings.TrimPrefix(cmd, "Search")))
return true
}
if strings.HasPrefix(cmd, "Milestone ") {
text := w.Selection()
w.setMilestone(strings.TrimSpace(strings.TrimPrefix(cmd, "Milestone")), text)
return true
}
return false
}
func (w *awin) loop() {
defer w.exit()
w.EventLoop(w)
}