From d37f7601d4ff911371464bfbaefdfc7d0577f212 Mon Sep 17 00:00:00 2001 From: Josh Cogan Date: Thu, 24 Aug 2023 17:14:52 +0000 Subject: [PATCH] Refactor the addRepo to not pass around a mutable object. Tweak a test to support gitlab better --- cmd/package_managers.go | 15 +++++++++------ cmd/package_managers_test.go | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/package_managers.go b/cmd/package_managers.go index a46e058bfd74..84626fd9b72e 100644 --- a/cmd/package_managers.go +++ b/cmd/package_managers.go @@ -113,21 +113,22 @@ func fetchGitRepositoryFromNPM(packageName string, packageManager pmc.Client) (s return v.Objects[0].Package.Links.Repository, nil } -func addRepoIfValid(validURLs map[string]any, url string) { +func repoIfValid(url string) string { match := _GITHUB_DOMAIN_REGEXP.FindStringSubmatch(url) if len(match) >= 3 { - validURLs[fmt.Sprintf("https://github.com/%s/%s", match[1], match[2])] = nil + return fmt.Sprintf("https://github.com/%s/%s", match[1], match[2]) } match = _GITHUB_SUBDOMAIN_REGEXP.FindStringSubmatch(url) if len(match) >= 3 { - validURLs[fmt.Sprintf("https://github.com/%s/%s", match[1], match[2])] = nil + return fmt.Sprintf("https://github.com/%s/%s", match[1], match[2]) } match = _GITLAB_DOMAIN_REGEXP.FindStringSubmatch(url) if len(match) >= 3 { - validURLs[fmt.Sprintf("https://gitlab.com/%s/%s", match[1], match[2])] = nil + return fmt.Sprintf("https://gitlab.com/%s/%s", match[1], match[2]) } + return "" } func findGitRepositoryInPYPIResponse(packageName string, response io.Reader) (string, error) { @@ -137,10 +138,12 @@ func findGitRepositoryInPYPIResponse(packageName string, response io.Reader) (st return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse pypi package json: %v", err)) } + v.Info.ProjectURLs["key_not_used"] = v.Info.ProjectURL validURLs := make(map[string]any) - addRepoIfValid(validURLs, v.Info.ProjectURL) for _, url := range v.Info.ProjectURLs { - addRepoIfValid(validURLs, url) + if repo := repoIfValid(url); repo != "" { + validURLs[repo] = nil + } } if len(validURLs) > 1 { diff --git a/cmd/package_managers_test.go b/cmd/package_managers_test.go index a4ff889448fc..f29b72b3cd8c 100644 --- a/cmd/package_managers_test.go +++ b/cmd/package_managers_test.go @@ -176,7 +176,7 @@ func Test_findGitRepositoryInPYPIResponse(t *testing.T) { { "info": { "platform": "UNKNOWN", - "not_a_project_url": "https://pypi.org/project/color/", + "not_a_project_url": "https://github.com/htaslan/color", "project_urls": { "Homepage": "http://git_NOT_VALID_hub.com/htaslan/color" }