22 Star 209 Fork 63

GVP京东开源/sbom-tool

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
source.go 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
TK 提交于 2024-03-05 19:00 +08:00 . fix: remove user info from repository url
// Copyright (c) 2023 Jingdong Technology Information Technology Co., Ltd.
// SBOM-TOOL is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package source
import (
"net/url"
"path/filepath"
"time"
gogit "github.com/go-git/go-git/v5"
"gitee.com/JD-opensource/sbom-tool/pkg/config"
"gitee.com/JD-opensource/sbom-tool/pkg/fingerprint"
"gitee.com/JD-opensource/sbom-tool/pkg/model"
"gitee.com/JD-opensource/sbom-tool/pkg/util"
)
// GetSourceInfo returns the source information of the project
func GetSourceInfo(cfg *config.SourceConfig) (*model.Source, error) {
fp, _ := fingerprint.CalcFingerprint(cfg)
source := model.Source{
TotalSize: fp.Metadata.TotalSize,
TotalFile: fp.Metadata.TotalFiles,
TotalLine: fp.Metadata.TotalLines,
Fingerprint: model.Fingerprint{
TotalCount: fp.Metadata.TotalCount,
Created: time.UnixMilli(fp.Metadata.CreatedAt).Format(time.RFC3339),
Checksum: "",
ExternalRef: "",
Vendor: model.FingerprintVendor{
Name: fp.Metadata.Vendor.Name,
Tool: fp.Metadata.Vendor.ToolName + " " + fp.Metadata.Vendor.ToolVersion,
Algorithm: fp.Metadata.Vendor.AlgoName + " " + fp.Metadata.Vendor.AlgoVersion,
},
Files: util.SliceMap(fp.Files, toSBOMFileFingerprint),
},
}
repoInfo(cfg.SrcPath, &source)
return &source, nil
}
func repoInfo(projectPath string, source *model.Source) {
if len(projectPath) == 0 {
return
}
repo, err := gogit.PlainOpen(projectPath)
if err != nil {
parentPath := filepath.Dir(projectPath)
if parentPath == "." || parentPath == "/" || projectPath == parentPath {
return
}
repoInfo(parentPath, source)
} else {
conf, err := repo.Config()
if err == nil {
if remote, ok := conf.Remotes["origin"]; ok {
source.Repository = normalizeHttpUrl(remote.URLs[0])
}
}
ref, err := repo.Head()
if err == nil {
source.Branch = ref.Name().Short()
}
commit, err := repo.CommitObject(ref.Hash())
if err == nil {
source.Revision = commit.Hash.String()
}
}
}
func normalizeHttpUrl(rawUrl string) string {
if rawUrl == "" {
return ""
}
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return ""
}
parsedUrl.User = nil
return parsedUrl.String()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JD-opensource/sbom-tool.git
git@gitee.com:JD-opensource/sbom-tool.git
JD-opensource
sbom-tool
sbom-tool
4c020bca60a21d5b281a8d9e0a4ace684d0b4fc6

搜索帮助