Skip to content

Commit f470ef7

Browse files
Get the Golang code in sync with Bash (#489)
Co-authored-by: Azeem Shaikh <[email protected]>
1 parent 1ca6c49 commit f470ef7

4 files changed

Lines changed: 355 additions & 129 deletions

File tree

‎entrypoint/entrypoint.go‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ package entrypoint
1717
import (
1818
"errors"
1919
"fmt"
20+
"io"
2021
"os"
2122

2223
"github.com/spf13/cobra"
2324

2425
"github.com/ossf/scorecard-action/options"
25-
"github.com/ossf/scorecard/v4/cmd"
26+
sccmd "github.com/ossf/scorecard/v4/cmd"
2627
scopts "github.com/ossf/scorecard/v4/options"
2728
)
2829

@@ -33,22 +34,20 @@ func New() (*cobra.Command, error) {
3334
if err != nil {
3435
return nil, fmt.Errorf("creating new options: %w", err)
3536
}
36-
37-
if err := opts.Initialize(); err != nil {
38-
return nil, fmt.Errorf("initializing options: %w", err)
37+
if err := opts.Validate(); err != nil {
38+
return nil, fmt.Errorf("validating options: %w", err)
3939
}
40+
opts.Print()
4041

42+
// Adapt Scorecard CMD.
4143
scOpts := opts.ScorecardOpts
42-
43-
actionCmd := cmd.New(scOpts)
44-
44+
actionCmd := sccmd.New(scOpts)
4545
actionCmd.Flags().StringVar(
4646
&scOpts.ResultsFile,
4747
"output-file",
4848
scOpts.ResultsFile,
4949
"path to output results to",
5050
)
51-
5251
actionCmd.Flags().BoolVar(
5352
&opts.PublishResults,
5453
"publish",
@@ -60,11 +59,6 @@ func New() (*cobra.Command, error) {
6059
// TODO(scorecard): Move this into scorecard
6160
var out, stdout *os.File
6261
actionCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
63-
err := scOpts.Validate()
64-
if err != nil {
65-
return fmt.Errorf("validating options: %w", err)
66-
}
67-
6862
// TODO: the results file should be completed and validated by the time we get it.
6963
if scOpts.ResultsFile != "" {
7064
var err error
@@ -81,12 +75,15 @@ func New() (*cobra.Command, error) {
8175
os.Stdout = out
8276
actionCmd.SetOut(out)
8377
}
84-
8578
return nil
8679
}
8780

8881
actionCmd.PersistentPostRun = func(cmd *cobra.Command, args []string) {
8982
if out != nil {
83+
if _, err = out.Seek(0, io.SeekStart); err == nil {
84+
// nolint:errcheck
85+
_, _ = io.Copy(stdout, out)
86+
}
9087
_ = out.Close()
9188
}
9289
os.Stdout = stdout

‎options/options.go‎

Lines changed: 87 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,25 @@ import (
2626
"github.com/caarlos0/env/v6"
2727

2828
"github.com/ossf/scorecard-action/github"
29+
"github.com/ossf/scorecard/v4/checks"
2930
scopts "github.com/ossf/scorecard/v4/options"
3031
)
3132

33+
const (
34+
defaultScorecardPolicyFile = "/policy.yml"
35+
trueStr = "true"
36+
formatSarif = scopts.FormatSarif
37+
38+
pullRequestEvent = "pull_request"
39+
pushEvent = "push"
40+
branchProtectionEvent = "branch_protection_rule"
41+
)
42+
3243
var (
3344
// Errors.
3445
errGithubEventPathEmpty = errors.New("GitHub event path is empty")
3546
errResultsPathEmpty = errors.New("results path is empty")
3647
errOnlyDefaultBranchSupported = errors.New("only default branch is supported")
37-
38-
trueStr = "true"
3948
)
4049

4150
// Options are options for running scorecard via GitHub Actions.
@@ -72,99 +81,20 @@ type Options struct {
7281
PublishResults bool
7382
}
7483

75-
const (
76-
defaultScorecardPolicyFile = "/policy.yml"
77-
formatSarif = scopts.FormatSarif
78-
)
79-
8084
// New creates a new options set for running scorecard via GitHub Actions.
8185
func New() (*Options, error) {
82-
// Enable scorecard command to use SARIF format.
83-
os.Setenv(scopts.EnvVarEnableSarif, trueStr)
84-
85-
opts := &Options{
86-
ScorecardOpts: scopts.New(),
87-
}
86+
opts := &Options{}
8887
if err := env.Parse(opts); err != nil {
8988
return opts, fmt.Errorf("parsing entrypoint env vars: %w", err)
9089
}
91-
92-
opts.ScorecardOpts.ShowDetails = true
93-
// This section restores functionality that was removed in
94-
// https://github.com/ossf/scorecard/pull/1898.
95-
// TODO(options): Consider moving this to its own function.
96-
opts.ScorecardOpts.Repo = opts.GithubRepository
97-
98-
if err := opts.Initialize(); err != nil {
99-
return opts, fmt.Errorf(
100-
"initializing scorecard-action options: %w",
101-
err,
102-
)
103-
}
104-
105-
// TODO(options): Move this set-or-default logic to its own function.
106-
opts.ScorecardOpts.Format = formatSarif
107-
opts.ScorecardOpts.EnableSarif = true
108-
if opts.InputResultsFormat != "" {
109-
opts.ScorecardOpts.Format = opts.InputResultsFormat
110-
}
111-
112-
if opts.ScorecardOpts.Format == formatSarif {
113-
if opts.ScorecardOpts.PolicyFile == "" {
114-
// TODO(policy): Should we default or error here?
115-
opts.ScorecardOpts.PolicyFile = defaultScorecardPolicyFile
116-
}
117-
}
118-
119-
// TODO(scorecard): Reset commit options. Fix this in scorecard.
120-
opts.ScorecardOpts.Commit = scopts.DefaultCommit
121-
122-
if err := opts.ScorecardOpts.Validate(); err != nil {
123-
return opts, fmt.Errorf("validating scorecard options: %w", err)
124-
}
125-
126-
opts.SetPublishResults()
127-
128-
if opts.ScorecardOpts.ResultsFile == "" {
129-
opts.ScorecardOpts.ResultsFile = opts.InputResultsFile
130-
}
131-
132-
if opts.ScorecardOpts.ResultsFile == "" {
133-
// TODO(test): Reassess test case for this code path
134-
return opts, errResultsPathEmpty
90+
if err := opts.setRepoInfo(); err != nil {
91+
return opts, fmt.Errorf("parsing repo info: %w", err)
13592
}
136-
137-
if err := opts.Validate(); err != nil {
138-
return opts, fmt.Errorf("validating scorecard-action options: %w", err)
139-
}
140-
93+
opts.setScorecardOpts()
94+
opts.setPublishResults()
14195
return opts, nil
14296
}
14397

144-
// Initialize initializes the environment variables required for the action.
145-
func (o *Options) Initialize() error {
146-
/*
147-
https://docs.github.com/en/actions/learn-github-actions/environment-variables
148-
GITHUB_EVENT_PATH contains the json file for the event.
149-
GITHUB_SHA contains the commit hash.
150-
GITHUB_WORKSPACE contains the repo folder.
151-
GITHUB_EVENT_NAME contains the event name.
152-
GITHUB_ACTIONS is true in GitHub env.
153-
*/
154-
155-
// TODO(checks): Do we actually expect to use these?
156-
// o.EnableLicense = "1"
157-
// o.EnableDangerousWorkflow = "1"
158-
159-
_, tokenSet := os.LookupEnv(EnvGithubAuthToken)
160-
if !tokenSet {
161-
inputToken := os.Getenv(EnvInputRepoToken)
162-
os.Setenv(EnvGithubAuthToken, inputToken)
163-
}
164-
165-
return o.SetRepoInfo()
166-
}
167-
16898
// Validate validates the scorecard configuration.
16999
func (o *Options) Validate() error {
170100
if os.Getenv(EnvGithubAuthToken) == "" {
@@ -180,14 +110,20 @@ func (o *Options) Validate() error {
180110
return errEmptyGitHubAuthToken
181111
}
182112

183-
if strings.Contains(o.GithubEventName, "pull_request") &&
184-
o.GithubRef != o.DefaultBranch {
113+
if !o.isPullRequestEvent() &&
114+
!o.isDefaultBranch() {
185115
fmt.Printf("%s not supported with %s event.\n", o.GithubRef, o.GithubEventName)
186116
fmt.Printf("Only the default branch %s is supported.\n", o.DefaultBranch)
187117

188118
return errOnlyDefaultBranchSupported
189119
}
190-
120+
if err := o.ScorecardOpts.Validate(); err != nil {
121+
return fmt.Errorf("validating scorecard options: %w", err)
122+
}
123+
if o.ScorecardOpts.ResultsFile == "" {
124+
// TODO(test): Reassess test case for this code path
125+
return errResultsPathEmpty
126+
}
191127
return nil
192128
}
193129

@@ -205,9 +141,59 @@ func (o *Options) Print() {
205141
fmt.Printf("Default branch: %s\n", o.DefaultBranch)
206142
}
207143

208-
// SetPublishResults sets whether results should be published based on a
144+
func (o *Options) setScorecardOpts() {
145+
o.ScorecardOpts = scopts.New()
146+
// GITHUB_AUTH_TOKEN
147+
_, tokenSet := os.LookupEnv(EnvGithubAuthToken)
148+
if !tokenSet {
149+
inputToken := os.Getenv(EnvInputRepoToken)
150+
os.Setenv(EnvGithubAuthToken, inputToken)
151+
}
152+
153+
// --repo= | --local
154+
// This section restores functionality that was removed in
155+
// https://github.com/ossf/scorecard/pull/1898.
156+
// TODO(options): Consider moving this to its own function.
157+
if !o.isPullRequestEvent() {
158+
o.ScorecardOpts.Repo = o.GithubRepository
159+
} else {
160+
o.ScorecardOpts.Local = "."
161+
}
162+
163+
// --format=
164+
// Enable scorecard command to use SARIF format (default format).
165+
os.Setenv(scopts.EnvVarEnableSarif, trueStr)
166+
o.ScorecardOpts.EnableSarif = true
167+
o.ScorecardOpts.Format = formatSarif
168+
if o.InputResultsFormat != "" {
169+
o.ScorecardOpts.Format = o.InputResultsFormat
170+
}
171+
if o.ScorecardOpts.Format == formatSarif && o.ScorecardOpts.PolicyFile == "" {
172+
// TODO(policy): Should we default or error here?
173+
o.ScorecardOpts.PolicyFile = defaultScorecardPolicyFile
174+
}
175+
176+
// --checks=
177+
if o.GithubEventName == branchProtectionEvent {
178+
o.ScorecardOpts.ChecksToRun = []string{checks.CheckBranchProtection}
179+
}
180+
181+
// --show-details
182+
o.ScorecardOpts.ShowDetails = true
183+
184+
// --commit=
185+
// TODO(scorecard): Reset commit options. Fix this in scorecard.
186+
o.ScorecardOpts.Commit = scopts.DefaultCommit
187+
188+
// --out-file=
189+
if o.ScorecardOpts.ResultsFile == "" {
190+
o.ScorecardOpts.ResultsFile = o.InputResultsFile
191+
}
192+
}
193+
194+
// setPublishResults sets whether results should be published based on a
209195
// repository's visibility.
210-
func (o *Options) SetPublishResults() {
196+
func (o *Options) setPublishResults() {
211197
privateRepo, err := strconv.ParseBool(o.PrivateRepoStr)
212198
if err != nil {
213199
// TODO(options): Consider making this an error.
@@ -221,11 +207,11 @@ func (o *Options) SetPublishResults() {
221207
o.PublishResults = o.PublishResults && !privateRepo
222208
}
223209

224-
// SetRepoInfo gets the path to the GitHub event and sets the
210+
// setRepoInfo gets the path to the GitHub event and sets the
225211
// SCORECARD_IS_FORK environment variable.
226212
// TODO(options): Check if this actually needs to be exported.
227213
// TODO(options): Choose a more accurate name for what this does.
228-
func (o *Options) SetRepoInfo() error {
214+
func (o *Options) setRepoInfo() error {
229215
eventPath := o.GithubEventPath
230216
if eventPath == "" {
231217
return errGithubEventPathEmpty
@@ -247,3 +233,11 @@ func (o *Options) SetRepoInfo() error {
247233

248234
return nil
249235
}
236+
237+
func (o *Options) isPullRequestEvent() bool {
238+
return strings.HasPrefix(o.GithubEventName, pullRequestEvent)
239+
}
240+
241+
func (o *Options) isDefaultBranch() bool {
242+
return o.GithubRef == fmt.Sprintf("refs/heads/%s", o.DefaultBranch)
243+
}

0 commit comments

Comments
 (0)