Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Bug fixing: Repo initialization in Dependencydiff #2070

Closed
wants to merge 67 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
d043b13
temp
aidenwang9867 Jul 8, 2022
5f95398
Update dependencies.go
aidenwang9867 Jul 8, 2022
c29a841
Update errors.go
aidenwang9867 Jul 8, 2022
572bef5
Update scorecard_results.go
aidenwang9867 Jul 8, 2022
dc50937
Update vulnerabilities.go
aidenwang9867 Jul 8, 2022
4e90206
save
aidenwang9867 Jul 8, 2022
1fee520
temp
aidenwang9867 Jul 9, 2022
0595cdf
temp
aidenwang9867 Jul 9, 2022
c5cb697
temp
aidenwang9867 Jul 9, 2022
7bc6911
temp
aidenwang9867 Jul 11, 2022
62fb494
temp
aidenwang9867 Jul 11, 2022
b6c3b2c
Merge branch 'ossf:main' into depdiff_p2
aidenwang9867 Jul 11, 2022
92a117e
temp
aidenwang9867 Jul 11, 2022
c49c7f4
temp
aidenwang9867 Jul 11, 2022
c733ba5
temp
aidenwang9867 Jul 11, 2022
1379082
temp
aidenwang9867 Jul 11, 2022
8a89984
temp
aidenwang9867 Jul 11, 2022
cdd1840
temp
aidenwang9867 Jul 11, 2022
0e1223d
temp
aidenwang9867 Jul 11, 2022
2ac26d7
temp
aidenwang9867 Jul 11, 2022
2b0ffed
temp
aidenwang9867 Jul 11, 2022
3fd0f77
Merge branch 'main' into depdiuff_p1_pr
aidenwang9867 Jul 11, 2022
5549e91
Merge branch 'ossf:main' into depdiff_p2
aidenwang9867 Jul 11, 2022
3faf9ed
tempsave:
aidenwang9867 Jul 12, 2022
f6049b8
Merge branch 'depdiff_p2' of https://github.com/aidenwang9867/scoreca…
aidenwang9867 Jul 12, 2022
6d24320
Merge branch 'ossf:main' into depdiff_p2
aidenwang9867 Jul 12, 2022
847b3e7
temp
aidenwang9867 Jul 12, 2022
5c64505
temp
aidenwang9867 Jul 12, 2022
af29bc9
Merge branch 'ossf:main' into depdiff_p2
aidenwang9867 Jul 12, 2022
ad9c056
temp
aidenwang9867 Jul 13, 2022
23a1745
temp
aidenwang9867 Jul 13, 2022
5fea8bd
temp0713-1
aidenwang9867 Jul 13, 2022
1c313bd
temp0713-2
aidenwang9867 Jul 13, 2022
5e9c33d
temp0713-3
aidenwang9867 Jul 13, 2022
cae5546
Merge branch 'main' into depdiff_p2
aidenwang9867 Jul 13, 2022
4da19cf
temp0713-4
aidenwang9867 Jul 13, 2022
26be711
temp0713-4
aidenwang9867 Jul 14, 2022
f3419b2
temp0713-5
aidenwang9867 Jul 14, 2022
70f81c2
temp0713-6
aidenwang9867 Jul 14, 2022
e3f4d87
temp0713-7
aidenwang9867 Jul 14, 2022
c9b8cc7
temp0713-8
aidenwang9867 Jul 14, 2022
aea729f
temp0713-9
aidenwang9867 Jul 14, 2022
fd3d7b1
temp0713-10
aidenwang9867 Jul 14, 2022
0e3cb52
temp0713-11
aidenwang9867 Jul 14, 2022
70a3894
temp0713-12
aidenwang9867 Jul 14, 2022
a9658df
1
aidenwang9867 Jul 14, 2022
e1e0653
temp
aidenwang9867 Jul 14, 2022
751d67d
temp
aidenwang9867 Jul 14, 2022
9de05d8
Merge branch 'main' into depdiff_p2
aidenwang9867 Jul 14, 2022
459cf97
temp
aidenwang9867 Jul 15, 2022
4e4e201
Merge branch 'depdiff_p2' of https://github.com/aidenwang9867/scoreca…
aidenwang9867 Jul 15, 2022
f243586
temp
aidenwang9867 Jul 15, 2022
0493379
temp
aidenwang9867 Jul 15, 2022
baae011
temp
aidenwang9867 Jul 15, 2022
1f42bf7
Merge branch 'main' into depdiff_p2
aidenwang9867 Jul 15, 2022
fc1e227
temp
aidenwang9867 Jul 16, 2022
a7547d4
temp
aidenwang9867 Jul 16, 2022
347d74d
save
aidenwang9867 Jul 18, 2022
4e5e7a4
Merge branch 'main' into depdiff_p2
aidenwang9867 Jul 18, 2022
e825159
save
aidenwang9867 Jul 18, 2022
a8f30d9
Merge branch 'depdiff_p2' of https://github.com/aidenwang9867/scoreca…
aidenwang9867 Jul 18, 2022
08fa625
save
aidenwang9867 Jul 18, 2022
3cb16cc
final_commit_before_merge
aidenwang9867 Jul 18, 2022
e2b13e0
Merge branch 'main' into depdiff_p2
aidenwang9867 Jul 18, 2022
155cfc6
temp
aidenwang9867 Jul 18, 2022
a776c2c
Merge branch 'main' into depdiff_p3
aidenwang9867 Jul 18, 2022
259fa98
temp
aidenwang9867 Jul 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions dependencydiff/dependencydiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package dependencydiff
import (
"context"
"fmt"
"path"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
Expand All @@ -36,7 +35,6 @@ type dependencydiffContext struct {
logger *log.Logger
ownerName, repoName, baseSHA, headSHA string
ctx context.Context
ghRepo clients.Repo
ghRepoClient clients.RepoClient
ossFuzzClient clients.RepoClient
vulnsClient clients.VulnerabilitiesClient
Expand Down Expand Up @@ -69,10 +67,8 @@ func GetDependencyDiffResults(
return nil, fmt.Errorf("error in fetchRawDependencyDiffData: %w", err)
}

// Initialize the repo and client(s) corresponding to the checks to run.
err = initRepoAndClientByChecks(&dCtx)
if err != nil {
return nil, fmt.Errorf("error in initRepoAndClientByChecks: %w", err)
return nil, fmt.Errorf("error in initClientByChecks: %w", err)
}
err = getScorecardCheckResults(&dCtx)
if err != nil {
Expand All @@ -81,20 +77,19 @@ func GetDependencyDiffResults(
return dCtx.results, nil
}

func initRepoAndClientByChecks(dCtx *dependencydiffContext) error {
repo, repoClient, ossFuzzClient, ciiClient, vulnsClient, err := checker.GetClients(
dCtx.ctx, path.Join(dCtx.ownerName, dCtx.repoName), "", dCtx.logger,
func initClientByChecks(dCtx *dependencydiffContext, dSrcRepo string) error {
_, repoClient, ossFuzzClient, ciiClient, vulnsClient, err := checker.GetClients(
dCtx.ctx, dSrcRepo, "", dCtx.logger,
)
if err != nil {
return fmt.Errorf("error creating the github repo: %w", err)
}
// If the caller doesn't specify the checks to run, run all checks and return all the clients.
if dCtx.checkNamesToRun == nil || len(dCtx.checkNamesToRun) == 0 {
dCtx.ghRepo, dCtx.ghRepoClient, dCtx.ossFuzzClient, dCtx.ciiClient, dCtx.vulnsClient =
repo, repoClient, ossFuzzClient, ciiClient, vulnsClient
dCtx.ghRepoClient, dCtx.ossFuzzClient, dCtx.ciiClient, dCtx.vulnsClient =
repoClient, ossFuzzClient, ciiClient, vulnsClient
return nil
}
dCtx.ghRepo = repo
dCtx.ghRepoClient = githubrepo.CreateGithubRepoClient(dCtx.ctx, dCtx.logger)
for _, cn := range dCtx.checkNamesToRun {
switch cn {
Expand Down Expand Up @@ -129,12 +124,22 @@ func getScorecardCheckResults(dCtx *dependencydiffContext) error {
// TODO (#2063): use the BigQuery dataset to supplement null source repo URLs to fetch the Scorecard results for them.
if d.SourceRepository != nil && *d.SourceRepository != "" {
if d.ChangeType != nil && (dCtx.changeTypesToCheck[*d.ChangeType] || dCtx.changeTypesToCheck == nil) {
// Initialize the repo and client(s) corresponding to the checks to run.
ghRepo, err := githubrepo.MakeGithubRepo(*d.SourceRepository)
err = initClientByChecks(dCtx, *d.SourceRepository)
if err != nil {
return fmt.Errorf("error getting github repo: %w", err)
}
err = initClientByChecks(dCtx, *d.SourceRepository)
if err != nil {
return fmt.Errorf("error initializing clients: %w", err)
}
// Run scorecard on those types of dependencies that the caller would like to check.
// If the input map changeTypesToCheck is empty, by default, we run checks for all valid types.
// TODO (#2064): use the Scorecare REST API to retrieve the Scorecard result statelessly.
scorecardResult, err := pkg.RunScorecards(
dCtx.ctx,
dCtx.ghRepo,
ghRepo,
// TODO (#2065): In future versions, ideally, this should be
// the commitSHA corresponding to d.Version instead of HEAD.
clients.HeadSHA,
Expand Down
36 changes: 15 additions & 21 deletions dependencydiff/dependencydiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,29 @@ func Test_fetchRawDependencyDiffData(t *testing.T) {
}
}

func Test_initRepoAndClientByChecks(t *testing.T) {
func Test_initClientByChecks(t *testing.T) {
t.Parallel()
//nolint
tests := []struct {
name string
dCtx dependencydiffContext
wantGhRepo, wantRepoClient, wantFuzzClient bool
wantVulnClient, wantCIIClient bool
wantErr bool
name string
dCtx dependencydiffContext
srcRepo string
wantRepoClient, wantFuzzClient bool
wantVulnClient, wantCIIClient bool
wantErr bool
}{
{
name: "error creating repo",
dCtx: dependencydiffContext{
logger: log.NewLogger(log.InfoLevel),
ctx: context.Background(),
ownerName: path.Join("host_not_exist.com", "owner_not_exist"),
repoName: "repo_not_exist",
checkNamesToRun: []string{},
},
wantGhRepo: false,
srcRepo: path.Join(
"host_not_exist.com",
"owner_not_exist",
"repo_not_exist",
),
wantRepoClient: false,
wantFuzzClient: false,
wantVulnClient: false,
Expand All @@ -99,13 +102,9 @@ func Test_initRepoAndClientByChecks(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := initRepoAndClientByChecks(&tt.dCtx)
err := initClientByChecks(&tt.dCtx, tt.srcRepo)
if (err != nil) != tt.wantErr {
t.Errorf("initRepoAndClientByChecks() error = {%v}, want error: %v", err, tt.wantErr)
return
}
if (tt.dCtx.ghRepo != nil) != tt.wantGhRepo {
t.Errorf("init repo error, wantGhRepo: %v, got %v", tt.wantGhRepo, tt.dCtx.ghRepo)
t.Errorf("initClientByChecks() error = {%v}, want error: %v", err, tt.wantErr)
return
}
if (tt.dCtx.ghRepoClient != nil) != tt.wantRepoClient {
Expand Down Expand Up @@ -151,12 +150,7 @@ func Test_getScorecardCheckResults(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := initRepoAndClientByChecks(&tt.dCtx)
if err != nil {
t.Errorf("init repo and client error")
return
}
err = getScorecardCheckResults(&tt.dCtx)
err := getScorecardCheckResults(&tt.dCtx)
if (err != nil) != tt.wantErr {
t.Errorf("getScorecardCheckResults() error = {%v}, want error: %v", err, tt.wantErr)
return
Expand Down