diff --git a/internal/assets/asset.go b/internal/assets/asset.go index ebbda51..ec20c3d 100644 --- a/internal/assets/asset.go +++ b/internal/assets/asset.go @@ -7,6 +7,7 @@ import ( "crypto/sha256" "crypto/sha512" "encoding/hex" + "fmt" "hash" "hash/crc32" "io" @@ -24,6 +25,7 @@ import ( type Asset struct { name string path string + zippedPath string algorithm string isCompressed bool } @@ -63,8 +65,12 @@ func NewAsset(repository string, assetConfig config.Asset, algorithm string) (*A } func (a *Asset) getChecksum() (string, error) { - log.Debugf("Calculating checksum for %s", a.path) - file, err := os.Open(a.path) + path, err := a.GetPath() + if err != nil { + return "", nil + } + log.Debugf("Calculating checksum for %s", path) + file, err := os.Open(path) if err != nil { return "", errors.Wrapf(err, "Failed to open file %s to calculate checksum", a.name) } @@ -98,13 +104,16 @@ func (a *Asset) getChecksum() (string, error) { // GetPath where the file is located, if zipped true, it will compress it and give you the new location func (a *Asset) GetPath() (string, error) { if a.isCompressed { - return a.zipFile() + return a.ZipFile() } return a.path, nil } // GetName of asset func (a *Asset) GetName() string { + if a.isCompressed { + return fmt.Sprintf("%s.zip", a.name) + } return a.name } @@ -114,7 +123,11 @@ func (a *Asset) IsCompressed() bool { } // ZipFile compress given file in zip format -func (a *Asset) zipFile() (string, error) { +func (a *Asset) ZipFile() (string, error) { + + if a.zippedPath != "" { + return a.zippedPath, nil + } path := a.path fileToZip, err := os.Open(path) @@ -156,5 +169,6 @@ func (a *Asset) zipFile() (string, error) { if err := zipFile.Close(); err != nil { return "", errors.Wrap(err, "Could not close file") } - return filepath.Abs(fileToZipInfo.Name()) + a.zippedPath, err = filepath.Abs(zipFile.Name()) + return a.zippedPath, err } diff --git a/internal/assets/assets.go b/internal/assets/assets.go index 1910ea8..9a30586 100644 --- a/internal/assets/assets.go +++ b/internal/assets/assets.go @@ -13,7 +13,7 @@ import ( // Set struct type Set struct { - Assets []*Asset + assets []*Asset repository string algorithm string } @@ -21,7 +21,7 @@ type Set struct { //New container for assets func New(repository, algorithm string) *Set { return &Set{ - Assets: []*Asset{}, + assets: []*Asset{}, repository: repository, algorithm: algorithm, } @@ -34,13 +34,13 @@ func (s *Set) Add(assetConfigs ...config.Asset) error { if err != nil { return err } - s.Assets = append(s.Assets, asset) + s.assets = append(s.assets, asset) } return nil } func (s *Set) All() []*Asset { - return s.Assets + return s.assets } func (s *Set) GenerateChecksum() error { @@ -50,7 +50,7 @@ func (s *Set) GenerateChecksum() error { } defer checksumFile.Close() lines := []string{} - for _, asset := range s.Assets { + for _, asset := range s.assets { checksum, err := asset.getChecksum() if err != nil { return err @@ -68,7 +68,7 @@ func (s *Set) GenerateChecksum() error { return err } - s.Assets = append(s.Assets, &Asset{ + s.assets = append(s.assets, &Asset{ path: filePath, name: "checksum.txt", isCompressed: false, diff --git a/internal/releaser/github/github.go b/internal/releaser/github/github.go index 149bfb8..c1828cb 100644 --- a/internal/releaser/github/github.go +++ b/internal/releaser/github/github.go @@ -128,15 +128,14 @@ func (g *Client) uploadAssets(assets *assets.Set) error { if err != nil { return err } - fileInfo, _ := file.Stat() - _, resp, err := g.client.Repositories.UploadReleaseAsset(g.context, g.config.User, g.config.Repo, g.release.GetID(), &github.UploadOptions{Name: fileInfo.Name()}, file) + _, resp, err := g.client.Repositories.UploadReleaseAsset(g.context, g.config.User, g.config.Repo, g.release.GetID(), &github.UploadOptions{Name: asset.GetName()}, file) if err != nil { return err } if resp.StatusCode >= http.StatusBadRequest { - return fmt.Errorf("could not upload asset %s: %s", file.Name(), resp.Status) + return fmt.Errorf("could not upload asset %s: %s", asset.GetName(), resp.Status) } } } diff --git a/internal/releaser/gitlab/gitlab.go b/internal/releaser/gitlab/gitlab.go index 770174f..9186b7b 100644 --- a/internal/releaser/gitlab/gitlab.go +++ b/internal/releaser/gitlab/gitlab.go @@ -148,9 +148,8 @@ func (g *Client) uploadAssets(assets *assets.Set) error { } defer file.Close() - fileInfo, _ := file.Stat() - result, err := g.uploadFile(fileInfo.Name(), file) + result, err := g.uploadFile(asset.GetName(), file) if err != nil { return fmt.Errorf("could not upload asset %s: %s", file.Name(), err.Error()) } @@ -159,7 +158,7 @@ func (g *Client) uploadAssets(assets *assets.Set) error { g.log.Infof("Uploaded file %s to gitlab can be downloaded under %s", file.Name(), downloadURL) - uploadURL := fmt.Sprintf("%s/projects/%s/releases/%s/assets/links?name=%s&url=%s", g.apiURL, util.PathEscape(g.config.Repo), g.Release, util.PathEscape(fileInfo.Name()), downloadURL) + uploadURL := fmt.Sprintf("%s/projects/%s/releases/%s/assets/links?name=%s&url=%s", g.apiURL, util.PathEscape(g.config.Repo), g.Release, util.PathEscape(asset.GetName()), downloadURL) req, err := http.NewRequest("POST", uploadURL, nil) if err != nil { diff --git a/pkg/semanticrelease/semantic-release.go b/pkg/semanticrelease/semantic-release.go index 956b506..ce6f693 100644 --- a/pkg/semanticrelease/semantic-release.go +++ b/pkg/semanticrelease/semantic-release.go @@ -5,6 +5,8 @@ import ( "time" "github.com/Masterminds/semver" + log "github.com/sirupsen/logrus" + "github.com/Nightapes/go-semantic-release/internal/analyzer" "github.com/Nightapes/go-semantic-release/internal/assets" "github.com/Nightapes/go-semantic-release/internal/cache" @@ -16,7 +18,6 @@ import ( "github.com/Nightapes/go-semantic-release/internal/releaser" "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" - log "github.com/sirupsen/logrus" ) // SemanticRelease struct @@ -228,16 +229,26 @@ func (s *SemanticRelease) Release(provider *ci.ProviderConfig, force bool) error return err } - if err := s.assets.GenerateChecksum(); err != nil { - return err - } - hook := hooks.New(s.config, releaseVersion) if err := hook.PreRelease(); err != nil { log.Debugf("Error during pre release hook") return err } + if s.config.Checksum.Algorithm != "" { + if err := s.assets.GenerateChecksum(); err != nil { + return err + } + } + + for _, asset := range s.assets.All() { + if asset.IsCompressed() { + if _, err := asset.ZipFile(); err != nil { + return err + } + } + } + if err = s.releaser.CreateRelease(releaseVersion, generatedChangelog, s.assets); err != nil { return err }