-
Notifications
You must be signed in to change notification settings - Fork 375
/
filetree.go
973 lines (848 loc) · 29.1 KB
/
filetree.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
// 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 filetree
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/gittar-adaptor/service/apierrors"
"github.com/erda-project/erda/modules/gittar-adaptor/service/pipeline"
"github.com/erda-project/erda/modules/pkg/diceworkspace"
"github.com/erda-project/erda/pkg/strutil"
"github.com/erda-project/erda/pkg/uuid"
)
const gittarPrefixOpenApi = "/wb/"
const gittarEntryTreeType = "tree"
const gittarEntryBlobType = "blob"
// Pipeline pipeline 结构体
type GittarFileTree struct {
bdl *bundle.Bundle
}
// Option Pipeline 配置选项
type Option func(*GittarFileTree)
// New Pipeline service
func New(options ...Option) *GittarFileTree {
r := &GittarFileTree{}
for _, op := range options {
op(r)
}
return r
}
// WithBundle 配置 bundle
func WithBundle(bdl *bundle.Bundle) Option {
return func(f *GittarFileTree) {
f.bdl = bdl
}
}
func (svc *GittarFileTree) ListFileTreeNodes(req apistructs.UnifiedFileTreeNodeListRequest, orgID uint64) ([]*apistructs.UnifiedFileTreeNode, error) {
// 参数校验
if err := req.BasicValidate(); err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InvalidParameter(err)
}
// 项目id转化
projectID, err := strconv.ParseInt(req.ScopeID, 10, 64)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
var list []*apistructs.UnifiedFileTreeNode
// 假如 pinode 是根节点,就根据 scopeID 查询项目下都应用列表
if req.Pinode == apistructs.RootPinode {
// 查询项目名称
project, err := svc.bdl.GetProject(uint64(projectID))
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
// 查询项目下都应用列表
apps, err := svc.bdl.GetAppsByProjectSimple(uint64(projectID), orgID, req.UserID)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
if apps == nil || apps.List == nil || len(apps.List) <= 0 {
return nil, nil
}
// 将应用转化为第一层的目录树
for _, app := range apps.List {
list = append(list, appConvertToUnifiedFileTreeNode(&app, req.Scope, req.ScopeID, strconv.Itoa(int(project.ID))))
}
} else {
// 不是就根据 pinode 查询其在 gittar 的目录
// pinode 反base64 获取父类的路径
pinodeBytes, err := base64.StdEncoding.DecodeString(req.Pinode)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
pinode := string(pinodeBytes)
pathSplit := strings.Split(pinode, "/")
length := len(pathSplit)
// 因为分支是 feature/sss/sss 这种模式根据 / 分割就会有问题
branchExcessLength := getBranchExcessLength(pinode)
if length < 2 {
return nil, fmt.Errorf("not find nodes: pinode length error")
}
// 第一个为projectID的字符串, 获取其名称
// 第二个为appId的字符串, 获取其名称
appID, err := strconv.ParseUint(pathSplit[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid appID: %s, err: %v", pathSplit[1], err)
}
app, err := svc.bdl.GetApp(appID)
if err != nil {
return nil, fmt.Errorf("failed to get app, appID: %d, err: %v", appID, err)
}
var realPinode = app.ProjectName + "/" + app.Name
for i := 2; i < len(pathSplit); i++ {
realPinode += "/" + pathSplit[i]
}
// 根据 / 分割判定长度为 2 的时候,代表需要查询分支列表
if length == 2 {
branchs, err := svc.bdl.GetGittarBranchesV2(gittarPrefixOpenApi+realPinode, strconv.Itoa(int(orgID)), true)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
if branchs == nil || len(branchs) <= 0 {
return nil, nil
}
for _, branch := range branchs {
// 过滤掉不符合分支规则
_, err := svc.GetWorkspaceByBranch(pathSplit[0], branch)
if err != nil {
continue
}
list = append(list, branchConvertToUnifiedFileTreeNode(branch, req.Scope, req.ScopeID, pinode, req.Pinode))
}
} else if length > 3+branchExcessLength {
// 长度大于 3 就表达查询子节点了 /projectName/appName/tree/branchName
entrys, err := svc.bdl.GetGittarTreeNode(gittarPrefixOpenApi+realPinode, strconv.Itoa(int(orgID)), true)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
if entrys == nil || len(entrys) <= 0 {
return nil, nil
}
for _, node := range entrys {
if strings.Contains(node.Name, "/") {
node.Name = strings.Split(node.Name, "/")[0]
}
// 代表是分支下, 过滤出 pipeline.yml 和 .dice
if length == 4+branchExcessLength {
// 假如不是 tree yml 结构的文件
if node.Type != gittarEntryTreeType && node.Name != "pipeline.yml" {
continue
}
if node.Type == gittarEntryTreeType && node.Name != ".dice" {
continue
}
}
// 代表在.dice 目录下, 文件全部过滤,只有pipelines
if length == 5+branchExcessLength {
if node.Type != gittarEntryTreeType {
continue
}
if node.Type == gittarEntryTreeType && node.Name != "pipelines" {
continue
}
}
// 长度大于 5 就代表过滤出 .yml 就行了
if length > 5+branchExcessLength {
if node.Type != gittarEntryTreeType && !strings.HasSuffix(node.Name, ".yml") {
continue
}
}
list = append(list, entryConvertToUnifiedFileTreeNode(&node, req.Scope, req.ScopeID, pinode, req.Pinode))
}
}
}
return list, nil
}
func (svc *GittarFileTree) GetFileTreeNode(req apistructs.UnifiedFileTreeNodeGetRequest, orgID uint64) (*apistructs.UnifiedFileTreeNode, error) {
// 参数校验
if err := req.BasicValidate(); err != nil {
return nil, apierrors.ErrGetGittarFileTreeNode.InvalidParameter(err)
}
// pinode 反base64 获取父类的路径
inodeBytes, err := base64.StdEncoding.DecodeString(req.Inode)
if err != nil {
return nil, apierrors.ErrGetGittarFileTreeNode.InternalError(err)
}
inode := string(inodeBytes)
inode = strings.Replace(inode, "/"+gittarEntryTreeType, "/"+gittarEntryBlobType, 1)
pathSplit := strings.Split(inode, "/")
// 因为分支是 feature/sss/sss 这种模式根据 / 分割就会有问题
branchExcessLength := getBranchExcessLength(inode)
// 第一个为projectID的字符串, 获取其名称
// 第二个为appId的字符串, 获取其名称
appID, err := strconv.ParseUint(pathSplit[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid appID: %s, err: %v", pathSplit[1], err)
}
app, err := svc.bdl.GetApp(appID)
if err != nil {
return nil, fmt.Errorf("failed to get app, appID: %d, err: %v", appID, err)
}
realinode, treeRepo := app.ProjectName+"/"+app.Name, app.ProjectName+"/"+app.Name
for i := 2; i < len(pathSplit); i++ {
realinode += "/" + pathSplit[i]
if i == 2 && pathSplit[i] == gittarEntryBlobType {
treeRepo += "/" + gittarEntryTreeType
} else {
treeRepo += "/" + pathSplit[i]
}
}
orgIDStr := strconv.Itoa(int(orgID))
// 获取文件内容
context, err := svc.bdl.GetGittarBlobNode(gittarPrefixOpenApi+realinode, orgIDStr)
if err != nil {
return nil, apierrors.ErrGetGittarFileTreeNode.InternalError(err)
}
// 获取文件commit信息
commitMessage, err := svc.bdl.GetGittarTree(gittarPrefixOpenApi+treeRepo, orgIDStr)
if err != nil {
return nil, apierrors.ErrGetGittarFileTreeNode.InternalError(err)
}
// 获取 snippetConfigName
snippetConfigName := ""
for i := 4 + branchExcessLength; i < len(pathSplit); i++ {
snippetConfigName += "/" + pathSplit[i]
}
// 根据分支获取 workspace
branch := getBranchStr(inode)
workspace, err := svc.GetWorkspaceByBranch(pathSplit[0], branch)
if err != nil {
return nil, err
}
return &apistructs.UnifiedFileTreeNode{
Type: apistructs.UnifiedFileTreeNodeTypeFile,
Inode: req.Inode,
UpdaterID: commitMessage.Commit.Committer.Name,
UpdatedAt: commitMessage.Commit.Committer.When,
Desc: commitMessage.Commit.CommitMessage,
Name: strings.ReplaceAll(strings.ReplaceAll(pathSplit[len(pathSplit)-1], ".", "-"), "/", "-"),
Meta: map[string]interface{}{
apistructs.AutoTestFileTreeNodeMetaKeyPipelineYml: context,
apistructs.AutoTestFileTreeNodeMetaKeySnippetAction: apistructs.PipelineYmlAction{
SnippetConfig: &apistructs.SnippetConfig{
Name: snippetConfigName,
Source: apistructs.SnippetSourceLocal,
Labels: map[string]string{
apistructs.LabelGittarYmlPath: getGittarYmlNamesLabels(app.Name, workspace, branch, snippetConfigName[1:]),
},
},
Type: "snippet",
},
},
}, nil
}
func (svc *GittarFileTree) GetWorkspaceByBranch(projectIDStr, branch string) (string, error) {
projectID, err := strconv.Atoi(projectIDStr)
if err != nil {
return "", err
}
rules, err := svc.bdl.GetProjectBranchRules(uint64(projectID))
if err != nil {
return "", err
}
// get workspace by branch
wsByBranch, err := diceworkspace.GetByGitReference(branch, rules)
if err != nil {
return "", err
}
return wsByBranch.String(), nil
}
func getGittarYmlNamesLabels(appID, workspace, branch, ymlName string) string {
return fmt.Sprintf("%s/%s/%s/%s", appID, workspace, branch, ymlName)
}
func (svc *GittarFileTree) DeleteFileTreeNode(req apistructs.UnifiedFileTreeNodeDeleteRequest, orgID uint64) (*apistructs.UnifiedFileTreeNode, error) {
// 参数校验
if err := req.BasicValidate(); err != nil {
return nil, apierrors.ErrDeleteGittarFileTreeNode.InvalidParameter(err)
}
// 解析
inodeBytes, err := base64.StdEncoding.DecodeString(req.Inode)
if err != nil {
return nil, apierrors.ErrDeleteGittarFileTreeNode.InvalidParameter(err)
}
inode := string(inodeBytes)
inodeSplit := strings.Split(inode, "/")
// 因为分支是 feature/sss/sss 这种模式根据 / 分割就会有问题
branchExcessLength := getBranchExcessLength(inode)
var length = len(inodeSplit)
if length < 4+branchExcessLength {
return nil, fmt.Errorf("wrong format inode error: length too short")
}
appID, err := strconv.ParseUint(inodeSplit[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid appID: %s, err: %v", inodeSplit[1], err)
}
app, err := svc.bdl.GetApp(appID)
if err != nil {
return nil, fmt.Errorf("failed to get app, appID: %d, err: %v", appID, err)
}
// path 是给 gittar commit 用的地址,地址是除了 项目名分支名和应用名
var path = ""
for i := 4 + branchExcessLength; i < length; i++ {
path += inodeSplit[i] + "/"
}
path = path[:len(path)-1]
// 这里认定以 .yml 结尾的就是文件,其他都是文件夹
var pathType string
if strings.HasSuffix(path, ".yml") {
pathType = "blob"
} else {
pathType = "tree"
}
var request = apistructs.GittarCreateCommitRequest{
Branch: getBranchStr(inode),
Message: "delete file" + path,
Actions: []apistructs.EditActionItem{
{
Action: "delete",
PathType: pathType,
Path: path,
},
},
}
resp, err := svc.bdl.CreateGittarCommitV2(fmt.Sprintf("wb/%s/%s", app.ProjectName, app.Name), request, int(orgID))
if err != nil {
return nil, err
}
if !resp.Success {
return nil, fmt.Errorf("delete filetree node error: %v", resp.Error)
}
return nil, nil
}
func (svc *GittarFileTree) FuzzySearchFileTreeNodes(req apistructs.UnifiedFileTreeNodeFuzzySearchRequest, orgID uint64) ([]apistructs.UnifiedFileTreeNode, error) {
// 参数校验
if err := req.BasicValidate(); err != nil {
return nil, apierrors.ErrFuzzySearchGittarFileTreeNodes.InvalidParameter(err)
}
// 项目id转化
projectID, err := strconv.ParseInt(req.ScopeID, 10, 64)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
// 查询项目下都应用列表
apps, err := svc.bdl.GetAppsByProjectSimple(uint64(projectID), orgID, req.UserID)
if err != nil {
return nil, apierrors.ErrListGittarFileTreeNodes.InternalError(err)
}
if len(apps.List) <= 0 {
return nil, nil
}
projectName := apps.List[0].ProjectName
var results []apistructs.UnifiedFileTreeNode
// 异步设置值
if req.FromPinode == "" {
results, err = svc.searchGittarYmlList(apps.List, orgID, projectName, "", "", projectID)
if err != nil {
return nil, err
}
} else {
inodeBytes, err := base64.StdEncoding.DecodeString(req.FromPinode)
if err != nil {
return nil, apierrors.ErrGetGittarFileTreeNode.InternalError(err)
}
pathSplit := strings.Split(string(inodeBytes), "/")
length := len(pathSplit)
var filterAppName string
var filterBranch string
if length == 2 {
// 第二个为appId的字符串, 获取其名称
appID, err := strconv.ParseUint(pathSplit[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid appID: %s, err: %v", pathSplit[1], err)
}
app, err := svc.bdl.GetApp(appID)
if err != nil {
return nil, fmt.Errorf("failed to get app, appID: %d, err: %v", appID, err)
}
filterAppName = app.Name
} else if length >= 3 {
filterBranch = getBranchStr(string(inodeBytes))
}
results, err = svc.searchGittarYmlList(apps.List, orgID, projectName, filterAppName, filterBranch, projectID)
if err != nil {
return nil, err
}
}
// 过滤查询
var filterResult []apistructs.UnifiedFileTreeNode
for _, v := range results {
if !strings.Contains(v.Name, req.Fuzzy) {
continue
}
filterResult = append(filterResult, v)
}
return filterResult, nil
}
func (svc *GittarFileTree) CreateFileTreeNode(req apistructs.UnifiedFileTreeNodeCreateRequest, orgID uint64) (*apistructs.UnifiedFileTreeNode, error) {
// 创建前校验
node, err := svc.validateNodeBeforeCreate(req)
if err != nil {
return nil, apierrors.ErrCreateGittarFileTreeNode.InvalidParameter(err)
}
// 解析
pinodeBytes, err := base64.StdEncoding.DecodeString(node.Pinode)
if err != nil {
return nil, apierrors.ErrCreateGittarFileTreeNode.InvalidParameter(err)
}
pinode := string(pinodeBytes)
pinodeSplit := strings.Split(pinode, "/")
// 因为分支是 feature/sss/sss 这种模式根据 / 分割就会有问题
branchExcessLength := getBranchExcessLength(pinode)
// 父节点一定是 /project/app/blob(tree)/branch
var length = len(pinodeSplit)
if length < 4+branchExcessLength {
return nil, fmt.Errorf("wrong format pinode error: length too short")
}
// 获取各个信息
appID := pinodeSplit[1]
branch := getBranchStr(pinode)
// 获取project的name 和app的name
appIDUint, err := strconv.ParseUint(appID, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid appID: %s, err: %v", appID, err)
}
app, err := svc.bdl.GetApp(appIDUint)
if err != nil {
return nil, fmt.Errorf("failed to get app, appID: %v, err: %v", appID, err)
}
// 假如是创建文件
var request = apistructs.GittarCreateCommitRequest{}
if node.Type == apistructs.UnifiedFileTreeNodeTypeDir {
// .dice 只能是 pinode 为 /project/app/blob(tree)/branch
if length == 4+branchExcessLength && node.Name != ".dice" {
return nil, fmt.Errorf("error create folder: only '.dice' files can be created under the branch")
}
// pipelines 只能是 pinode 为 /project/app/blob(tree)/branch/.dice
if length == 5+branchExcessLength && node.Name != "pipelines" {
return nil, fmt.Errorf("error create folder: only 'pipelines' files can be created under the .dice")
}
var path = ""
for i := 4 + branchExcessLength; i < length; i++ {
path += pinodeSplit[i] + "/"
}
path += node.Name
// 否则文件夹就随便创建
request.Branch = branch
request.Actions = []apistructs.EditActionItem{
{
Action: "add",
Path: path,
PathType: gittarEntryTreeType,
},
}
request.Message = "add new directory"
} else {
// 这里就是文件创建逻辑,目前只能创建.yml文件
if !strings.HasSuffix(node.Name, ".yml") {
return nil, fmt.Errorf("only '.yml' suffix file can be created")
}
request.Branch = branch
request.Actions = []apistructs.EditActionItem{}
request.Message = "add " + node.Name
action := apistructs.EditActionItem{
Action: "add",
PathType: gittarEntryBlobType,
}
// 假如长度为4,那就是应用的根目录下,就只能创建pipeline.yml文件了
if length == 4+branchExcessLength {
if node.Name != "pipeline.yml" {
return nil, fmt.Errorf("only 'pipeline.yml' can be created under the branch")
}
action.Path = node.Name
request.Actions = append(request.Actions, action)
} else {
// 否则长度就要大于6 /project/app/blob(tree)/branch/.dice/pipelines 的父目录下才能创建文件
if length >= 6+branchExcessLength {
// 获取除了branch的path
var path = ""
for i := 4 + branchExcessLength; i < length; i++ {
path += pinodeSplit[i] + "/"
}
path += node.Name
action.Path = path
request.Actions = append(request.Actions, action)
} else {
return nil, fmt.Errorf("error create file: create .yml files can only be in the .dice/pipelines folder")
}
}
}
// 创建文件
resp, err := svc.bdl.CreateGittarCommitV2(fmt.Sprintf("wb/%s/%s", app.ProjectName, app.Name), request, int(orgID))
if err != nil {
return nil, err
}
if !resp.Success {
return nil, fmt.Errorf("create filetree node error: %v", resp.Error)
}
if node.Type == apistructs.UnifiedFileTreeNodeTypeDir {
return &apistructs.UnifiedFileTreeNode{
Type: apistructs.UnifiedFileTreeNodeTypeDir,
Inode: base64.StdEncoding.EncodeToString([]byte(pinode + "/" + node.Name)),
Pinode: node.Pinode,
Name: node.Name,
Scope: node.Scope,
ScopeID: node.ScopeID,
Desc: node.Desc,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatorID: node.CreatorID,
UpdaterID: node.UpdaterID,
}, nil
}
var treeNodeGetRequest = apistructs.UnifiedFileTreeNodeGetRequest{
Inode: base64.StdEncoding.EncodeToString([]byte(pinode + "/" + node.Name)),
ScopeID: node.ScopeID,
Scope: node.Scope,
}
return svc.GetFileTreeNode(treeNodeGetRequest, orgID)
}
func (svc *GittarFileTree) validateNodeBeforeCreate(req apistructs.UnifiedFileTreeNodeCreateRequest) (*apistructs.AutoTestFileTreeNode, error) {
// 构造 node
node := apistructs.AutoTestFileTreeNode{
Type: req.Type,
Scope: req.Scope,
ScopeID: req.ScopeID,
Pinode: req.Pinode,
Name: req.Name,
Desc: req.Desc,
CreatorID: req.IdentityInfo.UserID,
UpdaterID: req.IdentityInfo.UserID,
}
// 参数校验
if !req.Type.Valid() {
return nil, fmt.Errorf("invalid node type: %s", req.Type.String())
}
if req.Type.IsDir() {
if req.Pinode == "" {
return nil, fmt.Errorf("pinode can not be empty")
} else {
// non-root dir
if err := req.ValidateNonRootDir(); err != nil {
return nil, err
}
}
}
if req.Type.IsFile() {
if err := req.ValidateFile(); err != nil {
return nil, err
}
}
// 字段最大长度校验
if err := strutil.Validate(node.Name, strutil.MaxLenValidator(apistructs.MaxSetNameLen)); err != nil {
return nil, err
}
if err := strutil.Validate(node.Desc, strutil.MaxLenValidator(apistructs.MaxSetDescLen)); err != nil {
return nil, err
}
// 分配 inode
node.Inode = uuid.SnowFlakeID()
return &node, nil
}
// 查询项目应用分支的节点内容列表
// apps 项目下的 app 列表
// orgID 企业id 用作获取yml内容的
// projectName 项目的名称
// filterAppName 应用名称,没有传递就是查询项目下所有的app节点内容
// filterBranchName 分支名称 没有传递就是查询所有的分支节点内容
// projectID 项目的id
func (svc *GittarFileTree) searchGittarYmlList(apps []apistructs.ApplicationDTO, orgID uint64, projectName string,
filterAppName string, filterBranchName string, projectID int64) ([]apistructs.UnifiedFileTreeNode, error) {
var wait sync.WaitGroup
var results []apistructs.UnifiedFileTreeNode
var warn error
for _, app := range apps {
// 不为空且不相等就查询全部
if filterAppName != "" && app.Name != filterAppName {
continue
}
wait.Add(1)
// 异步查询 app 中的分支
go func(app apistructs.ApplicationDTO) {
defer wait.Done()
list, err := svc.searchBranch(app, orgID, projectName, filterBranchName, projectID)
if err != nil {
warn = err
return
}
if list == nil || len(list) <= 0 {
return
}
results = append(results, list...)
}(app)
}
wait.Wait()
if warn != nil {
return nil, warn
}
return results, nil
}
// 根据分支查询
// branchName 用作过滤
func (svc *GittarFileTree) searchBranch(app apistructs.ApplicationDTO, orgID uint64, projectName string, branchName string, projectID int64) ([]apistructs.UnifiedFileTreeNode, error) {
var warn error
var results []apistructs.UnifiedFileTreeNode
var realPinode = projectName + "/" + app.Name
// 查询应用下的分支
branchs, err := svc.bdl.GetGittarBranchesV2(gittarPrefixOpenApi+realPinode, strconv.Itoa(int(orgID)), true)
if err != nil {
return nil, err
}
if branchs == nil || len(branchs) <= 0 {
return nil, nil
}
// 异步查询分支中的 yml
var branchGroupWait sync.WaitGroup
for _, branch := range branchs {
// 分支不为空且不想等就跳过
if branchName != "" && branch != branchName {
continue
}
branchGroupWait.Add(1)
go func(branch string) {
defer branchGroupWait.Done()
list, err := svc.searchYmlContext(app, orgID, projectName, branch, projectID)
if err != nil {
warn = err
return
}
if list == nil || len(list) <= 0 {
return
}
results = append(results, list...)
}(branch)
}
// 等待 branch 查询
branchGroupWait.Wait()
if warn != nil {
return nil, warn
}
return results, nil
}
func (svc *GittarFileTree) GetGittarFileByPipelineId(pipelineId uint64, orgID uint64) (*apistructs.UnifiedFileTreeNode, error) {
// 参数校验
pipelineDetail, err := svc.bdl.GetPipeline(pipelineId)
if err != nil {
return nil, err
}
labels := pipelineDetail.Labels
appID := labels[apistructs.LabelAppID]
projectID := labels[apistructs.LabelProjectID]
branch := labels[apistructs.LabelBranch]
names := strings.Split(pipelineDetail.YmlName, branch)
ymlName := pipelineDetail.YmlName
if len(names) >= 2 {
ymlName = names[1]
}
ymlName = strings.TrimPrefix(ymlName, "/")
inode := fmt.Sprintf("%s/%s/tree/%s/%s", projectID, appID, branch, ymlName)
base64Inode := base64.StdEncoding.EncodeToString([]byte(inode))
var req apistructs.UnifiedFileTreeNodeGetRequest
req.Inode = base64Inode
return svc.GetFileTreeNode(req, orgID)
}
// 根据分支下的yml列表,查询所有的内容
// branch 是分支名称
func (svc *GittarFileTree) searchYmlContext(app apistructs.ApplicationDTO, orgID uint64, projectName string, branch string, projectID int64) ([]apistructs.UnifiedFileTreeNode, error) {
var results []apistructs.UnifiedFileTreeNode
var warn error
var wait sync.WaitGroup
// 查询分支下所有的yml列表
list := pipeline.GetPipelineYmlList(apistructs.CICDPipelineYmlListRequest{
AppID: int64(app.ID),
Branch: branch,
}, svc.bdl)
if list == nil && len(list) <= 0 {
return nil, nil
}
for _, ymlPath := range list {
wait.Add(1)
go func(ymlPath string) {
defer wait.Done()
searchINode := projectName + "/" + app.Name + "/" + gittarEntryBlobType + "/" + branch + "/" + ymlPath
// 获取文件内容
context, err := svc.bdl.GetGittarBlobNode(gittarPrefixOpenApi+searchINode, strconv.Itoa(int(orgID)))
if err != nil {
warn = err
return
}
var pathSplit = strings.Split(ymlPath, "/")
inode := strconv.Itoa(int(projectID)) + "/" + strconv.Itoa(int(app.ID)) + "/" + gittarEntryBlobType + "/" + branch + "/" + ymlPath
results = append(results, apistructs.UnifiedFileTreeNode{
Name: pathSplit[len(pathSplit)-1],
Type: apistructs.UnifiedFileTreeNodeTypeFile,
Inode: base64.StdEncoding.EncodeToString([]byte(inode)),
Meta: map[string]interface{}{
apistructs.AutoTestFileTreeNodeMetaKeyPipelineYml: context,
apistructs.AutoTestFileTreeNodeMetaKeySnippetAction: apistructs.SnippetConfig{
Name: "/" + ymlPath,
Source: apistructs.SnippetSourceLocal,
Labels: map[string]string{
apistructs.LabelAppID: strconv.Itoa(int(app.ID)),
apistructs.LabelBranch: branch,
},
},
},
})
}(ymlPath)
}
wait.Wait()
if warn != nil {
return nil, warn
}
return results, nil
}
func (svc *GittarFileTree) FindFileTreeNodeAncestors(req apistructs.UnifiedFileTreeNodeFindAncestorsRequest) ([]apistructs.UnifiedFileTreeNode, error) {
// 参数校验
if err := req.BasicValidate(); err != nil {
return nil, apierrors.ErrFindGittarFileTreeNodeAncestors.InvalidParameter(err)
}
var results []apistructs.UnifiedFileTreeNode
inodeBytes, err := base64.StdEncoding.DecodeString(req.Inode)
if err != nil {
return nil, apierrors.ErrFindGittarFileTreeNodeAncestors.InvalidParameter(err)
}
branch := getBranchStr(string(inodeBytes))
err = recursivelyFindAncestors(req.Inode, &results, branch)
if err != nil {
return nil, apierrors.ErrFindGittarFileTreeNodeAncestors.InvalidParameter(err)
}
return results, nil
}
func recursivelyFindAncestors(inode string, ancestors *[]apistructs.UnifiedFileTreeNode, branch string) error {
// inode == 0 或者 ""
if inode == "" || inode == "MA==" {
return nil
}
// 解析
inodeBytes, err := base64.StdEncoding.DecodeString(inode)
if err != nil {
return apierrors.ErrFindGittarFileTreeNodeAncestors.InvalidParameter(err)
}
inodeStr := string(inodeBytes)
// 把分支用占位符替换掉
inodeStr = strings.Replace(inodeStr, branch, "${branch}", 1)
inodeSplit := strings.Split(inodeStr, "/")
var length = len(inodeSplit)
var pinode = ""
// 把 a/b/tree 中的 tree 去除
if length == 3 {
inode = base64.StdEncoding.EncodeToString([]byte(inodeSplit[0] + "/" + inodeSplit[1]))
length = 2
}
// 去除最后一个文件名或者文件夹名称
for i := 0; i < length-1; i++ {
pinode = pinode + inodeSplit[i] + "/"
}
// 把最后面的 / 去处
if strings.HasSuffix(pinode, "/") {
pinode = pinode[:len(pinode)-1]
}
// 长度为 3 的时候去除 tree
pinodeSplit := strings.Split(pinode, "/")
if len(pinodeSplit) == 3 {
pinode = strings.ReplaceAll(pinode, "/tree", "")
}
if pinode == "" {
pinode = "0"
}
// 把分支用占位符替换回来
pinode = strings.Replace(pinode, "${branch}", branch, 1)
pinode = base64.StdEncoding.EncodeToString([]byte(pinode))
*ancestors = append(*ancestors, apistructs.UnifiedFileTreeNode{
Inode: inode,
Pinode: pinode,
})
err = recursivelyFindAncestors(pinode, ancestors, branch)
return err
}
func branchConvertToUnifiedFileTreeNode(branch string, scope, scopeID, decodePInode, pinode string) *apistructs.UnifiedFileTreeNode {
// 根据绝对路径base64下,转化成一个唯一id
inode := decodePInode + "/" + gittarEntryTreeType + "/" + branch
encryptINode := base64.StdEncoding.EncodeToString([]byte(inode))
return &apistructs.UnifiedFileTreeNode{
Type: apistructs.UnifiedFileTreeNodeTypeDir,
Scope: scope,
ScopeID: scopeID,
Inode: encryptINode,
Pinode: pinode,
Name: branch,
}
}
func entryConvertToUnifiedFileTreeNode(entry *apistructs.TreeEntry, scope, scopeID, decodePInode, pinode string) *apistructs.UnifiedFileTreeNode {
// 根据绝对路径base64下,转化成一个唯一id
inode := decodePInode + "/" + entry.Name
encryptINode := base64.StdEncoding.EncodeToString([]byte(inode))
node := &apistructs.UnifiedFileTreeNode{
Scope: scope,
ScopeID: scopeID,
Inode: encryptINode,
Pinode: pinode,
Name: entry.Name,
}
if entry.Type == gittarEntryTreeType {
node.Type = apistructs.UnifiedFileTreeNodeTypeDir
} else {
node.Type = apistructs.UnifiedFileTreeNodeTypeFile
}
return node
}
func appConvertToUnifiedFileTreeNode(app *apistructs.ApplicationDTO, scope, scopeID, projectID string) *apistructs.UnifiedFileTreeNode {
// 根据绝对路径base64下,转化成一个唯一id
inode := projectID + "/" + strconv.Itoa(int(app.ID))
encryptINode := base64.StdEncoding.EncodeToString([]byte(inode))
return &apistructs.UnifiedFileTreeNode{
Type: apistructs.UnifiedFileTreeNodeTypeDir,
Scope: scope,
ScopeID: scopeID,
Inode: encryptINode,
Pinode: apistructs.RootPinode,
Name: app.Name,
Desc: app.Desc,
CreatorID: app.Creator,
UpdaterID: app.Creator,
CreatedAt: app.CreatedAt,
UpdatedAt: app.UpdatedAt,
Meta: nil,
}
}
func getBranchExcessLength(name string) int {
return len(strings.Split(getBranchStr(name), "/")) - 1
}
func getBranchStr(name string) string {
treeIndex := strings.Index(name, "tree")
blobIndex := strings.Index(name, "blob")
diceIndex := strings.Index(name, ".dice")
pipelineIndex := strings.Index(name, "pipeline.yml")
beforeIndex := treeIndex
if beforeIndex == -1 {
beforeIndex = blobIndex
}
if beforeIndex == -1 {
return ""
}
afterIndex := diceIndex
if afterIndex == -1 {
afterIndex = pipelineIndex
}
if afterIndex == -1 {
return name[beforeIndex+5:]
}
result := name[beforeIndex+5 : afterIndex-1]
return result
}