MCPcopy
hub / github.com/ossf/scorecard / AsJSON2

Function AsJSON2

cron/internal/format/json.go:119–176  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

117
118// AsJSON2 exports results as JSON for the cron job and in the new detail format.
119func 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}

Callers 2

processRequestFunction · 0.92
TestJSONOutputFunction · 0.85

Calls 7

DetailToStringFunction · 0.92
GetAggregateScoreMethod · 0.80
WithMessageMethod · 0.80
jsonFloatScoreTypeAlias · 0.70
GetCheckMethod · 0.65
GetDocumentationURLMethod · 0.65
GetShortMethod · 0.65

Tested by 1

TestJSONOutputFunction · 0.68