retry 10 times if upload release assets fails

[#121455813]

Signed-off-by: Yucheng Tu <ytu@pivotal.io>
This commit is contained in:
Evan Short
2016-06-14 10:28:26 -07:00
committed by Yucheng Tu
parent b0114ca8b4
commit 7c95978718
2 changed files with 109 additions and 8 deletions

View File

@@ -134,7 +134,27 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
fmt.Fprintf(c.writer, "uploading %s\n", filePath) fmt.Fprintf(c.writer, "uploading %s\n", filePath)
name := filepath.Base(filePath) name := filepath.Base(filePath)
err = c.github.UploadReleaseAsset(*release, name, file) err = c.github.UploadReleaseAsset(*release, name, file)
for i := 0; i < 9 && err != nil; i++ {
assets, err := c.github.ListReleaseAssets(*release)
if err != nil {
return OutResponse{}, err
}
for _, asset := range assets {
if asset.Name != nil && *asset.Name == name {
err = c.github.DeleteReleaseAsset(asset)
if err != nil {
return OutResponse{}, err
}
break
}
}
err = c.github.UploadReleaseAsset(*release, name, file)
}
if err != nil { if err != nil {
return OutResponse{}, err return OutResponse{}, err
} }

View File

@@ -1,6 +1,7 @@
package resource_test package resource_test
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -26,7 +27,6 @@ var _ = Describe("Out Command", func() {
sourcesDir string sourcesDir string
request resource.OutRequest request resource.OutRequest
response resource.OutResponse
) )
BeforeEach(func() { BeforeEach(func() {
@@ -52,12 +52,6 @@ var _ = Describe("Out Command", func() {
} }
}) })
JustBeforeEach(func() {
var err error
response, err = command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
})
AfterEach(func() { AfterEach(func() {
Ω(os.RemoveAll(sourcesDir)).Should(Succeed()) Ω(os.RemoveAll(sourcesDir)).Should(Succeed())
}) })
@@ -111,6 +105,9 @@ var _ = Describe("Out Command", func() {
}) })
It("deletes the existing assets", func() { It("deletes the existing assets", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.ListReleaseAssetsCallCount()).Should(Equal(1)) Ω(githubClient.ListReleaseAssetsCallCount()).Should(Equal(1))
Ω(githubClient.ListReleaseAssetsArgsForCall(0)).Should(Equal(existingReleases[1])) Ω(githubClient.ListReleaseAssetsArgsForCall(0)).Should(Equal(existingReleases[1]))
@@ -126,6 +123,9 @@ var _ = Describe("Out Command", func() {
}) })
It("updates the existing release to a non-draft", func() { It("updates the existing release to a non-draft", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
updatedRelease := githubClient.UpdateReleaseArgsForCall(0) updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
@@ -140,6 +140,9 @@ var _ = Describe("Out Command", func() {
}) })
It("updates the existing release to a draft", func() { It("updates the existing release to a draft", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
updatedRelease := githubClient.UpdateReleaseArgsForCall(0) updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
@@ -154,6 +157,9 @@ var _ = Describe("Out Command", func() {
}) })
It("does not blow away the body", func() { It("does not blow away the body", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
updatedRelease := githubClient.UpdateReleaseArgsForCall(0) updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
@@ -164,6 +170,9 @@ var _ = Describe("Out Command", func() {
Context("when a commitish is not supplied", func() { Context("when a commitish is not supplied", func() {
It("updates the existing release", func() { It("updates the existing release", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
updatedRelease := githubClient.UpdateReleaseArgsForCall(0) updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
@@ -181,6 +190,9 @@ var _ = Describe("Out Command", func() {
}) })
It("updates the existing release", func() { It("updates the existing release", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
updatedRelease := githubClient.UpdateReleaseArgsForCall(0) updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
@@ -215,6 +227,9 @@ var _ = Describe("Out Command", func() {
}) })
It("creates a release on GitHub with the commitish", func() { It("creates a release on GitHub with the commitish", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -224,6 +239,9 @@ var _ = Describe("Out Command", func() {
Context("without a commitish", func() { Context("without a commitish", func() {
It("creates a release on GitHub without the commitish", func() { It("creates a release on GitHub without the commitish", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -240,6 +258,9 @@ var _ = Describe("Out Command", func() {
}) })
It("creates a release on GitHub", func() { It("creates a release on GitHub", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -251,6 +272,9 @@ var _ = Describe("Out Command", func() {
Context("without a body", func() { Context("without a body", func() {
It("works", func() { It("works", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -261,6 +285,9 @@ var _ = Describe("Out Command", func() {
}) })
It("always defaults to non-draft mode", func() { It("always defaults to non-draft mode", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -275,6 +302,9 @@ var _ = Describe("Out Command", func() {
}) })
It("creates a release on GitHub in draft mode", func() { It("creates a release on GitHub in draft mode", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)
@@ -324,6 +354,9 @@ var _ = Describe("Out Command", func() {
}) })
It("uploads matching file globs", func() { It("uploads matching file globs", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(1)) Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(1))
release, name, file := githubClient.UploadReleaseAssetArgsForCall(0) release, name, file := githubClient.UploadReleaseAssetArgsForCall(0)
@@ -354,6 +387,51 @@ var _ = Describe("Out Command", func() {
Ω(err).Should(HaveOccurred()) Ω(err).Should(HaveOccurred())
Ω(err).Should(MatchError("could not find file that matches glob '*.gif'")) Ω(err).Should(MatchError("could not find file that matches glob '*.gif'"))
}) })
Context("when upload release asset fails", func() {
BeforeEach(func() {
existingAsset := false
githubClient.DeleteReleaseAssetStub = func(github.ReleaseAsset) error {
existingAsset = false
return nil
}
githubClient.ListReleaseAssetsReturns([]github.ReleaseAsset{
{
ID: github.Int(456789),
Name: github.String("great-file.tgz"),
},
{
ID: github.Int(3450798),
Name: github.String("whatever.tgz"),
},
}, nil)
githubClient.UploadReleaseAssetStub = func(github.RepositoryRelease, string, *os.File) error {
Expect(existingAsset).To(BeFalse())
existingAsset = true
return errors.New("some-error")
}
})
It("retries 10 times", func() {
_, err := command.Run(sourcesDir, request)
Expect(err).To(Equal(errors.New("some-error")))
Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(10))
Ω(githubClient.ListReleaseAssetsCallCount()).Should(Equal(9))
Ω(*githubClient.ListReleaseAssetsArgsForCall(8).ID).Should(Equal(112))
actualRelease, actualName, actualFile := githubClient.UploadReleaseAssetArgsForCall(9)
Ω(*actualRelease.ID).Should(Equal(112))
Ω(actualName).Should(Equal("great-file.tgz"))
Ω(ioutil.ReadAll(actualFile)).Should(Equal([]byte("matching")))
Ω(githubClient.DeleteReleaseAssetCallCount()).Should(Equal(9))
actualAsset := githubClient.DeleteReleaseAssetArgsForCall(8)
Expect(*actualAsset.ID).To(Equal(456789))
})
})
}) })
Context("when the tag_prefix is set", func() { Context("when the tag_prefix is set", func() {
@@ -374,6 +452,9 @@ var _ = Describe("Out Command", func() {
}) })
It("appends the TagPrefix onto the TagName", func() { It("appends the TagPrefix onto the TagName", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1)) Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0) release := githubClient.CreateReleaseArgsForCall(0)