Skip to content

Commit

Permalink
commit-checker: move to an importable dir
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Nov 27, 2023
1 parent 329a666 commit eaa9fc2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 51 deletions.
51 changes: 7 additions & 44 deletions commitchecker/commitchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,12 @@ import (
"flag"
"fmt"
"os"
)

type FetchMode string

const (
HTTPS FetchMode = "https"
SSH FetchMode = "ssh"
"github.com/openshift/build-machinery-go/commitchecker/commitchecker"
)

type Options struct {
Start string
End string
ConfigFile string
FetchMode string
}

func DefaultOptions() Options {
return Options{
Start: "main",
End: "HEAD",
ConfigFile: "./commitchecker.yaml",
FetchMode: string(HTTPS),
}
}

func (o *Options) Bind(fs *flag.FlagSet) {
fs.StringVar(&o.Start, "start", o.Start, "The start of the revision range for analysis")
fs.StringVar(&o.End, "end", o.End, "The end of the revision range for analysis")
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The configuration file to use (optional).")
fs.StringVar(&o.FetchMode, "fetch-mode", o.FetchMode, "Method to use for fetching from git remotes.")
}

func (o *Options) Validate() error {
switch FetchMode(o.FetchMode) {
case SSH, HTTPS:
default:
return fmt.Errorf("--fetch-mode must be one of %v", []FetchMode{HTTPS, SSH})
}
return nil
}

func main() {
opts := DefaultOptions()
opts := commitchecker.DefaultOptions()
opts.Bind(flag.CommandLine)
flag.Parse()

Expand All @@ -55,13 +18,13 @@ func main() {
os.Exit(1)
}

cfg, err := Load(opts.ConfigFile)
cfg, err := commitchecker.Load(opts.ConfigFile)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: couldn't load config: %v\n", err)
os.Exit(1)
}

mergeBase, err := DetermineMergeBase(cfg, FetchMode(opts.FetchMode), opts.End)
mergeBase, err := commitchecker.DetermineMergeBase(cfg, commitchecker.FetchMode(opts.FetchMode), opts.End)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: couldn't determine merge base: %v\n", err)
os.Exit(1)
Expand All @@ -71,9 +34,9 @@ func main() {
start = mergeBase
}

commits, err := CommitsBetween(start, opts.End)
commits, err := commitchecker.CommitsBetween(start, opts.End)
if err != nil {
if err == ErrNotCommit {
if err == commitchecker.ErrNotCommit {
_, _ = fmt.Fprintf(os.Stderr, "WARNING: one of the provided commits does not exist, not a true branch\n")
os.Exit(0)
}
Expand All @@ -82,7 +45,7 @@ func main() {
}

var errs []string
for _, validate := range allCommitValidators {
for _, validate := range commitchecker.AllCommitValidators {
for _, commit := range commits {
errs = append(errs, validate(commit)...)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commitchecker

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commitchecker

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commitchecker

import (
"testing"
Expand Down
45 changes: 45 additions & 0 deletions commitchecker/commitchecker/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package commitchecker

import (
"flag"
"fmt"
)

type FetchMode string

const (
HTTPS FetchMode = "https"
SSH FetchMode = "ssh"
)

type Options struct {
Start string
End string
ConfigFile string
FetchMode string
}

func DefaultOptions() Options {
return Options{
Start: "main",
End: "HEAD",
ConfigFile: "./commitchecker.yaml",
FetchMode: string(HTTPS),
}
}

func (o *Options) Bind(fs *flag.FlagSet) {
fs.StringVar(&o.Start, "start", o.Start, "The start of the revision range for analysis")
fs.StringVar(&o.End, "end", o.End, "The end of the revision range for analysis")
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The configuration file to use (optional).")
fs.StringVar(&o.FetchMode, "fetch-mode", o.FetchMode, "Method to use for fetching from git remotes.")
}

func (o *Options) Validate() error {
switch FetchMode(o.FetchMode) {
case SSH, HTTPS:
default:
return fmt.Errorf("--fetch-mode must be one of %v", []FetchMode{HTTPS, SSH})
}
return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commitchecker

import (
"bytes"
Expand All @@ -9,8 +9,8 @@ import (
)

var (
// allCommitValidators holds all registered checks.
allCommitValidators = []func(Commit) []string{
// AllCommitValidators holds all registered checks.
AllCommitValidators = []func(Commit) []string{
ValidateCommitAuthor,

// Local commit messages must be prefixed with UPSTREAM as per
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commitchecker

import (
"reflect"
Expand Down

0 comments on commit eaa9fc2

Please sign in to comment.