Skip to content

Commit

Permalink
Retry uploads even if ghost asset was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-yurchenko committed Feb 5, 2022
1 parent 3fe7173 commit ea6f994
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Changed

- Keep retrying assets upload even if `422/UnprocessableEntity`/`502/BadGateway` is encountered and asset was not found on the partially created release (this will make git-release more robust to unexpected API responses)

## [4.2.2] - 2022-02-05

### Fixed
Expand Down Expand Up @@ -328,6 +334,7 @@ This is a major release as most of the code was refactored and some behavior was
- Control Release Draft through env.var 'DRAFT_RELEASE'
- Control Release Pre Release through env.var 'PRE_RELEASE'

[Unreleased]: https://github.com/anton-yurchenko/git-release/compare/v4.2.2...HEAD
[4.2.2]: https://github.com/anton-yurchenko/git-release/compare/v4.2.1...v4.2.2
[4.2.1]: https://github.com/anton-yurchenko/git-release/compare/v4.2.0...v4.2.1
[4.2.0]: https://github.com/anton-yurchenko/git-release/compare/v4.1.2...v4.2.0
Expand Down
41 changes: 14 additions & 27 deletions release/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
log "github.com/sirupsen/logrus"
)

const ghostReleaseAssetNotFound string = "ghost release asset not found"

// GetAssets returns validated assets supplied via 'args'
func GetAssets(fs afero.Fs, args []string) (*[]Asset, error) {
assets := make([]Asset, 0)
Expand Down Expand Up @@ -75,13 +73,12 @@ func (a *Asset) Upload(release *Release, cli RepositoriesClient, id int64, errs
if err == nil {
errs <- nil
break
} else if strings.Contains(err.Error(), "error opening a file") || strings.Contains(err.Error(), ghostReleaseAssetNotFound) {
} else if strings.Contains(err.Error(), "error opening a file") {
errs <- err
return
}

if i == maxRetries {
log.WithField("asset", a.Name).Warnf("error uploading asset: %v", err.Error())
errs <- errors.New(fmt.Sprintf("maximum attempts reached uploading asset: %v", a.Name))
break
}
Expand Down Expand Up @@ -114,13 +111,9 @@ func (a *Asset) uploadHandler(release *Release, cli RepositoriesClient, id int64
_ = file.Close()

if err != nil {
if lastTry {
return err
}

log.WithField("asset", a.Name).Warnf("error uploading asset: %v", err.Error())

if res.StatusCode == http.StatusBadGateway || res.StatusCode == http.StatusUnprocessableEntity {
if !lastTry && (res.StatusCode == http.StatusBadGateway || res.StatusCode == http.StatusUnprocessableEntity) {
rel, _, err := cli.GetReleaseByTag(
context.Background(),
release.Slug.Owner,
Expand All @@ -131,29 +124,23 @@ func (a *Asset) uploadHandler(release *Release, cli RepositoriesClient, id int64
return errors.Wrap(err, "error retrieving release")
}

var assetID int64
for _, s := range rel.Assets {
if *s.Name == strings.ReplaceAll(a.Name, "/", "-") {
assetID = *s.ID
break
_, err = cli.DeleteReleaseAsset(
context.Background(),
release.Slug.Owner,
release.Slug.Name,
*s.ID,
)
if err != nil {
return errors.Wrap(err, "error deleting ghost release asset")
}

return errors.New("ghost release asset deleted")
}
}

if assetID == 0 {
return errors.New(ghostReleaseAssetNotFound)
}

_, err = cli.DeleteReleaseAsset(
context.Background(),
release.Slug.Owner,
release.Slug.Name,
assetID,
)
if err != nil {
return errors.Wrap(err, "error deleting ghost release asset")
}

return errors.New("ghost release asset deleted")
return errors.New("ghost release asset not found")
}

return err
Expand Down
9 changes: 8 additions & 1 deletion release/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestUpload(t *testing.T) {
Error: "",
},
},
"Ghost Release Asset Not Found": {
"Ghost Release Asset Not Found [very long test]": {
Asset: release.Asset{
Name: "testFile1",
Path: "testFile1",
Expand Down Expand Up @@ -369,6 +369,13 @@ func TestUpload(t *testing.T) {
},
GetReleaseByTagError: nil,
},
{
LastTry: true,
UploadReleaseAssetResponse: &github.Response{
Response: &http.Response{StatusCode: http.StatusOK},
},
UploadReleaseAssetError: nil,
},
},
Expected: expected{
Error: "ghost release asset not found",
Expand Down

0 comments on commit ea6f994

Please sign in to comment.