Skip to content

Commit

Permalink
Merge pull request #18 from kyoshidajp/refactor_diagnose_func
Browse files Browse the repository at this point in the history
Refactor diagnose functions for scaling out other package managers
  • Loading branch information
kyoshidajp authored Oct 28, 2023
2 parents aad5a18 + 19af265 commit cb48098
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 389 deletions.
91 changes: 9 additions & 82 deletions cmd/bundler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package cmd

import (
"fmt"

parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/ruby/bundler"
"github.com/kyoshidajp/dep-doctor/cmd/github"
"golang.org/x/exp/slices"
"github.com/aquasecurity/go-dep-parser/pkg/types"
)

type BundlerDoctor struct {
Expand All @@ -16,84 +13,14 @@ func NewBundlerDoctor() *BundlerDoctor {
return &BundlerDoctor{}
}

func (d *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedParams := [][]github.FetchRepositoryParam{}
fetchRepositoryParams := d.FetchRepositoryParams(r)
sliceSize := len(fetchRepositoryParams)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedParams = append(slicedParams, fetchRepositoryParams[i:end])
}

for _, param := range slicedParams {
repos := github.FetchFromGitHub(param)
for _, r := range repos {
isIgnore := slices.Contains(ignores, r.Name)
diagnosis := Diagnosis{
Name: r.Name,
Url: r.Url,
Archived: r.Archived,
Ignored: isIgnore,
Diagnosed: true,
IsActive: r.IsActive(year),
}
diagnoses[r.Name] = diagnosis
}
}

for _, fetchRepositoryParam := range fetchRepositoryParams {
if fetchRepositoryParam.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: fetchRepositoryParam.PackageName,
Diagnosed: false,
}
diagnoses[fetchRepositoryParam.PackageName] = diagnosis
}
return diagnoses
}

func (d *BundlerDoctor) FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam {
var params []github.FetchRepositoryParam
func (d *BundlerDoctor) Deps(r parser_io.ReadSeekerAt) []types.Library {
p := &bundler.Parser{}
libs, _, _ := p.Parse(r)

rubyGems := RubyGems{}
for _, lib := range libs {
fmt.Printf("%s\n", lib.Name)

githubUrl, err := rubyGems.fetchURLFromRegistry(lib.Name)
if err != nil {
continue
}

repo, err := github.ParseGitHubUrl(githubUrl)
if err != nil {
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

params = append(params,
github.FetchRepositoryParam{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
CanSearch: true,
},
)
}
deps, _, _ := p.Parse(r)
return deps
}

return params
func (d *BundlerDoctor) SourceCodeURL(name string) (string, error) {
rubyGems := RubyGems{name: name}
url, err := rubyGems.fetchURLFromRegistry()
return url, err
}
96 changes: 81 additions & 15 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/aquasecurity/go-dep-parser/pkg/io"
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/fatih/color"
"github.com/kyoshidajp/dep-doctor/cmd/github"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

const MAX_YEAR_TO_BE_BLANK = 5

type Doctor interface {
Diagnose(r io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis
FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam
}

type Diagnosis struct {
Name string
Url string
Expand All @@ -31,18 +28,88 @@ type Diagnosis struct {
IsActive bool
}

type Department struct {
doctor Doctor
type MedicalTechnician interface {
Deps(r parser_io.ReadSeekerAt) []types.Library
SourceCodeURL(name string) (string, error)
}

func NewDepartment(d Doctor) *Department {
return &Department{
doctor: d,
func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) []github.FetchRepositoryParam {
var params []github.FetchRepositoryParam
for _, lib := range libs {
fmt.Printf("%s\n", lib.Name)

githubUrl, err := g.SourceCodeURL(lib.Name)
if err != nil {
continue
}

repo, err := github.ParseGitHubUrl(githubUrl)
if err != nil {
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

params = append(params,
github.FetchRepositoryParam{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
CanSearch: true,
},
)
}

return params
}

func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
return d.doctor.Diagnose(r, year, ignores)
func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedParams := [][]github.FetchRepositoryParam{}
deps := d.Deps(r)
fetchRepositoryParams := FetchRepositoryParams(deps, d)
sliceSize := len(fetchRepositoryParams)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedParams = append(slicedParams, fetchRepositoryParams[i:end])
}

for _, param := range slicedParams {
repos := github.FetchFromGitHub(param)
for _, r := range repos {
isIgnore := slices.Contains(ignores, r.Name)
diagnosis := Diagnosis{
Name: r.Name,
Url: r.Url,
Archived: r.Archived,
Ignored: isIgnore,
Diagnosed: true,
IsActive: r.IsActive(year),
}
diagnoses[r.Name] = diagnosis
}
}

for _, fetchRepositoryParam := range fetchRepositoryParams {
if fetchRepositoryParam.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: fetchRepositoryParam.PackageName,
Diagnosed: false,
}
diagnoses[fetchRepositoryParam.PackageName] = diagnosis
}
return diagnoses
}

type Options struct {
Expand All @@ -60,7 +127,7 @@ var (
o = &Options{}
)

var doctors = map[string]Doctor{
var doctors = map[string]MedicalTechnician{
"bundler": NewBundlerDoctor(),
"yarn": NewYarnDoctor(),
"pip": NewPipDoctor(),
Expand Down Expand Up @@ -91,8 +158,7 @@ var diagnoseCmd = &cobra.Command{
log.Fatal(m)
}

department := NewDepartment(doctor)
diagnoses := department.Diagnose(f, o.year, o.Ignores())
diagnoses := Diagnose(doctor, f, o.year, o.Ignores())
if err := Report(diagnoses); err != nil {
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func TestDiagnose(t *testing.T) {
require.NoError(t, err)
defer f.Close()

doctor := NewDepartment(NewBundlerDoctor())
doctor := NewBundlerDoctor()
ignores := []string{"i18n"}
diagnoses := doctor.Diagnose(f, 2, ignores)
diagnoses := Diagnose(doctor, f, 2, ignores)
assert.Equal(t, expect, diagnoses)
})
}
5 changes: 3 additions & 2 deletions cmd/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ type NodejsRegistryResponse struct {
}

type Nodejs struct {
name string
}

func (n *Nodejs) fetchURLFromRegistry(name string) (string, error) {
url := fmt.Sprintf(NODEJS_REGISTRY_API, name)
func (n *Nodejs) fetchURLFromRegistry() (string, error) {
url := fmt.Sprintf(NODEJS_REGISTRY_API, n.name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
Expand Down
8 changes: 4 additions & 4 deletions cmd/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func TestNodejs_fetchURLFromRegistry(t *testing.T) {
tests := []struct {
name string
gem_name string
dep_name string
}{
{
name: "source_code_uri exists",
gem_name: "react",
dep_name: "react",
},
}
expects := []struct {
Expand All @@ -29,8 +29,8 @@ func TestNodejs_fetchURLFromRegistry(t *testing.T) {

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Nodejs{}
r, _ := n.fetchURLFromRegistry(tt.gem_name)
n := Nodejs{name: tt.dep_name}
r, _ := n.fetchURLFromRegistry()
expect := expects[i]
assert.Equal(t, true, strings.HasPrefix(r, expect.url))
})
Expand Down
Loading

0 comments on commit cb48098

Please sign in to comment.