From c4436c0350bf28ee853c4ff41931b0deb7dd30de Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Wed, 18 Feb 2015 10:06:04 -0800 Subject: [PATCH] emit metadata in output [#76848252] Signed-off-by: Chris Brown --- in_command.go | 6 +----- in_command_test.go | 34 ++++++++++++++++++++++------------ metadata.go | 37 +++++++++++++++++++++++++++++++++++++ out_command.go | 2 ++ out_command_test.go | 41 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 metadata.go diff --git a/in_command.go b/in_command.go index 1f05f72..2a08f04 100644 --- a/in_command.go +++ b/in_command.go @@ -94,11 +94,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) { Version: Version{ Tag: *foundRelease.TagName, }, - Metadata: []MetadataPair{ - {Name: "name", Value: *foundRelease.Name, URL: *foundRelease.HTMLURL}, - {Name: "url", Value: *foundRelease.HTMLURL}, - {Name: "body", Value: *foundRelease.Body, Markdown: true}, - }, + Metadata: metadataFromRelease(foundRelease), }, nil } diff --git a/in_command_test.go b/in_command_test.go index 6d0a9c3..69fbad0 100644 --- a/in_command_test.go +++ b/in_command_test.go @@ -55,25 +55,19 @@ var _ = Describe("In Command", func() { }) buildRelease := func(id int, tag string) github.RepositoryRelease { - url := "http://google.com" - name := "release-name" - body := "*markdown*" - return github.RepositoryRelease{ - ID: &id, - TagName: &tag, - HTMLURL: &url, - Name: &name, - Body: &body, + ID: github.Int(id), + TagName: github.String(tag), + HTMLURL: github.String("http://google.com"), + Name: github.String("release-name"), + Body: github.String("*markdown*"), } } buildAsset := func(name string) github.ReleaseAsset { - url := server.URL() + "/" + name - return github.ReleaseAsset{ Name: &name, - BrowserDownloadURL: &url, + BrowserDownloadURL: github.String(server.URL() + "/" + name), } } @@ -158,6 +152,14 @@ var _ = Describe("In Command", func() { Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.35.0"})) }) + It("has some sweet metadata", func() { + Ω(inResponse.Metadata).Should(ConsistOf( + resource.MetadataPair{Name: "url", Value: "http://google.com"}, + resource.MetadataPair{Name: "name", Value: "release-name", URL: "http://google.com"}, + resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true}, + )) + }) + It("downloads all of the files", func() { _, err := os.Stat(filepath.Join(destDir, "example.txt")) Ω(err).ShouldNot(HaveOccurred()) @@ -196,6 +198,14 @@ var _ = Describe("In Command", func() { Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.35.0"})) }) + It("has some sweet metadata", func() { + Ω(inResponse.Metadata).Should(ConsistOf( + resource.MetadataPair{Name: "url", Value: "http://google.com"}, + resource.MetadataPair{Name: "name", Value: "release-name", URL: "http://google.com"}, + resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true}, + )) + }) + It("fetches from the latest release", func() { _, err := os.Stat(filepath.Join(destDir, "example.txt")) Ω(err).ShouldNot(HaveOccurred()) diff --git a/metadata.go b/metadata.go new file mode 100644 index 0000000..5c0c828 --- /dev/null +++ b/metadata.go @@ -0,0 +1,37 @@ +package resource + +import "github.com/google/go-github/github" + +func metadataFromRelease(release *github.RepositoryRelease) []MetadataPair { + metadata := []MetadataPair{} + + if release.Name != nil { + nameMeta := MetadataPair{ + Name: "name", + Value: *release.Name, + } + + if release.HTMLURL != nil { + nameMeta.URL = *release.HTMLURL + } + + metadata = append(metadata, nameMeta) + } + + if release.HTMLURL != nil { + metadata = append(metadata, MetadataPair{ + Name: "url", + Value: *release.HTMLURL, + }) + } + + if release.Body != nil { + metadata = append(metadata, MetadataPair{ + Name: "body", + Value: *release.Body, + Markdown: true, + }) + } + + return metadata +} diff --git a/out_command.go b/out_command.go index 35f8d88..eb19748 100644 --- a/out_command.go +++ b/out_command.go @@ -45,6 +45,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err TagName: github.String(tag), Body: github.String(body), } + createdRelease, err := c.github.CreateRelease(release) if err != nil { return OutResponse{}, err @@ -76,6 +77,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err Version: Version{ Tag: tag, }, + Metadata: metadataFromRelease(createdRelease), }, nil } diff --git a/out_command_test.go b/out_command_test.go index 4c84f17..d1d3a38 100644 --- a/out_command_test.go +++ b/out_command_test.go @@ -30,6 +30,15 @@ var _ = Describe("Out Command", func() { sourcesDir, err = ioutil.TempDir("", "github-release") Ω(err).ShouldNot(HaveOccurred()) + + githubClient.CreateReleaseStub = func(gh *github.RepositoryRelease) (*github.RepositoryRelease, error) { + createdRel := *gh + createdRel.ID = github.Int(112) + createdRel.HTMLURL = github.String("http://google.com") + createdRel.Name = github.String("release-name") + createdRel.Body = github.String("*markdown*") + return &createdRel, nil + } }) AfterEach(func() { @@ -105,8 +114,9 @@ var _ = Describe("Out Command", func() { file(globNotMatching, "not matching") githubClient.CreateReleaseStub = func(gh *github.RepositoryRelease) (*github.RepositoryRelease, error) { - gh.ID = github.Int(112) - return gh, nil + createdRel := *gh + createdRel.ID = github.Int(112) + return &createdRel, nil } request := resource.OutRequest{ @@ -131,6 +141,33 @@ var _ = Describe("Out Command", func() { Ω(name).Should(Equal("great-file.tgz")) Ω(file.Name()).Should(Equal(filepath.Join(sourcesDir, "great-file.tgz"))) }) + + 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) + Ω(err).ShouldNot(HaveOccurred()) + + Ω(outResponse.Metadata).Should(ConsistOf( + resource.MetadataPair{Name: "url", Value: "http://google.com"}, + resource.MetadataPair{Name: "name", Value: "release-name", URL: "http://google.com"}, + resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true}, + )) + }) }) func file(path, contents string) {