Skip to content

Commit

Permalink
add input for specifying --file-mode
Browse files Browse the repository at this point in the history
ossf/scorecard#4474

Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Feb 12, 2025
1 parent 9165624 commit 3f0895b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ First, [create a new file](https://docs.github.com/en/repositories/working-with-

| Name | Required | Description |
| ----- | -------- | ----------- |
| `result_file` | yes | The file that contains the results. |
| `result_format` | yes | The format in which to store the results [json \| sarif]. For GitHub's scanning dashboard, select `sarif`. |
| `results_file` | yes | The file that contains the results. |
| `results_format` | yes | The format in which to store the results [json \| sarif]. For GitHub's scanning dashboard, select `sarif`. |
| `repo_token` | no | PAT token with repository read access. Follow [these steps](/docs/authentication/fine-grained-auth-token.md) to create it. |
| `publish_results` | recommended | This will allow you to display a badge on your repository to show off your hard work. See details [here](#publishing-results).|
| `file_mode` | no | The method to fetch files from the repository: `archive` or `git` (default `archive`).

### Publishing Results
The Scorecard team runs a weekly scan of public GitHub repositories in order to track
Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ inputs:
required: false
default: false

file_mode:
description: "INPUT: Method to fetch files from GitHub"
required: false
default: archive

internal_publish_base_url:
description: "INPUT: Base URL for publishing results. Used for testing."
required: false
Expand Down
7 changes: 6 additions & 1 deletion internal/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/ossf/scorecard-action/options"
"github.com/ossf/scorecard/v5/clients"
Expand All @@ -35,7 +36,11 @@ func Run(opts *options.Options) (scorecard.Result, error) {
return scorecard.Result{}, fmt.Errorf("unable to create repo: %w", err)
}

result, err := scorecard.Run(context.Background(), repo)
var scOpts []scorecard.Option
if strings.EqualFold(opts.InputFileMode, "git") {
scOpts = append(scOpts, scorecard.WithFileModeGit())
}
result, err := scorecard.Run(context.Background(), repo, scOpts...)
if err != nil && !errors.Is(err, sce.ErrCheckRuntime) {
return scorecard.Result{}, fmt.Errorf("scorecard had an error: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions options/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
EnvInputResultsFile = "INPUT_RESULTS_FILE"
EnvInputResultsFormat = "INPUT_RESULTS_FORMAT"
EnvInputPublishResults = "INPUT_PUBLISH_RESULTS"
EnvInputFileMode = "INPUT_FILE_MODE"
EnvInputInternalPublishBaseURL = "INPUT_INTERNAL_PUBLISH_BASE_URL"
)

Expand Down
4 changes: 4 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Options struct {
// Input parameters
InputResultsFile string `env:"INPUT_RESULTS_FILE"`
InputResultsFormat string `env:"INPUT_RESULTS_FORMAT"`
InputFileMode string `env:"INPUT_FILE_MODE"`

PublishResults bool
}
Expand Down Expand Up @@ -199,6 +200,9 @@ func (o *Options) setScorecardOpts() {
if o.ScorecardOpts.ResultsFile == "" {
o.ScorecardOpts.ResultsFile = o.InputResultsFile
}

// --file-mode=
o.ScorecardOpts.FileMode = o.InputFileMode
}

// setPublishResults sets whether results should be published based on a
Expand Down
13 changes: 12 additions & 1 deletion options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//nolint
// nolint
package options

import (
Expand Down Expand Up @@ -58,6 +58,7 @@ func TestNew(t *testing.T) {
resultsFile string
resultsFormat string
publishResults string
fileMode string
want fields
unsetResultsPath bool
unsetToken bool
Expand All @@ -71,6 +72,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand All @@ -91,6 +93,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "json",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: options.FormatJSON,
Expand All @@ -110,6 +113,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "json",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: options.FormatJSON,
Expand All @@ -129,6 +133,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "json",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: options.FormatJSON,
Expand All @@ -148,6 +153,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand All @@ -166,6 +172,7 @@ func TestNew(t *testing.T) {
githubEventPath: githubEventPathNonFork,
githubEventName: pushEvent,
githubRef: "refs/heads/main",
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand All @@ -183,6 +190,7 @@ func TestNew(t *testing.T) {
githubEventName: pushEvent,
githubRef: "refs/heads/main",
resultsFile: "",
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand All @@ -202,6 +210,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
fileMode: options.FileModeArchive,
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand Down Expand Up @@ -243,6 +252,8 @@ func TestNew(t *testing.T) {
os.Setenv(EnvInputResultsFormat, tt.resultsFormat)
defer os.Unsetenv(EnvInputResultsFormat)

t.Setenv(EnvInputFileMode, tt.fileMode)

if tt.unsetResultsPath {
os.Unsetenv(EnvInputResultsFile)
} else {
Expand Down

0 comments on commit 3f0895b

Please sign in to comment.