Skip to content

Commit

Permalink
Merge pull request #41 from Nightapes/fix.assets
Browse files Browse the repository at this point in the history
fix(assets): when file is zipped, upload zipped file instead of unzipped file
  • Loading branch information
fwiedmann authored Mar 25, 2020
2 parents 322455b + be35b74 commit 11acc95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
24 changes: 19 additions & 5 deletions internal/assets/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"hash/crc32"
"io"
Expand All @@ -24,6 +25,7 @@ import (
type Asset struct {
name string
path string
zippedPath string
algorithm string
isCompressed bool
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions internal/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

// Set struct
type Set struct {
Assets []*Asset
assets []*Asset
repository string
algorithm string
}

//New container for assets
func New(repository, algorithm string) *Set {
return &Set{
Assets: []*Asset{},
assets: []*Asset{},
repository: repository,
algorithm: algorithm,
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions internal/releaser/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions internal/releaser/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions pkg/semanticrelease/semantic-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 11acc95

Please sign in to comment.