Skip to content

Commit

Permalink
feat: 优化DependencyCheck扫描NPM的解析逻辑 #37
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Apr 26, 2023
1 parent 14bf801 commit e1b085c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
69 changes: 69 additions & 0 deletions dependency-check/pkg/npm_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package pkg

import (
"archive/tar"
"compress/gzip"
"encoding/json"
"github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang/util"
"io"
"os"
"strings"
)

// ParsePackageNameAndVersion 从文件名解析包名和版本
func ParsePackageNameAndVersion(fileBaseName string) (string, string) {
// 获取 pkgName 和 pkgVersion
indexOfLastHyphens := strings.LastIndex(fileBaseName, "-")
if indexOfLastHyphens == -1 {
return "", ""
}
indexOfLastDot := strings.LastIndex(fileBaseName, ".")
if indexOfLastDot == -1 {
return "", ""
}
pkgName := fileBaseName[:indexOfLastHyphens]
pkgVersion := fileBaseName[indexOfLastHyphens+1 : indexOfLastDot]
util.Info("npm package %s, version %s", pkgName, pkgVersion)

return pkgName, pkgVersion
}

// ExtractPackageNameAndVersion 从package.json文件中解析出packageName、version
func ExtractPackageNameAndVersion(npmPkgPath string) (string, string, error) {
f, err := os.Open(npmPkgPath)
if err != nil {
return "", "", err
}
defer f.Close()

uncompressedStream, err := gzip.NewReader(f)
if err != nil {
return "", "", err
}
defer uncompressedStream.Close()
tarReader := tar.NewReader(uncompressedStream)

for true {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", "", err
}
if header.Typeflag == tar.TypeReg && header.Name == "package/package.json" {
npmPkg := &npmPackage{}
if err := json.NewDecoder(tarReader).Decode(npmPkg); err != nil {
return "", "", err
}
return npmPkg.Name, npmPkg.Version, nil
}
}

return "", "", nil
}

type npmPackage struct {
Name string `json:"name"`
Version string `json:"version"`
}
22 changes: 22 additions & 0 deletions dependency-check/pkg/npm_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pkg

import (
"testing"
)

func TestParsePackageNameAndVersion(t *testing.T) {
pkgName, pkgVersion := ParsePackageNameAndVersion("npm-test-0.0.1.tgz")
if pkgName != "npm-test" || pkgVersion != "0.0.1" {
t.Fatalf("parese failed pkgName[%s] pkgVersion[%s]", pkgName, pkgVersion)
}
}

func TestExtractPackageNameAndVersion(t *testing.T) {
pkgName, pkgVersion, err := ExtractPackageNameAndVersion("testdata/axios-0.16.2.tgz")
if err != nil {
t.Fatal(err.Error())
}
if pkgName != "axios" || pkgVersion != "0.16.2" {
t.Fatalf("parese failed pkgName[%s] pkgVersion[%s]", pkgName, pkgVersion)
}
}
17 changes: 8 additions & 9 deletions dependency-check/pkg/scan_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang/util"
"os"
"path/filepath"
"strings"
)

const PackageTypeNpm = "NPM"
Expand Down Expand Up @@ -59,16 +58,16 @@ func npmPrepare(file *os.File) error {
}

// 获取 pkgName 和 pkgVersion
indexOfHyphens := strings.Index(fileBaseName, "-")
if indexOfHyphens == -1 {
return errors.New("'-' not found in file name " + fileBaseName)
pkgName, pkgVersion, err := ExtractPackageNameAndVersion(fileAbsPath)
if err != nil {
return err
}
if len(pkgName) == 0 || len(pkgVersion) == 0 {
pkgName, pkgVersion = ParsePackageNameAndVersion(fileBaseName)
}
indexOfLastDot := strings.LastIndex(fileBaseName, ".")
if indexOfLastDot == -1 {
return errors.New("'.' not found in file name " + fileBaseName)
if len(pkgName) == 0 || len(pkgVersion) == 0 {
return errors.New("failed to parse npm pkgName and pkgVersion")
}
pkgName := fileBaseName[:indexOfHyphens]
pkgVersion := fileBaseName[indexOfHyphens+1 : indexOfLastDot]
util.Info("npm package %s, version %s", pkgName, pkgVersion)

// 替换 package-lock.json中的file:xxx 为实际版本号
Expand Down
Empty file.

0 comments on commit e1b085c

Please sign in to comment.