Skip to content

Commit

Permalink
Fixup lint warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus committed Feb 22, 2022
1 parent a4cd73f commit e9d8801
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 35 deletions.
4 changes: 2 additions & 2 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.R
return ossFuzzRepoClient, nil
}

// TODO(repo): Pass a `http.RoundTripper` here
// GetClients returns a list of clients for running scorecard checks.
// TODO(repo): Pass a `http.RoundTripper` here.
func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logger) (
clients.Repo, // repo
clients.RepoClient, // repoClient
Expand All @@ -261,7 +262,6 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge

githubRepo, errGitHub = MakeGithubRepo(repoURI)
if errGitHub != nil {
// nolint: wrapcheck
return githubRepo,
nil,
nil,
Expand Down
18 changes: 14 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func scorecardCmd(cmd *cobra.Command, args []string) {
RunScorecard(args)
}

// RunScorecard runs scorecard checks given a set of arguments.
// TODO(cmd): Is `args` required?
func RunScorecard(args []string) {
// TODO(cmd): Catch validation errors
Expand Down Expand Up @@ -167,8 +168,17 @@ func RunScorecard(args []string) {
}
}

repoResult, err := pkg.RunScorecards(ctx, repoURI, opts.Commit, opts.Format == options.FormatRaw, enabledChecks, repoClient,
ossFuzzRepoClient, ciiClient, vulnsClient)
repoResult, err := pkg.RunScorecards(
ctx,
repoURI,
opts.Commit,
opts.Format == options.FormatRaw,
enabledChecks,
repoClient,
ossFuzzRepoClient,
ciiClient,
vulnsClient,
)
if err != nil {
log.Panic(err)
}
Expand All @@ -186,9 +196,9 @@ func RunScorecard(args []string) {
fmt.Println("\nRESULTS\n-------")
}

resultsErr := format.FormatResults(
resultsErr := format.Results(
opts,
repoResult,
&repoResult,
checkDocs,
pol,
)
Expand Down
7 changes: 4 additions & 3 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
spol "github.com/ossf/scorecard/v4/policy"
)

func FormatResults(
// Results formats scorecard results.
func Results(
opts *options.Options,
results pkg.ScorecardResult,
results *pkg.ScorecardResult,
docs checks.Doc,
policy *spol.ScorecardPolicy,
) error {
Expand All @@ -55,7 +56,7 @@ func FormatResults(
}

if err != nil {
return fmt.Errorf("Failed to output results: %v", err)
return fmt.Errorf("failed to output results: %w", err)
}

return nil
Expand Down
61 changes: 43 additions & 18 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// package options implements Scorecard options.
// Package options implements Scorecard options.
package options

import (
"fmt"
"errors"
"os"

"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/log"
)

// Options define common options for configuring scorecard.
type Options struct {
Repo string
Local string
Expand All @@ -33,35 +34,58 @@ type Options struct {
PyPI string
RubyGems string
PolicyFile string
ShowDetails bool
ChecksToRun []string
Metadata []string
ShowDetails bool
}

// New creates a new instance of `Options`.
func New() *Options {
return &Options{}
}

const (
// DefaultCommit specifies the default commit reference to use.
DefaultCommit = clients.HeadSHA

// Formats
FormatJSON = "json"
FormatSarif = "sarif"
// Formats.

// FormatJSON specifies that results should be output in JSON format.
FormatJSON = "json"
// FormatSarif specifies that results should be output in SARIF format.
FormatSarif = "sarif"
// FormatDefault specifies that results should be output in default format.
FormatDefault = "default"
FormatRaw = "raw"
// FormatRaw specifies that results should be output in raw format.
FormatRaw = "raw"

// Environment variables
// Environment variables.

// EnvVarEnableSarif is the environment variable which controls enabling
// SARIF logging.
EnvVarEnableSarif = "ENABLE_SARIF"
// EnvVarScorecardV6 is the environment variable which enables scorecard v6
// options.
EnvVarScorecardV6 = "SCORECARD_V6"
)

var (
// DefaultLogLevel retrieves the default log level.
DefaultLogLevel = log.DefaultLevel.String()

errCommitIsEmpty = errors.New("commit should be non-empty")
errCommitOptionNotSupported = errors.New("commit option is not supported yet")
errFormatNotSupported = errors.New("unsupported format")
errPolicyFileNotSupported = errors.New("policy file is not supported yet")
errRawOptionNotSupported = errors.New("raw option is not supported yet")
errRepoOptionMustBeSet = errors.New(
"exactly one of `repo`, `npm`, `pypi`, `rubygems` or `local` must be set",
)
errSARIFNotSupported = errors.New("SARIF format is not supported yet")
)

// TODO(options): Create explicit error types
// TODO(options): Cleanup error messages
// Validate validates scorecard configuration options.
// TODO(options): Cleanup error messages.
func (o *Options) Validate() []error {
var errs []error

Expand All @@ -73,7 +97,7 @@ func (o *Options) Validate() []error {
o.Local != "") != 1 {
errs = append(
errs,
fmt.Errorf("Exactly one of `--repo`, `--npm`, `--pypi`, `--rubygems` or `--local` must be set"),
errRepoOptionMustBeSet,
)
}

Expand All @@ -82,13 +106,13 @@ func (o *Options) Validate() []error {
if o.Format == FormatSarif {
errs = append(
errs,
fmt.Errorf("sarif format not supported yet"),
errSARIFNotSupported,
)
}
if o.PolicyFile != "" {
errs = append(
errs,
fmt.Errorf("policy file not supported yet"),
errPolicyFileNotSupported,
)
}
}
Expand All @@ -98,13 +122,13 @@ func (o *Options) Validate() []error {
if o.Format == FormatRaw {
errs = append(
errs,
fmt.Errorf("raw option not supported yet"),
errRawOptionNotSupported,
)
}
if o.Commit != clients.HeadSHA {
errs = append(
errs,
fmt.Errorf("--commit option not supported yet"),
errCommitOptionNotSupported,
)
}
}
Expand All @@ -113,15 +137,15 @@ func (o *Options) Validate() []error {
if !validateFormat(o.Format) {
errs = append(
errs,
fmt.Errorf("unsupported format '%s'", o.Format),
errFormatNotSupported,
)
}

// Validate `commit` is non-empty.
if o.Commit == "" {
errs = append(
errs,
fmt.Errorf("commit should be non-empty"),
errCommitIsEmpty,
)
}

Expand All @@ -138,7 +162,8 @@ func boolSum(bools ...bool) int {
return sum
}

// TODO(options): This probably doesn't need to be exported
// IsSarifEnabled returns true if `EnvVarEnableSarif` is specified.
// TODO(options): This probably doesn't need to be exported.
func IsSarifEnabled() bool {
// UPGRADEv4: remove.
var sarifEnabled bool
Expand Down
19 changes: 12 additions & 7 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func modeToProto(m string) CheckPolicy_Mode {
}
}

// ParseFromFile takes a policy file and returns a `ScorecardPolicy`.
func ParseFromFile(policyFile string) (*ScorecardPolicy, error) {
if policyFile != "" {
data, err := os.ReadFile(policyFile)
Expand All @@ -74,7 +75,7 @@ func ParseFromFile(policyFile string) (*ScorecardPolicy, error) {
fmt.Sprintf("os.ReadFile: %v", err))
}

sp, err := ParseFromYAML(data)
sp, err := parseFromYAML(data)
if err != nil {
return nil,
sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("spol.ParseFromYAML: %v", err))
Expand All @@ -86,9 +87,8 @@ func ParseFromFile(policyFile string) (*ScorecardPolicy, error) {
return nil, nil
}

// ParseFromYAML parses a policy file and returns
// a scorecardPolicy.
func ParseFromYAML(b []byte) (*ScorecardPolicy, error) {
// parseFromYAML parses a policy file and returns a `ScorecardPolicy`.
func parseFromYAML(b []byte) (*ScorecardPolicy, error) {
// Internal golang for unmarshalling the policy file.
sp := scorecardPolicy{}
// Protobuf-defined policy (policy.proto and policy.pb.go).
Expand Down Expand Up @@ -137,14 +137,19 @@ func ParseFromYAML(b []byte) (*ScorecardPolicy, error) {
return &retPolicy, nil
}

// GetAll returns the full list of checks, given any environment variable
// constraints.
func GetAll() checker.CheckNameToFnMap {
// Returns the full list of checks, given any environment variable constraints.
possibleChecks := checks.AllChecks
return possibleChecks
}

func GetEnabled(sp *ScorecardPolicy, argsChecks []string,
requiredRequestTypes []checker.RequestType) (checker.CheckNameToFnMap, error) {
// GetEnabled returns the list of enabled checks.
func GetEnabled(
sp *ScorecardPolicy,
argsChecks []string,
requiredRequestTypes []checker.RequestType,
) (checker.CheckNameToFnMap, error) {
enabledChecks := checker.CheckNameToFnMap{}

switch {
Expand Down
2 changes: 1 addition & 1 deletion policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestPolicyRead(t *testing.T) {
t.Fatalf("cannot read file: %v", err)
}

p, err := ParseFromYAML(content)
p, err := parseFromYAML(content)

if !errors.Is(err, tt.err) {
t.Fatalf("%s: expected %v, got %v", tt.name, tt.err, err)
Expand Down

0 comments on commit e9d8801

Please sign in to comment.