Skip to content

Commit

Permalink
Add a prepare and continue release step
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 19, 2017
1 parent a4809e4 commit 7ead5e0
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 30 deletions.
7 changes: 6 additions & 1 deletion commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ type releaser struct {

// Will be zero for main releases.
patchLevel int

step int
}

func createReleaser() *releaser {
// Note: This is a command only meant for internal use and must be run
// via "go run main.go release" on the actual code base that is in the release.
r := &releaser{
cmd: &cobra.Command{
Use: "release",
Expand All @@ -39,10 +43,11 @@ func createReleaser() *releaser {
}

r.cmd.PersistentFlags().IntVarP(&r.patchLevel, "patch", "p", 0, "Patch level, defaults to 0 for main releases")
r.cmd.PersistentFlags().IntVarP(&r.step, "step", "s", -1, "Release step, defaults to -1 for all steps.")

return r
}

func (r *releaser) release() error {
return release.New(r.patchLevel).Run()
return release.New(r.patchLevel, r.step).Run()
}
83 changes: 54 additions & 29 deletions release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,57 @@ import (

type ReleaseHandler struct {
patch int
step int
}

func New(patch int) *ReleaseHandler {
return &ReleaseHandler{patch: patch}
func (r ReleaseHandler) shouldRelease() bool {
return r.step < 1 || r.shouldContinue()
}

func (r *ReleaseHandler) Run() error {
if os.Getenv("GITHUB_TOKEN") == "" {
return errors.New("GITHUB_TOKEN not set, needed by goreleaser")
}
func (r ReleaseHandler) shouldContinue() bool {
return r.step == 2
}

func (r ReleaseHandler) shouldPrepare() bool {
return r.step < 1 || r.step == 1
}

func (r ReleaseHandler) calculateVersions(current helpers.HugoVersion) (helpers.HugoVersion, helpers.HugoVersion) {
var (
newVersion helpers.HugoVersion
finalVersion = helpers.CurrentHugoVersion
newVersion = current
finalVersion = current
)

if r.patch > 0 {
newVersion.Suffix = ""

if r.shouldContinue() {
// The version in the current code base is in the state we want for
// the release.
if r.patch == 0 {
finalVersion = newVersion.Next()
}
} else if r.patch > 0 {
newVersion = helpers.CurrentHugoVersion.NextPatchLevel(r.patch)
} else {
newVersion = helpers.CurrentHugoVersion.Next()
finalVersion = newVersion
finalVersion.Suffix = "-DEV"
finalVersion = newVersion.Next()
}

finalVersion.Suffix = "-DEV"

return newVersion, finalVersion
}

func New(patch, step int) *ReleaseHandler {
return &ReleaseHandler{patch: patch, step: step}
}

func (r *ReleaseHandler) Run() error {
if r.shouldRelease() && os.Getenv("GITHUB_TOKEN") == "" {
return errors.New("GITHUB_TOKEN not set, needed by goreleaser")
}

newVersion, finalVersion := r.calculateVersions(helpers.CurrentHugoVersion)

if true || confirm(fmt.Sprint("Start release of ", newVersion, "?")) {

tag := "v" + newVersion.String()
Expand All @@ -72,16 +99,19 @@ func (r *ReleaseHandler) Run() error {
return fmt.Errorf("Tag %q already exists", tag)
}

// Plan:
// Release notes?
// OK Adapt version numbers
// TODO(bep) push before release?
if err := bumpVersions(newVersion); err != nil {
return err
if r.shouldPrepare() {
if err := bumpVersions(newVersion); err != nil {
return err
}

if _, err := git("commit", "-a", "-m", fmt.Sprintf("release: Bump versions for release of %s", newVersion)); err != nil {
return err
}
}

if _, err := git("commit", "-a", "-m", fmt.Sprintf("release: Bump versions for release of %s", newVersion)); err != nil {
return err
if !r.shouldRelease() {
fmt.Println("Skip release ... Use --state=2 to continue.")
return nil
}

if _, err := git("tag", "-a", tag, "-m", fmt.Sprintf("release: %s", newVersion)); err != nil {
Expand All @@ -104,13 +134,6 @@ func (r *ReleaseHandler) Run() error {

}

// Commit
// Tag
//
// Run goreleaser
// Prepare version numbers for next release.
// Commit.

return nil
}

Expand Down Expand Up @@ -138,9 +161,11 @@ func confirm(s string) bool {

func release() error {
cmd := exec.Command("goreleaser")
out, err := cmd.CombinedOutput()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("goreleaser failed: %q: %q", err, out)
return fmt.Errorf("goreleaser failed: %s", err)
}
return nil
}
Expand Down
78 changes: 78 additions & 0 deletions release/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2017-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package commands defines and implements command-line commands and flags
// used by Hugo. Commands and flags are implemented using Cobra.

package release

import (
"testing"

"github.com/spf13/hugo/helpers"
"github.com/stretchr/testify/require"
)

func TestCalculateVersions(t *testing.T) {
startVersion := helpers.HugoVersion{Number: 0.20, Suffix: "-DEV"}

tests := []struct {
handler *ReleaseHandler
version helpers.HugoVersion
v1 string
v2 string
}{
{
New(0, 0),
startVersion,
"0.20",
"0.21-DEV",
},
{
New(2, 0),
startVersion,
"0.20.2",
"0.20-DEV",
},
{
New(0, 1),
startVersion,
"0.20",
"0.21-DEV",
},
{
New(0, 2),
startVersion,
"0.20",
"0.21-DEV",
},
{
New(3, 1),
startVersion,
"0.20.3",
"0.20-DEV",
},
{
New(3, 2),
startVersion.Next(),
"0.21",
"0.21-DEV",
},
}

for _, test := range tests {
v1, v2 := test.handler.calculateVersions(test.version)
require.Equal(t, test.v1, v1.String(), "Release version")
require.Equal(t, test.v2, v2.String(), "Final version")
}
}

0 comments on commit 7ead5e0

Please sign in to comment.