Skip to content

Commit

Permalink
Refactor pick up registry
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshidajp committed Oct 27, 2023
1 parent 2081ea3 commit d8cc652
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 110 deletions.
38 changes: 2 additions & 36 deletions cmd/bundler.go
Original file line number Diff line number Diff line change
@@ -1,26 +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"
)

// 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 Down Expand Up @@ -70,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
1 change: 0 additions & 1 deletion 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
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
38 changes: 2 additions & 36 deletions cmd/pip.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,20 @@
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{}
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
30 changes: 2 additions & 28 deletions cmd/yarn.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
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/nodejs/yarn"
"github.com/kyoshidajp/dep-doctor/cmd/github"
)

const NODEJS_REGISTRY_API = "https://registry.npmjs.org/%s"

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

type YarnDoctor struct {
}

func NewYarnDoctor() *YarnDoctor {
return &YarnDoctor{}
}

func (d *YarnDoctor) fetchURLFromRepository(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
}

func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
Expand Down Expand Up @@ -88,10 +61,11 @@ func (d *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithO
var nameWithOwners []github.NameWithOwner
libs, _, _ := yarn.NewParser().Parse(r)

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

githubUrl, err := d.fetchURLFromRepository(lib.Name)
githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
Expand Down

0 comments on commit d8cc652

Please sign in to comment.