77 "fmt"
88 "os"
99 "sheriff/internal/config"
10- "sheriff/internal/git"
1110 "sheriff/internal/publish"
12- "sheriff/internal/repo"
11+ "sheriff/internal/repository"
12+ "sheriff/internal/repository/provider"
1313 "sheriff/internal/scanner"
1414 "sheriff/internal/slack"
1515 "sync"
@@ -29,21 +29,19 @@ type securityPatroller interface {
2929
3030// sheriffService is the implementation of the SecurityPatroller interface.
3131type sheriffService struct {
32- gitlabService repo.IService
33- slackService slack.IService
34- gitService git.IService
35- osvService scanner.VulnScanner [scanner.OsvReport ]
32+ repoService provider.IProvider
33+ slackService slack.IService
34+ osvService scanner.VulnScanner [scanner.OsvReport ]
3635}
3736
3837// New creates a new securityPatroller service.
3938// It contains the main "loop" logic of this tool.
4039// A "patrol" is defined as scanning GitLab groups for vulnerabilities and publishing reports where needed.
41- func New (gitlabService repo. IService , slackService slack. IService , gitService git .IService , osvService scanner.VulnScanner [scanner.OsvReport ]) securityPatroller {
40+ func New (repoService provider. IProvider , slackService slack.IService , osvService scanner.VulnScanner [scanner.OsvReport ]) securityPatroller {
4241 return & sheriffService {
43- gitlabService : gitlabService ,
44- slackService : slackService ,
45- gitService : gitService ,
46- osvService : osvService ,
42+ repoService : repoService ,
43+ slackService : slackService ,
44+ osvService : osvService ,
4745 }
4846}
4947
@@ -65,7 +63,7 @@ func (s *sheriffService) Patrol(args config.PatrolConfig) (warn error, err error
6563
6664 if args .ReportToIssue {
6765 log .Info ().Msg ("Creating issue in affected projects" )
68- if gwarn := publish .PublishAsGitlabIssues (scanReports , s .gitlabService ); gwarn != nil {
66+ if gwarn := publish .PublishAsIssues (scanReports , s .repoService ); gwarn != nil {
6967 gwarn = errors .Join (errors .New ("errors occured when creating issues" ), gwarn )
7068 warn = errors .Join (gwarn , warn )
7169 }
@@ -107,13 +105,7 @@ func (s *sheriffService) scanAndGetReports(locations []config.ProjectLocation) (
107105 defer os .RemoveAll (tempScanDir )
108106 log .Info ().Str ("path" , tempScanDir ).Msg ("Created temporary directory" )
109107
110- gitlabLocs := pie .Map (
111- pie .Filter (locations , func (v config.ProjectLocation ) bool { return v .Type == repo .Gitlab }),
112- func (v config.ProjectLocation ) string { return v .Path },
113- )
114- log .Info ().Strs ("locations" , gitlabLocs ).Msg ("Getting the list of projects to scan" )
115-
116- projects , pwarn := s .gitlabService .GetProjectList (gitlabLocs )
108+ projects , pwarn := s .getProjectList (locations )
117109 if pwarn != nil {
118110 pwarn = errors .Join (errors .New ("errors occured when getting project list" ), pwarn )
119111 warn = errors .Join (pwarn , warn )
@@ -152,18 +144,51 @@ func (s *sheriffService) scanAndGetReports(locations []config.ProjectLocation) (
152144 return
153145}
154146
147+ func (s * sheriffService ) getProjectList (locs []config.ProjectLocation ) (projects []repository.Project , warn error ) {
148+ gitlabLocs := pie .Map (
149+ pie .Filter (locs , func (loc config.ProjectLocation ) bool { return loc .Type == repository .Gitlab }),
150+ func (loc config.ProjectLocation ) string { return loc .Path },
151+ )
152+ githubLocs := pie .Map (
153+ pie .Filter (locs , func (loc config.ProjectLocation ) bool { return loc .Type == repository .Github }),
154+ func (loc config.ProjectLocation ) string { return loc .Path },
155+ )
156+
157+ if len (gitlabLocs ) > 0 {
158+ log .Info ().Strs ("locations" , gitlabLocs ).Msg ("Getting the list of projects from gitlab to scan" )
159+ gitlabProjects , err := s .repoService .Provide (repository .Gitlab ).GetProjectList (gitlabLocs )
160+ if err != nil {
161+ warn = errors .Join (errors .New ("non-critical errors encountered when scanning for gitlab projects" ), err )
162+ }
163+
164+ projects = append (projects , gitlabProjects ... )
165+ }
166+
167+ if len (githubLocs ) > 0 {
168+ log .Info ().Strs ("locations" , githubLocs ).Msg ("Getting the list of projects from github to scan" )
169+ githubProjects , err := s .repoService .Provide (repository .Github ).GetProjectList (githubLocs )
170+ if err != nil {
171+ warn = errors .Join (errors .New ("non-critical errors encountered when scanning for github projects" ), err )
172+ }
173+
174+ projects = append (projects , githubProjects ... )
175+ }
176+
177+ return
178+ }
179+
155180// scanProject scans a project for vulnerabilities using the osv scanner.
156- func (s * sheriffService ) scanProject (project repo .Project ) (report * scanner.Report , err error ) {
181+ func (s * sheriffService ) scanProject (project repository .Project ) (report * scanner.Report , err error ) {
157182 dir , err := os .MkdirTemp (tempScanDir , fmt .Sprintf ("%v-" , project .Name ))
158183 if err != nil {
159184 return nil , errors .Join (errors .New ("failed to create project temporary directory" ), err )
160185 }
161186 defer os .RemoveAll (dir )
162187
163188 // Clone the project
164- log .Info ().Str ("project" , project .Path ).Str ("dir" , dir ).Msg ("Cloning project" )
165- if err = s .gitService . Clone (dir , project .RepoUrl ); err != nil {
166- return nil , errors .Join (errors . New ("failed to clone project" ), err )
189+ log .Info ().Str ("project" , project .Path ).Str ("dir" , dir ).Str ( "url" , project . RepoUrl ). Msg ("Cloning project" )
190+ if err : = s .repoService . Provide ( project . Repository ). Clone (project .RepoUrl , dir ); err != nil {
191+ return nil , errors .Join (fmt . Errorf ("failed to clone project %v" , project . Path ), err )
167192 }
168193
169194 config := config .GetProjectConfiguration (project .Path , dir )
0 commit comments