Skip to content

Commit

Permalink
feat: extend uplift to support config validation (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-paul-t authored Feb 1, 2023
1 parent 1b1f4a7 commit 37fb34d
Show file tree
Hide file tree
Showing 38 changed files with 684 additions and 95 deletions.
4 changes: 2 additions & 2 deletions cmd/uplift/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ func setupBumpContext(opts bumpOptions, out io.Writer) (*context.Context, error)

// Handle git config. Command line flag takes precedences
ctx.IgnoreDetached = opts.IgnoreDetached
if !ctx.IgnoreDetached {
if !ctx.IgnoreDetached && ctx.Config.Git != nil {
ctx.IgnoreDetached = ctx.Config.Git.IgnoreDetached
}

ctx.IgnoreShallow = opts.IgnoreShallow
if !ctx.IgnoreShallow {
if !ctx.IgnoreShallow && ctx.Config.Git != nil {
ctx.IgnoreShallow = ctx.Config.Git.IgnoreShallow
}

Expand Down
14 changes: 9 additions & 5 deletions cmd/uplift/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ func setupChangelogContext(opts changelogOptions, out io.Writer) (*context.Conte

// Sort order provided as a command-line flag takes precedence
ctx.Changelog.Sort = opts.Sort
if ctx.Changelog.Sort == "" {
if ctx.Changelog.Sort == "" && cfg.Changelog != nil {
ctx.Changelog.Sort = strings.ToLower(cfg.Changelog.Sort)
}

// Merge config and command line arguments together
ctx.Changelog.Include = append(opts.Include, ctx.Config.Changelog.Include...)
ctx.Changelog.Exclude = append(opts.Exclude, ctx.Config.Changelog.Exclude...)
ctx.Changelog.Include = opts.Include
ctx.Changelog.Exclude = opts.Exclude
if ctx.Config.Changelog != nil {
ctx.Changelog.Include = append(ctx.Changelog.Include, ctx.Config.Changelog.Include...)
ctx.Changelog.Exclude = append(ctx.Changelog.Exclude, ctx.Config.Changelog.Exclude...)
}

// By default ensure the ci(uplift): commits are excluded also
ctx.Changelog.Exclude = append(ctx.Changelog.Exclude, `ci\(uplift\)`)
Expand All @@ -212,12 +216,12 @@ func setupChangelogContext(opts changelogOptions, out io.Writer) (*context.Conte

// Handle git config. Command line flag takes precedences
ctx.IgnoreDetached = opts.IgnoreDetached
if !ctx.IgnoreDetached {
if !ctx.IgnoreDetached && ctx.Config.Git != nil {
ctx.IgnoreDetached = ctx.Config.Git.IgnoreDetached
}

ctx.IgnoreShallow = opts.IgnoreShallow
if !ctx.IgnoreShallow {
if !ctx.IgnoreShallow && ctx.Config.Git != nil {
ctx.IgnoreShallow = ctx.Config.Git.IgnoreShallow
}

Expand Down
46 changes: 46 additions & 0 deletions cmd/uplift/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package main

import (
"io"

"github.com/spf13/cobra"
)

func newCheckCmd(gopts *globalOptions, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check if a configuration file is valid",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadConfig(gopts.ConfigDir)
if err != nil {
return err
}

return cfg.Validate()
},
}

return cmd
}
69 changes: 69 additions & 0 deletions cmd/uplift/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package main

import (
"os"
"testing"

"github.com/gembaadvantage/uplift/internal/git"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCheck(t *testing.T) {
cfg := `
commitAuthor:
name: joe.bloggs
email: joe.bloggs@example.com
`
configFileWith(t, cfg)

checkCmd := newCheckCmd(&globalOptions{}, os.Stdout)
err := checkCmd.Execute()

assert.NoError(t, err)
}

func TestCheck_InvalidConfig(t *testing.T) {
cfg := `
bumps:
- file: text.txt
regex:
- pattern: ""
`
configFileWith(t, cfg)

checkCmd := newCheckCmd(&globalOptions{}, os.Stdout)
err := checkCmd.Execute()

assert.Error(t, err)
}

func configFileWith(t *testing.T, content string) {
t.Helper()
git.MkTmpDir(t)

err := os.WriteFile(".uplift.yml", []byte(content), 0o644)
require.NoError(t, err)
}
2 changes: 2 additions & 0 deletions cmd/uplift/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ SOFTWARE.
package main

import (
"fmt"
"os"
)

func main() {
rootCmd := newRootCmd(os.Stdout)

if err := rootCmd.Cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
12 changes: 8 additions & 4 deletions cmd/uplift/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ func setupReleaseContext(opts releaseOptions, out io.Writer) (*context.Context,
ctx.Changelog.PreTag = true

// Merge config and command line arguments together
ctx.Changelog.Include = append(opts.Include, ctx.Config.Changelog.Include...)
ctx.Changelog.Exclude = append(opts.Exclude, ctx.Config.Changelog.Exclude...)
ctx.Changelog.Include = opts.Include
ctx.Changelog.Exclude = opts.Exclude
if ctx.Config.Changelog != nil {
ctx.Changelog.Include = append(ctx.Changelog.Include, ctx.Config.Changelog.Include...)
ctx.Changelog.Exclude = append(ctx.Changelog.Exclude, ctx.Config.Changelog.Exclude...)
}

// By default ensure the ci(uplift): commits are excluded also
ctx.Changelog.Exclude = append(ctx.Changelog.Exclude, "ci(uplift):")
Expand All @@ -209,12 +213,12 @@ func setupReleaseContext(opts releaseOptions, out io.Writer) (*context.Context,

// Handle git config. Command line flag takes precedences
ctx.IgnoreDetached = opts.IgnoreDetached
if !ctx.IgnoreDetached {
if !ctx.IgnoreDetached && ctx.Config.Git != nil {
ctx.IgnoreDetached = ctx.Config.Git.IgnoreDetached
}

ctx.IgnoreShallow = opts.IgnoreShallow
if !ctx.IgnoreShallow {
if !ctx.IgnoreShallow && ctx.Config.Git != nil {
ctx.IgnoreShallow = ctx.Config.Git.IgnoreShallow
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/uplift/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func newRootCmd(out io.Writer) *rootCommand {
}

cmd := &cobra.Command{
Use: "uplift",
Short: "Semantic versioning the easy way",
SilenceUsage: true,
Use: "uplift",
Short: "Semantic versioning the easy way",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if rootCmd.Opts.Debug {
log.SetLevel(log.InvalidLevel)
Expand Down Expand Up @@ -91,6 +92,7 @@ func newRootCmd(out io.Writer) *rootCommand {
newReleaseCmd(rootCmd.Opts, out).Cmd,
newChangelogCmd(rootCmd.Opts, out).Cmd,
newManPageCmd(out).Cmd,
newCheckCmd(rootCmd.Opts, out),
)

rootCmd.Cmd = cmd
Expand Down
4 changes: 2 additions & 2 deletions cmd/uplift/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ func setupTagContext(opts tagOptions, out io.Writer) (*context.Context, error) {

// Handle git config. Command line flag takes precedences
ctx.IgnoreDetached = opts.IgnoreDetached
if !ctx.IgnoreDetached {
if !ctx.IgnoreDetached && ctx.Config.Git != nil {
ctx.IgnoreDetached = ctx.Config.Git.IgnoreDetached
}

ctx.IgnoreShallow = opts.IgnoreShallow
if !ctx.IgnoreShallow {
if !ctx.IgnoreShallow && ctx.Config.Git != nil {
ctx.IgnoreShallow = ctx.Config.Git.IgnoreShallow
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/uplift/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func configWithHooks(t *testing.T) {
require.NoError(t, err)

cfg := &config.Uplift{
Hooks: config.Hooks{
Hooks: &config.Hooks{
Before: []string{"touch " + BeforeFile},
BeforeBump: []string{"touch " + BeforeBumpFile},
BeforeTag: []string{"touch " + BeforeTagFile},
Expand Down
3 changes: 2 additions & 1 deletion docs/static/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"file": {
"$comment": "https://upliftci.dev/reference/config#bumps",
"description": "The path of the file relative to where Uplift is executed. Glob patterns can be used to match multiple files at the same time. Glob syntax is based on https://github.com/goreleaser/fileglob",
"type": "string"
"type": "string",
"minLength": 1
},
"regex": {
"$comment": "https://upliftci.dev/reference/config#bumps",
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/apex/log v1.9.0
github.com/gembaadvantage/codecommit-sign v1.4.0
github.com/go-playground/validator/v10 v10.11.1
github.com/goreleaser/fileglob v1.3.0
github.com/joho/godotenv v1.4.0
github.com/muesli/mango-cobra v1.2.0
Expand All @@ -21,8 +22,11 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/muesli/mango v0.1.0 // indirect
Expand All @@ -33,8 +37,9 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/text v0.3.8 // indirect
)
Loading

0 comments on commit 37fb34d

Please sign in to comment.