Skip to content

Commit

Permalink
✨ Decouple scorecard json from cron json (#941)
Browse files Browse the repository at this point in the history
* decouple

* linnter
  • Loading branch information
laurentsimon committed Aug 31, 2021
1 parent 001ba67 commit bb6e010
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 6 deletions.
135 changes: 135 additions & 0 deletions cron/worker/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2021 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main implements cron worker job.
package main

import (
"encoding/json"
"fmt"
"io"

// nolint:gosec
_ "net/http/pprof"

"go.uber.org/zap/zapcore"

sce "github.com/ossf/scorecard/v2/errors"
"github.com/ossf/scorecard/v2/pkg"
)

//nolint
type jsonCheckCronResult struct {
Name string
Details []string
Confidence int
Pass bool
}

type jsonScorecardCronResult struct {
Repo string
Date string
Checks []jsonCheckCronResult
Metadata []string
}

//nolint
type jsonCheckCronResultV2 struct {
Details []string
Score int
Reason string
Name string
}

type jsonScorecardCronResultV2 struct {
Repo string
Date string
Commit string
Checks []jsonCheckCronResultV2
Metadata []string
}

// AsJSON exports results as JSON for new detail format.
func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel zapcore.Level, writer io.Writer) error {
encoder := json.NewEncoder(writer)

out := jsonScorecardCronResult{
Repo: r.Repo,
Date: r.Date.Format("2006-01-02"),
Metadata: r.Metadata,
}

//nolint
for _, checkResult := range r.Checks {
tmpResult := jsonCheckCronResult{
Name: checkResult.Name,
Pass: checkResult.Pass,
Confidence: checkResult.Confidence,
}
if showDetails {
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := pkg.DetailToString(&d, logLevel)
if m == "" {
continue
}
tmpResult.Details = append(tmpResult.Details, m)
}
}
out.Checks = append(out.Checks, tmpResult)
}
if err := encoder.Encode(out); err != nil {
//nolint:wrapcheck
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}
return nil
}

// AsJSON2 exports results as JSON for the cron job and in the new detail format.
func AsJSON2(r *pkg.ScorecardResult, showDetails bool, logLevel zapcore.Level, writer io.Writer) error {
encoder := json.NewEncoder(writer)

out := jsonScorecardCronResultV2{
Repo: r.Repo,
Date: r.Date.Format("2006-01-02"),
Commit: r.CommitSHA,
Metadata: r.Metadata,
}

//nolint
for _, checkResult := range r.Checks {
tmpResult := jsonCheckCronResultV2{
Name: checkResult.Name,
Reason: checkResult.Reason,
Score: checkResult.Score,
}
if showDetails {
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := pkg.DetailToString(&d, logLevel)
if m == "" {
continue
}
tmpResult.Details = append(tmpResult.Details, m)
}
}
out.Checks = append(out.Checks, tmpResult)
}
if err := encoder.Encode(out); err != nil {
//nolint:wrapcheck
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}

return nil
}
4 changes: 2 additions & 2 deletions cron/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func processRequest(ctx context.Context,
log.Print(errorMsg)
}
result.Date = batchRequest.GetJobTime().AsTime()
if err := result.AsJSON(true /*showDetails*/, zapcore.InfoLevel, &buffer); err != nil {
if err := AsJSON(&result, true /*showDetails*/, zapcore.InfoLevel, &buffer); err != nil {
return fmt.Errorf("error during result.AsJSON: %w", err)
}

if err := result.AsJSON2(true /*showDetails*/, zapcore.InfoLevel, &buffer2); err != nil {
if err := AsJSON2(&result, true /*showDetails*/, zapcore.InfoLevel, &buffer2); err != nil {
return fmt.Errorf("error during result.AsJSON2: %w", err)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func textToMarkdown(s string) string {
return strings.ReplaceAll(s, "\n", " ")
}

func detailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
// DetailToString turns a detail information into a string.
func DetailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
// UPGRADEv3: remove swtch statement.
switch d.Msg.Version {
//nolint
Expand Down Expand Up @@ -61,7 +62,7 @@ func detailsToString(details []checker.CheckDetail, logLevel zapcore.Level) (str
var sa []string
for i := range details {
v := details[i]
s := detailToString(&v, logLevel)
s := DetailToString(&v, logLevel)
if s != "" {
sa = append(sa, s)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r *ScorecardResult) AsJSON(showDetails bool, logLevel zapcore.Level, write
if showDetails {
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := detailToString(&d, logLevel)
m := DetailToString(&d, logLevel)
if m == "" {
continue
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (r *ScorecardResult) AsJSON2(showDetails bool, logLevel zapcore.Level, writ
if showDetails {
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := detailToString(&d, logLevel)
m := DetailToString(&d, logLevel)
if m == "" {
continue
}
Expand Down

0 comments on commit bb6e010

Please sign in to comment.