-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Scorecard library entrypoint instead of Cobra hooking (#1423)
* use new Scorecard entrypoint Scorecard V5 released a new entrypoint, so make use of it instead of hooking into the underlying Cobra CLI. This gives us more flexibility when running Scorecard, such as writing the result to multiple formats. Signed-off-by: Spencer Schrock <sschrock@google.com> * add basic format tests Signed-off-by: Spencer Schrock <sschrock@google.com> * run go mod tidy to cleanup removed transitive deps Signed-off-by: Spencer Schrock <sschrock@google.com> * Apply suggestions from code review Signed-off-by: Stephen Augustus <justaugustus@users.noreply.github.com> --------- Signed-off-by: Spencer Schrock <sschrock@google.com> Signed-off-by: Stephen Augustus <justaugustus@users.noreply.github.com> Co-authored-by: Stephen Augustus <justaugustus@users.noreply.github.com>
- Loading branch information
1 parent
bf01931
commit d10a2ed
Showing
8 changed files
with
264 additions
and
189 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2024 OpenSSF 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 scorecard | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ossf/scorecard-action/options" | ||
"github.com/ossf/scorecard/v5/docs/checks" | ||
sclog "github.com/ossf/scorecard/v5/log" | ||
"github.com/ossf/scorecard/v5/pkg/scorecard" | ||
"github.com/ossf/scorecard/v5/policy" | ||
) | ||
|
||
const ( | ||
defaultScorecardPolicyFile = "/policy.yml" | ||
) | ||
|
||
var ( | ||
errNoResult = errors.New("must provide a result") | ||
errUnknownFormat = errors.New("unknown result format") | ||
) | ||
|
||
// Format provides a wrapper around the Scorecard library's various formatting functions, | ||
// converting our options into theirs. | ||
func Format(result *scorecard.Result, opts *options.Options) error { | ||
if result == nil { | ||
return errNoResult | ||
} | ||
|
||
// write results to both stdout and result file | ||
resultFile, err := os.Create(opts.GithubWorkspace + opts.InputResultsFile) | ||
if err != nil { | ||
return fmt.Errorf("creating result file: %w", err) | ||
} | ||
defer resultFile.Close() | ||
writer := io.MultiWriter(resultFile, os.Stdout) | ||
|
||
docs, err := checks.Read() | ||
if err != nil { | ||
return fmt.Errorf("read check docs: %w", err) | ||
} | ||
|
||
switch strings.ToLower(opts.InputResultsFormat) { | ||
// sarif is considered the default format when unset | ||
case "", "sarif": | ||
if opts.ScorecardOpts.PolicyFile == "" { | ||
opts.ScorecardOpts.PolicyFile = defaultScorecardPolicyFile | ||
} | ||
pol, err := policy.ParseFromFile(opts.ScorecardOpts.PolicyFile) | ||
if err != nil { | ||
return fmt.Errorf("parse policy file: %w", err) | ||
} | ||
err = result.AsSARIF(true, sclog.DefaultLevel, writer, docs, pol, opts.ScorecardOpts) | ||
if err != nil { | ||
return fmt.Errorf("format as sarif: %w", err) | ||
} | ||
case "json": | ||
err = result.AsJSON2(writer, docs, &scorecard.AsJSON2ResultOption{ | ||
Details: true, | ||
Annotations: false, // TODO | ||
LogLevel: sclog.DefaultLevel, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("format as JSON: %w", err) | ||
} | ||
default: | ||
return errUnknownFormat | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024 OpenSSF 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 scorecard | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ossf/scorecard-action/options" | ||
scopts "github.com/ossf/scorecard/v5/options" | ||
"github.com/ossf/scorecard/v5/pkg/scorecard" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name, format string | ||
pattern []byte | ||
}{ | ||
{ | ||
name: "default is sarif", | ||
format: "", | ||
pattern: []byte("sarif-schema"), | ||
}, | ||
{ | ||
name: "sarif format supported", | ||
format: "sarif", | ||
pattern: []byte("sarif-schema"), | ||
}, | ||
{ | ||
name: "json format supported", | ||
format: "json", | ||
// This isn't quite as strong of a guarantee, but dont expect this to change | ||
pattern: []byte(`"name":"github.com/foo/bar"`), | ||
}, | ||
{ | ||
name: "format is case insensitive", | ||
format: "SARIF", | ||
pattern: []byte("sarif-schema"), | ||
}, | ||
} | ||
result := scorecard.Result{ | ||
Repo: scorecard.RepoInfo{ | ||
Name: "github.com/foo/bar", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
opts := options.Options{ | ||
InputResultsFile: t.TempDir() + "/results", | ||
InputResultsFormat: tt.format, | ||
ScorecardOpts: &scopts.Options{ | ||
PolicyFile: "../../policies/template.yml", | ||
}, | ||
} | ||
err := Format(&result, &opts) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
contents, err := os.ReadFile(opts.InputResultsFile) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if !bytes.Contains(contents, tt.pattern) { | ||
t.Errorf("Output didn't match expected pattern (%s)", tt.pattern) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.