⚠️ Make all ScorecardResult format options pointers (#4151)

* make format options pointers

Callers can pass in a nil pointer to use the default values.
This is also consistent with AsProbe which already used a pointer.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* remove unused FJSON format

Signed-off-by: Spencer Schrock <sschrock@google.com>

---------

Signed-off-by: Spencer Schrock <sschrock@google.com>
This commit is contained in:
Spencer Schrock 2024-06-10 13:32:20 -07:00 committed by GitHub
parent f591fbb551
commit 20ec42c2b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 24 deletions

View File

@ -56,7 +56,7 @@ func JSON(r *pkg.ScorecardResult, w io.Writer) error {
return err
}
Normalize(r)
o := pkg.AsJSON2ResultOption{
o := &pkg.AsJSON2ResultOption{
Details: details,
LogLevel: logLevel,
}

View File

@ -129,9 +129,14 @@ func (r *ScorecardResult) AsJSON(showDetails bool, logLevel log.Level, writer io
}
// AsJSON2 exports results as JSON for new detail format.
func (r *ScorecardResult) AsJSON2(writer io.Writer,
checkDocs docs.Doc, o AsJSON2ResultOption,
) error {
func (r *ScorecardResult) AsJSON2(writer io.Writer, checkDocs docs.Doc, opt *AsJSON2ResultOption) error {
if opt == nil {
opt = &AsJSON2ResultOption{
LogLevel: log.DefaultLevel,
Details: false,
Annotations: false,
}
}
score, err := r.GetAggregateScore(checkDocs)
if err != nil {
return err
@ -170,17 +175,17 @@ func (r *ScorecardResult) AsJSON2(writer io.Writer,
Reason: checkResult.Reason,
Score: checkResult.Score,
}
if o.Details {
if opt.Details {
for i := range checkResult.Details {
d := checkResult.Details[i]
m := DetailToString(&d, o.LogLevel)
m := DetailToString(&d, opt.LogLevel)
if m == "" {
continue
}
tmpResult.Details = append(tmpResult.Details, m)
}
}
if o.Annotations {
if opt.Annotations {
exempted, reasons := checkResult.IsExempted(r.Config)
if exempted {
tmpResult.Annotations = reasons
@ -243,9 +248,3 @@ func ExperimentalFromJSON2(r io.Reader) (result ScorecardResult, score float64,
return sr, float64(jsr.AggregateScore), nil
}
func (r *ScorecardResult) AsFJSON(showDetails bool,
logLevel log.Level, checkDocs docs.Doc, writer io.Writer,
) error {
return sce.WithMessage(sce.ErrScorecardInternal, "WIP: not supported")
}

View File

@ -499,7 +499,7 @@ func TestJSONOutput(t *testing.T) {
}
var result bytes.Buffer
o := AsJSON2ResultOption{
o := &AsJSON2ResultOption{
Details: tt.showDetails,
LogLevel: tt.logLevel,
Annotations: tt.showAnnotations,

View File

@ -140,7 +140,7 @@ func FormatResults(
switch opts.Format {
case options.FormatDefault:
o := AsStringResultOption{
o := &AsStringResultOption{
Details: opts.ShowDetails,
Annotations: opts.ShowAnnotations,
LogLevel: log.ParseLevel(opts.LogLevel),
@ -150,7 +150,7 @@ func FormatResults(
// TODO: support config files and update checker.MaxResultScore.
err = results.AsSARIF(opts.ShowDetails, log.ParseLevel(opts.LogLevel), output, doc, policy, opts)
case options.FormatJSON:
o := AsJSON2ResultOption{
o := &AsJSON2ResultOption{
Details: opts.ShowDetails,
Annotations: opts.ShowAnnotations,
LogLevel: log.ParseLevel(opts.LogLevel),
@ -179,9 +179,15 @@ func FormatResults(
}
// AsString returns ScorecardResult in string format.
func (r *ScorecardResult) AsString(writer io.Writer,
checkDocs docChecks.Doc, o AsStringResultOption,
) error {
func (r *ScorecardResult) AsString(writer io.Writer, checkDocs docChecks.Doc, opt *AsStringResultOption) error {
if opt == nil {
opt = &AsStringResultOption{
LogLevel: log.DefaultLevel,
Details: false,
Annotations: false,
}
}
data := make([][]string, len(r.Checks))
for i, row := range r.Checks {
@ -201,14 +207,14 @@ func (r *ScorecardResult) AsString(writer io.Writer,
doc := cdoc.GetDocumentationURL(r.Scorecard.CommitSHA)
x = append(x, row.Name, row.Reason)
if o.Details {
details, show := detailsToString(row.Details, o.LogLevel)
if opt.Details {
details, show := detailsToString(row.Details, opt.LogLevel)
if show {
x = append(x, details)
}
}
x = append(x, doc)
if o.Annotations {
if opt.Annotations {
_, reasons := row.IsExempted(r.Config)
x = append(x, strings.Join(reasons, "\n"))
}
@ -228,11 +234,11 @@ func (r *ScorecardResult) AsString(writer io.Writer,
table := tablewriter.NewWriter(writer)
header := []string{"Score", "Name", "Reason"}
if o.Details {
if opt.Details {
header = append(header, "Details")
}
header = append(header, "Documentation/Remediation")
if o.Annotations {
if opt.Annotations {
header = append(header, "Annotation")
}
table.SetHeader(header)