mirror of
https://github.com/ossf/scorecard.git
synced 2024-10-26 10:28:10 +03:00
🌱 maintainer annotations: search for config (#4152)
* search for annotation file Signed-off-by: Raghav Kaul <raghavkaul+github@google.com> * search for config file Signed-off-by: Raghav Kaul <raghavkaul+github@google.com> * address cr: logging + tests Signed-off-by: Raghav Kaul <raghavkaul+github@google.com> --------- Signed-off-by: Raghav Kaul <raghavkaul+github@google.com>
This commit is contained in:
parent
91532e12d1
commit
f591fbb551
@ -19,6 +19,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -168,19 +169,19 @@ func runScorecard(ctx context.Context,
|
||||
go runEnabledChecks(ctx, repo, request, checksToRun, resultsCh)
|
||||
|
||||
if os.Getenv(options.EnvVarScorecardExperimental) == "1" {
|
||||
// Get configuration
|
||||
rc, err := repoClient.GetFileReader("scorecard.yml")
|
||||
// If configuration file exists, continue. Otherwise, ignore
|
||||
if err == nil {
|
||||
defer rc.Close()
|
||||
r, path := findConfigFile(repoClient)
|
||||
logger := sclog.NewLogger(sclog.DefaultLevel)
|
||||
|
||||
if r != nil {
|
||||
defer r.Close()
|
||||
logger.Info(fmt.Sprintf("using maintainer annotations: %s", path))
|
||||
checks := []string{}
|
||||
for check := range checksToRun {
|
||||
checks = append(checks, check)
|
||||
}
|
||||
c, err := config.Parse(rc, checks)
|
||||
c, err := config.Parse(r, checks)
|
||||
if err != nil {
|
||||
logger := sclog.NewLogger(sclog.DefaultLevel)
|
||||
logger.Error(err, "parsing configuration file")
|
||||
logger.Info(fmt.Sprintf("couldn't parse maintainer annotations: %v", err))
|
||||
}
|
||||
ret.Config = c
|
||||
}
|
||||
@ -193,6 +194,21 @@ func runScorecard(ctx context.Context,
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func findConfigFile(rc clients.RepoClient) (io.ReadCloser, string) {
|
||||
// Look for a config file. Return first one regardless of validity
|
||||
locs := []string{"scorecard.yml", ".scorecard.yml", ".github/scorecard.yml"}
|
||||
|
||||
for i := range locs {
|
||||
cfr, err := rc.GetFileReader(locs[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return cfr, locs[i]
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func runEnabledProbes(request *checker.CheckRequest,
|
||||
probesToRun []string,
|
||||
ret *ScorecardResult,
|
||||
|
@ -15,6 +15,11 @@ package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
@ -341,3 +346,74 @@ func TestExperimentalRunProbes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findConfigFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
//nolint:govet
|
||||
tests := []struct {
|
||||
locs []string
|
||||
desc string
|
||||
found string
|
||||
wantFound bool
|
||||
}{
|
||||
{
|
||||
desc: "scorecard.yml exists",
|
||||
locs: []string{"scorecard.yml"},
|
||||
found: "scorecard.yml",
|
||||
wantFound: true,
|
||||
},
|
||||
{
|
||||
desc: ".scorecard.yml exists",
|
||||
locs: []string{".scorecard.yml"},
|
||||
found: ".scorecard.yml",
|
||||
wantFound: true,
|
||||
},
|
||||
{
|
||||
desc: ".github/scorecard.yml exists",
|
||||
locs: []string{".github/scorecard.yml"},
|
||||
found: ".github/scorecard.yml",
|
||||
wantFound: true,
|
||||
},
|
||||
{
|
||||
desc: "multiple configs exist",
|
||||
locs: []string{"scorecard.yml", ".github/scorecard.yml"},
|
||||
found: "scorecard.yml",
|
||||
wantFound: true,
|
||||
},
|
||||
{
|
||||
desc: "no config exists so shouldn't find one",
|
||||
locs: []string{},
|
||||
wantFound: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctrl := gomock.NewController(t)
|
||||
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
|
||||
mockRepoClient.EXPECT().GetFileReader(gomock.Any()).AnyTimes().DoAndReturn(func(filename string) (io.ReadCloser, error) {
|
||||
if !slices.Contains(tt.locs, filename) {
|
||||
return nil, fmt.Errorf("os.Open: %s", filename)
|
||||
}
|
||||
fullPath := filepath.Join("./testdata", filename)
|
||||
f, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("os.Open: %w", err)
|
||||
}
|
||||
return f, nil
|
||||
})
|
||||
r, path := findConfigFile(mockRepoClient)
|
||||
|
||||
if tt.found != "" && tt.found != path {
|
||||
t.Errorf("expected config file %+v got %+v", tt.found, path)
|
||||
}
|
||||
|
||||
if tt.wantFound != (r != nil) {
|
||||
t.Errorf("wantFound: %+v got %+v", tt.wantFound, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
5
pkg/testdata/.github/scorecard.yml
vendored
Normal file
5
pkg/testdata/.github/scorecard.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
annotations:
|
||||
- checks:
|
||||
- binary-artifacts
|
||||
reasons:
|
||||
- reason: test-data
|
5
pkg/testdata/.scorecard.yml
vendored
Normal file
5
pkg/testdata/.scorecard.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
annotations:
|
||||
- checks:
|
||||
- binary-artifacts
|
||||
reasons:
|
||||
- reason: test-data
|
5
pkg/testdata/scorecard.yml
vendored
Normal file
5
pkg/testdata/scorecard.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
annotations:
|
||||
- checks:
|
||||
- binary-artifacts
|
||||
reasons:
|
||||
- reason: test-data
|
Loading…
Reference in New Issue
Block a user