Skip to content

Commit baae4da

Browse files
authored
Merge branch 'main' into fix/pip-install-e-flag
2 parents 6a2c051 + 110e352 commit baae4da

27 files changed

Lines changed: 289 additions & 158 deletions

.github/workflows/integration.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,17 @@ jobs:
7171
with:
7272
files: ./e2e-coverage.out
7373
verbose: true
74+
75+
- name: Run GitLab E2E #using retry because the GitHub token is being throttled.
76+
uses: nick-invision/retry@943e742917ac94714d2f408a0e8320f2d1fcafcd
77+
with:
78+
max_attempts: 3
79+
retry_on: error
80+
timeout_minutes: 30
81+
command: make e2e-gitlab
82+
83+
- name: codecov
84+
uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # 2.1.0
85+
with:
86+
files: ./e2e-coverage.out
87+
verbose: true

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ e2e-gh-token: build-scorecard check-env | $(GINKGO)
334334
# Run e2e tests. GITHUB_AUTH_TOKEN set to secrets.GITHUB_TOKEN must be used to run this.
335335
TOKEN_TYPE="GITHUB_TOKEN" $(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out --keep-separate-coverprofiles ./...
336336

337+
e2e-gitlab-token: ## Runs e2e tests that require a GITLAB_TOKEN
338+
TOKEN_TYPE="GITLAB_PAT" $(GINKGO) --race -p -vv --focus '.*GitLab Token' ./...
339+
340+
e2e-gitlab: ## Runs e2e tests for GitLab only. TOKEN_TYPE is not used (since these are public APIs), but must be set to something
341+
TOKEN_TYPE="GITLAB_PAT" $(GINKGO) --race -p -vv --focus '.*GitLab' ./...
342+
337343
e2e-attestor: ## Runs e2e tests for scorecard-attestor
338344
cd attestor/e2e; go test -covermode=atomic -coverprofile=e2e-coverage.out; cd ../..
339345

checker/client.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package checker
1717
import (
1818
"context"
1919
"fmt"
20+
"os"
2021

2122
"github.com/ossf/scorecard/v4/clients"
2223
ghrepo "github.com/ossf/scorecard/v4/clients/githubrepo"
24+
glrepo "github.com/ossf/scorecard/v4/clients/gitlabrepo"
2325
"github.com/ossf/scorecard/v4/clients/localdir"
2426
"github.com/ossf/scorecard/v4/clients/ossfuzz"
2527
"github.com/ossf/scorecard/v4/log"
@@ -35,7 +37,9 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge
3537
clients.VulnerabilitiesClient, // vulnClient
3638
error,
3739
) {
38-
var githubRepo clients.Repo
40+
var repo clients.Repo
41+
var makeRepoError error
42+
3943
if localURI != "" {
4044
localRepo, errLocal := localdir.MakeLocalDirRepo(localURI)
4145
var retErr error
@@ -50,18 +54,46 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge
5054
retErr
5155
}
5256

53-
githubRepo, errGitHub := ghrepo.MakeGithubRepo(repoURI)
54-
if errGitHub != nil {
55-
return githubRepo,
56-
nil,
57-
nil,
58-
nil,
59-
nil,
60-
fmt.Errorf("getting local directory client: %w", errGitHub)
57+
_, experimental := os.LookupEnv("SCORECARD_EXPERIMENTAL")
58+
var repoClient clients.RepoClient
59+
60+
//nolint:nestif
61+
if experimental && glrepo.DetectGitLab(repoURI) {
62+
repo, makeRepoError = glrepo.MakeGitlabRepo(repoURI)
63+
if makeRepoError != nil {
64+
return repo,
65+
nil,
66+
nil,
67+
nil,
68+
nil,
69+
fmt.Errorf("getting local directory client: %w", makeRepoError)
70+
}
71+
72+
var err error
73+
repoClient, err = glrepo.CreateGitlabClientWithToken(ctx, os.Getenv("GITLAB_AUTH_TOKEN"), repo)
74+
if err != nil {
75+
return repo,
76+
nil,
77+
nil,
78+
nil,
79+
nil,
80+
fmt.Errorf("error creating gitlab client: %w", err)
81+
}
82+
} else {
83+
repo, makeRepoError = ghrepo.MakeGithubRepo(repoURI)
84+
if makeRepoError != nil {
85+
return repo,
86+
nil,
87+
nil,
88+
nil,
89+
nil,
90+
fmt.Errorf("getting local directory client: %w", makeRepoError)
91+
}
92+
repoClient = ghrepo.CreateGithubRepoClient(ctx, logger)
6193
}
6294

63-
return githubRepo, /*repo*/
64-
ghrepo.CreateGithubRepoClient(ctx, logger), /*repoClient*/
95+
return repo, /*repo*/
96+
repoClient, /*repoClient*/
6597
ossfuzz.CreateOSSFuzzClient(ossfuzz.StatusURL), /*ossFuzzClient*/
6698
clients.DefaultCIIBestPracticesClient(), /*ciiClient*/
6799
clients.DefaultVulnerabilitiesClient(), /*vulnClient*/

clients/githubrepo/repo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func (r *repoURL) URI() string {
7676
return fmt.Sprintf("%s/%s/%s", r.host, r.owner, r.repo)
7777
}
7878

79+
func (r *repoURL) Host() string {
80+
return r.host
81+
}
82+
7983
// String implements Repo.String.
8084
func (r *repoURL) String() string {
8185
return fmt.Sprintf("%s-%s-%s", r.host, r.owner, r.repo)

clients/githubrepo/repo_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func TestRepoURL_IsValid(t *testing.T) {
9797
if !tt.wantErr && !cmp.Equal(tt.expected, r, cmp.AllowUnexported(repoURL{})) {
9898
t.Errorf("Got diff: %s", cmp.Diff(tt.expected, r))
9999
}
100+
101+
if !cmp.Equal(r.Host(), tt.expected.host) {
102+
t.Errorf("%s expected host: %s got host %s", tt.inputURL, tt.expected.host, r.Host())
103+
}
100104
})
101105
}
102106
}

clients/gitlabrepo/branches.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ func (handler *branchesHandler) setup() error {
4646
return
4747
}
4848

49-
proj, _, err := handler.glClient.Projects.GetProject(handler.repourl.projectID, &gitlab.GetProjectOptions{})
49+
proj, _, err := handler.glClient.Projects.GetProject(handler.repourl.project, &gitlab.GetProjectOptions{})
5050
if err != nil {
5151
handler.errSetup = fmt.Errorf("requirest for project failed with error %w", err)
5252
return
5353
}
5454

55-
branch, _, err := handler.glClient.Branches.GetBranch(handler.repourl.projectID, proj.DefaultBranch)
55+
branch, _, err := handler.glClient.Branches.GetBranch(handler.repourl.project, proj.DefaultBranch)
5656
if err != nil {
5757
handler.errSetup = fmt.Errorf("request for default branch failed with error %w", err)
5858
return
5959
}
6060

6161
if branch.Protected {
6262
protectedBranch, resp, err := handler.glClient.ProtectedBranches.GetProtectedBranch(
63-
handler.repourl.projectID, branch.Name)
63+
handler.repourl.project, branch.Name)
6464
if err != nil && resp.StatusCode != 403 {
6565
handler.errSetup = fmt.Errorf("request for protected branch failed with error %w", err)
6666
return
@@ -70,13 +70,13 @@ func (handler *branchesHandler) setup() error {
7070
}
7171

7272
projectStatusChecks, resp, err := handler.glClient.ExternalStatusChecks.ListProjectStatusChecks(
73-
handler.repourl.projectID, &gitlab.ListOptions{})
73+
handler.repourl.project, &gitlab.ListOptions{})
7474
if err != nil && resp.StatusCode != 404 {
7575
handler.errSetup = fmt.Errorf("request for external status checks failed with error %w", err)
7676
return
7777
}
7878

79-
projectApprovalRule, resp, err := handler.glClient.Projects.GetApprovalConfiguration(handler.repourl.projectID)
79+
projectApprovalRule, resp, err := handler.glClient.Projects.GetApprovalConfiguration(handler.repourl.project)
8080
if err != nil && resp.StatusCode != 404 {
8181
handler.errSetup = fmt.Errorf("request for project approval rule failed with %w", err)
8282
return
@@ -105,24 +105,24 @@ func (handler *branchesHandler) getDefaultBranch() (*clients.BranchRef, error) {
105105
}
106106

107107
func (handler *branchesHandler) getBranch(branch string) (*clients.BranchRef, error) {
108-
bran, _, err := handler.glClient.Branches.GetBranch(handler.repourl.projectID, branch)
108+
bran, _, err := handler.glClient.Branches.GetBranch(handler.repourl.project, branch)
109109
if err != nil {
110110
return nil, fmt.Errorf("error getting branch in branchsHandler.getBranch: %w", err)
111111
}
112112

113113
if bran.Protected {
114-
protectedBranch, _, err := handler.glClient.ProtectedBranches.GetProtectedBranch(handler.repourl.projectID, bran.Name)
114+
protectedBranch, _, err := handler.glClient.ProtectedBranches.GetProtectedBranch(handler.repourl.project, bran.Name)
115115
if err != nil {
116116
return nil, fmt.Errorf("request for protected branch failed with error %w", err)
117117
}
118118

119119
projectStatusChecks, resp, err := handler.glClient.ExternalStatusChecks.ListProjectStatusChecks(
120-
handler.repourl.projectID, &gitlab.ListOptions{})
120+
handler.repourl.project, &gitlab.ListOptions{})
121121
if err != nil && resp.StatusCode != 404 {
122122
return nil, fmt.Errorf("request for external status checks failed with error %w", err)
123123
}
124124

125-
projectApprovalRule, resp, err := handler.glClient.Projects.GetApprovalConfiguration(handler.repourl.projectID)
125+
projectApprovalRule, resp, err := handler.glClient.Projects.GetApprovalConfiguration(handler.repourl.project)
126126
if err != nil && resp.StatusCode != 404 {
127127
return nil, fmt.Errorf("request for project approval rule failed with %w", err)
128128
}

clients/gitlabrepo/checkruns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (handler *checkrunsHandler) init(repourl *repoURL) {
3434

3535
func (handler *checkrunsHandler) listCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
3636
pipelines, _, err := handler.glClient.Pipelines.ListProjectPipelines(
37-
handler.repourl.projectID, &gitlab.ListProjectPipelinesOptions{})
37+
handler.repourl.project, &gitlab.ListProjectPipelinesOptions{})
3838
if err != nil {
3939
return nil, fmt.Errorf("request for pipelines returned error: %w", err)
4040
}

clients/gitlabrepo/client.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ type Client struct {
5252
languages *languagesHandler
5353
licenses *licensesHandler
5454
ctx context.Context
55-
// tarball tarballHandler
56-
commitDepth int
55+
commitDepth int
5756
}
5857

5958
// InitRepo sets up the GitLab project in local storage for improving performance and GitLab token usage efficiency.
@@ -64,9 +63,10 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
6463
}
6564

6665
// Sanity check.
67-
repo, _, err := client.glClient.Projects.GetProject(glRepo.projectID, &gitlab.GetProjectOptions{})
66+
proj := fmt.Sprintf("%s/%s", glRepo.owner, glRepo.project)
67+
repo, _, err := client.glClient.Projects.GetProject(proj, &gitlab.GetProjectOptions{})
6868
if err != nil {
69-
return sce.WithMessage(sce.ErrRepoUnreachable, err.Error())
69+
return sce.WithMessage(sce.ErrRepoUnreachable, proj+"\t"+err.Error())
7070
}
7171
if commitDepth <= 0 {
7272
client.commitDepth = 30 // default
@@ -75,8 +75,9 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
7575
}
7676
client.repo = repo
7777
client.repourl = &repoURL{
78-
hostname: inputRepo.URI(),
79-
projectID: fmt.Sprint(repo.ID),
78+
scheme: glRepo.scheme,
79+
host: glRepo.host,
80+
project: fmt.Sprint(repo.ID),
8081
defaultBranch: repo.DefaultBranch,
8182
commitSHA: commitSHA,
8283
}
@@ -127,13 +128,11 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
127128
// Init languagesHandler
128129
client.licenses.init(client.repourl)
129130

130-
// Init tarballHandler.
131-
// client.tarball.init(client.ctx, client.repourl, client.repo, commitSHA)
132131
return nil
133132
}
134133

135134
func (client *Client) URI() string {
136-
return fmt.Sprintf("%s/%s/%s", client.repourl.hostname, client.repourl.owner, client.repourl.projectID)
135+
return fmt.Sprintf("%s/%s/%s", client.repourl.host, client.repourl.owner, client.repourl.project)
137136
}
138137

139138
func (client *Client) LocalPath() (string, error) {
@@ -222,7 +221,7 @@ func (client *Client) Close() error {
222221
}
223222

224223
func CreateGitlabClientWithToken(ctx context.Context, token string, repo clients.Repo) (clients.RepoClient, error) {
225-
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(repo.URI()))
224+
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(repo.Host()))
226225
if err != nil {
227226
return nil, fmt.Errorf("could not create gitlab client with error: %w", err)
228227
}
@@ -269,10 +268,22 @@ func CreateGitlabClientWithToken(ctx context.Context, token string, repo clients
269268
languages: &languagesHandler{
270269
glClient: client,
271270
},
271+
licenses: &licensesHandler{},
272272
}, nil
273273
}
274274

275275
// TODO(#2266): implement CreateOssFuzzRepoClient.
276276
func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.RepoClient, error) {
277277
return nil, fmt.Errorf("%w, oss fuzz currently only supported for github repos", clients.ErrUnsupportedFeature)
278278
}
279+
280+
// DetectGitLab: check whether the repoURI is a GitLab URI
281+
// Makes HTTP request to GitLab API.
282+
func DetectGitLab(repoURI string) bool {
283+
var repo repoURL
284+
if err := repo.parse(repoURI); err != nil {
285+
return false
286+
}
287+
288+
return repo.IsValid() == nil
289+
}

clients/gitlabrepo/commits.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (handler *commitsHandler) init(repourl *repoURL) {
4141
// nolint: gocognit
4242
func (handler *commitsHandler) setup() error {
4343
handler.once.Do(func() {
44-
commits, _, err := handler.glClient.Commits.ListCommits(handler.repourl.projectID, &gitlab.ListCommitsOptions{})
44+
commits, _, err := handler.glClient.Commits.ListCommits(handler.repourl.project, &gitlab.ListCommitsOptions{})
4545
if err != nil {
4646
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
4747
return
@@ -76,7 +76,7 @@ func (handler *commitsHandler) setup() error {
7676

7777
// Commits are able to be a part of multiple merge requests, but the only one that will be important
7878
// here is the earliest one.
79-
mergeRequests, _, err := handler.glClient.Commits.ListMergeRequestsByCommit(handler.repourl.projectID, commit.ID)
79+
mergeRequests, _, err := handler.glClient.Commits.ListMergeRequestsByCommit(handler.repourl.project, commit.ID)
8080
if err != nil {
8181
handler.errSetup = fmt.Errorf("unable to find merge requests associated with commit: %w", err)
8282
return

clients/gitlabrepo/contributors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (handler *contributorsHandler) setup() error {
4646
return
4747
}
4848
contribs, _, err := handler.glClient.Repositories.Contributors(
49-
handler.repourl.projectID, &gitlab.ListContributorsOptions{})
49+
handler.repourl.project, &gitlab.ListContributorsOptions{})
5050
if err != nil {
5151
handler.errSetup = fmt.Errorf("error during ListContributors: %w", err)
5252
return

0 commit comments

Comments
 (0)