Skip to content

Commit

Permalink
refactor: rename summarizer to reporter
Browse files Browse the repository at this point in the history
This changes the nomenclature of the interface for reporting policy
statuses.

BREAKING CHANGE: This renames the `--summary` flag to `--reporter`.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
  • Loading branch information
andrewrynhard committed Apr 11, 2020
1 parent b2f63c1 commit f5826e5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var enforceCmd = &cobra.Command{
// after this point
cmd.SilenceUsage = true

summarizer := cmd.Flags().Lookup("summary").Value.String()
e, err := enforcer.New(summarizer)
reporter := cmd.Flags().Lookup("reporter").Value.String()
e, err := enforcer.New(reporter)
if err != nil {
return fmt.Errorf("failed to create enforcer: %+v", err)
}
Expand All @@ -48,6 +48,6 @@ var enforceCmd = &cobra.Command{

func init() {
enforceCmd.Flags().String("commit-msg-file", "", "the path to the temporary commit message file")
enforceCmd.Flags().String("summary", "none", "the summary method to use")
enforceCmd.Flags().String("reporter", "none", "the reporter method to use")
rootCmd.AddCommand(enforceCmd)
}
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var serveCmd = &cobra.Command{
log.Printf("failed to write event to disk: %+v\n", err)
return
}
cmd := exec.Command("/proc/self/exe", "enforce", "--summary=github")
cmd := exec.Command("/proc/self/exe", "enforce", "--reporter=github")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
cmd.Dir = cloneRepo
Expand Down
24 changes: 12 additions & 12 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
"github.com/talos-systems/conform/internal/policy"
"github.com/talos-systems/conform/internal/policy/commit"
"github.com/talos-systems/conform/internal/policy/license"
"github.com/talos-systems/conform/internal/summarizer"
"github.com/talos-systems/conform/internal/reporter"

yaml "gopkg.in/yaml.v2"
)

// Conform is a struct that conform.yaml gets decoded into.
type Conform struct {
Policies []*PolicyDeclaration `yaml:"policies"`
summarizer summarizer.Summarizer
Policies []*PolicyDeclaration `yaml:"policies"`
reporter reporter.Reporter
}

// PolicyDeclaration allows a user to declare an arbitrary type along with a
Expand All @@ -42,18 +42,18 @@ var policyMap = map[string]policy.Policy{
}

// New loads the conform.yaml file and unmarshals it into a Conform struct.
func New(s string) (*Conform, error) {
func New(r string) (*Conform, error) {
c := &Conform{}

switch s {
switch r {
case "github":
s, err := summarizer.NewGitHubSummarizer()
s, err := reporter.NewGitHubReporter()
if err != nil {
return nil, err
}
c.summarizer = s
c.reporter = s
default:
c.summarizer = &summarizer.Noop{}
c.reporter = &reporter.Noop{}
}

configBytes, err := ioutil.ReadFile(".conform.yaml")
Expand Down Expand Up @@ -87,14 +87,14 @@ func (c *Conform) Enforce(setters ...policy.Option) error {
for _, err := range check.Errors() {
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t\n", p.Type, check.Name(), "FAILED", err)
}
if err := c.summarizer.SetStatus("failure", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: summary failed: %+v", err)
if err := c.reporter.SetStatus("failure", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: report failed: %+v", err)
}
pass = false
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", p.Type, check.Name(), "PASS", "<none>")
if err := c.summarizer.SetStatus("success", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: summary failed: %+v", err)
if err := c.reporter.SetStatus("success", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: report failed: %+v", err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package policy

// Report summarizes the compliance of a policy.
// Report reports the compliance of a policy.
type Report struct {
checks []Check
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package summarizer
package reporter

import (
"context"
Expand All @@ -20,20 +20,20 @@ import (
"github.com/talos-systems/conform/internal/git"
)

// Summarizer describes a hook for send summarized results to a remote API.
type Summarizer interface {
// Reporter describes a hook for sending summarized results to a remote API.
type Reporter interface {
SetStatus(string, string, string, string) error
}

// GitHub is a summarizer that summarizes policies statuses as GitHub statuses.
// GitHub is a reporter that summarizes policy statuses as GitHub statuses.
type GitHub struct {
token string
owner string
repo string
sha string
}

// Noop is a summarizer that does nothing.
// Noop is a reporter that does nothing.
type Noop struct {
}

Expand All @@ -42,9 +42,9 @@ func (n *Noop) SetStatus(state, policy, check, message string) error {
return nil
}

// NewGitHubSummarizer returns a summarizer that posts policy checks as
// NewGitHubReporter returns a reporter that posts policy checks as
// status checks on a pull request.
func NewGitHubSummarizer() (*GitHub, error) {
func NewGitHubReporter() (*GitHub, error) {
token, ok := os.LookupEnv("INPUT_TOKEN")
if !ok {
return nil, errors.New("missing INPUT_TOKEN")
Expand Down

0 comments on commit f5826e5

Please sign in to comment.