Search in sources :

Example 1 with BitbucketPushEvent

use of com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushEvent in project cx-flow by checkmarx-ltd.

the class PostWebhookController method pushRequest.

@PostMapping(value = { ROOT + "/{product}", ROOT }, headers = PUSH)
public ResponseEntity<EventResponse> pushRequest(@RequestBody String body, @PathVariable(value = "product", required = false) String product, @RequestHeader(value = AUTH_HEADER, required = false) String credentials, @RequestParam(value = TOKEN_PARAM, required = false) String token, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing BitBucket(Post Web Hook) PUSH request");
    validateCredentials(credentials, token);
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    BitbucketPushEvent event;
    try {
        event = mapper.readValue(body, BitbucketPushEvent.class);
    } catch (IOException e) {
        throw new MachinaRuntimeException(e);
    }
    String application = event.getRepository().getSlug();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    if (event.getPush().getChanges() == null || event.getPush().getChanges().length == 0) {
        log.warn("Empty commit, nothing to handle.");
        return getEmptyCommitMessage();
    }
    BitbucketPushChange change = event.getPush().getChanges()[CHANGE_INDEX];
    if (change.isClosed() && change.getNewState() == null) {
        return handleDelete(event, controllerRequest, product, application, body, uid);
    }
    BitbucketServerEventHandler handler = BitbucketServerPushHandler.builder().controllerRequest(controllerRequest).toSlug(event.getRepository().getSlug()).repositoryName(event.getRepository().getSlug()).fromSlug(event.getRepository().getSlug()).branchFromRef(!change.isCreated() ? change.getOldState().getName() : change.getNewState().getName()).toHash(change.getNewState().getTarget().getHash()).email(event.getActor().getEmailAddress()).fromProjectKey(event.getRepository().getProject().getKey()).toProjectKey(event.getRepository().getProject().getKey()).refId(change.getNewState().getName()).browseUrl(event.getRepository().getLinks().get(BROWSE_LINK_NAME).get(BROWSE_URL_INDEX).getHref()).webhookPayload(body).configProvider(this).product(product).application(application).build();
    return handler.execute(uid);
}
Also used : MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) BitbucketPushEvent(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushEvent) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BitbucketPushChange(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushChange) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

BitbucketPushChange (com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushChange)1 BitbucketPushEvent (com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushEvent)1 MachinaRuntimeException (com.checkmarx.flow.exception.MachinaRuntimeException)1 BitbucketServerEventHandler (com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1