Skip to content

Commit

Permalink
Feature to delete project in CxSAST when unprotected branch is delete…
Browse files Browse the repository at this point in the history
…d in bitbucket server
  • Loading branch information
HussainS12 authored Feb 16, 2022
1 parent 9da2c09 commit fa02b88
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
11 changes: 9 additions & 2 deletions docs/Delete-Branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#### <a name="github">GitHub</a>
Expand Down Expand Up @@ -71,7 +71,14 @@ GitLab does not support webhook delete events therefore CxFlow does not support
#### <a name="bitbucket">Bitbucket Server</a>
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.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,6 +202,11 @@ public ResponseEntity<EventResponse> 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();

Expand Down Expand Up @@ -244,6 +250,38 @@ private void verifyHmacSignature(String message, String signature) {
log.info("Signature verified");
}

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);
}

public FlowProperties getFlowProperties() {
return flowProperties;
}
Expand Down

0 comments on commit fa02b88

Please sign in to comment.