diff --git a/docs/Delete-Branch.md b/docs/Delete-Branch.md index 09c05944a..027da9930 100644 --- a/docs/Delete-Branch.md +++ b/docs/Delete-Branch.md @@ -32,7 +32,7 @@ This will make CxFlow to always read configuration-as-code from repository defau * [GitHub](#github) * [Azure Devops](#ado) * [GitLab](#gitlab) -* [BitBucket] (#bitbucket) +* [BitBucket](#bitbucket) #### GitHub @@ -71,7 +71,14 @@ GitLab does not support webhook delete events therefore CxFlow does not support #### Bitbucket Server -Bitbucket Server will delete a SAST project **only when using the Post Webhooks plugin**. The current implementation is limited in that: +* Uses the webhook PUSH event +* When an unprotected branch is deleted BitBucket server sends a PUSH event of type DELETE. + +Bitbucket Server will delete a SAST project either using the PUSH webhook event or using the Post Webhooks plugin. The current implementation is limited in that: * Project delete not work if using Config-As-Code given the settings for team and/or project name have been deleted from the branch. * Project delete will work if the project name is calculated or scripted and the team assigned to the project matches the default team in the CxFlow YAML configuration. + +**Bitbucket Cloud** + +Bitbucket cloud currently does not support deleting project in CxSAST when unprotected branch is deleted. diff --git a/src/main/java/com/checkmarx/flow/controller/bitbucket/server/BitbucketServerController.java b/src/main/java/com/checkmarx/flow/controller/bitbucket/server/BitbucketServerController.java index 94df53363..17931ff22 100644 --- a/src/main/java/com/checkmarx/flow/controller/bitbucket/server/BitbucketServerController.java +++ b/src/main/java/com/checkmarx/flow/controller/bitbucket/server/BitbucketServerController.java @@ -22,6 +22,7 @@ import com.checkmarx.flow.dto.bitbucketserver.PushEvent; import com.checkmarx.flow.exception.InvalidTokenException; import com.checkmarx.flow.exception.MachinaRuntimeException; +import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerDeleteHandler; import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler; import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerMergeHandler; import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerPushHandler; @@ -201,6 +202,11 @@ public ResponseEntity pushRequest( } 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(); @@ -244,6 +250,38 @@ private void verifyHmacSignature(String message, String signature) { log.info("Signature verified"); } + public ResponseEntity 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); + } + public FlowProperties getFlowProperties() { return flowProperties; }