|
| 1 | +// Copyright 2023 OpenSSF Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package runner |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/ossf/scorecard/v4/checker" |
| 21 | + "github.com/ossf/scorecard/v4/checks" |
| 22 | + "github.com/ossf/scorecard/v4/clients" |
| 23 | + "github.com/ossf/scorecard/v4/clients/githubrepo" |
| 24 | + "github.com/ossf/scorecard/v4/clients/ossfuzz" |
| 25 | + "github.com/ossf/scorecard/v4/log" |
| 26 | + "github.com/ossf/scorecard/v4/pkg" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + commit = clients.HeadSHA |
| 31 | + commitDepth = 0 // default |
| 32 | +) |
| 33 | + |
| 34 | +// Runner holds the clients and configuration needed to run Scorecard on multiple repos. |
| 35 | +type Runner struct { |
| 36 | + ctx context.Context |
| 37 | + logger *log.Logger |
| 38 | + enabledChecks checker.CheckNameToFnMap |
| 39 | + repoClient clients.RepoClient |
| 40 | + ossFuzz clients.RepoClient |
| 41 | + cii clients.CIIBestPracticesClient |
| 42 | + vuln clients.VulnerabilitiesClient |
| 43 | +} |
| 44 | + |
| 45 | +func New() Runner { |
| 46 | + ctx := context.Background() |
| 47 | + logger := log.NewLogger(log.DefaultLevel) |
| 48 | + return Runner{ |
| 49 | + ctx: ctx, |
| 50 | + logger: logger, |
| 51 | + repoClient: githubrepo.CreateGithubRepoClient(ctx, logger), |
| 52 | + ossFuzz: ossfuzz.CreateOSSFuzzClient(ossfuzz.StatusURL), |
| 53 | + cii: clients.DefaultCIIBestPracticesClient(), |
| 54 | + vuln: clients.DefaultVulnerabilitiesClient(), |
| 55 | + enabledChecks: checks.GetAll(), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +//nolint:wrapcheck |
| 60 | +func (r *Runner) Run(repoURI string) (pkg.ScorecardResult, error) { |
| 61 | + r.log("processing repo: " + repoURI) |
| 62 | + // TODO (gitlab?) |
| 63 | + repo, err := githubrepo.MakeGithubRepo(repoURI) |
| 64 | + if err != nil { |
| 65 | + return pkg.ScorecardResult{}, err |
| 66 | + } |
| 67 | + return pkg.RunScorecard(r.ctx, repo, commit, commitDepth, r.enabledChecks, r.repoClient, r.ossFuzz, r.cii, r.vuln) |
| 68 | +} |
| 69 | + |
| 70 | +// logs only if logger is set. |
| 71 | +func (r *Runner) log(msg string) { |
| 72 | + if r.logger != nil { |
| 73 | + r.logger.Info(msg) |
| 74 | + } |
| 75 | +} |
0 commit comments