Skip to content

Commit

Permalink
Merge pull request #13 from kyoshidajp/refactor_registry
Browse files Browse the repository at this point in the history
Refactor registry
  • Loading branch information
kyoshidajp authored Oct 27, 2023
2 parents 24b98b5 + d8cc652 commit c75aede
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 119 deletions.
44 changes: 4 additions & 40 deletions cmd/bundler.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

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"
)

const GITHUB_SEARCH_REPO_COUNT_PER_ONCE = 20

// https://guides.rubygems.org/rubygems-org-api/
const RUBYGEMS_ORG_API = "https://rubygems.org/api/v1/gems/%s.json"

type GemResponse struct {
Name string `json:"name"`
SourceCodeUri string `json:"source_code_uri"`
HomepageUri string `json:"homepage_uri"`
}

type BundlerDoctor struct {
}

Expand All @@ -36,8 +21,8 @@ func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]
nameWithOwners := b.NameWithOwners(r)
sliceSize := len(nameWithOwners)

for i := 0; i < sliceSize; i += GITHUB_SEARCH_REPO_COUNT_PER_ONCE {
end := i + GITHUB_SEARCH_REPO_COUNT_PER_ONCE
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
Expand Down Expand Up @@ -72,37 +57,16 @@ func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]
return diagnoses
}

func (d *BundlerDoctor) fetchURLFromRepository(name string) (string, error) {
url := fmt.Sprintf(RUBYGEMS_ORG_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var Gem GemResponse
err := json.Unmarshal(body, &Gem)
if err != nil {
return "", errors.New("error: Unknown response")
}

if Gem.SourceCodeUri != "" {
return Gem.SourceCodeUri, nil
} else if Gem.HomepageUri != "" {
return Gem.HomepageUri, nil
}

return "", nil
}

func (d *BundlerDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
p := &bundler.Parser{}
libs, _, _ := p.Parse(r)

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

githubUrl, err := d.fetchURLFromRepository(lib.Name)
githubUrl, err := rubyGems.fetchURLFromRegistry(lib.Name)
if err != nil {
continue
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const MAX_YEAR_TO_BE_BLANK = 5

type Doctor interface {
Diagnose(r io.ReadSeekerAt, year int) map[string]Diagnosis
fetchURLFromRepository(name string) (string, error)
NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner
}

Expand Down Expand Up @@ -80,7 +79,6 @@ var diagnoseCmd = &cobra.Command{
_ = f.Close()
}()
if err != nil {
fmt.Fprintln(os.Stderr, err)
m := fmt.Sprintf("Can't open: %s.", o.lockFilePath)
log.Fatal(m)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

const QUERY_SEPARATOR = " "
const SEARCH_REPOS_PER_ONCE = 20

type GitHubRepository struct {
Name string
Expand Down
36 changes: 36 additions & 0 deletions cmd/nodejs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

// https://docs.npmjs.com/cli/v8/using-npm/registry
const NODEJS_REGISTRY_API = "https://registry.npmjs.org/%s"

type NodejsRegistryResponse struct {
Repository struct {
Url string `json:"url"`
}
}

type Nodejs struct {
}

func (n *Nodejs) fetchURLFromRegistry(name string) (string, error) {
url := fmt.Sprintf(NODEJS_REGISTRY_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var NodejsRegistryResponse NodejsRegistryResponse
err := json.Unmarshal(body, &NodejsRegistryResponse)
if err != nil {
return "", nil
}

return NodejsRegistryResponse.Repository.Url, nil
}
6 changes: 3 additions & 3 deletions cmd/yarn_test.go → cmd/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestYarnDoctor_fetchURLFromRepository(t *testing.T) {
func TestNodejs_fetchURLFromRegistry(t *testing.T) {
tests := []struct {
name string
gem_name string
Expand All @@ -29,8 +29,8 @@ func TestYarnDoctor_fetchURLFromRepository(t *testing.T) {

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := YarnDoctor{}
r, _ := s.fetchURLFromRepository(tt.gem_name)
n := Nodejs{}
r, _ := n.fetchURLFromRegistry(tt.gem_name)
expect := expects[i]
assert.Equal(t, true, strings.HasPrefix(r, expect.url))
})
Expand Down
42 changes: 4 additions & 38 deletions cmd/pip.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,28 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"net/http"

parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/python/pip"
"github.com/kyoshidajp/dep-doctor/cmd/github"
)

// https://warehouse.pypa.io/api-reference/json.html
const PYPI_REGISTRY_API = "https://pypi.org/pypi/%s/json"

type PipRegistryResponse struct {
Info struct {
ProjectUrls struct {
SourceCode string `json:"Source Code"`
Source string `json:"Source"`
} `json:"project_urls"`
} `json:"info"`
}

type PipDoctor struct {
}

func NewPipDoctor() *PipDoctor {
return &PipDoctor{}
}

func (d *PipDoctor) fetchURLFromRepository(name string) (string, error) {
url := fmt.Sprintf(PYPI_REGISTRY_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var PipRegistryResponse PipRegistryResponse
err := json.Unmarshal(body, &PipRegistryResponse)
if err != nil {
return "", nil
}

if PipRegistryResponse.Info.ProjectUrls.SourceCode != "" {
return PipRegistryResponse.Info.ProjectUrls.SourceCode, nil
}

return PipRegistryResponse.Info.ProjectUrls.Source, nil
}

func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
sliceSize := len(nameWithOwners)

for i := 0; i < sliceSize; i += GITHUB_SEARCH_REPO_COUNT_PER_ONCE {
end := i + GITHUB_SEARCH_REPO_COUNT_PER_ONCE
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
Expand Down Expand Up @@ -96,10 +61,11 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw
var nameWithOwners []github.NameWithOwner
libs, _, _ := pip.NewParser().Parse(r)

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

githubUrl, err := d.fetchURLFromRepository(lib.Name)
githubUrl, err := pypi.fetchURLFromRepository(lib.Name)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
Expand Down
43 changes: 43 additions & 0 deletions cmd/pypi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

// https://warehouse.pypa.io/api-reference/json.html
const PYPI_REGISTRY_API = "https://pypi.org/pypi/%s/json"

type PypiRegistryResponse struct {
Info struct {
ProjectUrls struct {
SourceCode string `json:"Source Code"`
Source string `json:"Source"`
} `json:"project_urls"`
} `json:"info"`
}

type Pypi struct {
}

func (p *Pypi) fetchURLFromRepository(name string) (string, error) {
url := fmt.Sprintf(PYPI_REGISTRY_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var PypiRegistryResponse PypiRegistryResponse
err := json.Unmarshal(body, &PypiRegistryResponse)
if err != nil {
return "", nil
}

if PypiRegistryResponse.Info.ProjectUrls.SourceCode != "" {
return PypiRegistryResponse.Info.ProjectUrls.SourceCode, nil
}

return PypiRegistryResponse.Info.ProjectUrls.Source, nil
}
6 changes: 3 additions & 3 deletions cmd/pip_test.go → cmd/pypi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPyPiDoctor_fetchURLFromRepository(t *testing.T) {
func TestPyPi_fetchURLFromRegistry(t *testing.T) {
tests := []struct {
name string
gem_name string
Expand All @@ -28,8 +28,8 @@ func TestPyPiDoctor_fetchURLFromRepository(t *testing.T) {

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := PipDoctor{}
r, _ := s.fetchURLFromRepository(tt.gem_name)
p := Pypi{}
r, _ := p.fetchURLFromRepository(tt.gem_name)
expect := expects[i]
assert.Equal(t, expect.url, r)
})
Expand Down
43 changes: 43 additions & 0 deletions cmd/ruby_gems.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

// https://guides.rubygems.org/rubygems-org-api/
const RUBY_GEMS_REGISTRY_API = "https://rubygems.org/api/v1/gems/%s.json"

type RubyGemsRegistryResponse struct {
Name string `json:"name"`
SourceCodeUri string `json:"source_code_uri"`
HomepageUri string `json:"homepage_uri"`
}

type RubyGems struct {
}

func (g *RubyGems) fetchURLFromRegistry(name string) (string, error) {
url := fmt.Sprintf(RUBY_GEMS_REGISTRY_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var Gem RubyGemsRegistryResponse
err := json.Unmarshal(body, &Gem)
if err != nil {
return "", errors.New("error: Unknown response")
}

if Gem.SourceCodeUri != "" {
return Gem.SourceCodeUri, nil
} else if Gem.HomepageUri != "" {
return Gem.HomepageUri, nil
}

return "", nil
}
6 changes: 3 additions & 3 deletions cmd/bundler_test.go → cmd/ruby_gems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBundlerDoctor_fetchURLFromRepository(t *testing.T) {
func TestRubyGems_fetchURLFromRepository(t *testing.T) {
tests := []struct {
name string
gem_name string
Expand Down Expand Up @@ -37,8 +37,8 @@ func TestBundlerDoctor_fetchURLFromRepository(t *testing.T) {

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

0 comments on commit c75aede

Please sign in to comment.