Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Gitlab: add cron repos #3208

Merged
merged 7 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ type Client struct {
commitDepth int
}

var errRepoAccess = errors.New("repo inaccessible")

// Raise an error if repository access level is private or disabled.
func checkRepoInaccessible(repo *gitlab.Project) error {
if (repo.RepositoryAccessLevel == gitlab.PrivateAccessControl) ||
(repo.RepositoryAccessLevel == gitlab.DisabledAccessControl) {
return fmt.Errorf("%w: %s access level %s",
errRepoAccess, repo.PathWithNamespace, string(repo.RepositoryAccessLevel),
)
}

return nil
}

// InitRepo sets up the GitLab project in local storage for improving performance and GitLab token usage efficiency.
func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error {
glRepo, ok := inputRepo.(*repoURL)
Expand All @@ -71,6 +85,11 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
if err != nil {
return sce.WithMessage(sce.ErrRepoUnreachable, proj+"\t"+err.Error())
}

if err = checkRepoInaccessible(repo); err != nil {
return sce.WithMessage(sce.ErrRepoUnreachable, err.Error())
}

if commitDepth <= 0 {
client.commitDepth = 30 // default
} else {
Expand Down
82 changes: 82 additions & 0 deletions clients/gitlabrepo/client_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2023 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gitlabrepo

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/xanzy/go-gitlab"
)

var _ = Describe("E2E TEST: gitlabrepo.commitsHandler", func() {
Context("Test whether commits are listed - GitLab", func() {
It("returns that the repo is inaccessible", func() {
repo, err := MakeGitlabRepo("https://gitlab.com/fdroid/fdroidclient")
Expect(err).Should(BeNil())

client, err := CreateGitlabClient(context.Background(), repo.Host())
Expect(err).Should(BeNil())

err = client.InitRepo(repo, "a4bbef5c70fd2ac7c15437a22ef0f9ef0b447d08", 20)
Expect(err).Should(BeNil())

c, err := client.ListCommits()
Expect(err).Should(BeNil())

Expect(len(c)).Should(BeNumerically(">", 0))
})
})
})

var _ = Describe("E2E TEST: gitlabrepo.client", func() {
Context("checkRepoInaccessible", func() {
It("returns that the repo is inaccessible - GitLab", func() {
skipIfTokenIsNot(gitlabPATTokenType, "GitLab only")
repo, err := MakeGitlabRepo("https://gitlab.com/ossf-test/private-project")
Expect(err).Should(BeNil())

client, err := CreateGitlabClient(context.Background(), repo.Host())
Expect(err).Should(BeNil())

glRepo, ok := repo.(*repoURL)
Expect(ok).Should(BeTrue())

// Sanity check.
glcl, ok := client.(*Client)
Expect(ok).Should(BeTrue())

path := fmt.Sprintf("%s/%s", glRepo.owner, glRepo.project)
proj, _, err := glcl.glClient.Projects.GetProject(path, &gitlab.GetProjectOptions{})
Expect(err).Should(BeNil())

err = checkRepoInaccessible(proj)
Expect(err).ShouldNot(BeNil())
})

It("should initialize repos without error - GitLab", func() {
repo, err := MakeGitlabRepo("https://gitlab.com/fdroid/fdroidclient")
Expect(err).Should(BeNil())

client, err := CreateGitlabClient(context.Background(), repo.Host())
Expect(err).Should(BeNil())

err = client.InitRepo(repo, "a4bbef5c70fd2ac7c15437a22ef0f9ef0b447d08", 20)
Expect(err).Should(BeNil())
})
})
})
55 changes: 0 additions & 55 deletions clients/gitlabrepo/client_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion cron/internal/controller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ RUN CGO_ENABLED=0 make build-controller

FROM gcr.io/distroless/base:nonroot@sha256:99133cb0878bb1f84d1753957c6fd4b84f006f2798535de22ebf7ba170bbf434
COPY ./cron/internal/data/projects*csv cron/internal/data/
COPY ./cron/internal/data/gitlab-projects-selected.csv cron/internal/data/
COPY ./cron/internal/data/gitlab-projects-releasetest.csv cron/internal/data/
COPY ./cron/internal/data/gitlab-projects.csv cron/internal/data/
COPY --from=shuffle /src/cron/internal/data/projects.release.csv cron/internal/data/projects.release.csv
COPY --from=controller /src/cron/internal/controller/controller cron/internal/controller/controller
ENTRYPOINT ["cron/internal/controller/controller"]
Loading