|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + parser_io "github.com/aquasecurity/go-dep-parser/pkg/io" |
| 7 | + "github.com/aquasecurity/go-dep-parser/pkg/nodejs/npm" |
| 8 | + "github.com/kyoshidajp/dep-doctor/cmd/github" |
| 9 | +) |
| 10 | + |
| 11 | +type NPMDoctor struct { |
| 12 | +} |
| 13 | + |
| 14 | +func NewNPMDoctor() *NPMDoctor { |
| 15 | + return &NPMDoctor{} |
| 16 | +} |
| 17 | + |
| 18 | +func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis { |
| 19 | + diagnoses := make(map[string]Diagnosis) |
| 20 | + slicedNameWithOwners := [][]github.NameWithOwner{} |
| 21 | + nameWithOwners := d.NameWithOwners(r) |
| 22 | + sliceSize := len(nameWithOwners) |
| 23 | + |
| 24 | + for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE { |
| 25 | + end := i + github.SEARCH_REPOS_PER_ONCE |
| 26 | + if sliceSize < end { |
| 27 | + end = sliceSize |
| 28 | + } |
| 29 | + slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end]) |
| 30 | + } |
| 31 | + |
| 32 | + for _, nameWithOwners := range slicedNameWithOwners { |
| 33 | + repos := github.FetchFromGitHub(nameWithOwners) |
| 34 | + for _, r := range repos { |
| 35 | + diagnosis := Diagnosis{ |
| 36 | + Name: r.Name, |
| 37 | + Url: r.Url, |
| 38 | + Archived: r.Archived, |
| 39 | + Diagnosed: true, |
| 40 | + IsActive: r.IsActive(year), |
| 41 | + } |
| 42 | + diagnoses[r.Name] = diagnosis |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for _, nameWithOwner := range nameWithOwners { |
| 47 | + if nameWithOwner.CanSearch { |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + diagnosis := Diagnosis{ |
| 52 | + Name: nameWithOwner.PackageName, |
| 53 | + Diagnosed: false, |
| 54 | + } |
| 55 | + diagnoses[nameWithOwner.PackageName] = diagnosis |
| 56 | + } |
| 57 | + return diagnoses |
| 58 | +} |
| 59 | + |
| 60 | +func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner { |
| 61 | + var nameWithOwners []github.NameWithOwner |
| 62 | + libs, _, _ := npm.NewParser().Parse(r) |
| 63 | + |
| 64 | + nodejs := Nodejs{} |
| 65 | + for _, lib := range libs { |
| 66 | + fmt.Printf("%s\n", lib.Name) |
| 67 | + |
| 68 | + githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name) |
| 69 | + if err != nil { |
| 70 | + nameWithOwners = append(nameWithOwners, |
| 71 | + github.NameWithOwner{ |
| 72 | + PackageName: lib.Name, |
| 73 | + CanSearch: false, |
| 74 | + }, |
| 75 | + ) |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + repo, err := github.ParseGitHubUrl(githubUrl) |
| 80 | + if err != nil { |
| 81 | + nameWithOwners = append(nameWithOwners, |
| 82 | + github.NameWithOwner{ |
| 83 | + PackageName: lib.Name, |
| 84 | + CanSearch: false, |
| 85 | + }, |
| 86 | + ) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + nameWithOwners = append(nameWithOwners, |
| 91 | + github.NameWithOwner{ |
| 92 | + Repo: repo.Repo, |
| 93 | + Owner: repo.Owner, |
| 94 | + PackageName: lib.Name, |
| 95 | + CanSearch: true, |
| 96 | + }, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + return nameWithOwners |
| 101 | +} |
0 commit comments