Compare commits

...

2 Commits

Author SHA1 Message Date
Felix Wiedmann
11acc95a34 Merge pull request #41 from Nightapes/fix.assets
fix(assets): when file is zipped, upload zipped file instead of unzipped file
2020-03-25 09:01:03 +01:00
Sebastian Beisch
be35b743b4 fix(assets): when file is zipped, upload zipped file instead of unzipped file 2020-03-25 08:45:27 +01:00
5 changed files with 45 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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)
}
}
}

View File

@@ -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 {

View File

@@ -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
}