Skip to content

Commit

Permalink
Refactor the addRepo to not pass around a mutable object. Tweak a tes…
Browse files Browse the repository at this point in the history
…t to support gitlab better
  • Loading branch information
joshgc committed Aug 24, 2023
1 parent 83d46c4 commit d37f760
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions cmd/package_managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/package_managers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit d37f760

Please sign in to comment.