-
Notifications
You must be signed in to change notification settings - Fork 70
/
entrypoint.go
142 lines (125 loc) · 3.55 KB
/
entrypoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2022 Security 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 entrypoint
import (
"errors"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/ossf/scorecard-action/options"
sccmd "github.com/ossf/scorecard/v4/cmd"
sce "github.com/ossf/scorecard/v4/errors"
scopts "github.com/ossf/scorecard/v4/options"
)
// New creates a new scorecard command which can be used as an entrypoint for
// GitHub Actions.
func New() (*cobra.Command, error) {
opts, err := options.New()
if err != nil {
return nil, fmt.Errorf("creating new options: %w", err)
}
if err := opts.Validate(); err != nil {
return nil, fmt.Errorf("validating options: %w", err)
}
opts.Print()
// Adapt Scorecard CMD.
scOpts := opts.ScorecardOpts
actionCmd := sccmd.New(scOpts)
actionCmd.Flags().StringVar(
&scOpts.ResultsFile,
"output-file",
scOpts.ResultsFile,
"path to output results to",
)
actionCmd.Flags().BoolVar(
&opts.PublishResults,
"publish",
opts.PublishResults,
"if set, results will be published (for public repositories only)",
)
// Adapt scorecard's PreRunE to support an output file
// TODO(scorecard): Move this into scorecard
var out, stdout *os.File
actionCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
// TODO: the results file should be completed and validated by the time we get it.
if scOpts.ResultsFile != "" {
var err error
resultsFilePath := fmt.Sprintf("%v/%v", opts.GithubWorkspace, scOpts.ResultsFile)
out, err = os.Create(resultsFilePath)
if err != nil {
return fmt.Errorf(
"creating output file (%s): %w",
resultsFilePath,
err,
)
}
stdout = os.Stdout
os.Stdout = out
actionCmd.SetOut(out)
}
return nil
}
// wrap scorecard's existing RunE, but ignore runtime errors that occur during checks
// users were having action failures due to secondary rate limits causing checks to fail
scorecardRunE := actionCmd.RunE
actionCmd.RunE = func(cmd *cobra.Command, args []string) error {
if err := scorecardRunE(cmd, args); err != nil && !errors.Is(err, sce.ErrorCheckRuntime) {
return err
}
return nil
}
actionCmd.PersistentPostRun = func(cmd *cobra.Command, args []string) {
if out != nil {
if _, err = out.Seek(0, io.SeekStart); err == nil {
//nolint:errcheck
_, _ = io.Copy(stdout, out)
}
_ = out.Close()
}
os.Stdout = stdout
}
var hideErrs []error
hiddenFlags := []string{
scopts.FlagNPM,
scopts.FlagPyPI,
scopts.FlagRubyGems,
}
for _, f := range hiddenFlags {
err := actionCmd.Flags().MarkHidden(f)
if err != nil {
hideErrs = append(hideErrs, err)
}
}
if len(hideErrs) > 0 {
return nil, fmt.Errorf(
"%w: %+v",
errHideFlags,
hideErrs,
)
}
// Add sub-commands.
actionCmd.AddCommand(printConfigCmd(opts))
return actionCmd, nil
}
func printConfigCmd(o *options.Options) *cobra.Command {
c := &cobra.Command{
Use: "print-config",
Run: func(cmd *cobra.Command, args []string) {
o.Print()
},
}
return c
}
var errHideFlags = errors.New("errors occurred while trying to hide scorecard flags")