11package io .mixeway .mixewayflowapi .api .coderepo .service ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import com .fasterxml .jackson .databind .SerializationFeature ;
35import io .mixeway .mixewayflowapi .api .coderepo .dto .GetCodeReposResponseDto ;
4- import io .mixeway .mixewayflowapi .db .entity .CodeRepo ;
5- import io .mixeway .mixewayflowapi .db .entity .Team ;
6+ import io .mixeway .mixewayflowapi .api .coderepo .dto .RunOrchScanDetailsDto ;
7+ import io .mixeway .mixewayflowapi .api .coderepo .dto .RunOrchScanReportDto ;
8+ import io .mixeway .mixewayflowapi .api .gitlabcicd .dto .GitLabCICDDetailsResponseDto ;
9+ import io .mixeway .mixewayflowapi .db .entity .*;
10+ import io .mixeway .mixewayflowapi .db .repository .UserRepository ;
611import io .mixeway .mixewayflowapi .domain .coderepo .FindCodeRepoService ;
712import io .mixeway .mixewayflowapi .domain .coderepo .UpdateCodeRepoService ;
13+ import io .mixeway .mixewayflowapi .domain .finding .FindFindingService ;
814import io .mixeway .mixewayflowapi .domain .team .FindTeamService ;
915import io .mixeway .mixewayflowapi .exceptions .CodeRepoNotFoundException ;
1016import io .mixeway .mixewayflowapi .exceptions .TeamNotFoundException ;
11- import io .mixeway .mixewayflowapi .exceptions .UnauthorizedException ;
1217import io .mixeway .mixewayflowapi .scanmanager .service .ScanManagerService ;
1318import io .mixeway .mixewayflowapi .utils .PermissionFactory ;
1419import lombok .RequiredArgsConstructor ;
1520import lombok .extern .log4j .Log4j2 ;
21+ import org .aspectj .apache .bcel .classfile .Code ;
1622import org .springframework .stereotype .Service ;
1723
1824import java .security .Principal ;
19- import java .util .List ;
25+ import java .util .* ;
2026import java .util .stream .Collectors ;
2127
2228@ Service
@@ -28,6 +34,8 @@ public class CodeRepoApiService {
2834 private final PermissionFactory permissionFactory ;
2935 private final FindTeamService findTeamService ;
3036 private final UpdateCodeRepoService updateCodeRepoService ;
37+ private final UserRepository userRepository ;
38+ private final FindFindingService findFindingService ;
3139
3240 public List <GetCodeReposResponseDto > getRepos (Principal principal ) {
3341 return findCodeRepoService .getCodeReposResponseDtos (principal );
@@ -101,4 +109,89 @@ public void renameCodeRepo(Long repoId, String newName, Principal principal) {
101109 permissionFactory .canUserManageTeam (repo .getTeam (), principal );
102110 updateCodeRepoService .renameCodeRepo (repo , newName );
103111 }
112+
113+ public Boolean isRepoInTeamById (String repoUrl , Long teamId ) {
114+ try {
115+ Optional <CodeRepo > codeRepo = findCodeRepoService .findCodeRepoByUrl (repoUrl );
116+
117+ return codeRepo .isPresent () && teamId .equals (codeRepo .get ().getTeam ().getId ());
118+ } catch (Exception e ) {
119+ log .error ("[CodeRepo] Error checking if repo '{}' belongs to team '{}': {}" , repoUrl , teamId , e .getMessage ());
120+ return false ;
121+ }
122+ }
123+
124+ public Boolean isValidApiKey (String apiKey , String repoUrl ) {
125+ Optional <UserInfo > userOptional = userRepository .findByApiKey (apiKey );
126+ Optional <Team > codeRepoTeam = findTeamService .findByRepoUrl (repoUrl );
127+
128+ if (userOptional .isEmpty () || codeRepoTeam .isEmpty ()) {
129+ return false ;
130+ }
131+
132+ List <UserInfo > teamUsers = userRepository .getUsersByTeamId (codeRepoTeam .get ().getId ());
133+
134+ if (teamUsers .contains (userOptional .get ())) {
135+ log .info ("[Team Service] User's {} API key validation succeeded" , userOptional .get ().getUsername ());
136+ return true ;
137+ } else {
138+ log .info ("[Team Service] User's {} API key validation failed" , userOptional .get ().getUsername ());
139+ return false ;
140+ }
141+ }
142+
143+ public String runScan (String repoUrl , String branch , String domain ) {
144+
145+ CodeRepo codeRepo = findCodeRepoService .findCodeRepoByUrl (repoUrl ).orElse (null );
146+
147+ CodeRepoBranch repoBranch ;
148+
149+ if (branch == null ) {
150+ repoBranch = codeRepo .getDefaultBranch ();
151+ } else {
152+ repoBranch = codeRepo .getBranches ().stream ().filter (b -> b .getName ().equals (branch )).findFirst ().orElse (null );
153+ }
154+
155+ scanManagerService .scanRepositorySync (codeRepo , repoBranch , null , null );
156+
157+ RunOrchScanReportDto scanReport = new RunOrchScanReportDto ();
158+
159+ scanReport .setRepoUrl (repoUrl );
160+ scanReport .setBranch (branch );
161+ if (domain != null ) {
162+ scanReport .setLinkToScanDetails ("https://" + domain + "/#/show-repo/" + codeRepo .getId ());
163+ }
164+
165+ ObjectMapper mapper = new ObjectMapper ();
166+ mapper .enable (SerializationFeature .INDENT_OUTPUT );
167+
168+ Collection <Finding .Status > statuses = Arrays .asList (Finding .Status .NEW , Finding .Status .EXISTING );
169+
170+ List <Finding > findings = findFindingService .findByCodeRepoAndCodeRepoBranchAndStatusIn (codeRepo , repoBranch , statuses );
171+
172+ List <RunOrchScanDetailsDto > findingsDetails = new ArrayList <>();
173+ for (Finding finding : findings ) {
174+ RunOrchScanDetailsDto scanDetails = new RunOrchScanDetailsDto ();
175+
176+ scanDetails .setName (finding .getVulnerability ().getName ());
177+ scanDetails .setDescription (finding .getVulnerability ().getDescription ());
178+ scanDetails .setExplanation (finding .getExplanation ());
179+ scanDetails .setRecommendation (finding .getVulnerability ().getRecommendation ());
180+ scanDetails .setLocation (finding .getLocation ());
181+ scanDetails .setSource (String .valueOf (finding .getSource ()));
182+ scanDetails .setStatus (String .valueOf (finding .getStatus ()));
183+ scanDetails .setSeverity (String .valueOf (finding .getSeverity ()));
184+
185+ findingsDetails .add (scanDetails );
186+ }
187+
188+ scanReport .setFindings (findingsDetails );
189+
190+ try {
191+ return mapper .writeValueAsString (scanReport );
192+ } catch (Exception e ) {
193+ log .error ("Error while serializing scan report: {}" , e .getMessage ());
194+ }
195+ return null ;
196+ }
104197}
0 commit comments