Skip to content

Commit

Permalink
🐛 Fix date cron issue (#914)
Browse files Browse the repository at this point in the history
* fix

* typo

* fix
  • Loading branch information
laurentsimon committed Aug 25, 2021
1 parent d8e49e0 commit e083f04
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
48 changes: 28 additions & 20 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ func textToMarkdown(s string) string {
return strings.ReplaceAll(s, "\n", " ")
}

func detailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
switch d.Msg.Version {
//nolint
case 3:
if d.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
return ""
}
switch {
case d.Msg.Path != "" && d.Msg.Offset != 0:
return fmt.Sprintf("%s: %s: %s:%d", typeToString(d.Type), d.Msg.Text, d.Msg.Path, d.Msg.Offset)
case d.Msg.Path != "" && d.Msg.Offset == 0:
return fmt.Sprintf("%s: %s: %s", typeToString(d.Type), d.Msg.Text, d.Msg.Path)
default:
return fmt.Sprintf("%s: %s", typeToString(d.Type), d.Msg.Text)
}
default:
if d.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
return ""
}
return fmt.Sprintf("%s: %s", typeToString(d.Type), d.Msg.Text)
}
}

func detailsToString(details []checker.CheckDetail, logLevel zapcore.Level) (string, bool) {
// UPGRADEv2: change to make([]string, len(details))
// followed by sa[i] = instead of append.
var sa []string
for _, v := range details {
switch v.Msg.Version {
//nolint
case 3:
if v.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
continue
}
switch {
case v.Msg.Path != "" && v.Msg.Offset != 0:
sa = append(sa, fmt.Sprintf("%s: %s: %s:%d", typeToString(v.Type), v.Msg.Text, v.Msg.Path, v.Msg.Offset))
case v.Msg.Path != "" && v.Msg.Offset == 0:
sa = append(sa, fmt.Sprintf("%s: %s: %s", typeToString(v.Type), v.Msg.Text, v.Msg.Path))
default:
sa = append(sa, fmt.Sprintf("%s: %s", typeToString(v.Type), v.Msg.Text))
}
default:
if v.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
continue
}
sa = append(sa, fmt.Sprintf("%s: %s", typeToString(v.Type), v.Msg.Text))
for i := range details {
v := details[i]
s := detailToString(&v, logLevel)
if s != "" {
sa = append(sa, s)
}
}
return strings.Join(sa, "\n"), len(sa) > 0
Expand Down
55 changes: 38 additions & 17 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ import (

"go.uber.org/zap/zapcore"

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

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

type jsonScorecardResult struct {
Repo string
Date string
Checks []jsonCheckResult
Metadata []string
}

//nolint
type jsonCheckResultV2 struct {
Details []string
Expand All @@ -41,31 +55,33 @@ type jsonScorecardResultV2 struct {
Metadata []string
}

// AsJSON outputs the result in JSON format with a newline at the end.
// If called on []ScorecardResult will create NDJson formatted output.
// UPGRADEv2: will be removed.
// AsJSON exports results as JSON for new detail format.
func (r *ScorecardResult) AsJSON(showDetails bool, logLevel zapcore.Level, writer io.Writer) error {
encoder := json.NewEncoder(writer)
if showDetails {
if err := encoder.Encode(r); err != nil {
//nolint:wrapcheck
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}
return nil
}
out := ScorecardResult{

out := jsonScorecardResult{
Repo: r.Repo,
Date: r.Date,
Date: r.Date.Format("2006-01-02"),
Metadata: r.Metadata,
}
// UPGRADEv2: remove nolint after ugrade.

//nolint
for _, checkResult := range r.Checks {
tmpResult := checker.CheckResult{
tmpResult := jsonCheckResult{
Name: checkResult.Name,
Pass: checkResult.Pass,
Confidence: checkResult.Confidence,
}
if showDetails {
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := 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 {
Expand Down Expand Up @@ -94,8 +110,13 @@ func (r *ScorecardResult) AsJSON2(showDetails bool, logLevel zapcore.Level, writ
Score: checkResult.Score,
}
if showDetails {
for _, d := range checkResult.Details2 {
tmpResult.Details = append(tmpResult.Details, d.Msg.Text)
for i := range checkResult.Details2 {
d := checkResult.Details2[i]
m := detailToString(&d, logLevel)
if m == "" {
continue
}
tmpResult.Details = append(tmpResult.Details, m)
}
}
out.Checks = append(out.Checks, tmpResult)
Expand Down

0 comments on commit e083f04

Please sign in to comment.