diff --git a/cve-vulner-manager/cve-ddd/infrastructure/latestrpmimpl/impl.go b/cve-vulner-manager/cve-ddd/infrastructure/latestrpmimpl/impl.go index 9d4fb358a9531155b33dafcc84948559e6cbe275..995ee5fc56385344c961ebd687bed1f8f198c695 100644 --- a/cve-vulner-manager/cve-ddd/infrastructure/latestrpmimpl/impl.go +++ b/cve-vulner-manager/cve-ddd/infrastructure/latestrpmimpl/impl.go @@ -7,14 +7,20 @@ import ( "errors" "fmt" "io" + "net/http" "strings" "time" "github.com/astaxie/beego" "github.com/opensourceways/robot-gitee-lib/client" + "github.com/opensourceways/server-common-lib/utils" "github.com/sirupsen/logrus" ) +const ( + retryTimes = 3 +) + type PkgRPM struct { Org string Repo string @@ -36,39 +42,59 @@ func NewLatestRpm() *latestRpm { } return &latestRpm{ - rpm: rpm, - cli: cli, - buildTime: make(map[string]map[string]time.Time), + rpm: rpm, + cli: cli, + buildTime: make(map[string]map[string]time.Time), + httpClient: utils.NewHttpClient(retryTimes), } } type latestRpm struct { - rpm PkgRPM - cli client.Client - buildTime map[string]map[string]time.Time + rpm PkgRPM + cli client.Client + buildTime map[string]map[string]time.Time + httpClient utils.HttpClient } func (l *latestRpm) InitData(branches []string) error { for _, branch := range branches { - path := fmt.Sprintf("%s%s.csv", l.rpm.PathPrefix, branch) - content, err := l.cli.GetPathContent(l.rpm.Org, l.rpm.Repo, path, l.rpm.Branch) + content, err := l.getContent(branch) if err != nil { - logrus.Errorf("new-cold-patch-cve-collect init Data of %s failed: %s", path, err.Error()) continue } - decodeContent, err := base64.StdEncoding.DecodeString(content.Content) - if err != nil { - logrus.Errorf("new-cold-patch-cve-collect decode Data of %s failed: %s", path, err.Error()) - continue - } - - l.buildTime[branch] = l.parseFile(decodeContent) + l.buildTime[branch] = l.parseFile(content) } return nil } +func (l *latestRpm) getContent(branch string) ([]byte, error) { + dailyBuildUrl := fmt.Sprintf("%s/repo.openeuler.org/openeuler_latest_rpms/%s.csv", + beego.AppConfig.String("testResult::host"), branch, + ) + req, err := http.NewRequest(http.MethodGet, dailyBuildUrl, nil) + if err != nil { + return nil, err + } + + fileContent, _, err := l.httpClient.Download(req) + if err == nil { + return fileContent, nil + } else { + logrus.Errorf("get latest rpms of %s from dailybuild failed: %s", branch, err.Error()) + } + + path := fmt.Sprintf("%s%s.csv", l.rpm.PathPrefix, branch) + content, err := l.cli.GetPathContent(l.rpm.Org, l.rpm.Repo, path, l.rpm.Branch) + if err != nil { + logrus.Errorf("get latest rpms of %s from gitee failed: %s", branch, err.Error()) + return nil, err + } + + return base64.StdEncoding.DecodeString(content.Content) +} + func (l *latestRpm) parseFile(content []byte) map[string]time.Time { buff := bytes.NewBuffer(content) r := csv.NewReader(buff)