Skip to content

Commit 032e16d

Browse files
committed
Refactor some code
1 parent fae5ff3 commit 032e16d

File tree

6 files changed

+337
-284
lines changed

6 files changed

+337
-284
lines changed

clients/localdir/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ func TestClient_CreationAndCaching(t *testing.T) {
3838
{
3939
name: "invalid fullpath",
4040
outputFiles: []string{},
41-
inputFolder: "file:///invalid/fullpath",
41+
inputFolder: "invalid/fullpath",
4242
err: os.ErrNotExist,
4343
},
4444
{
4545
name: "invalid relative path",
4646
outputFiles: []string{},
47-
inputFolder: "file://invalid/relative/path",
47+
inputFolder: "invalid/relative/path",
4848
err: os.ErrNotExist,
4949
},
5050
{
5151
name: "repo 0",
5252
outputFiles: []string{
5353
"file0", "dir1/file1", "dir1/dir2/file2",
5454
},
55-
inputFolder: "file://testdata/repo0",
55+
inputFolder: "testdata/repo0",
5656
err: nil,
5757
},
5858
}

clients/localdir/repo.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ import (
2121
"fmt"
2222
"os"
2323
"path"
24-
"strings"
2524

2625
clients "github.com/ossf/scorecard/v4/clients"
2726
)
2827

29-
var (
30-
errNotDirectory = errors.New("not a directory")
31-
errInvalidURI = errors.New("invalid URI")
32-
)
33-
34-
var filePrefix = "file://"
28+
var errNotDirectory = errors.New("not a directory")
3529

3630
type repoLocal struct {
3731
path string
@@ -78,10 +72,7 @@ func (r *repoLocal) AppendMetadata(m ...string) {
7872

7973
// MakeLocalDirRepo returns an implementation of clients.Repo interface.
8074
func MakeLocalDirRepo(pathfn string) (clients.Repo, error) {
81-
if !strings.HasPrefix(pathfn, filePrefix) {
82-
return nil, fmt.Errorf("%w", errInvalidURI)
83-
}
84-
p := path.Clean(pathfn[len(filePrefix):])
75+
p := path.Clean(pathfn)
8576
repo := &repoLocal{
8677
path: p,
8778
}

cmd/flags.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 Security 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 cmd implements Scorecard commandline.
16+
package cmd
17+
18+
var (
19+
flagRepo string
20+
flagLocal string
21+
flagChecksToRun []string
22+
flagMetadata []string
23+
flagLogLevel string
24+
flagFormat string
25+
flagNPM string
26+
flagPyPI string
27+
flagRubyGems string
28+
flagShowDetails bool
29+
flagPolicyFile string
30+
)

cmd/package_managers.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright 2020 Security 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 cmd implements Scorecard commandline.
16+
package cmd
17+
18+
import (
19+
"encoding/json"
20+
"fmt"
21+
"net/http"
22+
"time"
23+
24+
sce "github.com/ossf/scorecard/v4/errors"
25+
)
26+
27+
type packageMangerResponse struct {
28+
associatedRepo string
29+
exists bool
30+
}
31+
32+
// TODO(#1568): Add unit tests for this.
33+
func fetchGitRepositoryFromPackageManagers(npm, pypi, rubygems string) (packageMangerResponse, error) {
34+
if npm != "" {
35+
gitRepo, err := fetchGitRepositoryFromNPM(npm)
36+
return packageMangerResponse{
37+
exists: true,
38+
associatedRepo: gitRepo,
39+
}, err
40+
}
41+
if pypi != "" {
42+
gitRepo, err := fetchGitRepositoryFromPYPI(pypi)
43+
return packageMangerResponse{
44+
exists: true,
45+
associatedRepo: gitRepo,
46+
}, err
47+
}
48+
if rubygems != "" {
49+
gitRepo, err := fetchGitRepositoryFromRubyGems(rubygems)
50+
return packageMangerResponse{
51+
exists: true,
52+
associatedRepo: gitRepo,
53+
}, err
54+
}
55+
56+
return packageMangerResponse{}, nil
57+
}
58+
59+
type npmSearchResults struct {
60+
Objects []struct {
61+
Package struct {
62+
Links struct {
63+
Repository string `json:"repository"`
64+
} `json:"links"`
65+
} `json:"package"`
66+
} `json:"objects"`
67+
}
68+
69+
type pypiSearchResults struct {
70+
Info struct {
71+
ProjectUrls struct {
72+
Source string `json:"Source"`
73+
} `json:"project_urls"`
74+
} `json:"info"`
75+
}
76+
77+
type rubyGemsSearchResults struct {
78+
SourceCodeURI string `json:"source_code_uri"`
79+
}
80+
81+
// Gets the GitHub repository URL for the npm package.
82+
// nolint: noctx
83+
func fetchGitRepositoryFromNPM(packageName string) (string, error) {
84+
npmSearchURL := "https://registry.npmjs.org/-/v1/search?text=%s&size=1"
85+
const timeout = 10
86+
client := &http.Client{
87+
Timeout: timeout * time.Second,
88+
}
89+
resp, err := client.Get(fmt.Sprintf(npmSearchURL, packageName))
90+
if err != nil {
91+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get npm package json: %v", err))
92+
}
93+
94+
defer resp.Body.Close()
95+
v := &npmSearchResults{}
96+
err = json.NewDecoder(resp.Body).Decode(v)
97+
if err != nil {
98+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse npm package json: %v", err))
99+
}
100+
if len(v.Objects) == 0 {
101+
return "", sce.WithMessage(sce.ErrScorecardInternal,
102+
fmt.Sprintf("could not find source repo for npm package: %s", packageName))
103+
}
104+
return v.Objects[0].Package.Links.Repository, nil
105+
}
106+
107+
// Gets the GitHub repository URL for the pypi package.
108+
// nolint: noctx
109+
func fetchGitRepositoryFromPYPI(packageName string) (string, error) {
110+
pypiSearchURL := "https://pypi.org/pypi/%s/json"
111+
const timeout = 10
112+
client := &http.Client{
113+
Timeout: timeout * time.Second,
114+
}
115+
resp, err := client.Get(fmt.Sprintf(pypiSearchURL, packageName))
116+
if err != nil {
117+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get pypi package json: %v", err))
118+
}
119+
120+
defer resp.Body.Close()
121+
v := &pypiSearchResults{}
122+
err = json.NewDecoder(resp.Body).Decode(v)
123+
if err != nil {
124+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse pypi package json: %v", err))
125+
}
126+
if v.Info.ProjectUrls.Source == "" {
127+
return "", sce.WithMessage(sce.ErrScorecardInternal,
128+
fmt.Sprintf("could not find source repo for pypi package: %s", packageName))
129+
}
130+
return v.Info.ProjectUrls.Source, nil
131+
}
132+
133+
// Gets the GitHub repository URL for the rubygems package.
134+
// nolint: noctx
135+
func fetchGitRepositoryFromRubyGems(packageName string) (string, error) {
136+
rubyGemsSearchURL := "https://rubygems.org/api/v1/gems/%s.json"
137+
const timeout = 10
138+
client := &http.Client{
139+
Timeout: timeout * time.Second,
140+
}
141+
resp, err := client.Get(fmt.Sprintf(rubyGemsSearchURL, packageName))
142+
if err != nil {
143+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get ruby gem json: %v", err))
144+
}
145+
146+
defer resp.Body.Close()
147+
v := &rubyGemsSearchResults{}
148+
err = json.NewDecoder(resp.Body).Decode(v)
149+
if err != nil {
150+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse ruby gem json: %v", err))
151+
}
152+
if v.SourceCodeURI == "" {
153+
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("could not find source repo for ruby gem: %v", err))
154+
}
155+
return v.SourceCodeURI, nil
156+
}

0 commit comments

Comments
 (0)