Skip to content

Commit 2948452

Browse files
committed
fix: change interface of Clone
1 parent 3378f80 commit 2948452

File tree

7 files changed

+21
-58
lines changed

7 files changed

+21
-58
lines changed

internal/patrol/patrol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s *sheriffService) scanProject(project repository.Project) (report *scanne
198198

199199
// Clone the project
200200
log.Info().Str("project", project.Path).Str("dir", dir).Str("url", project.RepoUrl).Msg("Cloning project")
201-
if err := s.repoService.Provide(project.Repository).Clone(project.RepoUrl, dir); err != nil {
201+
if err := s.repoService.Provide(project.Repository).Clone(project, dir); err != nil {
202202
return nil, errors.Join(fmt.Errorf("failed to clone project %v", project.Path), err)
203203
}
204204

internal/patrol/patrol_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ func (c *mockClient) OpenVulnerabilityIssue(project repository.Project, report s
239239
return args.Get(0).(*repository.Issue), args.Error(1)
240240
}
241241

242-
func (c *mockClient) Clone(url string, dir string) error {
243-
args := c.Called(url, dir)
242+
func (c *mockClient) Clone(project repository.Project, dir string) error {
243+
args := c.Called(project.RepoUrl, dir)
244244
return args.Error(0)
245245
}
246246

internal/publish/to_issue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (c *mockGitlabService) OpenVulnerabilityIssue(project repository.Project, r
242242
return args.Get(0).(*repository.Issue), args.Error(1)
243243
}
244244

245-
func (c *mockGitlabService) Clone(url string, dir string) error {
246-
args := c.Called(url, dir)
245+
func (c *mockGitlabService) Clone(project repository.Project, dir string) error {
246+
args := c.Called(project.RepoUrl, dir)
247247
return args.Error(0)
248248
}

internal/repository/github/github.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func (s githubService) OpenVulnerabilityIssue(project repository.Project, report
6767
return nil, errors.New("OpenVulnerabilityIssue not yet implemented") // TODO #9 Add github support
6868
}
6969

70-
func (s githubService) Clone(url string, dir string) (err error) {
70+
func (s githubService) Clone(project repository.Project, dir string) (err error) {
7171
_, err = git.PlainClone(dir, false, &git.CloneOptions{
72-
URL: url,
72+
URL: project.RepoUrl,
7373
Auth: &http.BasicAuth{
7474
Username: "N/A",
7575
Password: s.token,

internal/repository/gitlab/gitlab.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package gitlab
22

33
import (
44
"archive/tar"
5+
"bytes"
56
"compress/gzip"
67
"errors"
78
"fmt"
89
"io"
9-
"net/url"
1010
"os"
1111
"path/filepath"
1212
"sheriff/internal/repository"
@@ -120,16 +120,8 @@ func (s gitlabService) OpenVulnerabilityIssue(project repository.Project, report
120120
return
121121
}
122122

123-
func (s gitlabService) Clone(url string, dir string) (err error) {
124-
// Extract project ID from GitLab URL
125-
projectID, err := s.extractProjectIDFromURL(url)
126-
if err != nil {
127-
return fmt.Errorf("failed to extract project ID from URL: %w", err)
128-
}
129-
130-
log.Debug().Str("url", url).Str("extractedProjectID", projectID).Msg("Attempting to download archive")
131-
132-
archiveData, _, err := s.client.Archive(projectID, &gitlab.ArchiveOptions{})
123+
func (s gitlabService) Clone(project repository.Project, dir string) (err error) {
124+
archiveData, _, err := s.client.Archive(project.ID, &gitlab.ArchiveOptions{})
133125
if err != nil {
134126
return fmt.Errorf("failed to download archive: %w", err)
135127
}
@@ -360,22 +352,9 @@ func mapIssuePtr(i *gitlab.Issue) *repository.Issue {
360352
return &issue
361353
}
362354

363-
// extractProjectIDFromURL extracts the project ID from a GitLab repository URL
364-
func (s gitlabService) extractProjectIDFromURL(repoURL string) (string, error) {
365-
// Parse URL to extract the project path
366-
u, err := url.Parse(repoURL)
367-
if err != nil {
368-
return "", err
369-
}
370-
371-
// Remove .git suffix if present and leading/trailing slashes
372-
path := strings.TrimSuffix(strings.Trim(u.Path, "/"), ".git")
373-
374-
return path, nil
375-
}
376-
377355
// extractTarGz extracts a tar.gz archive
378356
func (s gitlabService) extractTarGz(reader io.Reader, destDir string) error {
357+
// TODO(#52): Move to a separate package when implementing GitHub "clone" through downloading archives
379358
gzReader, err := gzip.NewReader(reader)
380359
if err != nil {
381360
return fmt.Errorf("failed to create gzip reader: %w", err)

internal/repository/gitlab/gitlab_test.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,44 +202,28 @@ func TestDereferenceProjectsPointers(t *testing.T) {
202202
assert.Equal(t, 2, dereferencedProjects[1].ID)
203203
assert.Equal(t, 2, errCount)
204204
}
205-
206-
func TestExtractProjectIDFromURL(t *testing.T) {
207-
svc := gitlabService{}
208-
209-
// Test basic GitLab URL with .git suffix
210-
result, err := svc.extractProjectIDFromURL("https://gitlab.com/group/project.git")
211-
assert.NoError(t, err)
212-
assert.Equal(t, "group/project", result)
213-
214-
// Test GitLab URL without .git suffix
215-
result, err = svc.extractProjectIDFromURL("https://gitlab.com/group/project")
216-
assert.NoError(t, err)
217-
assert.Equal(t, "group/project", result)
218-
219-
// Test nested group project
220-
result, err = svc.extractProjectIDFromURL("https://gitlab.com/group/subgroup/project.git")
221-
assert.NoError(t, err)
222-
assert.Equal(t, "group/subgroup/project", result)
223-
}
224-
225205
func TestCloneCompleteFlow(t *testing.T) {
226206
// Create temporary directory for testing
227207
tempDir, err := os.MkdirTemp("", "sheriff-clone-test-")
228208
require.NoError(t, err)
229209
defer os.RemoveAll(tempDir)
230210

231-
// Load the stub tar.gz file
232211
stubArchive, err := os.ReadFile("testdata/sample-archive.tar.gz")
233212
require.NoError(t, err)
234213

235-
// Setup mock client
236214
mockClient := mockClient{}
237-
mockClient.On("Archive", "group/project", mock.Anything, mock.Anything).Return(stubArchive, &gitlab.Response{}, nil)
215+
mockClient.On("Archive", 123, mock.Anything, mock.Anything).Return(stubArchive, &gitlab.Response{}, nil)
238216

239217
svc := gitlabService{client: &mockClient}
240218

241-
// Test the complete Clone flow
242-
err = svc.Clone("https://gitlab.com/group/project.git", tempDir)
219+
// Create a test project
220+
testProject := repository.Project{
221+
ID: 123,
222+
Name: "test-project",
223+
Path: "group/project",
224+
}
225+
226+
err = svc.Clone(testProject, tempDir)
243227

244228
// Verify no errors
245229
assert.NoError(t, err)

internal/repository/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ type IRepositoryService interface {
3030
GetProjectList(paths []string) (projects []Project, warn error)
3131
CloseVulnerabilityIssue(project Project) error
3232
OpenVulnerabilityIssue(project Project, report string) (*Issue, error)
33-
Clone(url string, dir string) error
33+
Clone(project Project, dir string) error
3434
}

0 commit comments

Comments
 (0)