Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Make all ScorecardResult format options pointers #4151

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/internal/scdiff/app/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@
}

// 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,
}
}

Check warning on line 139 in pkg/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/json.go#L134-L139

Added lines #L134 - L139 were not covered by tests
score, err := r.GetAggregateScore(checkDocs)
if err != nil {
return err
Expand Down Expand Up @@ -170,17 +175,17 @@
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
Expand Down Expand Up @@ -243,9 +248,3 @@

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")
}
2 changes: 1 addition & 1 deletion pkg/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 16 additions & 10 deletions pkg/scorecard_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

switch opts.Format {
case options.FormatDefault:
o := AsStringResultOption{
o := &AsStringResultOption{
Details: opts.ShowDetails,
Annotations: opts.ShowAnnotations,
LogLevel: log.ParseLevel(opts.LogLevel),
Expand All @@ -150,7 +150,7 @@
// 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),
Expand Down Expand Up @@ -179,9 +179,15 @@
}

// 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,
}
}

Check warning on line 189 in pkg/scorecard_result.go

View check run for this annotation

Codecov / codecov/patch

pkg/scorecard_result.go#L184-L189

Added lines #L184 - L189 were not covered by tests

data := make([][]string, len(r.Checks))

for i, row := range r.Checks {
Expand All @@ -201,14 +207,14 @@

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"))
}
Expand All @@ -228,11 +234,11 @@

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)
Expand Down
Loading