/
issue_kanban.go
1143 lines (1048 loc) · 32.6 KB
/
issue_kanban.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 (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package issueKanban
import (
"encoding/json"
"fmt"
"strconv"
"sync"
"time"
"github.com/golang-collections/collections/set"
"github.com/sirupsen/logrus"
"golang.org/x/text/message"
"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/pkg/strutil"
)
type IssueCart struct {
i18nPrinter *message.Printer
Assignee string `json:"assignee"`
Priority apistructs.IssuePriority `json:"priority"`
ID int64 `json:"id"`
Title string `json:"title"`
Type apistructs.IssueType `json:"type"`
State int64 `json:"state"`
IssueButton []apistructs.IssueStateButton `json:"issueButton"`
IterationID int64 `json:"iterationID"`
PlanFinishedAt *time.Time `json:"planFinishedAt"`
Operations map[string]interface{} `json:"operations"`
}
type CartList struct {
i18nPrinter *message.Printer
// 分类类型: 状态,处理人,时间,自定义,优先级
Label interface{} `json:"label"`
LabelKey interface{} `json:"labelKey"`
Total uint64 `json:"total"`
PageNo uint64 `json:"pageNo"`
PageSize uint64 `json:"pageSize"`
List []IssueCart `json:"list"`
Operations map[string]interface{} `json:"operations"`
}
type IssueBoard struct {
RefreshBoard bool `json:"refreshBoard"`
Board []CartList `json:"board"`
UserIDs []string `json:"userIDs"`
}
func (cl *CartList) Delete(issueID int64) {
for k, v := range cl.List {
if issueID == v.ID {
cl.List = append(cl.List[:k], cl.List[k+1:]...)
}
}
}
func (cl *CartList) Add(c IssueCart) {
cl.List = append([]IssueCart{c}, cl.List...)
}
const (
MoveOutConfirmMsg = "Confirm to move out iteration"
CreateBoardConfirmMsg = "Confirm to create board"
DeleteBoardConfirmMsg = "Confirm to delete board"
UpdateBoardConfirmMsg = "Confirm to update board"
CreateBoardDisabledTip = "The number of boards cannot exceed 5"
GuestCreateBoardDisabledTip = "No permission to operate"
)
type OperationBaseInfo struct {
// 操作展示名称
Text string `json:"text"`
// 是否有权限操作
Disabled bool `json:"disabled"`
// 确认提示
Confirm string `json:"confirm,omitempty"`
// 前端操作是否需要触发后端
Reload bool `json:"reload"`
DisabledTip string `json:"disabledTip"`
}
type OpMetaInfo struct {
IssueID int64 `json:"ID"`
IssueAssignee string `json:"issueAssignee"`
IssuePriority apistructs.IssuePriority `json:"issuePriority"`
StateID int64 `json:"stateID"`
apistructs.IssuePanel
}
type OperationInfo struct {
OperationBaseInfo
Meta OpMetaInfo `json:"meta"`
}
type DragOperationInfo struct {
Meta OpMetaInfo `json:"meta"`
// 前端操作是否需要触发后端
Reload bool `json:"reload"`
// 可拖拽的范围
TargetKeys interface{} `json:"targetKeys"`
Disabled bool `json:"disabled"`
}
type MoveOutOperation OperationInfo
// 状态
type DragOperation DragOperationInfo
type MoveToOperation OperationInfo
// 处理人
//type DragToAssigneeOperation DragOperationInfo
type MoveToAssigneeOperation OperationInfo
// 优先级
type MoveToPriorityOperation OperationInfo
// 自定义看板
type CreateBoardOperation OperationInfo
type DeleteBoardOperation OperationInfo
type UpdateBoardOperation OperationInfo
type MoveToCustomOperation OperationInfo
type ChangePageNoOperation struct {
Key string `json:"key"`
Reload bool `json:"reload"`
FillMeta string `json:"fillMeta"`
Meta struct {
KanbanKey string `json:"kanbanKey"`
} `json:"meta"`
}
const MaxBoardNum = 15
//type DragToCustomOperation DragOperationInfo
func (c *CartList) SetI18nPrinter(ip *message.Printer) {
c.i18nPrinter = ip
}
func (c *IssueCart) SetI18nPrinter(ip *message.Printer) {
c.i18nPrinter = ip
}
// 移除操作渲染生成
func (c *IssueCart) RenderMoveOutOperation() {
// 已经move out iteration
if c.IterationID < 0 {
return
}
o := MoveOutOperation{}
o.Disabled = false
o.Confirm = c.i18nPrinter.Sprintf(MoveOutConfirmMsg)
o.Text = c.i18nPrinter.Sprintf(apistructs.MoveOutOperation.String())
o.Reload = true
o.Meta = OpMetaInfo{IssueID: c.ID}
c.Operations[apistructs.MoveOutOperation.String()] = o
}
// 拖拽操作渲染生成
func (c *IssueCart) RenderDragOperation() {
targetKeys := make(map[int64]bool)
for _, v := range c.IssueButton {
if v.Permission == true {
targetKeys[v.StateID] = true
}
}
o := DragOperation{}
o.Reload = true
o.Meta = OpMetaInfo{IssueID: c.ID}
o.TargetKeys = targetKeys
c.Operations[apistructs.DragOperation.String()] = o
}
func (c *IssueCart) RenderMoveToOperation() {
for _, v := range c.IssueButton {
o := MoveToOperation{}
o.Disabled = !v.Permission
o.Text = c.i18nPrinter.Sprintf(apistructs.MoveToOperation.String()) + v.StateName
o.Reload = true
o.Meta = OpMetaInfo{IssueID: c.ID, StateID: v.StateID}
c.Operations[apistructs.MoveToOperation.String()+v.StateName] = o
}
}
func (c *IssueCart) RenderMoveToCustomOperation(mp map[apistructs.OperationKey]interface{}) {
p := mp[apistructs.MoveToCustomOperation].([]apistructs.IssuePanelIssues)
for _, v := range p {
o := MoveToCustomOperation{}
o.Disabled = false
o.Text = c.i18nPrinter.Sprintf(apistructs.MoveToCustomOperation.String()) + v.PanelName
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
IssuePanel: v.IssuePanel,
}
c.Operations[apistructs.MoveToCustomOperation.String()+v.PanelName] = o
}
}
func (c *IssueCart) RenderDragToCustomOperation(mp map[apistructs.OperationKey]interface{}) {
targetKeys := make(map[int64]bool)
p := mp[apistructs.DragToCustomOperation].([]apistructs.IssuePanelIssues)
for _, v := range p {
targetKeys[v.PanelID] = true
}
o := DragOperation{}
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
}
o.TargetKeys = targetKeys
c.Operations[apistructs.DragOperation.String()] = o
}
func (c *IssueCart) RenderMoveToAssigneeOperation(mp map[apistructs.OperationKey]interface{}) {
p := mp[apistructs.MoveToAssigneeOperation].([]apistructs.UserInfo)
for _, v := range p {
if v.Name == c.Assignee {
continue
}
o := MoveToAssigneeOperation{}
o.Disabled = false
o.Text = c.i18nPrinter.Sprintf(apistructs.MoveToAssigneeOperation.String()) + v.Nick
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
IssueAssignee: v.ID,
}
c.Operations[apistructs.MoveToAssigneeOperation.String()+v.Name] = o
}
}
func (c *IssueCart) RenderDragToAssigneeOperation(mp map[apistructs.OperationKey]interface{}) {
targetKeys := make(map[string]bool)
p := mp[apistructs.DragToAssigneeOperation].([]apistructs.UserInfo)
for _, v := range p {
targetKeys[v.ID] = true
}
o := DragOperation{}
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
}
o.TargetKeys = targetKeys
c.Operations[apistructs.DragOperation.String()] = o
}
func (c *IssueCart) RenderMoveToPriorityOperation(i apistructs.Issue, mp map[apistructs.OperationKey]interface{}) {
p := mp[apistructs.MoveToPriorityOperation].([]apistructs.IssuePriority)
for _, v := range p {
o := MoveToPriorityOperation{}
o.Disabled = false
if v == i.Priority {
o.Disabled = true
}
o.Text = c.i18nPrinter.Sprintf(apistructs.MoveToPriorityOperation.String()) + v.GetZhName()
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
IssuePriority: v,
}
c.Operations[apistructs.MoveToPriorityOperation.String()+string(v)] = o
}
}
func (c *IssueCart) RenderDragToPriorityOperation(i apistructs.Issue, mp map[apistructs.OperationKey]interface{}) {
targetKeys := make(map[apistructs.IssuePriority]bool)
p := mp[apistructs.DragToPriorityOperation].([]apistructs.IssuePriority)
for _, v := range p {
if v == i.Priority {
continue
}
targetKeys[v] = true
}
o := DragOperation{}
o.Reload = true
o.Meta = OpMetaInfo{
IssueID: c.ID,
}
o.TargetKeys = targetKeys
c.Operations[apistructs.DragOperation.String()] = o
}
func (c *CartList) RenderChangePageNoOperation(kanbanKey string) {
if c.Operations == nil {
c.Operations = make(map[string]interface{})
}
o := ChangePageNoOperation{
Key: apistructs.ChangePageNoOperation.String(),
Reload: true,
FillMeta: "pageData",
Meta: struct {
KanbanKey string `json:"kanbanKey"`
}{kanbanKey},
}
c.Operations[apistructs.ChangePageNoOperation.String()] = o
}
func (c *IssueCart) RenderCartOperations(s ChartOperationSwitch, i apistructs.Issue, mp map[apistructs.OperationKey]interface{}) {
if c.Operations == nil {
c.Operations = make(map[string]interface{})
}
// for task 60429. pd's roadmap, temporarily remove 'Move Out of Iteration',
// and add a button for more options later.
// if s.enableMoveOut {
// c.RenderMoveOutOperation()
// }
if s.enableMoveTo {
c.RenderMoveToOperation()
}
if s.enableDrag {
c.RenderDragOperation()
}
if s.enableMoveToCustom {
c.RenderMoveToCustomOperation(mp)
}
if s.enableDragToCustom {
c.RenderDragToCustomOperation(mp)
}
if s.enableMoveToAssignee {
c.RenderMoveToAssigneeOperation(mp)
}
if s.enableDragToAssignee {
c.RenderDragToAssigneeOperation(mp)
}
if s.enableMoveToPriority {
c.RenderMoveToPriorityOperation(i, mp)
}
if s.enableDragToPriority {
c.RenderDragToPriorityOperation(i, mp)
}
}
func (cl *CartList) RenderCartListOperations(s ChartOperationSwitch) {
cl.Operations = make(map[string]interface{})
// 删除看板
if len(cl.List) == 0 && cl.LabelKey.(int64) != 0 {
o := DeleteBoardOperation{}
o.Disabled = false
o.Confirm = cl.i18nPrinter.Sprintf(DeleteBoardConfirmMsg)
o.Text = cl.i18nPrinter.Sprintf(apistructs.DeleteCustomOperation.String())
o.Reload = true
o.Meta = OpMetaInfo{IssuePanel: apistructs.IssuePanel{PanelID: cl.LabelKey.(int64)}}
cl.Operations[apistructs.DeleteCustomOperation.String()] = o
}
// 更新看板
if cl.LabelKey.(int64) != 0 {
o := UpdateBoardOperation{}
o.Disabled = false
o.Confirm = cl.i18nPrinter.Sprintf(UpdateBoardConfirmMsg)
o.Text = cl.i18nPrinter.Sprintf(apistructs.UpdateCustomOperation.String())
o.Reload = true
o.Meta = OpMetaInfo{IssuePanel: apistructs.IssuePanel{PanelID: cl.LabelKey.(int64)}}
cl.Operations[apistructs.UpdateCustomOperation.String()] = o
}
if s.enableChangePageNo {
cl.RenderChangePageNoOperation(strconv.FormatInt(cl.LabelKey.(int64), 10))
}
}
// 根据完成时间(planFinishedAt)分为:未分类,已过期,1天内过期,2天内,3天内,30天,未来
type ExpireType string
func (e ExpireType) String() string {
return string(e)
}
const (
ExpireTypeUndefined ExpireType = "Undefined"
ExpireTypeExpired ExpireType = "Expired"
ExpireTypeExpireIn1Day ExpireType = "ExpireIn1Day"
ExpireTypeExpireIn2Days ExpireType = "ExpireIn2Days"
ExpireTypeExpireIn7Days ExpireType = "ExpireIn7Days"
ExpireTypeExpireIn30Days ExpireType = "ExpireIn30Days"
ExpireTypeExpireInFuture ExpireType = "ExpireInFuture"
)
var ExpireTypes = []ExpireType{ExpireTypeUndefined, ExpireTypeExpired, ExpireTypeExpireIn1Day, ExpireTypeExpireIn2Days, ExpireTypeExpireIn7Days, ExpireTypeExpireIn30Days, ExpireTypeExpireInFuture}
// FinishTime和now都是date;hour, min, sec 都为0
func (c IssueCart) GetExpireType(now time.Time) ExpireType {
if c.PlanFinishedAt == nil {
return ExpireTypeUndefined
}
finishTime := *c.PlanFinishedAt
if finishTime.Before(now) {
return ExpireTypeExpired
} else if finishTime.Before(now.Add(1 * 24 * time.Hour)) {
return ExpireTypeExpireIn1Day
} else if finishTime.Before(now.Add(2 * 24 * time.Hour)) {
return ExpireTypeExpireIn2Days
} else if finishTime.Before(now.Add(7 * 24 * time.Hour)) {
return ExpireTypeExpireIn7Days
} else if finishTime.Before(now.Add(30 * 24 * time.Hour)) {
return ExpireTypeExpireIn30Days
} else {
return ExpireTypeExpireInFuture
}
}
type ChartOperationSwitch struct {
// 所有迭代时,disable move out
enableMoveOut bool
// disable drag
enableDrag bool
// 按状态分类
enableMoveTo bool
// 按处理人分类
enableMoveToAssignee bool
enableDragToAssignee bool
// 按自定义看板分类
enableMoveToCustom bool
enableDragToCustom bool
// 按优先级分类
enableMoveToPriority bool
enableDragToPriority bool
enableChangePageNo bool
}
func (c *ChartOperationSwitch) ClearAble() {
c.enableMoveOut = false
c.enableDrag = false
c.enableMoveTo = false
c.enableMoveToAssignee = false
c.enableDragToAssignee = false
c.enableMoveToCustom = false
c.enableDragToCustom = false
c.enableMoveToPriority = false
c.enableDragToPriority = false
c.enableChangePageNo = false
}
type ComponentIssueBoard struct {
ctxBdl protocol.ContextBundle
boardType BoardType
swt ChartOperationSwitch
CompName string `json:"name"`
Data IssueBoard `json:"data"`
Operations map[string]interface{} `json:"operations"`
State IssueBoardState `json:"state"`
}
type IssueBoardState struct {
DropTarget interface{} `json:"dropTarget"`
apistructs.IssuePanel
Priority apistructs.IssuePriority `json:"priority"`
FilterConditions apistructs.IssuePagingRequest `json:"filterConditions"`
IssueViewGroupChildrenValue map[string]string `json:"issueViewGroupChildrenValue"`
IssueViewGroupValue string `json:"issueViewGroupValue"`
}
type IssueFilterRequest struct {
apistructs.IssuePagingRequest
BoardType BoardType `json:"boardType"`
KanbanKey string `json:"kanbanKey"`
}
type issueRenderInparams struct {
ProjectID string `json:"projectID"`
BoardType BoardType `json:"boardType"`
FixedIssueType apistructs.IssueType `json:"fixedIssueType"`
FixedIssueIteration string `json:"fixedIssueIteration"`
}
type BoardType string
func (b BoardType) String() string {
return string(b)
}
const (
BoardTypeStatus BoardType = "status"
BoardTypeTime BoardType = "deadline"
BoardTypeAssignee BoardType = "assignee" // 已经移除处理人看板
BoardTypeCustom BoardType = "custom"
BoardTypePriority BoardType = "priority"
)
var SupportBoardTypes = []BoardType{BoardTypeStatus, BoardTypeTime, BoardTypeAssignee, BoardTypeCustom}
var IssueTypes = []apistructs.IssueType{apistructs.IssueTypeTask, apistructs.IssueTypeRequirement, apistructs.IssueTypeBug, apistructs.IssueTypeEpic}
var IssueTypeStates = map[apistructs.IssueType][]apistructs.IssueState{
apistructs.IssueTypeTask: {apistructs.IssueStateOpen, apistructs.IssueStateWorking, apistructs.IssueStateDone},
apistructs.IssueTypeRequirement: {apistructs.IssueStateOpen, apistructs.IssueStateWorking, apistructs.IssueStateDone},
apistructs.IssueTypeBug: {apistructs.IssueStateOpen, apistructs.IssueStateWontfix, apistructs.IssueStateReopen, apistructs.IssueStateResolved, apistructs.IssueStateClosed},
}
func uniq(in []string) []string {
var out []string
s := set.New()
for _, v := range in {
if !s.Has(v) {
s.Insert(v)
out = append(out, v)
}
}
return out
}
func (i *ComponentIssueBoard) SetOperationSwitch(req *IssueFilterRequest) error {
i.swt.ClearAble()
switch i.boardType {
case BoardTypeAssignee:
i.swt.enableMoveToAssignee = true
i.swt.enableDragToAssignee = true
case BoardTypeTime:
case BoardTypeStatus:
i.swt.enableMoveTo = true
i.swt.enableDrag = true
case BoardTypePriority:
i.swt.enableMoveToPriority = true
i.swt.enableDragToPriority = true
case BoardTypeCustom:
i.swt.enableMoveToCustom = true
i.swt.enableDragToCustom = true
default:
return nil
}
// 全部看板都可以移出迭代
i.swt.enableMoveOut = true
i.swt.enableChangePageNo = true
return nil
}
func (i *ComponentIssueBoard) SetBoardDate(c apistructs.Component) error {
var board []CartList
cont, err := json.Marshal(c.Data["board"].([]CartList))
if err != nil {
logrus.Errorf("marshal component data failed, content:%v, err:%v", c.State, err)
return err
}
err = json.Unmarshal(cont, &board)
if err != nil {
logrus.Errorf("unmarshal component dat failed, content:%v, err:%v", cont, err)
return err
}
i.Data.Board = board
return nil
}
func (i *ComponentIssueBoard) SetBoardType(bt BoardType) {
i.boardType = bt
}
// GetUserPermission check Guest permission
func (i *ComponentIssueBoard) CheckUserPermission(projectID uint64) error {
isGuest := false
scopeRole, err := i.ctxBdl.Bdl.ScopeRoleAccess(i.ctxBdl.Identity.UserID, &apistructs.ScopeRoleAccessRequest{
Scope: apistructs.Scope{
Type: apistructs.ProjectScope,
ID: strconv.FormatUint(projectID, 10),
},
})
if err != nil {
return err
}
for _, role := range scopeRole.Roles {
if role == "Guest" {
isGuest = true
}
}
if !isGuest {
return nil
}
// if the permission of user is guest , could't to do operation
for k, v := range i.Operations {
i.Operations[k] = i.disableOperationPermission(v)
}
for ib, board := range i.Data.Board {
for k, v := range board.Operations {
i.Data.Board[ib].Operations[k] = i.disableOperationPermission(v)
}
for ic, ca := range board.List {
for k, v := range ca.Operations {
i.Data.Board[ib].List[ic].Operations[k] = i.disableOperationPermission(v)
}
}
}
return nil
}
func (i *ComponentIssueBoard) disableOperationPermission(op interface{}) interface{} {
switch op.(type) {
case MoveOutOperation:
o := op.(MoveOutOperation)
o.Disabled = true
return o
case DragOperation:
o := op.(DragOperation)
o.Disabled = true
return o
case MoveToOperation:
o := op.(MoveToOperation)
o.Disabled = true
return o
case MoveToPriorityOperation:
o := op.(MoveToPriorityOperation)
o.Disabled = true
return o
case CreateBoardOperation:
o := op.(CreateBoardOperation)
o.Disabled = true
o.DisabledTip = i.ctxBdl.I18nPrinter.Sprintf(GuestCreateBoardDisabledTip)
return o
case DeleteBoardOperation:
o := op.(DeleteBoardOperation)
o.Disabled = true
return o
case UpdateBoardOperation:
o := op.(UpdateBoardOperation)
o.Disabled = true
return o
case MoveToCustomOperation:
o := op.(MoveToCustomOperation)
o.Disabled = true
return o
default:
panic("errors: invalid operationInfo type")
}
return nil
}
func (i *ComponentIssueBoard) Filter(req IssueFilterRequest) (ib *IssueBoard, err error) {
var (
cls []CartList
board IssueBoard
uids []string
)
i.Operations = make(map[string]interface{})
// 事件分类: 所有,需求,任务,缺陷
switch req.BoardType {
// 所有,不含此分类
case BoardTypeStatus:
cls, uids, err = i.FilterByStatusConcurrent(req.IssuePagingRequest, req.KanbanKey)
case BoardTypeTime:
cls, uids, err = i.FilterByTime(req.IssuePagingRequest, req.KanbanKey)
case BoardTypePriority:
cls, uids, err = i.FilterByPriority(req.IssuePagingRequest, req.KanbanKey)
// 所有,不含此分类
case BoardTypeCustom:
cls, uids, err = i.FilterByCustom(req.IssuePagingRequest, req.KanbanKey)
// Created custom board max num 15
o := CreateBoardOperation{}
o.Disabled = false
o.Confirm = i.ctxBdl.I18nPrinter.Sprintf(CreateBoardConfirmMsg)
o.Text = i.ctxBdl.I18nPrinter.Sprintf(apistructs.CreateCustomOperation.String())
o.Reload = true
o.DisabledTip = i.ctxBdl.I18nPrinter.Sprintf(CreateBoardDisabledTip)
if len(cls) > MaxBoardNum {
o.Disabled = true
}
i.Operations[apistructs.CreateCustomOperation.String()] = o
default:
//err = fmt.Errorf("invalid board type [%s], must in [%v]", req.BoardType, SupportBoardTypes)
cls, uids, err = i.FilterByPriority(req.IssuePagingRequest, req.KanbanKey)
}
if err != nil {
return
}
// 时间分类i18n
for k, v := range cls {
cls[k].Label = i.ctxBdl.I18nPrinter.Sprintf(v.Label)
}
if req.KanbanKey != "" {
board.RefreshBoard = false
} else {
board.RefreshBoard = true
}
board.Board = cls
board.UserIDs = uniq(uids)
ib = &board
return
}
func (i ComponentIssueBoard) GetByStatus(req apistructs.IssuePagingRequest, ignoreDone bool) (rsp *apistructs.IssuePagingResponse, err error) {
if len(req.Type) == 0 || len(req.Type) != 1 {
err = fmt.Errorf("issue type number is not 1, type: %v", req.Type)
return
}
if req.PageSize == 0 {
req.PageSize = 200
}
// 获取当前项目,特定IssueType的IssueStates
//it := req.Type[0]
//isReq := apistructs.IssueStateRelationGetRequest{ProjectID: int64(req.ProjectID), IssueType: it}
//is, err := i.ctxBdl.Bdl.GetIssueStateBelong(isReq)
//if err != nil {
// logrus.Errorf("get issue state belong failed, request:%+v, err:%v", isReq, err)
// return
//}
//var states []apistructs.IssueStateName
//var issueStates []int64
//for _, v := range is {
// if ignoreDone && (v.StateBelong == apistructs.IssueStateBelongClosed || v.StateBelong == apistructs.IssueStateBelongDone) {
// continue
// }
// states = append(states, v.States...)
//}
//for _, v := range states {
// issueStates = append(issueStates, v.ID)
//}
//if ignoreDone {
// req.State = issueStates
//}
rsp, err = i.ctxBdl.Bdl.PageIssues(req)
if err != nil {
logrus.Errorf("page issues failed, request:%+v, err:%v", req, err)
return
}
return
}
func (i ComponentIssueBoard) GetIssues(req apistructs.IssuePagingRequest, ignoreDone bool) (result []apistructs.Issue, uids []string, err error) {
if req.Type == nil {
req.Type = IssueTypes
}
var wg sync.WaitGroup
wg.Add(len(req.Type))
for _, v := range req.Type {
v := v
go func() {
defer func() {
wg.Done()
}()
if err != nil {
return
}
r := req
r.Type = []apistructs.IssueType{v}
rsp, err := i.GetByStatus(r, ignoreDone)
if err != nil {
return
}
result = append(result, rsp.Data.List...)
uids = append(uids, rsp.UserIDs...)
}()
}
wg.Wait()
return
}
func GenCart(bt BoardType, i apistructs.Issue, p *message.Printer, s ChartOperationSwitch, mp map[apistructs.OperationKey]interface{}) IssueCart {
var c IssueCart
switch bt {
case BoardTypeTime:
c.Assignee = i.Assignee
c.Priority = i.Priority
c.PlanFinishedAt = i.PlanFinishedAt
c.State = i.State
case BoardTypeCustom:
c.Assignee = i.Assignee
c.Priority = i.Priority
c.State = i.State
case BoardTypePriority:
c.Assignee = i.Assignee
c.State = i.State
case BoardTypeAssignee:
c.Priority = i.Priority
c.State = i.State
case BoardTypeStatus:
c.Assignee = i.Assignee
c.Priority = i.Priority
default:
}
c.ID = i.ID
c.Title = i.Title
c.Type = i.Type
c.IssueButton = i.IssueButton
c.IterationID = i.IterationID
c.SetI18nPrinter(p)
c.RenderCartOperations(s, i, mp)
return c
}
func (c *CartList) GenCartList(p *message.Printer, s ChartOperationSwitch) {
c.SetI18nPrinter(p)
c.RenderCartListOperations(s)
}
// 按状态过滤 并发
func (i ComponentIssueBoard) FilterByStatusConcurrent(req apistructs.IssuePagingRequest, kanbanKey string) (cls []CartList, uids []string, err error) {
if len(req.Type) == 0 || len(req.Type) != 1 {
err = fmt.Errorf("issue type number is not 1, type: %v", req.Type)
return
}
// 获取当前项目,特定IssueType的IssueStates
it := req.Type[0]
isReq := apistructs.IssueStateRelationGetRequest{ProjectID: req.ProjectID, IssueType: it}
is, err := i.ctxBdl.Bdl.GetIssueStateBelong(isReq)
if err != nil {
logrus.Errorf("get issue state belong failed, request:%+v, err:%v", isReq, err)
return
}
var states []apistructs.IssueStateName
if kanbanKey == "" {
for _, v := range is {
states = append(states, v.States...)
}
} else {
for _, v := range is {
for _, v2 := range v.States {
if strconv.FormatInt(v2.ID, 10) == kanbanKey {
states = append(states, v2)
goto loop
}
}
}
}
loop:
// filter by status is not avialble in status board
if len(req.StateBelongs) > 0 {
req.StateBelongs = nil
}
date := struct {
Map map[int64]CartList
Lock sync.Mutex
}{
Map: make(map[int64]CartList),
}
var wg sync.WaitGroup
wg.Add(len(states))
for _, v := range states {
go func(state apistructs.IssueStateName) {
defer func() {
wg.Done()
}()
if err != nil {
return
}
r := req
// 按特定种类IssueType的一个IssueState并发查询
r.State = []int64{state.ID}
rsp, e := i.ctxBdl.Bdl.PageIssues(r)
if e != nil {
err = e
logrus.Errorf("page issues failed, request:%+v, err:%v", r, e)
return
}
// 生成CartList
cl := CartList{}
cl.Label = state.Name
cl.LabelKey = state.ID
cl.Total = rsp.Data.Total
cl.PageNo = req.PageNo
cl.PageSize = req.PageSize
if i.swt.enableChangePageNo {
cl.RenderChangePageNoOperation(strconv.FormatInt(cl.LabelKey.(int64), 10))
}
for _, v := range rsp.Data.List {
c := GenCart(i.boardType, v, i.ctxBdl.I18nPrinter, i.swt, nil)
cl.Add(c)
}
date.Lock.Lock()
date.Map[state.ID] = cl
date.Lock.Unlock()
// uid
uids = append(uids, rsp.UserIDs...)
}(v)
}
wg.Wait()
for _, v := range states {
cls = append(cls, date.Map[v.ID])
}
return
}
// FilterByPriority 按优先级过滤 去掉终态状态 [CLOSED, DONE]
func (i ComponentIssueBoard) FilterByPriority(req apistructs.IssuePagingRequest, kanbanKey string) (cls []CartList, uids []string, err error) {
// 用于生成权限
var priorityList []apistructs.IssuePriority
if kanbanKey != "" {
priorityList = append(priorityList, apistructs.IssuePriority(kanbanKey))
} else {
priorityList = apistructs.IssuePriorityList
}
mp := make(map[apistructs.OperationKey]interface{})
mp[apistructs.MoveToPriorityOperation] = priorityList
mp[apistructs.DragToPriorityOperation] = priorityList
date := struct {
Map map[apistructs.IssuePriority]CartList
Lock sync.Mutex
}{
Map: make(map[apistructs.IssuePriority]CartList),
}
var wg sync.WaitGroup
wg.Add(len(priorityList))
for _, v := range priorityList {
go func(state apistructs.IssuePriority) {
defer func() {
wg.Done()
}()
if err != nil {
return
}
r := req
// 按特定种类IssueType的一个IssueState并发查询
r.Priority = []apistructs.IssuePriority{state}
rsp, e := i.ctxBdl.Bdl.PageIssues(r)
if e != nil {
err = e
logrus.Errorf("page issues failed, request:%+v, err:%v", r, e)
return
}
// 生成CartList
cl := CartList{}
cl.Label = string(state)
cl.LabelKey = cl.Label
cl.Total = rsp.Data.Total
cl.PageNo = req.PageNo
cl.PageSize = req.PageSize
if i.swt.enableChangePageNo {
cl.RenderChangePageNoOperation(cl.LabelKey.(string))
}
for _, v := range rsp.Data.List {
c := GenCart(i.boardType, v, i.ctxBdl.I18nPrinter, i.swt, mp)
cl.Add(c)
}
date.Lock.Lock()
date.Map[state] = cl
date.Lock.Unlock()
// uid
uids = append(uids, rsp.UserIDs...)
}(v)
}
wg.Wait()
for _, v := range priorityList {
cls = append(cls, date.Map[v])
}
return
}
// FilterByTime 根据完成时间(planFinishedAt)分为:未分类,已过期,1天内过期,2天内,7天内,30天,未来
func (i ComponentIssueBoard) FilterByTime(req apistructs.IssuePagingRequest, kanbanKey string) (cls []CartList, uids []string, err error) {
// 为减少事件数量,不需要展示终态的事项[CLOSED, DONE]
timeMap := getTimeMap()
// map并发写需要加锁
date := struct {
Map map[ExpireType]CartList
Lock sync.Mutex
}{
Map: make(map[ExpireType]CartList),
}
var expireTypes []ExpireType
if kanbanKey != "" {
expireTypes = append(expireTypes, ExpireType(kanbanKey))
} else {
expireTypes = ExpireTypes
}
var wg sync.WaitGroup
wg.Add(len(expireTypes))
// 按特定PlanFinishedAt的一个并发查询、
for _, et := range expireTypes {
go func(tm ExpireType) {
defer func() {
wg.Done()
}()
if err != nil {
return
}
r := req
if tm == ExpireTypeUndefined {
r.IsEmptyPlanFinishedAt = true