diff --git a/internal/app/n3dr/artifactsv2/upload/maven2/snapshot/upload.go b/internal/app/n3dr/artifactsv2/upload/maven2/snapshot/upload.go index 2c0075be..bf27b580 100644 --- a/internal/app/n3dr/artifactsv2/upload/maven2/snapshot/upload.go +++ b/internal/app/n3dr/artifactsv2/upload/maven2/snapshot/upload.go @@ -9,7 +9,6 @@ import ( "path/filepath" "regexp" - "github.com/030/n3dr/internal/app/n3dr/artifactsv2/artifacts" "github.com/hashicorp/go-retryablehttp" log "github.com/sirupsen/logrus" ) @@ -22,8 +21,6 @@ type Nexus3 struct { func (n *Nexus3) statusCode(resp *http.Response) error { if resp.StatusCode == http.StatusCreated { log.Trace("file has been uploaded") - artifacts.PrintType(n.RepoFormat) - return nil } else { bodyBytes, err := io.ReadAll(resp.Body) if err != nil { @@ -65,7 +62,7 @@ func (n *Nexus3) readRetryAndUpload(path string) error { log.Tracef("uri: '%s'", uri) u := protocol + "://" + n.FQDN + "/repository/" + n.RepoName + "/" + uri - log.Tracef("upload url: '%s'", u) + log.Tracef("snapshot upload url: '%s'", u) req, err := http.NewRequest("PUT", u, f) if err != nil { return err diff --git a/internal/app/n3dr/artifactsv2/upload/upload.go b/internal/app/n3dr/artifactsv2/upload/upload.go index e7df4129..7770325d 100644 --- a/internal/app/n3dr/artifactsv2/upload/upload.go +++ b/internal/app/n3dr/artifactsv2/upload/upload.go @@ -74,6 +74,7 @@ func (n *Nexus3) reposOnDisk() (localDiskRepos []string, err error) { return nil, err } log.Infof("found the following localDiskRepos: '%v'", localDiskRepos) + return localDiskRepos, nil } @@ -304,7 +305,7 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d if err != nil { return false, err } - log.Info(f, downloadedFileChecksum) + log.Debugf("checksum of file: '%s' is '%s'", f, downloadedFileChecksum) retryClient := retryablehttp.NewClient() retryClient.Logger = nil @@ -317,7 +318,7 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d } u := scheme + "://" + n.FQDN + "/repository/" + localDiskRepo + "/" + dir + "/" + filename + ".sha512" - log.Info("URL: ", u) + log.Debugf("upload URL: '%s'", u) req, err := http.NewRequest("GET", u, nil) if err != nil { @@ -341,10 +342,10 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d }() bodyBytes, err := io.ReadAll(resp.Body) if err != nil { - return identical, err + return false, err } bodyString := string(bodyBytes) - log.Info(bodyString) + log.Debugf("checksum of artifact in nexus3: '%s'", bodyString) if bodyString == downloadedFileChecksum { identical = true @@ -353,7 +354,7 @@ func (n *Nexus3) checkLocalChecksumAndCompareWithOneInRemote(f, localDiskRepo, d return identical, nil } -func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo, localDiskRepoHome, repoFormat string, skipErrors bool) error { +func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo, localDiskRepoHome, repoFormat string, skipErrors bool) (bool, error) { dir := strings.Replace(filepath.Dir(path), localDiskRepoHome+"/", "", -1) filename := filepath.Base(path) @@ -361,8 +362,8 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo af := artifactFiles{} if identical, _ := n.checkLocalChecksumAndCompareWithOneInRemote(filepath.Clean(path), localDiskRepo, dir, filename); identical { - log.Info("Already uploaded") - return nil + log.Debugf("artifact: '%s' has already been uploaded", filename) + return true, nil } c := components.UploadComponentParams{} @@ -371,7 +372,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.AptAsset = f case "maven2": @@ -381,7 +382,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo filePathPom := fileNameWithoutExtIncludingDir + ".pom" if slices.Contains(checkedMavenFolders, dirPath) { - return nil + return false, nil } if _, err := os.Stat(filePathPom); err == nil { @@ -390,13 +391,13 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo if err := af.mavenJarAndOtherExtensions(&c, fileNameWithoutExtIncludingDir, skipErrors); err != nil { - return err + return false, nil } var err error f, err = os.Open(filepath.Clean(filePathPom)) if err != nil { - return err + return false, nil } c.Maven2Asset1 = f ext1 := "pom" @@ -447,13 +448,13 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo if err := af.mavenJarAndOtherExtensions(&c, fileNameWithoutExtIncludingDir, skipErrors); err != nil { - return err + return false, nil } // mp, err := maven(path, skipErrors) if err != nil { - return err + return false, nil } c.Maven2ArtifactID = &mp.artifact c.Maven2Version = &mp.version @@ -469,7 +470,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo groupID = match[1] groupID = strings.ReplaceAll(groupID, `/`, `.`) } else { - return fmt.Errorf("groupID should not be empty, path: '%s' and regex: '%s'", path, regex) + return false, fmt.Errorf("groupID should not be empty, path: '%s' and regex: '%s'", path, regex) } c.Maven2GroupID = &groupID generatePOM := true @@ -522,21 +523,21 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.NpmAsset = f case "nuget": c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.NugetAsset = f case "raw": c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.RawAsset1 = f c.RawDirectory = &dir @@ -547,7 +548,7 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.RubygemsAsset = f } @@ -555,20 +556,20 @@ func (n *Nexus3) UploadSingleArtifact(client *client.Nexus3, path, localDiskRepo c.Repository = localDiskRepo f, err := os.Open(filepath.Clean(path)) if err != nil { - return err + return false, nil } c.YumAsset = f c.YumAssetFilename = &filename default: - return nil + return false, nil } files := []*os.File{f, af.f2, af.f3, af.f4, af.f5, af.f6, af.f2, af.f7} if err := upload(c, client, path, files); err != nil { - return err + return false, nil } - return nil + return false, nil } func upload(c components.UploadComponentParams, client *client.Nexus3, path string, files []*os.File) error { @@ -623,7 +624,8 @@ func (n *Nexus3) ReadLocalDirAndUploadArtifacts(localDiskRepoHome, localDiskRepo go func(path, localDiskRepo, localDiskRepoHome, repoFormat string, skipErrors bool) { defer wg.Done() - if err := n.UploadSingleArtifact(c, path, localDiskRepo, localDiskRepoHome, repoFormat, skipErrors); err != nil { + identical, err := n.UploadSingleArtifact(c, path, localDiskRepo, localDiskRepoHome, repoFormat, skipErrors) + if err != nil { uploaded, errRegex := regexp.MatchString("status 400", err.Error()) if errRegex != nil { panic(err) @@ -639,9 +641,13 @@ func (n *Nexus3) ReadLocalDirAndUploadArtifacts(localDiskRepoHome, localDiskRepo } else { panic(errString) } - } else { - artifacts.PrintType(repoFormat) } + if identical { + log.Debugf("checksum file: '%s' locally is identical compared to one in nexus", path) + return + } + + artifacts.PrintType(repoFormat) }(path, localDiskRepo, localDiskRepoHome, repoFormat, n.SkipErrors) } return nil @@ -682,8 +688,17 @@ func (n *Nexus3) maven2SnapshotsUpload(localDiskRepo string) { log.Tracef("VersionPolicy: '%s'", vp) if strings.EqualFold(vp, "snapshot") { - s := snapshot.Nexus3{DownloadDirName: n.DownloadDirName, FQDN: n.FQDN, Pass: n.Pass, RepoFormat: "maven2", RepoName: localDiskRepo, SkipErrors: n.SkipErrors, User: n.User} + 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} + if err := s.Upload(); err != nil { + uploaded, errRegex := regexp.MatchString("bad status: 400 Repository does not allow updating assets", err.Error()) + if errRegex != nil { + panic(err) + } + if uploaded { + log.Debugf("artifact from localDiskRepo: '%s' has been uploaded already", localDiskRepo) + return + } if !n.SkipErrors { panic(err) } @@ -716,6 +731,7 @@ func (n *Nexus3) uploadArtifactsSingleDir(localDiskRepo string) { log.Warnf("only uploads to 'hosted' repositories are supported. Current: '%v'", repoFormatAndType) if repoFormatAndType.repoType == "hosted" { if repoFormatAndType.format == "maven2" { + log.Info("upload to snapshot repo") n.maven2SnapshotsUpload(localDiskRepo) }