Skip to content

Commit

Permalink
fix: [#384] Create groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
030 committed Nov 19, 2023
1 parent 6450e9b commit e66a0bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
68 changes: 42 additions & 26 deletions internal/app/n3dr/artifactsv2/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -353,16 +354,16 @@ 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)

var f *os.File
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{}
Expand All @@ -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

Check failure on line 375 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 373) but it returns nil (nilerr)
}
c.AptAsset = f
case "maven2":
Expand All @@ -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 {
Expand All @@ -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

Check failure on line 394 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 393) but it returns nil (nilerr)
}

var err error
f, err = os.Open(filepath.Clean(filePathPom))
if err != nil {
return err
return false, nil

Check failure on line 400 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 398) but it returns nil (nilerr)
}
c.Maven2Asset1 = f
ext1 := "pom"
Expand Down Expand Up @@ -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

Check failure on line 451 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 450) but it returns nil (nilerr)
}

//
mp, err := maven(path, skipErrors)
if err != nil {
return err
return false, nil

Check failure on line 457 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 455) but it returns nil (nilerr)
}
c.Maven2ArtifactID = &mp.artifact
c.Maven2Version = &mp.version
Expand All @@ -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
Expand Down Expand Up @@ -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

Check failure on line 526 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 524) but it returns nil (nilerr)
}
c.NpmAsset = f
case "nuget":
c.Repository = localDiskRepo
f, err := os.Open(filepath.Clean(path))
if err != nil {
return err
return false, nil

Check failure on line 533 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 531) but it returns nil (nilerr)
}
c.NugetAsset = f
case "raw":
c.Repository = localDiskRepo
f, err := os.Open(filepath.Clean(path))
if err != nil {
return err
return false, nil

Check failure on line 540 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 538) but it returns nil (nilerr)
}
c.RawAsset1 = f
c.RawDirectory = &dir
Expand All @@ -547,28 +548,28 @@ 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
}
case "yum":
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

Check failure on line 569 in internal/app/n3dr/artifactsv2/upload/upload.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, sha512sum)

error is not nil (line 568) but it returns nil (nilerr)
}

return nil
return false, nil
}

func upload(c components.UploadComponentParams, client *client.Nexus3, path string, files []*os.File) error {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit e66a0bd

Please sign in to comment.