Skip to content

Commit d488de3

Browse files
committed
refactor: add GroupOrOwner to Project struct
1 parent aa333ad commit d488de3

File tree

8 files changed

+48
-54
lines changed

8 files changed

+48
-54
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
// Download 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).DownloadRepository(project, dir); err != nil {
201+
if err := s.repoService.Provide(project.Repository).Download(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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestScanNonVulnerableProject(t *testing.T) {
5252
mockClient := &mockClient{}
5353
mockClient.On("GetProjectList", []string{"group/to/scan"}).Return([]repository.Project{{Name: "Hello World", RepoUrl: "https://gitlab.com/group/to/scan.git", Repository: repository.Gitlab}}, nil)
5454
mockClient.On("CloseVulnerabilityIssue", mock.Anything).Return(nil)
55-
mockClient.On("DownloadRepository", "https://gitlab.com/group/to/scan.git", mock.Anything).Return(nil)
55+
mockClient.On("Download", "https://gitlab.com/group/to/scan.git", mock.Anything).Return(nil)
5656

5757
mockRepoService := &mockRepoService{}
5858
mockRepoService.On("Provide", repository.Gitlab).Return(mockClient)
@@ -87,7 +87,7 @@ func TestScanVulnerableProject(t *testing.T) {
8787
mockClient := &mockClient{}
8888
mockClient.On("GetProjectList", []string{"group/to/scan"}).Return([]repository.Project{{Name: "Hello World", RepoUrl: "https://gitlab.com/group/to/scan.git", Repository: repository.Gitlab}}, nil)
8989
mockClient.On("OpenVulnerabilityIssue", mock.Anything, mock.Anything).Return(&repository.Issue{}, nil)
90-
mockClient.On("DownloadRepository", "https://gitlab.com/group/to/scan.git", mock.Anything).Return(nil)
90+
mockClient.On("Download", "https://gitlab.com/group/to/scan.git", mock.Anything).Return(nil)
9191

9292
mockRepoService := &mockRepoService{}
9393
mockRepoService.On("Provide", repository.Gitlab).Return(mockClient)
@@ -239,7 +239,7 @@ 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) DownloadRepository(project repository.Project, dir string) error {
242+
func (c *mockClient) Download(project repository.Project, dir string) error {
243243
args := c.Called(project.RepoUrl, dir)
244244
return args.Error(0)
245245
}

internal/publish/to_issue_test.go

Lines changed: 1 addition & 1 deletion
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) DownloadRepository(project repository.Project, dir string) error {
245+
func (c *mockGitlabService) Download(project repository.Project, dir string) error {
246246
args := c.Called(project.RepoUrl, dir)
247247
return args.Error(0)
248248
}

internal/repository/github/github.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,9 @@ func (s githubService) OpenVulnerabilityIssue(project repository.Project, report
7878
return nil, errors.New("OpenVulnerabilityIssue not yet implemented") // TODO #9 Add github support
7979
}
8080

81-
func (s githubService) DownloadRepository(project repository.Project, dir string) (err error) {
82-
// Extract owner and repo from project path (format: "owner/repo")
83-
owner, repo, err := s.extractOwnerRepo(project.Path)
84-
if err != nil {
85-
return fmt.Errorf("failed to extract owner/repo from path %s: %w", project.Path, err)
86-
}
87-
88-
log.Debug().Str("owner", owner).Str("repo", repo).Str("dir", dir).Msg("Downloading GitHub archive")
89-
81+
func (s githubService) Download(project repository.Project, dir string) (err error) {
9082
// Get archive download URL using GitHub API
91-
archiveURL, _, err := s.client.GetArchiveLink(owner, repo, github.Tarball, &github.RepositoryContentGetOptions{})
83+
archiveURL, _, err := s.client.GetArchiveLink(project.GroupOrOwner, project.Name, github.Tarball, &github.RepositoryContentGetOptions{})
9284
if err != nil {
9385
return fmt.Errorf("failed to get GitHub archive link: %w", err)
9486
}
@@ -225,14 +217,22 @@ func derefRepoPtrs(owner string, repoPtrs []*github.Repository) (repos []github.
225217
}
226218

227219
func mapGithubProject(r github.Repository) repository.Project {
220+
// log.Warn().Str("id", fmt.Sprint(r.GetID())).Str("name", r.GetName()).Str("full_name", r.GetFullName()).Str("owner", r.GetOwner().Name).Msg("Mapping GitHub project")
221+
var groupName = ""
222+
owner := r.GetOwner()
223+
if owner != nil {
224+
groupName = owner.GetLogin()
225+
}
226+
228227
return repository.Project{
229-
ID: int(valueOrEmpty(r.ID)),
230-
Name: valueOrEmpty(r.Name),
231-
Path: valueOrEmpty(r.FullName),
232-
Slug: valueOrEmpty(r.Name),
233-
WebURL: valueOrEmpty(r.HTMLURL),
234-
RepoUrl: valueOrEmpty(r.HTMLURL),
235-
Repository: repository.Github,
228+
ID: int(valueOrEmpty(r.ID)),
229+
Name: valueOrEmpty(r.Name),
230+
GroupOrOwner: valueOrEmpty(&groupName),
231+
Path: valueOrEmpty(r.FullName),
232+
Slug: valueOrEmpty(r.Name),
233+
WebURL: valueOrEmpty(r.HTMLURL),
234+
RepoUrl: valueOrEmpty(r.HTMLURL),
235+
Repository: repository.Github,
236236
}
237237
}
238238

@@ -243,12 +243,3 @@ func valueOrEmpty[T interface{}](val *T) (r T) {
243243

244244
return r
245245
}
246-
247-
// extractOwnerRepo extracts owner and repo name from GitHub project path
248-
func (s githubService) extractOwnerRepo(path string) (owner, repo string, err error) {
249-
parts := strings.Split(path, "/")
250-
if len(parts) != 2 {
251-
return "", "", fmt.Errorf("invalid GitHub project path format, expected 'owner/repo', got: %s", path)
252-
}
253-
return parts[0], parts[1], nil
254-
}

internal/repository/github/github_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (c *mockService) GetArchiveLink(owner string, repo string, archiveFormat gi
149149
return args.Get(0).(*url.URL), r, args.Error(2)
150150
}
151151

152-
func TestDownloadRepository(t *testing.T) {
152+
func TestDownload(t *testing.T) {
153153
// Create temporary directory for testing
154154
tempDir, err := os.MkdirTemp("", "sheriff-clone-test-")
155155
require.NoError(t, err)
@@ -186,12 +186,13 @@ func TestDownloadRepository(t *testing.T) {
186186

187187
// Create a test project
188188
testProject := repository.Project{
189-
ID: 123,
190-
Name: "test-project",
191-
Path: "owner/repo",
189+
ID: 123,
190+
Name: "test-project",
191+
GroupOrOwner: "owner",
192+
Path: "owner/repo",
192193
}
193194

194-
err = svc.DownloadRepository(testProject, tempDir)
195+
err = svc.Download(testProject, tempDir)
195196

196197
// Verify no errors
197198
assert.NoError(t, err)

internal/repository/gitlab/gitlab.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s gitlabService) OpenVulnerabilityIssue(project repository.Project, report
116116
return
117117
}
118118

119-
func (s gitlabService) DownloadRepository(project repository.Project, dir string) (err error) {
119+
func (s gitlabService) Download(project repository.Project, dir string) (err error) {
120120
archiveData, _, err := s.client.Archive(project.ID, &gitlab.ArchiveOptions{})
121121
if err != nil {
122122
return fmt.Errorf("failed to download archive: %w", err)
@@ -321,13 +321,14 @@ func dereferenceProjectsPointers(projects []*gitlab.Project) (filteredProjects [
321321

322322
func mapProject(p gitlab.Project) repository.Project {
323323
return repository.Project{
324-
ID: p.ID,
325-
Name: p.Name,
326-
Slug: p.Path,
327-
Path: p.PathWithNamespace,
328-
WebURL: p.WebURL,
329-
RepoUrl: p.HTTPURLToRepo,
330-
Repository: repository.Gitlab,
324+
ID: p.ID,
325+
Name: p.Name,
326+
Slug: p.Path,
327+
GroupOrOwner: p.Namespace.Name,
328+
Path: p.PathWithNamespace,
329+
WebURL: p.WebURL,
330+
RepoUrl: p.HTTPURLToRepo,
331+
Repository: repository.Gitlab,
331332
}
332333
}
333334

internal/repository/gitlab/gitlab_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func TestDereferenceProjectsPointers(t *testing.T) {
202202
assert.Equal(t, 2, dereferencedProjects[1].ID)
203203
assert.Equal(t, 2, errCount)
204204
}
205-
func TestDownloadRepository(t *testing.T) {
205+
func TestDownload(t *testing.T) {
206206
// Create temporary directory for testing
207207
tempDir, err := os.MkdirTemp("", "sheriff-clone-test-")
208208
require.NoError(t, err)
@@ -223,7 +223,7 @@ func TestDownloadRepository(t *testing.T) {
223223
Path: "group/project",
224224
}
225225

226-
err = svc.DownloadRepository(testProject, tempDir)
226+
err = svc.Download(testProject, tempDir)
227227

228228
// Verify no errors
229229
assert.NoError(t, err)

internal/repository/repository.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const (
1010
)
1111

1212
type Project struct {
13-
ID int
14-
Name string
15-
Slug string
16-
Path string
17-
WebURL string
18-
RepoUrl string
19-
Repository RepositoryType
13+
ID int
14+
Name string
15+
Slug string
16+
GroupOrOwner string
17+
Path string
18+
WebURL string
19+
RepoUrl string
20+
Repository RepositoryType
2021
}
2122

2223
type Issue struct {
@@ -30,5 +31,5 @@ type IRepositoryService interface {
3031
GetProjectList(paths []string) (projects []Project, warn error)
3132
CloseVulnerabilityIssue(project Project) error
3233
OpenVulnerabilityIssue(project Project, report string) (*Issue, error)
33-
DownloadRepository(project Project, dir string) error
34+
Download(project Project, dir string) error
3435
}

0 commit comments

Comments
 (0)