AsJSON2 exports results as JSON for the cron job and in the new detail format.
(r *scorecard.Result, showDetails bool, logLevel log.Level, checkDocs docs.Doc, writer io.Writer, )
| 117 | |
| 118 | // AsJSON2 exports results as JSON for the cron job and in the new detail format. |
| 119 | func AsJSON2(r *scorecard.Result, showDetails bool, |
| 120 | logLevel log.Level, checkDocs docs.Doc, writer io.Writer, |
| 121 | ) error { |
| 122 | score, err := r.GetAggregateScore(checkDocs) |
| 123 | if err != nil { |
| 124 | //nolint:wrapcheck |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | encoder := json.NewEncoder(writer) |
| 129 | |
| 130 | out := jsonScorecardResultV2{ |
| 131 | Repo: jsonRepoV2{ |
| 132 | Name: r.Repo.Name, |
| 133 | Commit: r.Repo.CommitSHA, |
| 134 | }, |
| 135 | Scorecard: jsonScorecardV2{ |
| 136 | Version: r.Scorecard.Version, |
| 137 | Commit: r.Scorecard.CommitSHA, |
| 138 | }, |
| 139 | Date: r.Date.Format("2006-01-02"), |
| 140 | Metadata: r.Metadata, |
| 141 | AggregateScore: jsonFloatScore(score), |
| 142 | } |
| 143 | |
| 144 | for _, checkResult := range r.Checks { |
| 145 | doc, e := checkDocs.GetCheck(checkResult.Name) |
| 146 | if e != nil { |
| 147 | return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetCheck: %s: %v", checkResult.Name, e)) |
| 148 | } |
| 149 | |
| 150 | tmpResult := jsonCheckResultV2{ |
| 151 | Name: checkResult.Name, |
| 152 | Doc: jsonCheckDocumentationV2{ |
| 153 | URL: doc.GetDocumentationURL(r.Scorecard.CommitSHA), |
| 154 | Short: doc.GetShort(), |
| 155 | }, |
| 156 | Reason: checkResult.Reason, |
| 157 | Score: checkResult.Score, |
| 158 | } |
| 159 | if showDetails { |
| 160 | for i := range checkResult.Details { |
| 161 | d := checkResult.Details[i] |
| 162 | m := scorecard.DetailToString(&d, logLevel) |
| 163 | if m == "" { |
| 164 | continue |
| 165 | } |
| 166 | tmpResult.Details = append(tmpResult.Details, m) |
| 167 | } |
| 168 | } |
| 169 | out.Checks = append(out.Checks, tmpResult) |
| 170 | } |
| 171 | if err := encoder.Encode(out); err != nil { |
| 172 | return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err)) |
| 173 | } |
| 174 | |
| 175 | return nil |
| 176 | } |