Search in sources :

Example 1 with BitbucketServerEventHandler

use of com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler in project cx-flow by checkmarx-ltd.

the class BitbucketServerController method doMergeEvent.

private ResponseEntity<EventResponse> doMergeEvent(String body, String product, String signature, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing BitBucket MERGE request");
    verifyHmacSignature(body, signature);
    ObjectMapper mapper = new ObjectMapper();
    PullEvent event;
    try {
        event = mapper.readValue(body, PullEvent.class);
        log.debug("Successfully consumed request payload : {}", body);
    } catch (IOException e) {
        log.debug("Error occurred while consuming request payload, body {}, error {}", body, e);
        throw new MachinaRuntimeException(e);
    }
    String application = event.getPullRequest().getFromRef().getRepository().getName();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    BitbucketServerEventHandler handler = BitbucketServerMergeHandler.builder().controllerRequest(controllerRequest).application(application).currentBranch(event.getPullRequest().getFromRef().getDisplayId()).targetBranch(event.getPullRequest().getToRef().getDisplayId()).fromRefLatestCommit(event.getPullRequest().getFromRef().getLatestCommit()).fromProjectKey(event.getPullRequest().getFromRef().getRepository().getProject().getKey()).fromSlug(event.getPullRequest().getFromRef().getRepository().getSlug()).toProjectKey(event.getPullRequest().getToRef().getRepository().getProject().getKey()).toSlug(event.getPullRequest().getToRef().getRepository().getSlug()).pullRequestId(event.getPullRequest().getId().toString()).repositoryName(event.getPullRequest().getFromRef().getRepository().getName()).refId(event.getPullRequest().getFromRef().getId()).browseUrl(event.getPullRequest().getFromRef().getRepository().getLinks().getSelf().get(INDEX_FROM_SELF).getHref()).webhookPayload(body).configProvider(this).product(product).build();
    return handler.execute(uid);
}
Also used : MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) PullEvent(com.checkmarx.flow.dto.bitbucketserver.PullEvent) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with BitbucketServerEventHandler

use of com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler in project cx-flow by checkmarx-ltd.

the class BitbucketServerController method pushRequest.

/**
 * Receive Push event submitted from Bitbucket
 */
@PostMapping(value = { "/{product}", "/" }, headers = PUSH)
public ResponseEntity<EventResponse> pushRequest(@RequestBody String body, @PathVariable(value = "product", required = false) String product, @RequestHeader(value = SIGNATURE) String signature, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing BitBucket PUSH request");
    verifyHmacSignature(body, signature);
    ObjectMapper mapper = new ObjectMapper();
    PushEvent event;
    try {
        event = mapper.readValue(body, PushEvent.class);
    } catch (IOException e) {
        throw new MachinaRuntimeException(e);
    }
    if (event.getChanges().get(0).getType().equalsIgnoreCase("DELETE")) {
        log.info("Push event is associated with a Delete branch event...ignoring request");
        return handleDeleteEvent(body, uid, event, signature, product, controllerRequest);
    }
    String application = event.getRepository().getName();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    BitbucketServerEventHandler handler = BitbucketServerPushHandler.builder().controllerRequest(controllerRequest).branchFromRef(event.getChanges().get(INDEX_FROM_CHANGES).getRefId()).toHash(event.getChanges().get(INDEX_FROM_CHANGES).getToHash()).email(event.getActor().getEmailAddress()).fromProjectKey(event.getRepository().getProject().getKey()).fromSlug(event.getRepository().getSlug()).toProjectKey(event.getRepository().getProject().getKey()).toSlug(event.getRepository().getSlug()).repositoryName(event.getRepository().getName()).refId(event.getChanges().get(INDEX_FROM_CHANGES).getRefId()).browseUrl(event.getRepository().getLinks().getSelf().get(INDEX_FROM_SELF).getHref()).webhookPayload(body).configProvider(this).product(product).application(application).build();
    return handler.execute(uid);
}
Also used : PushEvent(com.checkmarx.flow.dto.bitbucketserver.PushEvent) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with BitbucketServerEventHandler

use of com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler in project cx-flow by checkmarx-ltd.

the class PostWebhookController method doMerge.

private ResponseEntity<EventResponse> doMerge(String body, String product, String credentials, String token, ControllerRequest controllerRequest, String eventType) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing BitBucket(Post Web Hook) {} request", eventType);
    validateCredentials(credentials, token);
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    BitbucketServerPullRequestEvent event;
    try {
        event = mapper.readValue(body, BitbucketServerPullRequestEvent.class);
    } catch (IOException e) {
        throw new MachinaRuntimeException(e);
    }
    String application = event.getPullrequest().getFromRef().getRepository().getSlug();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    BitbucketServerEventHandler handler = BitbucketServerMergeHandler.builder().controllerRequest(controllerRequest).currentBranch(event.getPullrequest().getFromRef().getBranch().getName()).targetBranch(event.getPullrequest().getToRef().getBranch().getName()).fromRefLatestCommit(event.getPullrequest().getFromRef().getCommit().getHash()).fromProjectKey(event.getPullrequest().getFromRef().getRepository().getProject().getKey()).fromSlug(event.getPullrequest().getFromRef().getRepository().getSlug()).toProjectKey(event.getPullrequest().getToRef().getRepository().getProject().getKey()).toSlug(event.getPullrequest().getToRef().getRepository().getSlug()).pullRequestId(event.getPullrequest().getId()).repositoryName(event.getPullrequest().getFromRef().getRepository().getSlug()).refId(event.getPullrequest().getFromRef().getBranch().getName()).browseUrl(event.getPullrequest().getFromRef().getRepository().getLinks().get("self").get(BROWSE_URL_INDEX).getHref()).webhookPayload(body).configProvider(this).product(product).application(application).build();
    return handler.execute(uid);
}
Also used : BitbucketServerPullRequestEvent(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketServerPullRequestEvent) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with BitbucketServerEventHandler

use of com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler in project cx-flow by checkmarx-ltd.

the class PostWebhookController method handleDelete.

private ResponseEntity<EventResponse> handleDelete(BitbucketPushEvent event, ControllerRequest controllerRequest, String product, String application, String body, String uid) {
    BitbucketPushChange change = event.getPush().getChanges()[CHANGE_INDEX];
    log.debug("{} {} deleted in repository {} at last commit {}", change.getOldState().getType(), change.getOldState().getName(), event.getRepository().getFullName(), change.getOldState().getTarget().getHash());
    if (change.getOldState().getType().compareTo("branch") != 0)
        return BitbucketServerDeleteHandler.getSuccessMessage();
    BitbucketServerEventHandler handler = BitbucketServerDeleteHandler.builder().controllerRequest(controllerRequest).webhookPayload(body).configProvider(this).application(application).product(product).repositoryName(event.getRepository().getSlug()).branchNameForDelete(change.getOldState().getName()).fromProjectKey(event.getRepository().getProject().getKey()).build();
    return handler.execute(uid);
}
Also used : BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) BitbucketPushChange(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushChange)

Example 5 with BitbucketServerEventHandler

use of com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler in project cx-flow by checkmarx-ltd.

the class BitbucketServerController method handleDeleteEvent.

public ResponseEntity<EventResponse> handleDeleteEvent(String body, String uid, PushEvent event, String signature, String product, ControllerRequest controllerRequest) {
    log.info("Processing BitBucket DELETE branch request");
    if (flowProperties == null) {
        log.error("Properties have null values");
        throw new MachinaRuntimeException();
    }
    verifyHmacSignature(body, signature);
    String application = event.getRepository().getName();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    BitbucketServerEventHandler handler = BitbucketServerDeleteHandler.builder().controllerRequest(controllerRequest).branchNameForDelete(event.getChanges().get(INDEX_FROM_CHANGES).getRefId()).fromProjectKey(event.getRepository().getProject().getKey()).repositoryName(event.getRepository().getName()).product(product).application(application).webhookPayload(body).configProvider(this).build();
    return handler.execute(uid);
}
Also used : MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler)

Aggregations

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