-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 优化DependencyCheck扫描NPM的解析逻辑 #37
- Loading branch information
Showing
4 changed files
with
99 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.