Skip to content

Commit

Permalink
Detect GitLab repos including self-hosted
Browse files Browse the repository at this point in the history
Signed-off-by: Raghav Kaul <[email protected]>
  • Loading branch information
raghavkaul committed Mar 1, 2023
1 parent f624c44 commit 70a412d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
24 changes: 13 additions & 11 deletions checker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/ossf/scorecard/v4/clients"
ghrepo "github.com/ossf/scorecard/v4/clients/githubrepo"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion checks/raw/ci_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
26 changes: 26 additions & 0 deletions clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"log"
"strings"
"time"

"github.com/xanzy/go-gitlab"
Expand Down Expand Up @@ -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
}

0 comments on commit 70a412d

Please sign in to comment.