Skip to content

Commit

Permalink
feat: adding command line flag for commit msg
Browse files Browse the repository at this point in the history
This allows someone to use `conform enforce` for commit-msg git hooks.

fix: removing commented code

fix: adding a check for ioutil reading the commit msg file
  • Loading branch information
test committed Oct 8, 2018
1 parent 088e0a7 commit 81dd051
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var buildCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
e, err := enforcer.New()
e, err := enforcer.New(cmd.Flags())
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var enforceCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
e, err := enforcer.New()
e, err := enforcer.New(cmd.Flags())
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -46,5 +46,6 @@ var enforceCmd = &cobra.Command{
}

func init() {
enforceCmd.Flags().String("commit-msg-file", "", "the path to the temporary commit message file")
RootCmd.AddCommand(enforceCmd)
}
5 changes: 4 additions & 1 deletion pkg/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/autonomy/conform/pkg/stage"
"github.com/autonomy/conform/pkg/task"
"github.com/mitchellh/mapstructure"
flag "github.com/spf13/pflag"

yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ var policyMap = map[string]policy.Policy{
}

// New loads the conform.yaml file and unmarshals it into a Conform struct.
func New() (*Conform, error) {
func New(flags *flag.FlagSet) (*Conform, error) {
configBytes, err := ioutil.ReadFile(".conform.yaml")
if err != nil {
return nil, err
Expand All @@ -52,6 +53,8 @@ func New() (*Conform, error) {
return nil, err
}

c.Metadata.Flags = flags

return c, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Masterminds/semver"
"github.com/autonomy/conform/pkg/git"
flag "github.com/spf13/pflag"
)

// Metadata contains metadata.
Expand All @@ -14,6 +15,7 @@ type Metadata struct {
Git *Git
Version *Version
Variables VariablesMap `yaml:"variables"`
Flags *flag.FlagSet
Built string
}

Expand Down
19 changes: 15 additions & 4 deletions pkg/policy/conventionalcommit/conventionalcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package conventionalcommit

import (
"fmt"
"io/ioutil"
"regexp"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/autonomy/conform/pkg/metadata"
"github.com/autonomy/conform/pkg/pipeline"
"github.com/autonomy/conform/pkg/policy"
Expand Down Expand Up @@ -37,12 +39,21 @@ const TypeFix = "fix"
// Compliance implements the policy.Policy.Compliance function.
func (c *Conventional) Compliance(metadata *metadata.Metadata, options ...policy.Option) (report policy.Report) {
report = policy.Report{}
if !metadata.Git.IsClean {
return
var commitMsgFile string
if metadata.Flags != nil {
commitMsgFile = metadata.Flags.Lookup("commit-msg-file").Value.String()
}
msg := metadata.Git.Message // start with last commit message in log
if commitMsgFile != "" {
contents, err := ioutil.ReadFile(commitMsgFile)
if err != nil {
log.Fatal(err)
}
msg = string(contents)
}
groups := parseHeader(metadata.Git.Message)
groups := parseHeader(msg)
if len(groups) != 6 {
report.Errors = append(report.Errors, fmt.Errorf("Invalid commit format: %s", metadata.Git.Message))
report.Errors = append(report.Errors, fmt.Errorf("Invalid commit format: %s", msg))
return
}
ValidateHeaderLength(&report, groups)
Expand Down

0 comments on commit 81dd051

Please sign in to comment.