diff --git a/checker/client.go b/checker/client.go index 075d67dc5528..8d59641eb4bd 100644 --- a/checker/client.go +++ b/checker/client.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "os" - "strings" "github.com/ossf/scorecard/v4/clients" ghrepo "github.com/ossf/scorecard/v4/clients/githubrepo" @@ -56,15 +55,18 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge _, experimental := os.LookupEnv("SCORECARD_EXPERIMENTAL") - if strings.Contains(repoURI, "gitlab.") && experimental { - repo, makeRepoError = glrepo.MakeGitlabRepo(repoURI) - if makeRepoError != nil { - return repo, - nil, - nil, - nil, - nil, - fmt.Errorf("getting local directory client: %w", makeRepoError) + //nolint:nestif + if experimental { + if isGl := glrepo.DetectGitLab(repoURI); isGl { + repo, makeRepoError = glrepo.MakeGitlabRepo(repoURI) + if makeRepoError != nil { + return repo, + nil, + nil, + nil, + nil, + fmt.Errorf("getting local directory client: %w", makeRepoError) + } } } else { repo, makeRepoError = ghrepo.MakeGithubRepo(repoURI) @@ -84,7 +86,7 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge retErr = fmt.Errorf("getting OSS-Fuzz repo client: %w", errOssFuzz) } // TODO(repo): Should we be handling the OSS-Fuzz client error like this? - if strings.Contains(repoURI, "gitlab.") && experimental { + if glrepo.DetectGitLab(repoURI) && experimental { glClient, err := glrepo.CreateGitlabClientWithToken(ctx, os.Getenv("GITLAB_AUTH_TOKEN"), repo) if err != nil { return repo, diff --git a/checks/raw/ci_tests.go b/checks/raw/ci_tests.go index b62477ca66e2..bbc77f77386f 100644 --- a/checks/raw/ci_tests.go +++ b/checks/raw/ci_tests.go @@ -57,7 +57,6 @@ func CITests(c clients.RepoClient) (checker.CITestData, error) { fmt.Sprintf("Client.Repositories.ListCheckRunsForRef: %v", err), ) } - fmt.Printf("crs: %v\n", crs) runs[pr.HeadSHA] = append(runs[pr.HeadSHA], crs...) diff --git a/clients/gitlabrepo/client.go b/clients/gitlabrepo/client.go index df81f26d8227..b8f4164359d2 100644 --- a/clients/gitlabrepo/client.go +++ b/clients/gitlabrepo/client.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "log" + "strings" "time" "github.com/xanzy/go-gitlab" @@ -280,3 +281,28 @@ func CreateGitlabClientWithToken(ctx context.Context, token string, repo clients func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.RepoClient, error) { return nil, fmt.Errorf("%w, oss fuzz currently only supported for github repos", clients.ErrUnsupportedFeature) } + +// DetectGitLab: check whether the repoURI is a GitLab URI +// Makes HTTP request to GitLab API. +func DetectGitLab(repoURI string) bool { + var repo repoURL + if err := repo.parse(repoURI); err != nil { + return false + } + if err := repo.IsValid(); err != nil { + return false + } + if strings.Contains(repo.host, "gitlab.") { + return true + } + client, err := gitlab.NewClient("", gitlab.WithBaseURL(repo.Host())) + if err != nil { + return false + } + _, r, err := client.Projects.ListProjects(&gitlab.ListProjectsOptions{}) + if r.StatusCode != 200 || err != nil { + return false + } + + return true +}