Search in sources :

Example 1 with Resource

use of com.checkmarx.flow.dto.azure.Resource in project cx-flow by checkmarx-ltd.

the class TfsController method pullPushRequest.

@PostMapping(value = { "/{product}/tfs/pull", "/tfs/pull", "/{product}/tfs/push", "/tfs/push" })
public ResponseEntity<EventResponse> pullPushRequest(HttpServletRequest httpRequest, @RequestBody PullEvent body, @RequestHeader(value = AUTHORIZATION) String auth, @PathVariable(value = "product", required = false) String product, ControllerRequest controllerRequest, AdoDetailsRequest adoDetailsRequest) {
    String action = getAction(httpRequest);
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    if (log.isInfoEnabled()) {
        log.info(String.format("Processing TFS %s request", action));
    }
    validateBasicAuth(auth);
    Resource resource = body.getResource();
    controllerRequest = ensureNotNull(controllerRequest);
    adoDetailsRequest = ensureDetailsNotNull(adoDetailsRequest);
    if (ACTION_PULL.equals(action) && !body.getEventType().equals(PULL_EVENT)) {
        log.info("Pull requested not processed.  Event was not 'opened' ({})", body.getEventType());
        return ResponseEntity.accepted().body(EventResponse.builder().message("No processing occurred for updates to Pull Request").success(true).build());
    }
    FlowOverride o = ScanUtils.getMachinaOverride(Optional.ofNullable(controllerRequest.getOverride()).orElse(null));
    Repository repository = resource.getRepository();
    String app = repository.getName();
    if (app.startsWith(properties.getTestRepository())) {
        log.info("Handling TFS Test Event");
        return ResponseEntity.ok(EventResponse.builder().message("Test Event").success(true).build());
    }
    Optional.ofNullable(controllerRequest.getAppOnly()).ifPresent(flowProperties::setTrackApplicationOnly);
    FilterConfiguration filter = filterFactory.getFilter(controllerRequest, flowProperties);
    ScanRequestBuilder requestBuilder = ScanRequest.builder().application(Optional.ofNullable(controllerRequest.getApplication()).orElse(app)).product(getProductForName(product)).project(Optional.ofNullable(controllerRequest.getProject()).orElse(null)).team(Optional.ofNullable(controllerRequest.getTeam()).orElse(null)).namespace(repository.getProject().getName().replace(" ", "_")).repoName(repository.getName()).repoType(ScanRequest.Repository.ADO).scanPreset(controllerRequest.getPreset()).incremental(controllerRequest.getIncremental()).excludeFolders(controllerRequest.getExcludeFolders()).excludeFiles(controllerRequest.getExcludeFiles()).filter(filter);
    if (ACTION_PULL.equals(action)) {
        BugTracker.Type bugType = Optional.ofNullable(controllerRequest.getBug()).map(theBug -> ScanUtils.getBugTypeEnum(theBug, flowProperties.getBugTrackerImpl())).orElse(BugTracker.Type.ADOPULL);
        Optional.ofNullable(controllerRequest.getAppOnly()).ifPresent(flowProperties::setTrackApplicationOnly);
        BugTracker bugTracker = ScanUtils.getBugTracker(Optional.ofNullable(controllerRequest.getAssignee()).orElse(null), bugType, jiraProperties, Optional.ofNullable(controllerRequest.getBug()).orElse(null));
        requestBuilder.refs(resource.getSourceRefName()).repoUrl(repository.getWebUrl()).repoUrlWithAuth(addTokenToUrl(repository.getWebUrl(), properties.getToken())).mergeNoteUri(resource.getUrl().concat("/threads")).branch(ScanUtils.getBranchFromRef(resource.getSourceRefName())).mergeTargetBranch(ScanUtils.getBranchFromRef(resource.getTargetRefName())).email(null).bugTracker(bugTracker);
    } else if (ACTION_PUSH.equals(action)) {
        String bug = Optional.ofNullable(controllerRequest.getBug()).orElse(flowProperties.getBugTracker());
        BugTracker.Type bugType = ScanUtils.getBugTypeEnum(bug, flowProperties.getBugTrackerImpl());
        BugTracker bugTracker = ScanUtils.getBugTracker(Optional.ofNullable(controllerRequest.getAssignee()).orElse(null), bugType, jiraProperties, Optional.ofNullable(controllerRequest.getBug()).orElse(null));
        requestBuilder.refs(resource.getRefUpdates().get(0).getName()).repoUrl(repository.getRemoteUrl()).repoUrlWithAuth(addTokenToUrl(repository.getRemoteUrl(), properties.getToken())).branch(ScanUtils.getBranchFromRef(resource.getRefUpdates().get(0).getName())).defaultBranch(repository.getDefaultBranch()).email(determineEmails(resource)).bugTracker(bugTracker);
    }
    ScanRequest request = requestBuilder.build();
    request = configOverrider.overrideScanRequestProperties(o, request);
    if (ACTION_PULL.equals(action)) {
        request.putAdditionalMetadata("statuses_url", resource.getUrl().concat("/statuses"));
    }
    addMetadataToScanRequest(adoDetailsRequest, request);
    request.putAdditionalMetadata(HTMLHelper.WEB_HOOK_PAYLOAD, body.toString());
    request.setId(uid);
    // only initiate scan/automation if target branch is applicable
    List<String> branches = new ArrayList<>();
    Optional<List<String>> branch = Optional.ofNullable(controllerRequest.getBranch());
    if (branch.isPresent()) {
        branches.addAll(branch.get());
    } else if (CollectionUtils.isNotEmpty(flowProperties.getBranches())) {
        branches.addAll(flowProperties.getBranches());
    }
    if (helperService.isBranch2Scan(request, branches)) {
        flowService.initiateAutomation(request);
    }
    return ResponseEntity.accepted().body(EventResponse.builder().message("Scan Request Successfully Submitted").success(true).build());
}
Also used : ADOProperties(com.checkmarx.flow.config.ADOProperties) java.util(java.util) ConfigurationOverrider(com.checkmarx.flow.service.ConfigurationOverrider) RequiredArgsConstructor(lombok.RequiredArgsConstructor) FlowProperties(com.checkmarx.flow.config.FlowProperties) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) HTMLHelper(com.checkmarx.flow.utils.HTMLHelper) Repository(com.checkmarx.flow.dto.azure.Repository) Product(com.checkmarx.flow.dto.ScanRequest.Product) AdoDetailsRequest(com.checkmarx.flow.dto.azure.AdoDetailsRequest) InvalidTokenException(com.checkmarx.flow.exception.InvalidTokenException) HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterFactory(com.checkmarx.flow.service.FilterFactory) com.checkmarx.flow.dto(com.checkmarx.flow.dto) FlowService(com.checkmarx.flow.service.FlowService) Resource(com.checkmarx.flow.dto.azure.Resource) Collectors(java.util.stream.Collectors) JiraProperties(com.checkmarx.flow.config.JiraProperties) HelperService(com.checkmarx.flow.service.HelperService) PullEvent(com.checkmarx.flow.dto.azure.PullEvent) Slf4j(lombok.extern.slf4j.Slf4j) MDC(org.slf4j.MDC) ScanUtils(com.checkmarx.flow.utils.ScanUtils) FlowConstants(com.checkmarx.flow.constants.FlowConstants) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) FilterConfiguration(com.checkmarx.sdk.dto.filtering.FilterConfiguration) ScanRequestBuilder(com.checkmarx.flow.dto.ScanRequest.ScanRequestBuilder) Resource(com.checkmarx.flow.dto.azure.Resource) FilterConfiguration(com.checkmarx.sdk.dto.filtering.FilterConfiguration) Repository(com.checkmarx.flow.dto.azure.Repository) ScanRequestBuilder(com.checkmarx.flow.dto.ScanRequest.ScanRequestBuilder)

Aggregations

ADOProperties (com.checkmarx.flow.config.ADOProperties)1 FlowProperties (com.checkmarx.flow.config.FlowProperties)1 JiraProperties (com.checkmarx.flow.config.JiraProperties)1 FlowConstants (com.checkmarx.flow.constants.FlowConstants)1 com.checkmarx.flow.dto (com.checkmarx.flow.dto)1 Product (com.checkmarx.flow.dto.ScanRequest.Product)1 ScanRequestBuilder (com.checkmarx.flow.dto.ScanRequest.ScanRequestBuilder)1 AdoDetailsRequest (com.checkmarx.flow.dto.azure.AdoDetailsRequest)1 PullEvent (com.checkmarx.flow.dto.azure.PullEvent)1 Repository (com.checkmarx.flow.dto.azure.Repository)1 Resource (com.checkmarx.flow.dto.azure.Resource)1 InvalidTokenException (com.checkmarx.flow.exception.InvalidTokenException)1 ConfigurationOverrider (com.checkmarx.flow.service.ConfigurationOverrider)1 FilterFactory (com.checkmarx.flow.service.FilterFactory)1 FlowService (com.checkmarx.flow.service.FlowService)1 HelperService (com.checkmarx.flow.service.HelperService)1 HTMLHelper (com.checkmarx.flow.utils.HTMLHelper)1 ScanUtils (com.checkmarx.flow.utils.ScanUtils)1 FilterConfiguration (com.checkmarx.sdk.dto.filtering.FilterConfiguration)1 java.util (java.util)1