mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-11 08:55:27 +03:00
⚠️ 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:
parent
f591fbb551
commit
20ec42c2b5
@ -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,
|
||||
}
|
||||
|
23
pkg/json.go
23
pkg/json.go
@ -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")
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user