update releases if they already exist
[#76848252] Signed-off-by: Chris Brown <cbrown@pivotal.io>
This commit is contained in:
@@ -26,6 +26,15 @@ type FakeGitHub struct {
|
|||||||
result1 *github.RepositoryRelease
|
result1 *github.RepositoryRelease
|
||||||
result2 error
|
result2 error
|
||||||
}
|
}
|
||||||
|
UpdateReleaseStub func(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
|
||||||
|
updateReleaseMutex sync.RWMutex
|
||||||
|
updateReleaseArgsForCall []struct {
|
||||||
|
release *github.RepositoryRelease
|
||||||
|
}
|
||||||
|
updateReleaseReturns struct {
|
||||||
|
result1 *github.RepositoryRelease
|
||||||
|
result2 error
|
||||||
|
}
|
||||||
ListReleaseAssetsStub func(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
|
ListReleaseAssetsStub func(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
|
||||||
listReleaseAssetsMutex sync.RWMutex
|
listReleaseAssetsMutex sync.RWMutex
|
||||||
listReleaseAssetsArgsForCall []struct {
|
listReleaseAssetsArgsForCall []struct {
|
||||||
@@ -105,6 +114,39 @@ func (fake *FakeGitHub) CreateReleaseReturns(result1 *github.RepositoryRelease,
|
|||||||
}{result1, result2}
|
}{result1, result2}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fake *FakeGitHub) UpdateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
|
||||||
|
fake.updateReleaseMutex.Lock()
|
||||||
|
fake.updateReleaseArgsForCall = append(fake.updateReleaseArgsForCall, struct {
|
||||||
|
release *github.RepositoryRelease
|
||||||
|
}{release})
|
||||||
|
fake.updateReleaseMutex.Unlock()
|
||||||
|
if fake.UpdateReleaseStub != nil {
|
||||||
|
return fake.UpdateReleaseStub(release)
|
||||||
|
} else {
|
||||||
|
return fake.updateReleaseReturns.result1, fake.updateReleaseReturns.result2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeGitHub) UpdateReleaseCallCount() int {
|
||||||
|
fake.updateReleaseMutex.RLock()
|
||||||
|
defer fake.updateReleaseMutex.RUnlock()
|
||||||
|
return len(fake.updateReleaseArgsForCall)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeGitHub) UpdateReleaseArgsForCall(i int) *github.RepositoryRelease {
|
||||||
|
fake.updateReleaseMutex.RLock()
|
||||||
|
defer fake.updateReleaseMutex.RUnlock()
|
||||||
|
return fake.updateReleaseArgsForCall[i].release
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeGitHub) UpdateReleaseReturns(result1 *github.RepositoryRelease, result2 error) {
|
||||||
|
fake.UpdateReleaseStub = nil
|
||||||
|
fake.updateReleaseReturns = struct {
|
||||||
|
result1 *github.RepositoryRelease
|
||||||
|
result2 error
|
||||||
|
}{result1, result2}
|
||||||
|
}
|
||||||
|
|
||||||
func (fake *FakeGitHub) ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error) {
|
func (fake *FakeGitHub) ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error) {
|
||||||
fake.listReleaseAssetsMutex.Lock()
|
fake.listReleaseAssetsMutex.Lock()
|
||||||
fake.listReleaseAssetsArgsForCall = append(fake.listReleaseAssetsArgsForCall, struct {
|
fake.listReleaseAssetsArgsForCall = append(fake.listReleaseAssetsArgsForCall, struct {
|
||||||
|
15
github.go
15
github.go
@@ -1,6 +1,7 @@
|
|||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"code.google.com/p/goauth2/oauth"
|
"code.google.com/p/goauth2/oauth"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
type GitHub interface {
|
type GitHub interface {
|
||||||
ListReleases() ([]github.RepositoryRelease, error)
|
ListReleases() ([]github.RepositoryRelease, error)
|
||||||
CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
|
CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
|
||||||
|
UpdateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
|
||||||
|
|
||||||
ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
|
ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error)
|
||||||
UploadReleaseAsset(release *github.RepositoryRelease, name string, file *os.File) error
|
UploadReleaseAsset(release *github.RepositoryRelease, name string, file *os.File) error
|
||||||
@@ -59,6 +61,19 @@ func (g *GitHubClient) CreateRelease(release *github.RepositoryRelease) (*github
|
|||||||
return createdRelease, nil
|
return createdRelease, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GitHubClient) UpdateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
|
||||||
|
if release.ID == nil {
|
||||||
|
return nil, errors.New("release did not have an ID: has it been saved yet?")
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedRelease, _, err := g.client.Repositories.EditRelease(g.user, g.repository, *release.ID, release)
|
||||||
|
if err != nil {
|
||||||
|
return &github.RepositoryRelease{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedRelease, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GitHubClient) ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error) {
|
func (g *GitHubClient) ListReleaseAssets(release *github.RepositoryRelease) ([]github.ReleaseAsset, error) {
|
||||||
assets, _, err := g.client.Repositories.ListReleaseAssets(g.user, g.repository, *release.ID, nil)
|
assets, _, err := g.client.Repositories.ListReleaseAssets(g.user, g.repository, *release.ID, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -46,7 +46,28 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
|
|||||||
Body: github.String(body),
|
Body: github.String(body),
|
||||||
}
|
}
|
||||||
|
|
||||||
createdRelease, err := c.github.CreateRelease(release)
|
existingReleases, err := c.github.ListReleases()
|
||||||
|
if err != nil {
|
||||||
|
return OutResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var existingRelease *github.RepositoryRelease
|
||||||
|
for _, e := range existingReleases {
|
||||||
|
if *e.TagName == tag {
|
||||||
|
existingRelease = &e
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if existingRelease != nil {
|
||||||
|
existingRelease.Name = github.String(name)
|
||||||
|
existingRelease.Body = github.String(body)
|
||||||
|
|
||||||
|
release, err = c.github.UpdateRelease(existingRelease)
|
||||||
|
} else {
|
||||||
|
release, err = c.github.CreateRelease(release)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OutResponse{}, err
|
return OutResponse{}, err
|
||||||
}
|
}
|
||||||
@@ -64,7 +85,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Base(filePath)
|
name := filepath.Base(filePath)
|
||||||
err = c.github.UploadReleaseAsset(createdRelease, name, file)
|
err = c.github.UploadReleaseAsset(release, name, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OutResponse{}, err
|
return OutResponse{}, err
|
||||||
}
|
}
|
||||||
@@ -77,7 +98,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
|
|||||||
Version: Version{
|
Version: Version{
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
},
|
},
|
||||||
Metadata: metadataFromRelease(createdRelease),
|
Metadata: metadataFromRelease(release),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,12 +14,19 @@ import (
|
|||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func file(path, contents string) {
|
||||||
|
Ω(ioutil.WriteFile(path, []byte(contents), 0644)).Should(Succeed())
|
||||||
|
}
|
||||||
|
|
||||||
var _ = Describe("Out Command", func() {
|
var _ = Describe("Out Command", func() {
|
||||||
var (
|
var (
|
||||||
command *resource.OutCommand
|
command *resource.OutCommand
|
||||||
githubClient *fakes.FakeGitHub
|
githubClient *fakes.FakeGitHub
|
||||||
|
|
||||||
sourcesDir string
|
sourcesDir string
|
||||||
|
|
||||||
|
request resource.OutRequest
|
||||||
|
response resource.OutResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
@@ -39,32 +46,78 @@ var _ = Describe("Out Command", func() {
|
|||||||
createdRel.Body = github.String("*markdown*")
|
createdRel.Body = github.String("*markdown*")
|
||||||
return &createdRel, nil
|
return &createdRel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
githubClient.UpdateReleaseStub = func(gh *github.RepositoryRelease) (*github.RepositoryRelease, error) {
|
||||||
|
return gh, nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
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())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("creates a release on GitHub", func() {
|
Context("when the release has already been created", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
githubClient.ListReleasesReturns([]github.RepositoryRelease{
|
||||||
|
{ID: github.Int(112), TagName: github.String("some-tag-name")},
|
||||||
|
}, nil)
|
||||||
|
|
||||||
namePath := filepath.Join(sourcesDir, "name")
|
namePath := filepath.Join(sourcesDir, "name")
|
||||||
bodyPath := filepath.Join(sourcesDir, "body")
|
bodyPath := filepath.Join(sourcesDir, "body")
|
||||||
tagPath := filepath.Join(sourcesDir, "tag")
|
tagPath := filepath.Join(sourcesDir, "tag")
|
||||||
|
|
||||||
file(namePath, "v0.3.12")
|
file(namePath, "v0.3.12")
|
||||||
file(bodyPath, "this is a great release")
|
file(bodyPath, "this is a great release")
|
||||||
file(tagPath, "0.3.12")
|
file(tagPath, "some-tag-name")
|
||||||
|
|
||||||
request := resource.OutRequest{
|
request = resource.OutRequest{
|
||||||
Params: resource.OutParams{
|
Params: resource.OutParams{
|
||||||
NamePath: "name",
|
NamePath: "name",
|
||||||
BodyPath: "body",
|
BodyPath: "body",
|
||||||
TagPath: "tag",
|
TagPath: "tag",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
_, err := command.Run(sourcesDir, request)
|
It("updates the existing release", func() {
|
||||||
Ω(err).ShouldNot(HaveOccurred())
|
Ω(githubClient.UpdateReleaseCallCount()).Should(Equal(1))
|
||||||
|
|
||||||
|
updatedRelease := githubClient.UpdateReleaseArgsForCall(0)
|
||||||
|
Ω(*updatedRelease.Name).Should(Equal("v0.3.12"))
|
||||||
|
Ω(*updatedRelease.Body).Should(Equal("this is a great release"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("when the release has not already been created", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
namePath := filepath.Join(sourcesDir, "name")
|
||||||
|
tagPath := filepath.Join(sourcesDir, "tag")
|
||||||
|
|
||||||
|
file(namePath, "v0.3.12")
|
||||||
|
file(tagPath, "0.3.12")
|
||||||
|
|
||||||
|
request = resource.OutRequest{
|
||||||
|
Params: resource.OutParams{
|
||||||
|
NamePath: "name",
|
||||||
|
TagPath: "tag",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("with a body", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
bodyPath := filepath.Join(sourcesDir, "body")
|
||||||
|
file(bodyPath, "this is a great release")
|
||||||
|
request.Params.BodyPath = "body"
|
||||||
|
})
|
||||||
|
|
||||||
|
It("creates a release on GitHub", func() {
|
||||||
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
|
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
|
||||||
release := githubClient.CreateReleaseArgsForCall(0)
|
release := githubClient.CreateReleaseArgsForCall(0)
|
||||||
|
|
||||||
@@ -72,24 +125,10 @@ var _ = Describe("Out Command", func() {
|
|||||||
Ω(*release.TagName).Should(Equal("0.3.12"))
|
Ω(*release.TagName).Should(Equal("0.3.12"))
|
||||||
Ω(*release.Body).Should(Equal("this is a great release"))
|
Ω(*release.Body).Should(Equal("this is a great release"))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
It("works without a body", func() {
|
Context("without a body", func() {
|
||||||
namePath := filepath.Join(sourcesDir, "name")
|
It("works", func() {
|
||||||
tagPath := filepath.Join(sourcesDir, "tag")
|
|
||||||
|
|
||||||
file(namePath, "v0.3.12")
|
|
||||||
file(tagPath, "0.3.12")
|
|
||||||
|
|
||||||
request := resource.OutRequest{
|
|
||||||
Params: resource.OutParams{
|
|
||||||
NamePath: "name",
|
|
||||||
TagPath: "tag",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
_, 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)
|
||||||
|
|
||||||
@@ -97,29 +136,17 @@ var _ = Describe("Out Command", func() {
|
|||||||
Ω(*release.TagName).Should(Equal("0.3.12"))
|
Ω(*release.TagName).Should(Equal("0.3.12"))
|
||||||
Ω(*release.Body).Should(Equal(""))
|
Ω(*release.Body).Should(Equal(""))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
It("uploads matching file globs", func() {
|
Context("with file globs", func() {
|
||||||
namePath := filepath.Join(sourcesDir, "name")
|
BeforeEach(func() {
|
||||||
bodyPath := filepath.Join(sourcesDir, "body")
|
|
||||||
tagPath := filepath.Join(sourcesDir, "tag")
|
|
||||||
|
|
||||||
file(namePath, "v0.3.12")
|
|
||||||
file(bodyPath, "this is a great release")
|
|
||||||
file(tagPath, "0.3.12")
|
|
||||||
|
|
||||||
globMatching := filepath.Join(sourcesDir, "great-file.tgz")
|
globMatching := filepath.Join(sourcesDir, "great-file.tgz")
|
||||||
globNotMatching := filepath.Join(sourcesDir, "bad-file.txt")
|
globNotMatching := filepath.Join(sourcesDir, "bad-file.txt")
|
||||||
|
|
||||||
file(globMatching, "matching")
|
file(globMatching, "matching")
|
||||||
file(globNotMatching, "not matching")
|
file(globNotMatching, "not matching")
|
||||||
|
|
||||||
githubClient.CreateReleaseStub = func(gh *github.RepositoryRelease) (*github.RepositoryRelease, error) {
|
request = resource.OutRequest{
|
||||||
createdRel := *gh
|
|
||||||
createdRel.ID = github.Int(112)
|
|
||||||
return &createdRel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
request := resource.OutRequest{
|
|
||||||
Params: resource.OutParams{
|
Params: resource.OutParams{
|
||||||
NamePath: "name",
|
NamePath: "name",
|
||||||
BodyPath: "body",
|
BodyPath: "body",
|
||||||
@@ -131,9 +158,12 @@ var _ = Describe("Out Command", func() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := command.Run(sourcesDir, request)
|
bodyPath := filepath.Join(sourcesDir, "body")
|
||||||
Ω(err).ShouldNot(HaveOccurred())
|
file(bodyPath, "*markdown*")
|
||||||
|
request.Params.BodyPath = "body"
|
||||||
|
})
|
||||||
|
|
||||||
|
It("uploads matching file globs", func() {
|
||||||
Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(1))
|
Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(1))
|
||||||
release, name, file := githubClient.UploadReleaseAssetArgsForCall(0)
|
release, name, file := githubClient.UploadReleaseAssetArgsForCall(0)
|
||||||
|
|
||||||
@@ -143,22 +173,6 @@ var _ = Describe("Out Command", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("has some sweet metadata", func() {
|
It("has some sweet metadata", func() {
|
||||||
namePath := filepath.Join(sourcesDir, "name")
|
|
||||||
bodyPath := filepath.Join(sourcesDir, "body")
|
|
||||||
tagPath := filepath.Join(sourcesDir, "tag")
|
|
||||||
|
|
||||||
file(namePath, "v0.3.12")
|
|
||||||
file(bodyPath, "this is a great release")
|
|
||||||
file(tagPath, "0.3.12")
|
|
||||||
|
|
||||||
request := resource.OutRequest{
|
|
||||||
Params: resource.OutParams{
|
|
||||||
NamePath: "name",
|
|
||||||
BodyPath: "body",
|
|
||||||
TagPath: "tag",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
outResponse, err := command.Run(sourcesDir, request)
|
outResponse, err := command.Run(sourcesDir, request)
|
||||||
Ω(err).ShouldNot(HaveOccurred())
|
Ω(err).ShouldNot(HaveOccurred())
|
||||||
|
|
||||||
@@ -168,8 +182,6 @@ var _ = Describe("Out Command", func() {
|
|||||||
resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true},
|
resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true},
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
func file(path, contents string) {
|
|
||||||
Ω(ioutil.WriteFile(path, []byte(contents), 0644)).Should(Succeed())
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user