@@ -40,6 +40,10 @@ type Nexus3 struct {
40
40
* connection.Nexus3
41
41
}
42
42
43
+ type repoFormatAndType struct {
44
+ format , repoType string
45
+ }
46
+
43
47
func uploadStatus (err error ) (int , error ) {
44
48
re := regexp .MustCompile (`status (\d{3})` )
45
49
match := re .FindStringSubmatch (err .Error ())
@@ -70,10 +74,11 @@ func (n *Nexus3) reposOnDisk() (localDiskRepos []string, err error) {
70
74
return nil , err
71
75
}
72
76
log .Infof ("found the following localDiskRepos: '%v'" , localDiskRepos )
77
+
73
78
return localDiskRepos , nil
74
79
}
75
80
76
- func (n * Nexus3 ) repoFormatLocalDiskRepo (localDiskRepo string ) (string , error ) {
81
+ func (n * Nexus3 ) repoFormatLocalDiskRepo (localDiskRepo string ) (repoFormatAndType , error ) {
77
82
cn := connection.Nexus3 {
78
83
BasePathPrefix : n .BasePathPrefix ,
79
84
FQDN : n .FQDN ,
@@ -85,19 +90,21 @@ func (n *Nexus3) repoFormatLocalDiskRepo(localDiskRepo string) (string, error) {
85
90
a := artifacts.Nexus3 {Nexus3 : & cn }
86
91
repos , err := a .Repos ()
87
92
if err != nil {
88
- return "" , err
93
+ return repoFormatAndType {} , err
89
94
}
90
95
91
96
var repoFormat string
97
+ var repoType string
92
98
for _ , repo := range repos {
93
99
repoName := repo .Name
94
100
if repoName == localDiskRepo {
95
101
repoFormat = repo .Format
102
+ repoType = repo .Type
96
103
}
97
104
}
98
- log .Infof ("format of repo: '%s' is: '%s'" , localDiskRepo , repoFormat )
105
+ log .Infof ("format of repo: '%s' is: '%s' and repoType: '%s' " , localDiskRepo , repoFormat , repoType )
99
106
100
- return repoFormat , nil
107
+ return repoFormatAndType { repoFormat , repoType } , nil
101
108
}
102
109
103
110
func maven (path string , skipErrors bool ) (mp mavenParts , err error ) {
@@ -298,7 +305,7 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d
298
305
if err != nil {
299
306
return false , err
300
307
}
301
- log .Info ( f , downloadedFileChecksum )
308
+ log .Debugf ( "checksum of file: '%s' is '%s'" , f , downloadedFileChecksum )
302
309
303
310
retryClient := retryablehttp .NewClient ()
304
311
retryClient .Logger = nil
@@ -311,7 +318,7 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d
311
318
}
312
319
313
320
u := scheme + "://" + n .FQDN + "/repository/" + localDiskRepo + "/" + dir + "/" + filename + ".sha512"
314
- log .Info ( " URL: " , u )
321
+ log .Debugf ( "upload URL: '%s' " , u )
315
322
316
323
req , err := http .NewRequest ("GET" , u , nil )
317
324
if err != nil {
@@ -335,10 +342,10 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d
335
342
}()
336
343
bodyBytes , err := io .ReadAll (resp .Body )
337
344
if err != nil {
338
- return identical , err
345
+ return false , err
339
346
}
340
347
bodyString := string (bodyBytes )
341
- log .Info ( bodyString )
348
+ log .Debugf ( "checksum of artifact in nexus3: '%s'" , bodyString )
342
349
343
350
if bodyString == downloadedFileChecksum {
344
351
identical = true
@@ -347,16 +354,16 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d
347
354
return identical , nil
348
355
}
349
356
350
- func (n * Nexus3 ) UploadSingleArtifact (client * client.Nexus3 , path , localDiskRepo , localDiskRepoHome , repoFormat string , skipErrors bool ) error {
357
+ func (n * Nexus3 ) UploadSingleArtifact (client * client.Nexus3 , path , localDiskRepo , localDiskRepoHome , repoFormat string , skipErrors bool ) ( bool , error ) {
351
358
dir := strings .Replace (filepath .Dir (path ), localDiskRepoHome + "/" , "" , - 1 )
352
359
filename := filepath .Base (path )
353
360
354
361
var f * os.File
355
362
af := artifactFiles {}
356
363
357
364
if identical , _ := n .checkLocalChecksumAndCompareWithOneInRemote (filepath .Clean (path ), localDiskRepo , dir , filename ); identical {
358
- log .Info ( "Already uploaded" )
359
- return nil
365
+ log .Debugf ( "artifact: '%s' has already been uploaded", filename )
366
+ return true , nil
360
367
}
361
368
362
369
c := components.UploadComponentParams {}
@@ -365,7 +372,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
365
372
c .Repository = localDiskRepo
366
373
f , err := os .Open (filepath .Clean (path ))
367
374
if err != nil {
368
- return err
375
+ return false , err
369
376
}
370
377
c .AptAsset = f
371
378
case "maven2" :
@@ -375,7 +382,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
375
382
filePathPom := fileNameWithoutExtIncludingDir + ".pom"
376
383
377
384
if slices .Contains (checkedMavenFolders , dirPath ) {
378
- return nil
385
+ return false , nil
379
386
}
380
387
381
388
if _ , err := os .Stat (filePathPom ); err == nil {
@@ -384,13 +391,13 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
384
391
c .Repository = localDiskRepo
385
392
386
393
if err := af .mavenJarAndOtherExtensions (& c , fileNameWithoutExtIncludingDir , skipErrors ); err != nil {
387
- return err
394
+ return false , err
388
395
}
389
396
390
397
var err error
391
398
f , err = os .Open (filepath .Clean (filePathPom ))
392
399
if err != nil {
393
- return err
400
+ return false , err
394
401
}
395
402
c .Maven2Asset1 = f
396
403
ext1 := "pom"
@@ -441,13 +448,13 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
441
448
c .Repository = localDiskRepo
442
449
443
450
if err := af .mavenJarAndOtherExtensions (& c , fileNameWithoutExtIncludingDir , skipErrors ); err != nil {
444
- return err
451
+ return false , err
445
452
}
446
453
447
454
//
448
455
mp , err := maven (path , skipErrors )
449
456
if err != nil {
450
- return err
457
+ return false , err
451
458
}
452
459
c .Maven2ArtifactID = & mp .artifact
453
460
c .Maven2Version = & mp .version
@@ -463,7 +470,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
463
470
groupID = match [1 ]
464
471
groupID = strings .ReplaceAll (groupID , `/` , `.` )
465
472
} else {
466
- return fmt .Errorf ("groupID should not be empty, path: '%s' and regex: '%s'" , path , regex )
473
+ return false , fmt .Errorf ("groupID should not be empty, path: '%s' and regex: '%s'" , path , regex )
467
474
}
468
475
c .Maven2GroupID = & groupID
469
476
generatePOM := true
@@ -516,21 +523,21 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
516
523
c .Repository = localDiskRepo
517
524
f , err := os .Open (filepath .Clean (path ))
518
525
if err != nil {
519
- return err
526
+ return false , err
520
527
}
521
528
c .NpmAsset = f
522
529
case "nuget" :
523
530
c .Repository = localDiskRepo
524
531
f , err := os .Open (filepath .Clean (path ))
525
532
if err != nil {
526
- return err
533
+ return false , err
527
534
}
528
535
c .NugetAsset = f
529
536
case "raw" :
530
537
c .Repository = localDiskRepo
531
538
f , err := os .Open (filepath .Clean (path ))
532
539
if err != nil {
533
- return err
540
+ return false , err
534
541
}
535
542
c .RawAsset1 = f
536
543
c .RawDirectory = & dir
@@ -541,28 +548,28 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo
541
548
c .Repository = localDiskRepo
542
549
f , err := os .Open (filepath .Clean (path ))
543
550
if err != nil {
544
- return err
551
+ return false , err
545
552
}
546
553
c .RubygemsAsset = f
547
554
}
548
555
case "yum" :
549
556
c .Repository = localDiskRepo
550
557
f , err := os .Open (filepath .Clean (path ))
551
558
if err != nil {
552
- return err
559
+ return false , err
553
560
}
554
561
c .YumAsset = f
555
562
c .YumAssetFilename = & filename
556
563
default :
557
- return nil
564
+ return false , nil
558
565
}
559
566
560
567
files := []* os.File {f , af .f2 , af .f3 , af .f4 , af .f5 , af .f6 , af .f2 , af .f7 }
561
568
if err := upload (c , client , path , files ); err != nil {
562
- return err
569
+ return false , err
563
570
}
564
571
565
- return nil
572
+ return false , nil
566
573
}
567
574
568
575
func upload (c components.UploadComponentParams , client * client.Nexus3 , path string , files []* os.File ) error {
@@ -595,6 +602,35 @@ func upload(c components.UploadComponentParams, client *client.Nexus3, path stri
595
602
return nil
596
603
}
597
604
605
+ func (n * Nexus3 ) uploadAndPrintRepoFormat (c * client.Nexus3 , path , localDiskRepo , localDiskRepoHome , repoFormat string , skipErrors bool ) error {
606
+ identical , err := n .UploadSingleArtifact (c , path , localDiskRepo , localDiskRepoHome , repoFormat , skipErrors )
607
+ if err != nil {
608
+ uploaded , errRegex := regexp .MatchString ("status 400" , err .Error ())
609
+ if errRegex != nil {
610
+ return err
611
+ }
612
+ if uploaded {
613
+ log .Debugf ("artifact: '%s' has already been uploaded" , path )
614
+ return nil
615
+ }
616
+
617
+ errString := fmt .Errorf ("could not upload artifact: '%s', err: '%w'" , path , err )
618
+ if n .SkipErrors {
619
+ log .Error (errString )
620
+ } else {
621
+ return errString
622
+ }
623
+ } else {
624
+ artifacts .PrintType (repoFormat )
625
+ }
626
+ if identical {
627
+ log .Debugf ("checksum file: '%s' locally is identical compared to one in nexus" , path )
628
+ return nil
629
+ }
630
+
631
+ return nil
632
+ }
633
+
598
634
func (n * Nexus3 ) ReadLocalDirAndUploadArtifacts (localDiskRepoHome , localDiskRepo , repoFormat string ) error {
599
635
var wg sync.WaitGroup
600
636
@@ -617,24 +653,8 @@ func (n *Nexus3) ReadLocalDirAndUploadArtifacts(localDiskRepoHome, localDiskRepo
617
653
go func (path , localDiskRepo , localDiskRepoHome , repoFormat string , skipErrors bool ) {
618
654
defer wg .Done ()
619
655
620
- if err := n .UploadSingleArtifact (c , path , localDiskRepo , localDiskRepoHome , repoFormat , skipErrors ); err != nil {
621
- uploaded , errRegex := regexp .MatchString ("status 400" , err .Error ())
622
- if errRegex != nil {
623
- panic (err )
624
- }
625
- if uploaded {
626
- log .Debugf ("artifact: '%s' has already been uploaded" , path )
627
- return
628
- }
629
-
630
- errString := fmt .Errorf ("could not upload artifact: '%s', err: '%w'" , path , err )
631
- if n .SkipErrors {
632
- log .Error (errString )
633
- } else {
634
- panic (errString )
635
- }
636
- } else {
637
- artifacts .PrintType (repoFormat )
656
+ if err := n .uploadAndPrintRepoFormat (c , path , localDiskRepo , localDiskRepoHome , repoFormat , skipErrors ); err != nil {
657
+ panic (err )
638
658
}
639
659
}(path , localDiskRepo , localDiskRepoHome , repoFormat , n .SkipErrors )
640
660
}
@@ -676,8 +696,17 @@ func (n *Nexus3) maven2SnapshotsUpload(localDiskRepo string) {
676
696
log .Tracef ("VersionPolicy: '%s'" , vp )
677
697
678
698
if strings .EqualFold (vp , "snapshot" ) {
679
- s := snapshot.Nexus3 {DownloadDirName : n .DownloadDirName , FQDN : n .FQDN , Pass : n .Pass , RepoFormat : "maven2" , RepoName : localDiskRepo , SkipErrors : n .SkipErrors , User : n .User }
699
+ s := snapshot.Nexus3 {DownloadDirName : n .DownloadDirName , FQDN : n .FQDN , HTTPS : * n .HTTPS , Pass : n .Pass , RepoFormat : "maven2" , RepoName : localDiskRepo , SkipErrors : n .SkipErrors , User : n .User }
700
+
680
701
if err := s .Upload (); err != nil {
702
+ uploaded , errRegex := regexp .MatchString ("bad status: 400 Repository does not allow updating assets" , err .Error ())
703
+ if errRegex != nil {
704
+ panic (err )
705
+ }
706
+ if uploaded {
707
+ log .Debugf ("artifact from localDiskRepo: '%s' has been uploaded already" , localDiskRepo )
708
+ return
709
+ }
681
710
if ! n .SkipErrors {
682
711
panic (err )
683
712
}
@@ -698,22 +727,26 @@ func (n *Nexus3) uploadArtifactsSingleDir(localDiskRepo string) {
698
727
return
699
728
}
700
729
701
- repoFormat , err := n .repoFormatLocalDiskRepo (localDiskRepo )
730
+ repoFormatAndType , err := n .repoFormatLocalDiskRepo (localDiskRepo )
702
731
if err != nil {
703
732
panic (err )
704
733
}
705
- if repoFormat == "" {
734
+ if repoFormatAndType . format == "" {
706
735
log .Errorf ("repoFormat not detected. Verify whether repo: '%s' resides in Nexus" , localDiskRepo )
707
736
return
708
737
}
709
738
710
- if repoFormat == "maven2" {
711
- n .maven2SnapshotsUpload (localDiskRepo )
712
- }
739
+ log .Warnf ("only uploads to 'hosted' repositories are supported. Current: '%v'" , repoFormatAndType )
740
+ if repoFormatAndType .repoType == "hosted" {
741
+ if repoFormatAndType .format == "maven2" {
742
+ log .Info ("upload to snapshot repo" )
743
+ n .maven2SnapshotsUpload (localDiskRepo )
744
+ }
713
745
714
- localDiskRepoHome := filepath .Join (n .DownloadDirName , localDiskRepo )
715
- if err := n .ReadLocalDirAndUploadArtifacts (localDiskRepoHome , localDiskRepo , repoFormat ); err != nil {
716
- panic (err )
746
+ localDiskRepoHome := filepath .Join (n .DownloadDirName , localDiskRepo )
747
+ if err := n .ReadLocalDirAndUploadArtifacts (localDiskRepoHome , localDiskRepo , repoFormatAndType .format ); err != nil {
748
+ panic (err )
749
+ }
717
750
}
718
751
}
719
752
0 commit comments