pboling commented on code in PR #255:
URL: https://github.com/apache/skywalking-eyes/pull/255#discussion_r2636700098


##########
pkg/deps/ruby.go:
##########
@@ -253,6 +416,193 @@ func runtimeDepsFromGemspecs(dir string) ([]string, 
error) {
        return res, nil
 }
 
+func parseGemspecDependencies(path string) ([]string, error) {
+       f, err := os.Open(path)
+       if err != nil {
+               return nil, err
+       }
+       defer f.Close()
+
+       scanner := bufio.NewScanner(f)
+       var deps []string
+       for scanner.Scan() {
+               line := scanner.Text()
+               trimLeft := strings.TrimLeft(line, " \t")
+               if strings.HasPrefix(trimLeft, "#") {
+                       continue
+               }
+               if m := gemspecRuntimeRe.FindStringSubmatch(line); len(m) == 2 {
+                       deps = append(deps, m[1])
+               }
+       }
+       return deps, scanner.Err()
+}
+
+func findInstalledGemspec(name, version string) (string, error) {
+       gemPaths := getGemPaths()
+       for _, dir := range gemPaths {
+               specsDir := filepath.Join(dir, "specifications")
+               if version != "" && rubyVersionRe.MatchString(version) {
+                       path := filepath.Join(specsDir, 
name+"-"+version+".gemspec")
+                       if _, err := os.Stat(path); err == nil {
+                               return path, nil
+                       }
+               } else {
+                       entries, err := os.ReadDir(specsDir)
+                       if err != nil {
+                               continue
+                       }
+                       for _, e := range entries {
+                               if e.IsDir() || !strings.HasPrefix(e.Name(), 
name+"-") || !strings.HasSuffix(e.Name(), ".gemspec") {
+                                       continue
+                               }
+                               stem := strings.TrimSuffix(e.Name(), ".gemspec")
+                               // Ensure that the character immediately after 
the "name-" prefix
+                               // is a digit, so we only consider filenames 
where the suffix is
+                               // a version component (e.g., 
"foo-1.0.0.gemspec") and avoid
+                               // similar names like "foo-bar-1.0.0.gemspec" 
when searching for "foo".
+                               if len(stem) <= len(name)+1 {
+                                       continue
+                               }
+                               versionStart := stem[len(name)+1]
+                               if versionStart < '0' || versionStart > '9' {
+                                       continue
+                               }
+                               ver := strings.TrimPrefix(stem, name+"-")
+                               if ver == stem {
+                                       continue
+                               }
+                               path := filepath.Join(specsDir, e.Name())
+                               if specName, _, err := parseGemspecInfo(path); 
err == nil && specName == name {
+                                       return path, nil
+                               }
+                       }
+               }
+       }
+       return "", os.ErrNotExist
+}
+
+func fetchLocalLicense(dir, targetName string) (string, error) {
+       entries, err := os.ReadDir(dir)
+       if err != nil {
+               return "", err
+       }
+       for _, e := range entries {
+               if e.IsDir() || !strings.HasSuffix(e.Name(), ".gemspec") {
+                       continue
+               }
+               path := filepath.Join(dir, e.Name())
+               specName, license, err := parseGemspecInfo(path)
+               if err == nil && specName == targetName && license != "" {
+                       return license, nil
+               }
+       }
+       return "", nil
+}
+
+func fetchInstalledLicense(name, version string) string {
+       gemPaths := getGemPaths()
+       for _, dir := range gemPaths {
+               specsDir := filepath.Join(dir, "specifications")
+               // If version is specific
+               if version != "" && rubyVersionRe.MatchString(version) {
+                       path := filepath.Join(specsDir, 
name+"-"+version+".gemspec")
+                       if _, license, err := parseGemspecInfo(path); err == 
nil && license != "" {
+                               return license
+                       }
+               } else {
+                       // Scan for any version
+                       entries, err := os.ReadDir(specsDir)
+                       if err != nil {
+                               continue
+                       }
+                       for _, e := range entries {
+                               if e.IsDir() || !strings.HasPrefix(e.Name(), 
name+"-") || !strings.HasSuffix(e.Name(), ".gemspec") {
+                                       continue
+                               }
+                               stem := strings.TrimSuffix(e.Name(), ".gemspec")
+                               ver := strings.TrimPrefix(stem, name+"-")
+                               if ver == stem { // didn't have prefix
+                                       continue
+                               }
+                               // Ensure the character after the gem name 
corresponds to the start of a version
+                               if ver == "" || ver[0] < '0' || ver[0] > '9' {
+                                       continue
+                               }
+                               path := filepath.Join(specsDir, e.Name())
+                               if specName, license, err := 
parseGemspecInfo(path); err == nil && specName == name && license != "" {
+                                       return license
+                               }
+                       }
+               }
+       }
+       return ""
+}

Review Comment:
   Fixed in 3625d88



##########
pkg/deps/ruby.go:
##########
@@ -104,7 +108,30 @@ func (r *GemfileLockResolver) Resolve(lockfile string, 
config *ConfigDeps, repor
                        continue
                }
 
-               licenseID, err := fetchRubyGemsLicense(name, version)
+               if localPath != "" {
+                       baseDir, err := filepath.Abs(dir)
+                       if err != nil {
+                               logrus.WithError(err).Warn("failed to resolve 
base directory for local gem path")
+                       } else {
+                               candidatePath := 
filepath.Clean(filepath.Join(baseDir, localPath))
+                               if candidatePath == baseDir || 
strings.HasPrefix(candidatePath, baseDir+string(os.PathSeparator)) {
+                                       fullPath := candidatePath
+                                       license, err := 
fetchLocalLicense(fullPath, name)
+                                       if err == nil && license != "" {
+                                               
report.Resolve(&Result{Dependency: name, LicenseSpdxID: license, Version: 
version})
+                                               continue
+                                       }
+                               } else {
+                                       logrus.WithField("path", 
localPath).Warn("ignoring potentially unsafe local gem path outside project 
directory")
+                               }
+                       }

Review Comment:
   Fixed in 3625d88



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to