Merge pull request #32 from ahelal/feature/pre_release

Support pre release
This commit is contained in:
Alex Suraci
2016-09-27 10:51:52 -07:00
committed by GitHub
11 changed files with 291 additions and 23 deletions

View File

@@ -23,6 +23,17 @@ Fetches and creates versioned GitHub resources.
for uploading. If `github_api_url` is set, this value defaults to the same for uploading. If `github_api_url` is set, this value defaults to the same
value, but if you have your own endpoint, this field will override it. value, but if you have your own endpoint, this field will override it.
* `release`: *Optional. Default `true`.* When set to `true`, `put` produces
release and `check` will detects releases. If `false`, `put` and `check` will ignore releases.
Note that releases must have semver compliant tags to be detected.
* `pre_release`: *Optional. Default `false`.* When set to `true`, `put` produces
pre-release and `check` detects prereleases. If `false`, only non-prerelease releases
will be detected and published. Note that releases must have semver compliant
tags to be detected.
If `release` and `pre_release` are set to `true` `put` produces
release and `check` will detects prereleases and releases.
* `drafts`: *Optional. Default `false`.* When set to `true`, `put` produces * `drafts`: *Optional. Default `false`.* When set to `true`, `put` produces
drafts and `check` only detects drafts. If `false`, only non-draft releases drafts and `check` only detects drafts. If `false`, only non-draft releases
will be detected and published. Note that releases must have semver compliant will be detected and published. Note that releases must have semver compliant

View File

@@ -35,6 +35,14 @@ func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
if request.Source.Drafts != *release.Draft { if request.Source.Drafts != *release.Draft {
continue continue
} }
// Should we skip this release
// a- prerelease condition dont match our source config
// b- release condition match prerealse in github since github has true/false to describe release/prerelase
if request.Source.PreRelease != *release.Prerelease && request.Source.Release == *release.Prerelease {
continue
}
if release.TagName == nil { if release.TagName == nil {
continue continue
} }
@@ -67,7 +75,7 @@ func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
for _, release := range filteredReleases { for _, release := range filteredReleases {
if !upToLatest { if !upToLatest {
if *release.Draft { if *release.Draft || *release.Prerelease {
id := *release.ID id := *release.ID
upToLatest = request.Version.ID == strconv.Itoa(id) upToLatest = request.Version.ID == strconv.Itoa(id)
} else { } else {

View File

@@ -200,6 +200,150 @@ var _ = Describe("Check Command", func() {
}) })
}) })
Context("when pre releases are allowed and releases are not", func() {
Context("and one of the releases is a final and another is a draft", func() {
BeforeEach(func() {
returnedReleases = []*github.RepositoryRelease{
newDraftRepositoryRelease(1, "v0.1.4"),
newRepositoryRelease(2, "0.4.0"),
newPreReleaseRepositoryRelease(1, "v0.4.1-rc.10"),
newPreReleaseRepositoryRelease(2, "0.4.1-rc.9"),
newPreReleaseRepositoryRelease(3, "v0.4.1-rc.8"),
}
})
It("returns all of the versions that are newer, and only pre relases", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{ID: "2"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: false},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "0.4.1-rc.9"},
{Tag: "v0.4.1-rc.10"},
}))
})
It("returns the latest prerelease version if the current version is not found", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{ID: "5"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: false},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "v0.4.1-rc.10"},
}))
})
})
})
Context("when releases and pre releases are allowed", func() {
Context("and final release is newer", func() {
BeforeEach(func() {
returnedReleases = []*github.RepositoryRelease{
newDraftRepositoryRelease(1, "v0.1.4"),
newRepositoryRelease(1, "0.3.9"),
newRepositoryRelease(2, "0.4.0"),
newRepositoryRelease(3, "v0.4.2"),
newPreReleaseRepositoryRelease(1, "v0.4.1-rc.10"),
newPreReleaseRepositoryRelease(2, "0.4.1-rc.9"),
newPreReleaseRepositoryRelease(3, "v0.4.2-rc.1"),
}
})
It("returns all of the versions that are newer, and are release and prerealse", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{Tag: "0.4.0"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: true},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "0.4.0"},
{Tag: "0.4.1-rc.9"},
{Tag: "v0.4.1-rc.10"},
{Tag: "v0.4.2-rc.1"},
{Tag: "v0.4.2"},
}))
})
It("returns the latest release version if the current version is not found", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{ID: "5"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: true},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "v0.4.2"},
}))
})
})
Context("and prerelease is newer", func() {
BeforeEach(func() {
returnedReleases = []*github.RepositoryRelease{
newDraftRepositoryRelease(1, "v0.1.4"),
newRepositoryRelease(1, "0.3.9"),
newRepositoryRelease(2, "0.4.0"),
newRepositoryRelease(3, "v0.4.2"),
newPreReleaseRepositoryRelease(1, "v0.4.1-rc.10"),
newPreReleaseRepositoryRelease(2, "0.4.1-rc.9"),
newPreReleaseRepositoryRelease(3, "v0.4.2-rc.1"),
newPreReleaseRepositoryRelease(4, "v0.4.3-rc.1"),
}
})
It("returns all of the versions that are newer, and are release and prerealse", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{Tag: "0.4.0"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: true},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "0.4.0"},
{Tag: "0.4.1-rc.9"},
{Tag: "v0.4.1-rc.10"},
{Tag: "v0.4.2-rc.1"},
{Tag: "v0.4.2"},
{Tag: "v0.4.3-rc.1"},
}))
})
It("returns the latest prerelease version if the current version is not found", func() {
command := resource.NewCheckCommand(githubClient)
response, err := command.Run(resource.CheckRequest{
Version: resource.Version{ID: "5"},
Source: resource.Source{Drafts: false, PreRelease: true, Release: true},
})
Ω(err).ShouldNot(HaveOccurred())
Ω(response).Should(Equal([]resource.Version{
{Tag: "v0.4.3-rc.1"},
}))
})
})
})
Context("when draft releases are allowed", func() { Context("when draft releases are allowed", func() {
Context("and one of the releases is a final release", func() { Context("and one of the releases is a final release", func() {
BeforeEach(func() { BeforeEach(func() {
@@ -277,7 +421,7 @@ var _ = Describe("Check Command", func() {
response, err := command.Run(resource.CheckRequest{ response, err := command.Run(resource.CheckRequest{
Version: resource.Version{}, Version: resource.Version{},
Source: resource.Source{Drafts: true}, Source: resource.Source{Drafts: true, PreRelease: false},
}) })
Ω(err).ShouldNot(HaveOccurred()) Ω(err).ShouldNot(HaveOccurred())

View File

@@ -8,7 +8,7 @@ import (
) )
func main() { func main() {
var request resource.CheckRequest request := resource.NewCheckRequest()
inputRequest(&request) inputRequest(&request)
github, err := resource.NewGitHubClient(request.Source) github, err := resource.NewGitHubClient(request.Source)

View File

@@ -13,7 +13,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
var request resource.OutRequest request := resource.NewCheckRequest()
inputRequest(&request) inputRequest(&request)
sourceDir := os.Args[1] sourceDir := os.Args[1]

View File

@@ -58,22 +58,24 @@ var _ = Describe("In Command", func() {
buildRelease := func(id int, tag string, draft bool) *github.RepositoryRelease { buildRelease := func(id int, tag string, draft bool) *github.RepositoryRelease {
return &github.RepositoryRelease{ return &github.RepositoryRelease{
ID: github.Int(id), ID: github.Int(id),
TagName: github.String(tag), TagName: github.String(tag),
HTMLURL: github.String("http://google.com"), HTMLURL: github.String("http://google.com"),
Name: github.String("release-name"), Name: github.String("release-name"),
Body: github.String("*markdown*"), Body: github.String("*markdown*"),
Draft: github.Bool(draft), Draft: github.Bool(draft),
Prerelease: github.Bool(false),
} }
} }
buildNilTagRelease := func(id int) *github.RepositoryRelease { buildNilTagRelease := func(id int) *github.RepositoryRelease {
return &github.RepositoryRelease{ return &github.RepositoryRelease{
ID: github.Int(id), ID: github.Int(id),
HTMLURL: github.String("http://google.com"), HTMLURL: github.String("http://google.com"),
Name: github.String("release-name"), Name: github.String("release-name"),
Body: github.String("*markdown*"), Body: github.String("*markdown*"),
Draft: github.Bool(true), Draft: github.Bool(true),
Prerelease: github.Bool(false),
} }
} }

View File

@@ -47,5 +47,11 @@ func metadataFromRelease(release *github.RepositoryRelease) []MetadataPair {
}) })
} }
if *release.Prerelease {
metadata = append(metadata, MetadataPair{
Name: "pre-release",
Value: "true",
})
}
return metadata return metadata
} }

View File

@@ -58,12 +58,17 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
} }
draft := request.Source.Drafts draft := request.Source.Drafts
prerelease := false
if request.Source.PreRelease == true && request.Source.Release == false {
prerelease = request.Source.PreRelease
}
release := &github.RepositoryRelease{ release := &github.RepositoryRelease{
Name: github.String(name), Name: github.String(name),
TagName: github.String(tag), TagName: github.String(tag),
Body: github.String(body), Body: github.String(body),
Draft: github.Bool(draft), Draft: github.Bool(draft),
Prerelease: github.Bool(prerelease),
TargetCommitish: github.String(targetCommitish), TargetCommitish: github.String(targetCommitish),
} }
@@ -89,6 +94,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
existingRelease.Name = github.String(name) existingRelease.Name = github.String(name)
existingRelease.TargetCommitish = github.String(targetCommitish) existingRelease.TargetCommitish = github.String(targetCommitish)
existingRelease.Draft = github.Bool(draft) existingRelease.Draft = github.Bool(draft)
existingRelease.Prerelease = github.Bool(prerelease)
if bodySpecified { if bodySpecified {
existingRelease.Body = github.String(body) existingRelease.Body = github.String(body)

View File

@@ -310,6 +310,77 @@ var _ = Describe("Out Command", func() {
Ω(*release.Draft).Should(Equal(false)) Ω(*release.Draft).Should(Equal(false))
}) })
Context("when pre-release are set and release are not", func() {
BeforeEach(func() {
bodyPath := filepath.Join(sourcesDir, "body")
file(bodyPath, "this is a great release")
request.Source.Release = false
request.Source.PreRelease = true
})
It("creates a non-draft pre-release in Github", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0)
Ω(*release.Name).Should(Equal("v0.3.12"))
Ω(*release.TagName).Should(Equal("0.3.12"))
Ω(*release.Body).Should(Equal(""))
Ω(*release.Draft).Should(Equal(false))
Ω(*release.Prerelease).Should(Equal(true))
})
It("has some sweet metadata", func() {
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},
resource.MetadataPair{Name: "tag", Value: "0.3.12"},
resource.MetadataPair{Name: "pre-release", Value: "true"},
))
})
})
Context("when release and pre-release are set", func() {
BeforeEach(func() {
bodyPath := filepath.Join(sourcesDir, "body")
file(bodyPath, "this is a great release")
request.Source.Release = true
request.Source.PreRelease = true
})
It("creates a final release in Github", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0)
Ω(*release.Name).Should(Equal("v0.3.12"))
Ω(*release.TagName).Should(Equal("0.3.12"))
Ω(*release.Body).Should(Equal(""))
Ω(*release.Draft).Should(Equal(false))
Ω(*release.Prerelease).Should(Equal(false))
})
It("has some sweet metadata", func() {
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},
resource.MetadataPair{Name: "tag", Value: "0.3.12"},
))
})
})
Context("when set as a draft release", func() { Context("when set as a draft release", func() {
BeforeEach(func() { BeforeEach(func() {
bodyPath := filepath.Join(sourcesDir, "body") bodyPath := filepath.Join(sourcesDir, "body")
@@ -328,6 +399,7 @@ 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(""))
Ω(*release.Draft).Should(Equal(true)) Ω(*release.Draft).Should(Equal(true))
Ω(*release.Prerelease).Should(Equal(false))
}) })
It("has some sweet metadata", func() { It("has some sweet metadata", func() {

View File

@@ -15,23 +15,34 @@ func TestGithubReleaseResource(t *testing.T) {
func newRepositoryRelease(id int, version string) *github.RepositoryRelease { func newRepositoryRelease(id int, version string) *github.RepositoryRelease {
return &github.RepositoryRelease{ return &github.RepositoryRelease{
TagName: github.String(version), TagName: github.String(version),
Draft: github.Bool(false), Draft: github.Bool(false),
ID: github.Int(id), Prerelease: github.Bool(false),
ID: github.Int(id),
} }
} }
func newPreReleaseRepositoryRelease(id int, version string) *github.RepositoryRelease {
return &github.RepositoryRelease{
TagName: github.String(version),
Draft: github.Bool(false),
Prerelease: github.Bool(true),
ID: github.Int(id),
}
}
func newDraftRepositoryRelease(id int, version string) *github.RepositoryRelease { func newDraftRepositoryRelease(id int, version string) *github.RepositoryRelease {
return &github.RepositoryRelease{ return &github.RepositoryRelease{
TagName: github.String(version), TagName: github.String(version),
Draft: github.Bool(true), Draft: github.Bool(true),
ID: github.Int(id), Prerelease: github.Bool(false),
ID: github.Int(id),
} }
} }
func newDraftWithNilTagRepositoryRelease(id int) *github.RepositoryRelease { func newDraftWithNilTagRepositoryRelease(id int) *github.RepositoryRelease {
return &github.RepositoryRelease{ return &github.RepositoryRelease{
Draft: github.Bool(true), Draft: github.Bool(true),
ID: github.Int(id), Prerelease: github.Bool(false),
ID: github.Int(id),
} }
} }

View File

@@ -8,6 +8,8 @@ type Source struct {
GitHubUploadsURL string `json:"github_uploads_url"` GitHubUploadsURL string `json:"github_uploads_url"`
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
Drafts bool `json:"drafts"` Drafts bool `json:"drafts"`
PreRelease bool `json:"pre_release"`
Release bool `json:"release"`
} }
type CheckRequest struct { type CheckRequest struct {
@@ -15,6 +17,12 @@ type CheckRequest struct {
Version Version `json:"version"` Version Version `json:"version"`
} }
func NewCheckRequest() CheckRequest {
res := CheckRequest{}
res.Source.Release = true
return res
}
type InRequest struct { type InRequest struct {
Source Source `json:"source"` Source Source `json:"source"`
Version *Version `json:"version"` Version *Version `json:"version"`