Skip to content

Commit

Permalink
handle GL_HOST without scheme
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock committed Jan 25, 2024
1 parent ea0cfa4 commit fb20f8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
21 changes: 12 additions & 9 deletions clients/gitlabrepo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,15 @@ func (r *repoURL) parse(input string) error {
t = input
}

// Allow skipping scheme for ease-of-use, default to https.
if !strings.Contains(t, "://") {
t = "https://" + t
}

u, e := url.Parse(t)
if e != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("url.Parse: %v", e))
u, err := url.Parse(withDefaultScheme(t))
if err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("url.Parse: %v", err))

Check warning on line 67 in clients/gitlabrepo/repo.go

View check run for this annotation

Codecov / codecov/patch

clients/gitlabrepo/repo.go#L67

Added line #L67 was not covered by tests
}

// fixup the URL, for situations where GL_HOST contains part of the path
// https://github.com/ossf/scorecard/issues/3696
if h := os.Getenv("GL_HOST"); h != "" {
hostURL, err := url.Parse(h)
hostURL, err := url.Parse(withDefaultScheme(h))
if err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("url.Parse GL_HOST: %v", err))
}

Check warning on line 76 in clients/gitlabrepo/repo.go

View check run for this annotation

Codecov / codecov/patch

clients/gitlabrepo/repo.go#L75-L76

Added lines #L75 - L76 were not covered by tests
Expand All @@ -99,6 +94,14 @@ func (r *repoURL) parse(input string) error {
return nil
}

// Allow skipping scheme for ease-of-use, default to https.
func withDefaultScheme(uri string) string {
if strings.Contains(uri, "://") {
return uri
}
return "https://" + uri
}

// URI implements Repo.URI().
func (r *repoURL) URI() string {
return fmt.Sprintf("%s/%s/%s", r.host, r.owner, r.project)
Expand Down
8 changes: 8 additions & 0 deletions clients/gitlabrepo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ func TestRepoURL_parse_GL_HOST(t *testing.T) {
owner: "ssdlc",
project: "scorecard-scanner",
},
{
name: "GL_HOST has no scheme",
glHost: "foo.com/bar/",
url: "foo.com/bar/ssdlc/scorecard-scanner",
host: "foo.com/bar",
owner: "ssdlc",
project: "scorecard-scanner",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit fb20f8b

Please sign in to comment.