This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jb.go
1002 lines (846 loc) · 29.1 KB
/
jb.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"flag"
"hash/fnv"
jira "github.com/andygrunwald/go-jira"
"github.com/jroimartin/gocui"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var jbVersion = "0.1"
type issueBox struct {
view *gocui.View
issue jira.Issue
}
type column struct {
view *gocui.View
members []issueBox
}
type activeBox struct {
columnname string
indexno int
issuetitle string
issueurl string
availableActions map[string]string
}
type configItem struct {
instanceURL string
username string
password string
query string
browserCommand string
}
var (
kanbanlist = []jira.Issue{}
kanbanMatrix = []column{}
active = &activeBox{}
jiraClient = &jira.Client{}
configColumns = []string{}
moveCounter = 0
infoText = "Navigation: Arrow keys | Actions Menu: Spacebar | Exit: Ctrl-C | Reload: F5"
)
var log = logrus.New()
// We will use ANSI color here
// I hope your terminal is clever enough
var resetColor = "\x1b[0m"
func colorHash(input string) string {
var colorPalette = []string{
"\x1b[0;31m", //red
"\x1b[0;32m", //green
"\x1b[0;33m", //brown
"\x1b[0;34m", //blue
"\x1b[0;35m", //purple
"\x1b[0;36m", //cyan
"\x1b[0;37m", //gray
}
h := fnv.New32a()
h.Write([]byte(input))
generatedInt := int(h.Sum32())
decidedColor := colorPalette[generatedInt%(len(colorPalette)-1)]
return decidedColor
}
// jiraAction function applies the specified action for the key, which
// is the issue.
func jiraAction(g *gocui.Gui, issue *jira.Issue, action string) {
if !jiraClient.Authentication.Authenticated() {
jiraClient = getJiraAuth()
}
updateStatusBar(g, "Action: "+action+". Issue: "+issue.ID)
realAction := strings.Split(action, "> ")[1]
actionID := active.availableActions[realAction]
res, err := jiraClient.Issue.DoTransition(issue.ID, actionID)
if err != nil {
updateStatusBar(g, err.Error())
}
menuView, _ := g.View("menu")
destroyView(g, menuView)
updateStatusBar(g, res.Status)
}
// getJiraAuth function creates the initial JIRA authentication cookie
// It uses the pre-read variables jiraUsername and jiraPassword
func getJiraAuth() *jira.Client {
config := readConfig()
jiraClient, err := jira.NewClient(nil, config.instanceURL)
if err != nil {
panic(err)
}
res, err := jiraClient.Authentication.AcquireSessionCookie(
config.username,
config.password,
)
if err != nil || res == false {
fmt.Printf("Result: %v\n", res)
panic(err)
}
return jiraClient
}
// executeQuery function executes the JQL query specified in config file
func executeQuery(conf configItem) []jira.Issue {
if !jiraClient.Authentication.Authenticated() {
jiraClient = getJiraAuth()
}
config := readConfig()
issuelist, _, err := jiraClient.Issue.Search(config.query, nil)
if err != nil {
fmt.Printf("Result: %v\n", err)
panic(err)
}
return issuelist
}
func activateFirstIssue(g *gocui.Gui) {
for i := range kanbanMatrix {
if len(kanbanMatrix[i].members) > 0 {
setCurrentViewOnTop(g, kanbanMatrix[i].members[0].view.Title)
active.columnname = kanbanMatrix[i].view.Title
active.issuetitle = kanbanMatrix[i].members[0].view.Title
active.indexno = 0
return
}
}
}
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
if _, err := g.SetCurrentView(name); err != nil {
return nil, err
}
return g.SetViewOnTop(name)
}
func getColumn(title string) (*column, error) {
for i := range kanbanMatrix {
if kanbanMatrix[i].view.Title == title {
return &kanbanMatrix[i], nil
}
}
return &column{}, errors.New("Couldn't find column with given title")
}
func registerIssue(box issueBox) bool {
col, err := getColumn(box.issue.Fields.Status.Name)
if err != nil {
return false
}
col.members = append(col.members, box)
return true
}
func getMenuSelection(g *gocui.Gui, v *gocui.View) error {
var line string
var err error
_, cy := v.Cursor()
if line, err = v.Line(cy); err != nil {
line = ""
}
// Get active column
currentColumn, err := getColumn(active.columnname)
if err != nil {
log.Fatal("Couldn't find active column, something is very wrong.")
}
switch line {
case "Comment on issue":
maxX, maxY := g.Size()
if v, err := g.SetView("msgBox", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Editable = true
v.Title = "Comment on issue " + active.issuetitle
setCurrentViewOnTop(g, "msgBox")
}
if err := g.SetKeybinding("msgBox", gocui.KeyEsc, gocui.ModNone, destroyView); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("msgBox", gocui.KeyCtrlS, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
updateStatusBar(g, "Sorry, not implemented yet!")
destroyView(g, v)
return nil
}); err != nil {
log.Panicln(err)
}
updateStatusBar(g, "Send: Ctrl-S (not implemented yet) | Close: Esc")
case "Preview issue":
maxX, maxY := g.Size()
if v, err := g.SetView("previewBox", 5, 3, maxX-5, maxY-3); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Details of " + active.issuetitle
v.Autoscroll = false
v.Wrap = true
v.Editable = true
issue := currentColumn.members[active.indexno].issue
lineSlice := strings.SplitN(
issue.Fields.Description,
"\r\n",
-1,
)
fmt.Fprintln(v, "Reporter: " + issue.Fields.Reporter.DisplayName)
if (issue.Fields.Assignee != nil) {
fmt.Fprintln(v, "Assignee: " + issue.Fields.Assignee.DisplayName)
}
if (issue.Fields.Labels != nil) {
if len(issue.Fields.Labels) > 0 {
fmt.Fprintln( v, "Labels: " + strings.Join(issue.Fields.Labels, ","))
}
}
if (issue.Fields.Priority != nil) {
fmt.Fprintln(v, "Priority: "+issue.Fields.Priority.Name)
}
fmt.Fprint(v, "Description:\n\n")
for i := range lineSlice {
fmt.Fprintln(v, lineSlice[i])
}
setCurrentViewOnTop(g, "previewBox")
}
if err := g.SetKeybinding("previewBox", gocui.KeyEsc, gocui.ModNone, destroyView); err != nil {
log.Panicln(err)
}
updateStatusBar(g, "Close: Esc")
case "Open in browser":
conf := readConfig()
issueURL := ""
issueURL = conf.instanceURL + "/browse/" + currentColumn.members[active.indexno].issue.Key
cmd := exec.Command(conf.browserCommand, issueURL)
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
menuView, _ := g.View("menu")
destroyView(g, menuView)
case "":
log.Debug("User chose nothing, closing the menu, that'll teach the user..")
menuView, _ := g.View("menu")
destroyView(g, menuView)
default:
jiraAction(g, ¤tColumn.members[active.indexno].issue, line)
refreshBoard(g, v)
}
return nil
}
func destroyView(g *gocui.Gui, v *gocui.View) error {
g.Cursor = false
if v != nil {
g.DeleteView(v.Name())
if _, err := g.View("menu"); err != nil {
setCurrentViewOnTop(g, active.issuetitle)
updateStatusBar(g, infoText)
} else {
updateStatusBar(g, "")
setCurrentViewOnTop(g, "menu")
}
}
return nil
}
func openMenu(g *gocui.Gui, v *gocui.View) error {
menuCoord := [4]int{}
maxX, maxY := g.Size()
activeIssue := jira.Issue{}
currentColumn, err := getColumn(active.columnname)
if err != nil {
log.Fatal("Couldn't find active column, something is very wrong.")
}
xzero, _, _, yone, _ := g.ViewPosition(currentColumn.members[active.indexno].view.Title)
menuCoord = [4]int{xzero, yone, xzero + 32, yone + 14}
if yone > maxY-14 {
log.Debug("Menu will not fit, lifting it up a bit")
menuCoord[1] = menuCoord[1] - 14
menuCoord[3] = yone
}
if xzero + 32 > maxX {
log.Debug("Menu will not fit, putting it to the left a bit")
menuCoord[0] = menuCoord[0] - 14
menuCoord[2] = menuCoord[2] - 14
}
activeIssue = currentColumn.members[active.indexno].issue
actionMap := map[string]string{}
// Let's first check if the issue belongs to us
availActions, _, err := jiraClient.Issue.GetTransitions(activeIssue.ID)
if err != nil {
return err
}
for k := range availActions {
actionMap[availActions[k].To.Name] = availActions[k].ID
}
active.availableActions = actionMap
if v, err := g.SetView(
"menu", menuCoord[0], menuCoord[1], menuCoord[2], menuCoord[3]); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Editable = false
v.Highlight = true
v.Title = v.Name()
keys := make([]string, 0, len(actionMap))
for k := range actionMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintln(v, "Send to -> "+k)
}
fmt.Fprintln(v, "Open in browser")
fmt.Fprintln(v, "Preview issue")
fmt.Fprintln(v, "Comment on issue")
}
if err := g.SetKeybinding("menu", gocui.KeyEsc, gocui.ModNone, destroyView); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("menu", gocui.KeySpace, gocui.ModNone, destroyView); err != nil {
log.Panicln(err)
}
updateStatusBar(g, "Run action: Enter | Close menu: Esc or Spacebar")
setCurrentViewOnTop(g, "menu")
return nil
}
func refreshBoard(g *gocui.Gui, v *gocui.View) error {
updateStatusBar(g, "Refreshing board...")
for i := range kanbanMatrix {
for m := range kanbanMatrix[i].members {
g.DeleteKeybindings(kanbanMatrix[i].members[m].view.Title)
g.DeleteView(kanbanMatrix[i].members[m].view.Title)
}
}
for i := range kanbanMatrix {
kanbanMatrix[i].members = kanbanMatrix[i].members[:0]
}
conf := readConfig()
for _, issue := range executeQuery(conf) {
createIssue(g, issue)
}
g.SetViewOnTop("statusLine")
activateFirstIssue(g)
updateStatusBar(g, infoText)
return nil
}
func giveNextIssueCoord(g *gocui.Gui, col column) ([4]int, error) {
issueCoord := [4]int{}
if len(col.members) == 0 {
xzero, yzero, xone, _, err := g.ViewPosition(col.view.Title)
if err != nil {
return [4]int{}, err
}
issueCoord = [4]int{xzero + 1, yzero + 1, (xzero + (xone - xzero)) - 1, yzero + 5}
} else {
// We are getting current member count of the column
// and determine the position over that
xzero, _, xone, yone, _ := g.ViewPosition(col.members[len(col.members)-1].view.Title)
//TODO error catch maybe?
issueCoord = [4]int{xzero, yone + 1, xone, yone + 6}
}
log.Debug("Calculated coordinates are: ", issueCoord)
return issueCoord, nil
}
func createIssue(g *gocui.Gui, issue jira.Issue) error {
correctColumn := column{}
undefinedColumn := true
for i := range kanbanMatrix {
if kanbanMatrix[i].view.Title == issue.Fields.Status.Name {
correctColumn = kanbanMatrix[i]
undefinedColumn = false
break
}
}
if undefinedColumn {
log.Warn("Issue with undefined column found and skipped: " + issue.Key)
return nil
}
log.Debug("Calculating coordinates for: " + issue.Key)
validCoords, err := giveNextIssueCoord(g, correctColumn)
if err != nil {
log.Warn("Calculating coordinates failed for: " + issue.Key)
}
if v, err := g.SetView(
issue.Key, validCoords[0], validCoords[1], validCoords[2], validCoords[3]); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Wrap = true
componentList := ""
for i, v := range issue.Fields.Components {
if i == 0 {
componentList = componentList + "["
}
componentList = componentList + colorHash(v.Name) + v.Name + resetColor + " "
if i == len(issue.Fields.Components)-1 {
componentList = strings.TrimRight(componentList, " ") + "]\n"
}
}
v.Title = issue.Key
fmt.Fprintln(v, componentList + issue.Fields.Summary)
registerIssue(issueBox{view: v, issue: issue})
if err := g.SetKeybinding(issue.Key, gocui.KeyArrowDown, gocui.ModNone, downHandler); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding(issue.Key, gocui.KeyArrowUp, gocui.ModNone, upHandler); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding(issue.Key, gocui.KeyArrowRight, gocui.ModNone, rightHandler); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding(issue.Key, gocui.KeyArrowLeft, gocui.ModNone, leftHandler); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding(issue.Key, gocui.KeySpace, gocui.ModNone, openMenu); err != nil {
log.Panicln(err)
}
}
return nil
}
func moveIssues(g *gocui.Gui, dy int, reset bool) error {
// Get active column
currentColumn, err := getColumn(active.columnname)
if err != nil {
log.Fatal("Exiting!")
}
if reset {
log.Debug("Got an ordering reset call")
// Let's see if we're going to bottom or top
goingToTop := false
if moveCounter != 0 {
// We're in moved position, request must be top
goingToTop = true
}
if dy == 1000 {
// This is the arrow message, we will reset previous column to top
goingToTop = true
}
if goingToTop {
log.Debug("View going to the top")
for vi := range currentColumn.members {
curView := currentColumn.members[vi].view.Title
c1, c2, c3, c4, _ := g.ViewPosition(curView)
if moveCounter > 0 {
log.Debug("New coordinates for " + curView + ": ", c1, c2, c3, c4)
if _, err := g.SetView(curView, c1, c2+6*moveCounter, c3, c4+6*moveCounter); err != nil {
return err
}
}
}
log.Debug("Arrangement is done, resetting move counter.")
moveCounter = 0
} else {
log.Debug("View going to bottom")
issueCount := len(currentColumn.members)
if issueCount > 5 {
for i := 1; i <= issueCount-5; i++ {
moveIssues(g, -6, false)
}
}
}
g.SetViewOnTop("statusLine")
return nil
}
for vi := range currentColumn.members {
curView := currentColumn.members[vi].view.Title
c1, c2, c3, c4, _ := g.ViewPosition(curView)
if _, err := g.SetView(curView, c1, c2+dy, c3, c4+dy); err != nil {
return err
}
}
if dy < 0 {
moveCounter++
} else {
moveCounter--
}
g.SetViewOnTop("statusLine")
return nil
}
func rightHandler(g *gocui.Gui, v *gocui.View) error {
rightLeftView(g, v, "right")
return nil
}
func leftHandler(g *gocui.Gui, v *gocui.View) error {
rightLeftView(g, v, "left")
return nil
}
func upHandler(g *gocui.Gui, v *gocui.View) error {
upDownView(g, v, "up")
return nil
}
func downHandler(g *gocui.Gui, v *gocui.View) error {
upDownView(g, v, "down")
return nil
}
func rightLeftView(g *gocui.Gui, v *gocui.View, direction string) error {
log.Debug(direction + " pressed")
newView := &gocui.View{}
newColumnName := active.columnname
slideNumber := 1
if direction == "left" {
slideNumber = -1
}
columnFound := false
for !columnFound {
for i := range kanbanMatrix {
if kanbanMatrix[i].view.Title == newColumnName {
if direction == "right" {
if i == len(kanbanMatrix)-1 {
newColumnName = kanbanMatrix[0].view.Title
if len(kanbanMatrix[0].members) > 0 {
columnFound = true
}
break
}
} else {
if i == 0 {
newColumnName = kanbanMatrix[len(kanbanMatrix)+slideNumber].view.Title
if len(kanbanMatrix[len(kanbanMatrix)+slideNumber].members) > 0 {
columnFound = true
}
break
}
}
newColumnName = kanbanMatrix[i+slideNumber].view.Title
if len(kanbanMatrix[i+slideNumber].members) > 0 {
columnFound = true
}
break
}
}
}
boxFound := false
for !boxFound {
for i := range kanbanMatrix {
if kanbanMatrix[i].view.Title == newColumnName {
if len(kanbanMatrix[i].members) > active.indexno && moveCounter == 0 {
log.Debug("We didn't move below screen, sliding to same place on new column")
active.issuetitle = kanbanMatrix[i].members[active.indexno].view.Title
newView = kanbanMatrix[i].members[active.indexno].view
log.Debug("Index no: " + strconv.Itoa(active.indexno))
log.Debug("Move counter: " + strconv.Itoa(moveCounter))
} else if moveCounter != 0 {
log.Debug("We already moved below screen, sliding to the first issue of new column")
active.indexno = 0
active.issuetitle = kanbanMatrix[i].members[active.indexno].view.Title
newView = kanbanMatrix[i].members[active.indexno].view
log.Debug("Index no: " + strconv.Itoa(active.indexno))
log.Debug("Move counter: " + strconv.Itoa(moveCounter))
} else {
log.Debug("Next column seems smaller, sliding to the last issue of new column")
active.indexno = len(kanbanMatrix[i].members) - 1
active.issuetitle = kanbanMatrix[i].members[active.indexno].view.Title
newView = kanbanMatrix[i].members[active.indexno].view
log.Debug("Index no: " + strconv.Itoa(active.indexno))
log.Debug("Move counter: " + strconv.Itoa(moveCounter))
}
boxFound = true
break
}
}
}
log.Debug("Resetting the view of the column we leave: " + active.columnname)
moveIssues(g, 1000, true)
if _, err := setCurrentViewOnTop(g, newView.Title); err != nil {
return err
}
active.columnname = newColumnName
updateStatusBar(g, "")
return nil
}
func upDownView(g *gocui.Gui, v *gocui.View, direction string) error {
log.Debug(direction + " pressed")
newView := &gocui.View{}
slideNumber := 1
if direction == "up" {
slideNumber = -1
}
// Get active column
currentColumn, err := getColumn(active.columnname)
if err != nil {
log.Fatal("Exiting!")
}
onEdge := false
log.Debug("moveCounter pre-ordering value: " + strconv.Itoa(moveCounter))
if direction == "up" {
if active.indexno == 0 {
log.Debug("Already on top, coordinating move to bottom")
if len(currentColumn.members) == 1 {
log.Debug("Wait, there is only one element here.. Doing nothing.")
return nil
}
onEdge = true
active.indexno = len(currentColumn.members) - 1
active.issuetitle = currentColumn.members[active.indexno].view.Title
newView = currentColumn.members[active.indexno].view
moveIssues(g, 0, true)
} else {
log.Debug("We're not on an edge, we'll just slide one " + direction)
}
} else {
if len(currentColumn.members) == active.indexno+1 {
log.Debug("Already on bottom, coordinating move to top")
if len(currentColumn.members) == 1 {
log.Debug("Wait, there is only one element here.. Doing nothing.")
return nil
}
onEdge = true
active.indexno = 0
active.issuetitle = currentColumn.members[0].view.Title
newView = currentColumn.members[0].view
moveIssues(g, 1000, true)
} else {
log.Debug("We're not on an edge, we'll just slide one " + direction)
}
}
if !onEdge {
active.indexno = active.indexno + slideNumber
active.issuetitle = currentColumn.members[active.indexno].view.Title
newView = currentColumn.members[active.indexno].view
}
columnNeedsToMove := false
_, maxY := g.Size()
_, y0, _, y1, _ := g.ViewPosition(newView.Title)
if (direction == "up") && (y0 < 0) {
log.Debug("We're close to the edge, will move whole column " + direction)
columnNeedsToMove = true
} else if (direction == "down") && (y1 > maxY-3) {
log.Debug("We're close to the edge, will move whole column " + direction)
columnNeedsToMove = true
}
if columnNeedsToMove {
moveIssues(g, -6*slideNumber, false)
}
log.Debug("moveCounter post-ordering value: " + strconv.Itoa(moveCounter))
if _, err := setCurrentViewOnTop(g, newView.Title); err != nil {
return err
}
updateStatusBar(g, "")
return nil
}
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1 //not found.
}
func indexOfView(element string, data []issueBox) int {
for k, v := range data {
if element == v.view.Title {
return k
}
}
return -1 //not found.
}
func drawBoard(g *gocui.Gui) error {
maxX, realmaxY := g.Size()
//Bottom area will be used for status line
maxY := realmaxY - 1
// This is needed (especially in parallel execution or refresh events),
// since configColumns will be filled with reading configuration.
readConfig()
for _, columnName := range configColumns {
if v, err := g.SetView(
columnName,
maxX/len(configColumns)*indexOf(columnName, configColumns),
0,
maxX/len(configColumns)*(indexOf(columnName, configColumns)+1),
maxY-1,
); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = v.Name()
v.Editable = false
v.Wrap = true
newcol := column{view: v, members: []issueBox{}}
kanbanMatrix = append(kanbanMatrix, newcol)
}
}
// Status line
if v, err := g.SetView(
"statusLine",
0,
realmaxY-2,
maxX,
realmaxY,
); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Editable = false
v.Frame = false
v.BgColor = gocui.ColorMagenta
fmt.Fprintln(v, "Loading issues...")
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func readConfig() configItem {
conf := viper.New()
conf.SetConfigName("jb") // name of config file (without extension)
conf.AddConfigPath("/etc/jb") // path to look for the config file in
conf.AddConfigPath("$HOME/.jb") // call multiple times to add many search paths
err := conf.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
}
instanceURL := conf.GetString("jira_instance")
username := conf.GetString("jira_username")
password := conf.GetString("jira_password")
query := conf.GetString("jira_query")
browserCommand := conf.GetString("browser_command")
configColumns = conf.GetStringSlice("board_list")
if containsEmpty(instanceURL, username, password, query, browserCommand) {
fmt.Println("Sorry, couldn't find all required config variables.")
printConfigHelp()
}
return configItem{
instanceURL: instanceURL,
username: username,
password: password,
query: query,
browserCommand: browserCommand,
}
}
func printVersion() {
fmt.Println("jb version: ", jbVersion)
fmt.Println("Please report bugs to: https://github.com/seqizz/jb/")
os.Exit(0)
}
func printConfigHelp() {
fmt.Println(`
Here is an example config, the file should be placed under ~/.jb/jb.yaml or /etc/jb/jb.yaml :
jira_instance: "https://my.jira.instance.address"
jira_username: "my.username"
jira_password: "my.password"
board_list: ["Open", "In Progress", "On Hold", "Blocked External", "In Review"] # Or whichever statuses you want to display
browser_command: "/usr/bin/xdg-open" # Or any other specific browser path
jira_query: "project = TECH AND assignee = my.username AND status not in (Resolved, Closed, Rejected)" # Or any valid JQL
`)
os.Exit(0)
}
func updateStatusBar(g *gocui.Gui, msg string) {
statusView, err := g.View("statusLine")
if err != nil {
// Probably the view didn't get created in parallel
// We will skip, we are not blocking anything
return
}
statusView.Clear()
if msg == "" {
return
}
fmt.Fprint(statusView, msg)
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
log.Warn("Problem setting the cursor down")
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
// ox, oy := v.Origin()
_, oy := v.Origin()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
log.Warn("Problem setting the cursor up")
}
}
return nil
}
func containsEmpty(ss ...string) bool {
for _, s := range ss {
if s == "" {
return true
}
}
return false
}
func main() {
var logLevel = flag.String("loglevel", "fatal", "Accepted values are: info, debug, warn, fatal")
var logFile = flag.String("logfile", "/tmp/jb.log", "Location of the log file")
var version = flag.Bool("version", false, "Show version information")
var configHelp = flag.Bool("confighelp", false, "Prints the configuration help")
flag.Usage = func() {
fmt.Println()
fmt.Println("jb is JIRA kanban board UI for terminal")
fmt.Println()
fmt.Println("Accepted parameters:")
fmt.Println()
flag.PrintDefaults()
}
flag.Parse()
if *version {
printVersion()
}
if *configHelp {
printConfigHelp()
}
switch *logLevel {
case "info", "debug", "warn", "fatal":
default:
fmt.Println("Could not understand requested loglevel")
os.Exit(1)
}
file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
requestedLevel, _ := logrus.ParseLevel(*logLevel)
log.SetLevel(requestedLevel)
log.Out = file
} else {
log.Warn("Failed to log to file, using default stderr")
}
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Mouse = false
g.Highlight = true
g.Cursor = true
g.InputEsc=true
g.SelFgColor = gocui.ColorBlue
g.SetManagerFunc(drawBoard)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("menu", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("menu", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("menu", gocui.KeyEnter, gocui.ModNone, getMenuSelection); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyF5, gocui.ModNone, refreshBoard); err != nil {
log.Panicln(err)
}
g.Update(func(g *gocui.Gui) error {
return refreshBoard(g, nil)
})
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}