Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: forward config param from GitHub action propperly #517

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ branding:
color: "blue"
inputs:
config:
description: "Path of the Commitsar config file"
description: "Path to the folder where your .commitsar.yaml is"
required: false
default: "."
runs:
using: "docker"
image: "Dockerfile"
args:
- commitsar
- "--config-path=${{ inputs.config }}"
42 changes: 42 additions & 0 deletions config/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import (
"github.com/aevea/commitsar/internal/root_runner"
"github.com/aevea/integrations"
"github.com/spf13/viper"
"fmt"
"os"
"github.com/apex/log"
"log"
)

const (
// CommitsarConfigPath is used an env variable to override the default location of the config file.
CommitsarConfigPath = "COMMITSAR_CONFIG_PATH"
)

// CommitConfig will return the RunnerOptions using defaults unless overridden in config or flags
Expand All @@ -15,6 +24,15 @@ func CommitConfig() root_runner.RunnerOptions {
upstreamBranch := integrations.FindCompareBranch()
requiredScopes := []string{}

if err := LoadConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}

if viper.IsSet("commits.strict") {
strict = viper.GetBool("commits.strict")
}
Expand Down Expand Up @@ -43,3 +61,27 @@ func CommitConfig() root_runner.RunnerOptions {
RequiredScopes: requiredScopes,
}
}

// LoadConfig iterates through possible config paths. No config will be loaded if no files are present.
func LoadConfig() error {
viper.AutomaticEnv()
viper.SetConfigName(".commitsar")
viper.SetConfigType("yaml")

if viper.IsSet(CommitsarConfigPath) {
viper.AddConfigPath(viper.GetString(CommitsarConfigPath))
}

viper.AddConfigPath(viper.GetString("config-path"))

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
log.Warn("config file not found, using defaults")
} else {
// Config file was found but another error was produced
return err
}
}
return nil
}
48 changes: 47 additions & 1 deletion config/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"os"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -35,3 +35,49 @@ func TestCommitConfig(t *testing.T) {
os.Clearenv()

}

func TestDefaultConfig(t *testing.T) {
viper.Reset()

err := LoadConfig()
assert.NoError(t, err)
assert.Equal(t, false, viper.GetBool("verbose"))

os.Clearenv()
}

func TestLoadConfigCustomPathFromEnv(t *testing.T) {
viper.Reset()
err := os.Setenv(CommitsarConfigPath, "./testdata")
assert.NoError(t, err)

err = LoadConfig()
assert.NoError(t, err)
assert.Equal(t, true, viper.GetBool("verbose"))

os.Clearenv()
}

func TestLoadConfigCustomPathFromParams(t *testing.T){
viper.Reset()
viper.Set("config-path", "./testdata")

err := LoadConfig()
assert.NoError(t, err)
assert.Equal(t, true, viper.GetBool("verbose"))

os.Clearenv()
}

func TestLoadConfigCustomPathParamOverridesEnv(t *testing.T){
viper.Reset()
err := os.Setenv(CommitsarConfigPath, "./wrong-path")
assert.NoError(t, err)
viper.Set("config-path", "./testdata")

err = LoadConfig()
assert.NoError(t, err)
assert.Equal(t, true, viper.GetBool("verbose"))

os.Clearenv()
}
36 changes: 0 additions & 36 deletions config/config.go

This file was deleted.

20 changes: 0 additions & 20 deletions config/config_test.go

This file was deleted.

1 change: 1 addition & 0 deletions docs/content/configuration/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For more advanced usage a set of flags are provided.
| Verbose | --v | false | false | Debug output into console |
| Strict | --s | false | true | Strict check of category types |
| All | --all | false | false | Whether to check all commits on given branch. **Takes precedence over LIMIT flag** |
| Config path | --config-path | false | current directory | Path where .commitsar.yaml file is |

On top of that a single argument is allowed:

Expand Down
14 changes: 5 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ var commit string
var date string

func runRoot(cmd *cobra.Command, args []string) error {
if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}

runner := root_runner.New()

commitConfig := config.CommitConfig()
Expand Down Expand Up @@ -64,6 +60,11 @@ func bindRootFlags(rootCmd *cobra.Command) error {
if err != nil {
return err
}
rootCmd.Flags().String("config-path", ".", "path to your .commitsar.yaml config file")
err = viper.BindPFlag("config-path", rootCmd.Flags().Lookup("config-path"))
if err != nil {
return err
}

// Not used. TODO: Documentation
rootCmd.Flags().StringP("path", "d", ".", "dir points to the path of the repository")
Expand All @@ -77,11 +78,6 @@ func bindRootFlags(rootCmd *cobra.Command) error {
func main() {
log.SetHandler(cli.Default)

if err := config.LoadConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

var rootCmd = &cobra.Command{
Use: "commitsar <from?>...<to>",
Short: "Checks if commits comply",
Expand Down